sxiv

simple X image viewer
git clone git://git.suckless.org/sxiv
Log | Files | Refs | LICENSE

commit 851e4288c102cc4177d54d87aded43d003e85885
parent b096cbd536b94ee848b94d873d0c0a898f574af1
Author: Bert M√ľnnich <ber.t@posteo.de>
Date:   Wed, 28 Oct 2015 22:29:01 +0100

Prefix safe allocation functions with 'e' instead of 's_'

Diffstat:
image.c | 12++++++------
main.c | 10+++++-----
thumbs.c | 6+++---
util.c | 29++++++++++++++---------------
util.h | 6+++---
window.c | 6+++---
6 files changed, 34 insertions(+), 35 deletions(-)

diff --git a/image.c b/image.c @@ -138,7 +138,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) if (img->multi.cap == 0) { img->multi.cap = 8; img->multi.frames = (img_frame_t*) - s_malloc(sizeof(img_frame_t) * img->multi.cap); + emalloc(sizeof(img_frame_t) * img->multi.cap); } img->multi.cnt = img->multi.sel = 0; img->multi.length = 0; @@ -190,9 +190,9 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) w = gif->Image.Width; h = gif->Image.Height; - rows = (GifRowType*) s_malloc(h * sizeof(GifRowType)); + rows = (GifRowType*) emalloc(h * sizeof(GifRowType)); for (i = 0; i < h; i++) - rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType)); + rows[i] = (GifRowType) emalloc(w * sizeof(GifPixelType)); if (gif->Image.Interlace) { for (i = 0; i < 4; i++) { for (j = intoffset[i]; j < h; j += intjump[i]) @@ -203,7 +203,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) DGifGetLine(gif, rows[i], w); } - ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * sw * sh); + ptr = data = (DATA32*) emalloc(sizeof(DATA32) * sw * sh); cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap; r = cmap->Colors[bg].Red; g = cmap->Colors[bg].Green; @@ -257,8 +257,8 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) if (img->multi.cnt == img->multi.cap) { img->multi.cap *= 2; img->multi.frames = (img_frame_t*) - s_realloc(img->multi.frames, - img->multi.cap * sizeof(img_frame_t)); + erealloc(img->multi.frames, + img->multi.cap * sizeof(img_frame_t)); } img->multi.frames[img->multi.cnt].im = im; img->multi.frames[img->multi.cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY; diff --git a/main.c b/main.c @@ -122,7 +122,7 @@ void check_add_file(char *filename, bool given) if (fileidx == filecnt) { filecnt *= 2; - files = s_realloc(files, filecnt * sizeof(*files)); + files = erealloc(files, filecnt * sizeof(*files)); memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files)); } @@ -131,7 +131,7 @@ void check_add_file(char *filename, bool given) return; } - files[fileidx].name = s_strdup(filename); + files[fileidx].name = estrdup(filename); if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0') files[fileidx].base = ++bn; else @@ -493,7 +493,7 @@ void run_key_handler(const char *key, unsigned int mask) warn("could not open pipe for key handler"); return; } - oldst = s_malloc(fcnt * sizeof(*oldst)); + oldst = emalloc(fcnt * sizeof(*oldst)); strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size); win_draw(&win); @@ -800,7 +800,7 @@ int main(int argc, char **argv) else filecnt = options->filecnt; - files = s_malloc(filecnt * sizeof(*files)); + files = emalloc(filecnt * sizeof(*files)); memset(files, 0, filecnt * sizeof(*files)); fileidx = 0; @@ -865,7 +865,7 @@ int main(int argc, char **argv) for (i = 0; i < ARRLEN(cmd); i++) { n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12; - *cmd[i] = (char*) s_malloc(n); + *cmd[i] = (char*) emalloc(n); snprintf(*cmd[i], n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]); if (access(*cmd[i], X_OK) != 0) { free(*cmd[i]); diff --git a/thumbs.c b/thumbs.c @@ -48,7 +48,7 @@ char* tns_cache_filepath(const char *filepath) if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) { /* don't cache images inside the cache directory! */ len = strlen(cache_dir) + strlen(filepath) + 2; - cfile = (char*) s_malloc(len); + cfile = (char*) emalloc(len); snprintf(cfile, len, "%s/%s", cache_dir, filepath + 1); } return cfile; @@ -155,7 +155,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, const char *homedir, *dsuffix = ""; if (cnt != NULL && *cnt > 0) { - tns->thumbs = (thumb_t*) s_malloc(*cnt * sizeof(thumb_t)); + tns->thumbs = (thumb_t*) emalloc(*cnt * sizeof(thumb_t)); memset(tns->thumbs, 0, *cnt * sizeof(thumb_t)); } else { tns->thumbs = NULL; @@ -178,7 +178,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, if (homedir != NULL) { free(cache_dir); len = strlen(homedir) + strlen(dsuffix) + 6; - cache_dir = (char*) s_malloc(len); + cache_dir = (char*) emalloc(len); snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix); } else { warn("could not locate thumbnail cache directory"); diff --git a/util.c b/util.c @@ -28,7 +28,7 @@ void cleanup(void); -void* s_malloc(size_t size) +void* emalloc(size_t size) { void *ptr; @@ -38,7 +38,7 @@ void* s_malloc(size_t size) return ptr; } -void* s_realloc(void *ptr, size_t size) +void* erealloc(void *ptr, size_t size) { ptr = realloc(ptr, size); if (ptr == NULL) @@ -46,16 +46,15 @@ void* s_realloc(void *ptr, size_t size) return ptr; } -char* s_strdup(const char *s) +char* estrdup(const char *s) { - char *d = NULL; + char *d; + size_t n = strlen(s) + 1; - if (s != NULL) { - d = malloc(strlen(s) + 1); - if (d == NULL) - die("could not allocate memory"); - strcpy(d, s); - } + d = malloc(n); + if (d == NULL) + die("could not allocate memory"); + memcpy(d, s, n); return d; } @@ -112,7 +111,7 @@ int r_opendir(r_dir_t *rdir, const char *dirname) } rdir->stcap = 512; - rdir->stack = (char**) s_malloc(rdir->stcap * sizeof(char*)); + rdir->stack = (char**) emalloc(rdir->stcap * sizeof(char*)); rdir->stlen = 0; rdir->name = (char*) dirname; @@ -158,7 +157,7 @@ char* r_readdir(r_dir_t *rdir) continue; len = strlen(rdir->name) + strlen(dentry->d_name) + 2; - filename = (char*) s_malloc(len); + filename = (char*) emalloc(len); snprintf(filename, len, "%s%s%s", rdir->name, rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/", dentry->d_name); @@ -169,8 +168,8 @@ char* r_readdir(r_dir_t *rdir) /* put subdirectory on the stack */ if (rdir->stlen == rdir->stcap) { rdir->stcap *= 2; - rdir->stack = (char**) s_realloc(rdir->stack, - rdir->stcap * sizeof(char*)); + rdir->stack = (char**) erealloc(rdir->stack, + rdir->stcap * sizeof(char*)); } rdir->stack[rdir->stlen++] = filename; continue; @@ -207,7 +206,7 @@ int r_mkdir(const char *path) if (stat(path, &stats) == 0) return S_ISDIR(stats.st_mode) ? 0 : -1; - d = dir = (char*) s_malloc(strlen(path) + 1); + d = dir = (char*) emalloc(strlen(path) + 1); strcpy(dir, path); while (d != NULL && err == 0) { diff --git a/util.h b/util.h @@ -61,9 +61,9 @@ typedef struct { int stlen; } r_dir_t; -void* s_malloc(size_t); -void* s_realloc(void*, size_t); -char* s_strdup(const char*); +void* emalloc(size_t); +void* erealloc(void*, size_t); +char* estrdup(const char*); void warn(const char*, ...); void die(const char*, ...); diff --git a/window.c b/window.c @@ -165,8 +165,8 @@ void win_init(win_t *win) win->bar.l.size = BAR_L_LEN; win->bar.r.size = BAR_R_LEN; - win->bar.l.buf = s_malloc(win->bar.l.size); - win->bar.r.buf = s_malloc(win->bar.r.size); + win->bar.l.buf = emalloc(win->bar.l.size); + win->bar.r.buf = emalloc(win->bar.r.size); win->bar.h = options->hide_bar ? 0 : barheight; INIT_ATOM_(WM_DELETE_WINDOW); @@ -257,7 +257,7 @@ void win_open(win_t *win) gc = XCreateGC(e->dpy, win->xwin, 0, None); n = icons[ARRLEN(icons)-1].size; - icon_data = s_malloc((n * n + 2) * sizeof(*icon_data)); + icon_data = emalloc((n * n + 2) * sizeof(*icon_data)); for (i = 0; i < ARRLEN(icons); i++) { n = 0;