Skip to content

Commit

Permalink
add sbase-style ecalloc(), calloc: or die
Browse files Browse the repository at this point in the history
... remove intermediary variables
  • Loading branch information
hiltjo committed Oct 20, 2015
1 parent 1649867 commit 5a20b40
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
19 changes: 7 additions & 12 deletions drw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))

void die(const char *errstr, ...);
void *ecalloc(size_t, size_t);

0 comments on commit 5a20b40

Please sign in to comment.