[hackers] [dmenu] add sbase-style ecalloc(), calloc: or die || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Tue, 20 Oct 2015 23:04:13 +0200 (CEST)

commit 5a20b409c673a6736c3f9326cb54edc32908f717
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
AuthorDate: Tue Oct 20 22:51:57 2015 +0200
Commit: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
CommitDate: Tue Oct 20 22:51:57 2015 +0200

    add sbase-style ecalloc(), calloc: or die
    
    ... remove intermediary variables

diff --git a/drw.c b/drw.c
index 6aeb59d..a98083e 100644
--- a/drw.c
+++ b/drw.c
_AT_@ -65,8 +65,7 @@ drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h
 {
         Drw *drw;
 
- if (!(drw = calloc(1, sizeof(Drw))))
- return NULL;
+ drw = ecalloc(1, sizeof(Drw));
         drw->dpy = dpy;
         drw->screen = screen;
         drw->root = root;
_AT_@ -189,16 +188,13 @@ Clr *
 drw_clr_create(Drw *drw, const char *clrname)
 {
         Clr *clr;
- Colormap cmap;
- Visual *vis;
-
         if (!drw)
                 return NULL;
- if (!(clr = calloc(1, sizeof(Clr))))
- return NULL;
- cmap = DefaultColormap(drw->dpy, drw->screen);
- vis = DefaultVisual(drw->dpy, drw->screen);
- if (!XftColorAllocName(drw->dpy, vis, cmap, clrname, &clr->rgb))
+
+ clr = ecalloc(1, sizeof(Clr));
+ if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
+ DefaultColormap(drw->dpy, drw->screen),
+ clrname, &clr->rgb))
                 die("error, cannot allocate color '%s'\n", clrname);
         clr->pix = clr->rgb.pixel;
 
_AT_@ -409,8 +405,7 @@ drw_cur_create(Drw *drw, int shape)
 
         if (!drw)
                 return NULL;
- if (!(cur = calloc(1, sizeof(Cur))))
- return NULL;
+ cur = ecalloc(1, sizeof(Cur));
         cur->cursor = XCreateFontCursor(drw->dpy, shape);
 
         return cur;
diff --git a/util.c b/util.c
index 9b27512..6b703e9 100644
--- a/util.c
+++ b/util.c
_AT_@ -6,6 +6,16 @@
 
 #include "util.h"
 
+void *
+ecalloc(size_t nmemb, size_t size)
+{
+ void *p;
+
+ if (!(p = calloc(nmemb, size)))
+ perror(NULL);
+ return p;
+}
+
 void
 die(const char *fmt, ...) {
         va_list ap;
diff --git a/util.h b/util.h
index f7ce721..cded043 100644
--- a/util.h
+++ b/util.h
_AT_@ -5,3 +5,4 @@
 #define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))
 
 void die(const char *errstr, ...);
+void *ecalloc(size_t, size_t);
Received on Tue Oct 20 2015 - 23:04:13 CEST

This archive was generated by hypermail 2.3.0 : Tue Oct 20 2015 - 23:12:13 CEST