Krolyxon
Commit 48343e9

Branches

Refs →

nsxiv48343e9

code-style: prefer calloc over malloc+memset

Author
NRK
Date
2022-02-03 14:22 +0600
Committer
N-R-K · 2022-02-17 06:16 +0000
Parent
9cdeeab
Commit
48343e99b8bb27fdc7763e521ea7e651ecc7a7ba

+15 −7

4 files changed

  1. M main.c +1 −2
  2. M nsxiv.h +1 −0
  3. M thumbs.c +3 −5
  4. M util.c +10 −0
M main.c View
@@ -849,8 +849,7 @@ int main(int argc, char *argv[])
849849 else
850850 filecnt = options->filecnt;
851851
852− files = emalloc(filecnt * sizeof(*files));
853− memset(files, 0, filecnt * sizeof(*files));
852+ files = ecalloc(filecnt, sizeof(*files));
854853 fileidx = 0;
855854
856855 if (options->from_stdin) {
M nsxiv.h View
@@ -345,6 +345,7 @@ typedef struct {
345345 extern const char *progname;
346346
347347 void* emalloc(size_t);
348+void* ecalloc(size_t, size_t);
348349 void* erealloc(void*, size_t);
349350 char* estrdup(const char*);
350351 void error(int, int, const char*, ...);
M thumbs.c View
@@ -149,12 +149,10 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, win_t *wi
149149 int len;
150150 const char *homedir, *dsuffix = "";
151151
152− if (cnt != NULL && *cnt > 0) {
153− tns->thumbs = emalloc(*cnt * sizeof(thumb_t));
154− memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
155− } else {
152+ if (cnt != NULL && *cnt > 0)
153+ tns->thumbs = ecalloc(*cnt, sizeof(thumb_t));
154+ else
156155 tns->thumbs = NULL;
157− }
158156 tns->files = files;
159157 tns->cnt = cnt;
160158 tns->initnext = tns->loadnext = 0;
M util.c View
@@ -38,6 +38,16 @@ void* emalloc(size_t size)
3838 return ptr;
3939 }
4040
41+void* ecalloc(size_t nmemb, size_t size)
42+{
43+ void *ptr;
44+
45+ ptr = calloc(nmemb, size);
46+ if (ptr == NULL)
47+ error(EXIT_FAILURE, errno, NULL);
48+ return ptr;
49+}
50+
4151 void* erealloc(void *ptr, size_t size)
4252 {
4353 ptr = realloc(ptr, size);