code-style: prefer calloc over malloc+memset

This commit is contained in:
NRK 2022-02-03 14:22:25 +06:00 committed by N-R-K
parent 9cdeeab9b8
commit 48343e99b8
4 changed files with 15 additions and 7 deletions

3
main.c
View File

@ -849,8 +849,7 @@ int main(int argc, char *argv[])
else else
filecnt = options->filecnt; filecnt = options->filecnt;
files = emalloc(filecnt * sizeof(*files)); files = ecalloc(filecnt, sizeof(*files));
memset(files, 0, filecnt * sizeof(*files));
fileidx = 0; fileidx = 0;
if (options->from_stdin) { if (options->from_stdin) {

View File

@ -345,6 +345,7 @@ typedef struct {
extern const char *progname; extern const char *progname;
void* emalloc(size_t); void* emalloc(size_t);
void* ecalloc(size_t, size_t);
void* erealloc(void*, size_t); void* erealloc(void*, size_t);
char* estrdup(const char*); char* estrdup(const char*);
void error(int, int, const char*, ...); void error(int, int, const char*, ...);

View File

@ -149,12 +149,10 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, win_t *wi
int len; int len;
const char *homedir, *dsuffix = ""; const char *homedir, *dsuffix = "";
if (cnt != NULL && *cnt > 0) { if (cnt != NULL && *cnt > 0)
tns->thumbs = emalloc(*cnt * sizeof(thumb_t)); tns->thumbs = ecalloc(*cnt, sizeof(thumb_t));
memset(tns->thumbs, 0, *cnt * sizeof(thumb_t)); else
} else {
tns->thumbs = NULL; tns->thumbs = NULL;
}
tns->files = files; tns->files = files;
tns->cnt = cnt; tns->cnt = cnt;
tns->initnext = tns->loadnext = 0; tns->initnext = tns->loadnext = 0;

10
util.c
View File

@ -38,6 +38,16 @@ void* emalloc(size_t size)
return ptr; return ptr;
} }
void* ecalloc(size_t nmemb, size_t size)
{
void *ptr;
ptr = calloc(nmemb, size);
if (ptr == NULL)
error(EXIT_FAILURE, errno, NULL);
return ptr;
}
void* erealloc(void *ptr, size_t size) void* erealloc(void *ptr, size_t size)
{ {
ptr = realloc(ptr, size); ptr = realloc(ptr, size);