aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNRK <nrk@disroot.org>2022-02-03 14:22:25 +0600
committerN-R-K <79544946+N-R-K@users.noreply.github.com>2022-02-17 06:16:19 +0000
commit48343e99b8bb27fdc7763e521ea7e651ecc7a7ba (patch)
treec541b394ebb9d03d8b51919eb6cadef917a819b3
parent9cdeeab9b813a44ac6f23c0494626fa0f0727f42 (diff)
code-style: prefer calloc over malloc+memset
-rw-r--r--main.c3
-rw-r--r--nsxiv.h1
-rw-r--r--thumbs.c8
-rw-r--r--util.c10
4 files changed, 15 insertions, 7 deletions
diff --git a/main.c b/main.c
index e29c051..58538f0 100644
--- a/main.c
+++ b/main.c
@@ -849,8 +849,7 @@ int main(int argc, char *argv[])
else
filecnt = options->filecnt;
- files = emalloc(filecnt * sizeof(*files));
- memset(files, 0, filecnt * sizeof(*files));
+ files = ecalloc(filecnt, sizeof(*files));
fileidx = 0;
if (options->from_stdin) {
diff --git a/nsxiv.h b/nsxiv.h
index 6ac6e4f..a9d4dad 100644
--- a/nsxiv.h
+++ b/nsxiv.h
@@ -345,6 +345,7 @@ typedef struct {
extern const char *progname;
void* emalloc(size_t);
+void* ecalloc(size_t, size_t);
void* erealloc(void*, size_t);
char* estrdup(const char*);
void error(int, int, const char*, ...);
diff --git a/thumbs.c b/thumbs.c
index b743e53..4c02e6a 100644
--- a/thumbs.c
+++ b/thumbs.c
@@ -149,12 +149,10 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, win_t *wi
int len;
const char *homedir, *dsuffix = "";
- if (cnt != NULL && *cnt > 0) {
- tns->thumbs = emalloc(*cnt * sizeof(thumb_t));
- memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
- } else {
+ if (cnt != NULL && *cnt > 0)
+ tns->thumbs = ecalloc(*cnt, sizeof(thumb_t));
+ else
tns->thumbs = NULL;
- }
tns->files = files;
tns->cnt = cnt;
tns->initnext = tns->loadnext = 0;
diff --git a/util.c b/util.c
index 71ff7be..ef0ec43 100644
--- a/util.c
+++ b/util.c
@@ -38,6 +38,16 @@ void* emalloc(size_t size)
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)
{
ptr = realloc(ptr, size);