add sbase-style ecalloc(), calloc: or die

... remove intermediary variables
This commit is contained in:
Hiltjo Posthuma 2015-10-20 22:51:57 +02:00
parent 164986763a
commit 5a20b409c6
3 changed files with 18 additions and 12 deletions

19
drw.c
View File

@ -65,8 +65,7 @@ drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h
{ {
Drw *drw; Drw *drw;
if (!(drw = calloc(1, sizeof(Drw)))) drw = ecalloc(1, sizeof(Drw));
return NULL;
drw->dpy = dpy; drw->dpy = dpy;
drw->screen = screen; drw->screen = screen;
drw->root = root; drw->root = root;
@ -189,16 +188,13 @@ Clr *
drw_clr_create(Drw *drw, const char *clrname) drw_clr_create(Drw *drw, const char *clrname)
{ {
Clr *clr; Clr *clr;
Colormap cmap;
Visual *vis;
if (!drw) if (!drw)
return NULL; return NULL;
if (!(clr = calloc(1, sizeof(Clr))))
return NULL; clr = ecalloc(1, sizeof(Clr));
cmap = DefaultColormap(drw->dpy, drw->screen); if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
vis = DefaultVisual(drw->dpy, drw->screen); DefaultColormap(drw->dpy, drw->screen),
if (!XftColorAllocName(drw->dpy, vis, cmap, clrname, &clr->rgb)) clrname, &clr->rgb))
die("error, cannot allocate color '%s'\n", clrname); die("error, cannot allocate color '%s'\n", clrname);
clr->pix = clr->rgb.pixel; clr->pix = clr->rgb.pixel;
@ -409,8 +405,7 @@ drw_cur_create(Drw *drw, int shape)
if (!drw) if (!drw)
return NULL; return NULL;
if (!(cur = calloc(1, sizeof(Cur)))) cur = ecalloc(1, sizeof(Cur));
return NULL;
cur->cursor = XCreateFontCursor(drw->dpy, shape); cur->cursor = XCreateFontCursor(drw->dpy, shape);
return cur; return cur;

10
util.c
View File

@ -6,6 +6,16 @@
#include "util.h" #include "util.h"
void *
ecalloc(size_t nmemb, size_t size)
{
void *p;
if (!(p = calloc(nmemb, size)))
perror(NULL);
return p;
}
void void
die(const char *fmt, ...) { die(const char *fmt, ...) {
va_list ap; va_list ap;

1
util.h
View File

@ -5,3 +5,4 @@
#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B)) #define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))
void die(const char *errstr, ...); void die(const char *errstr, ...);
void *ecalloc(size_t, size_t);