From 2c2c0781d9824436ea9d9d51807a250a34f29c5b Mon Sep 17 00:00:00 2001 From: H Hautakoski Date: Sat, 18 Sep 2010 19:01:33 +0200 Subject: [PATCH] xalloc --- src/common/die.c | 31 +++++++++++++++++ src/common/strbuf.c | 24 ++----------- src/common/util.h | 18 ++++++++++ src/common/xalloc.c | 85 +++++++++++++++++++++++++++++++++++++++++++++ src/common/xalloc.h | 26 ++++++++++++++ test/Makefile | 32 ++++++++++++++--- 6 files changed, 190 insertions(+), 26 deletions(-) create mode 100644 src/common/die.c create mode 100644 src/common/util.h create mode 100644 src/common/xalloc.c create mode 100644 src/common/xalloc.h diff --git a/src/common/die.c b/src/common/die.c new file mode 100644 index 0000000..ef2dcc4 --- /dev/null +++ b/src/common/die.c @@ -0,0 +1,31 @@ +/* common/die.c - functionality for killing things in creative ways. + * + * Copyright (C) 2010 Henrik Hautakoski + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + */ + +#include +#include +#include +#include +#include "util.h" + +void die(const char *err, ...) { + + va_list vl; + va_start(vl, err); + vfprintf(stderr, err, vl); + va_end(vl); + exit(EXIT_FAILURE); +} + +void die_errno(const char *desc) { + + if (errno) + perror(desc); + exit(EXIT_FAILURE); +} diff --git a/src/common/strbuf.c b/src/common/strbuf.c index 64e08b3..6672b39 100644 --- a/src/common/strbuf.c +++ b/src/common/strbuf.c @@ -10,31 +10,13 @@ #include #include -#include -#include +#include "xalloc.h" #include "strbuf.h" #define CHNK_SIZE 128 char strbuf_null = '\0'; -static void* xrealloc(void *ptr, size_t size) { - - assert(size); - ptr = realloc(ptr, size); - assert(ptr != NULL); - return ptr; -} - -static void* xcalloc(size_t nmemb, size_t size) { - - assert(nmemb); - assert(size); - void *ptr = calloc(nmemb, size); - assert(ptr != NULL); - return ptr; -} - void strbuf_init(strbuf_t *s) { s->buf = &strbuf_null; @@ -69,7 +51,7 @@ char* strbuf_release(strbuf_t *s) { char *ret; if (!s->alloc_size) - ret = xcalloc(1, 1); + ret = xmallocz(1); else if (s->len + 1 != s->alloc_size) ret = xrealloc(s->buf, s->len + 1); else @@ -85,7 +67,7 @@ void strbuf_free(strbuf_t *s) { if (!s->alloc_size) return; - free(s->buf); + xfree(s->buf); strbuf_init(s); } diff --git a/src/common/util.h b/src/common/util.h new file mode 100644 index 0000000..1d968ba --- /dev/null +++ b/src/common/util.h @@ -0,0 +1,18 @@ +/* common/util.h + * + * Copyright (C) 2010 Henrik Hautakoski + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + */ + +#ifndef __COMMON_UTIL_H +#define __COMMON_UTIL_H + +void die(const char *, ...); + +void die_errno(const char *); + +#endif /* __COMMOT_UTIL_H */ diff --git a/src/common/xalloc.c b/src/common/xalloc.c new file mode 100644 index 0000000..88531f9 --- /dev/null +++ b/src/common/xalloc.c @@ -0,0 +1,85 @@ +/* common/xalloc.c + * + * Copyright (C) 2010 Henrik Hautakoski + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + */ + +#include +#include +#include +#include "util.h" +#include "xalloc.h" + +#define CHECK(expr) \ + if (!(expr)) \ + goto bail + +#define CHECK_SET_ERRNO(expr, no) \ + if (!(expr)) { \ + errno = no; \ + goto bail; \ + } + +#ifdef __DEBUG__ +# define CHECK_INPUT(s) CHECK_SET_ERRNO(s, EINVAL) +#else +# define CHECK_INPUT(s) +#endif + +void* xmalloc(size_t size) { + + CHECK_INPUT(size); + + void *ptr = malloc(size); + CHECK(ptr); + return ptr; +bail: + die_errno("xmalloc"); +} + +void* xmallocz(size_t size) { + + CHECK_INPUT(size); + + void *ptr = malloc(size); + CHECK(ptr); + memset(ptr, 0, size); + return ptr; +bail: + die_errno("xmallocz"); +} + +void* xrealloc(void *ptr, size_t size) { + + CHECK_INPUT(size); + + ptr = realloc(ptr, size); + CHECK(ptr); + return ptr; +bail: + die_errno("xrealloc"); +} + +char* xstrdup(const char *s) { + + CHECK_INPUT(s); + + char *c = strdup(s); + CHECK(c); + return c; +bail: + die_errno("xstrdup"); +} + +void xfree(void *ptr) { + + CHECK_INPUT(ptr); + free(ptr); + return; +bail: + die_errno("xfree"); +} diff --git a/src/common/xalloc.h b/src/common/xalloc.h new file mode 100644 index 0000000..f77b862 --- /dev/null +++ b/src/common/xalloc.h @@ -0,0 +1,26 @@ +/* common/xalloc.h - stricter memory allocation. + * + * Copyright (C) 2010 Henrik Hautakoski + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + */ + +#ifndef __COMMON_XALLOC_H +#define __COMMON_XALLOC_H + +#include + +void* xmalloc(size_t); + +void* xmallocz(size_t); + +void* xrealloc(void *, size_t); + +char* xstrdup(const char *); + +void xfree(void *); + +#endif /* __COMMON_XALLOC_H */ diff --git a/test/Makefile b/test/Makefile index 5ba8c87..472b44a 100644 --- a/test/Makefile +++ b/test/Makefile @@ -9,17 +9,30 @@ raw_inotify : $(CC) -linotifytools t_raw_inotify.c -o test_raw_inotify strbuf : - $(CC) $(CFLAGS) ../src/common/strbuf.c t_strbuf.c -o test_strbuf + $(CC) $(CFLAGS) \ + ../src/common/strbuf.c \ + ../src/common/xalloc.c \ + ../src/common/die.c \ + t_strbuf.c -o test_strbuf path : - $(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/strbuf.c ../src/common/path.c t_path.c -o test_path + $(CC) $(CFLAGS) \ + unit.c \ + ../src/common/xalloc.c \ + ../src/common/die.c \ + ../src/common/strbuf.c \ + ../src/common/path.c \ + t_path.c -o test_path rbtree : - $(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/rbtree.c t_rbtree.c -o test_rbtree + $(CC) $(CFLAGS) unit.c ../src/common/rbtree.c t_rbtree.c -o test_rbtree inotify : $(CC) -lpthread -D INOTIFY_DEBUG -D RB_DEBUG $(CFLAGS) \ ../src/common/rbtree.c \ + ../src/common/xalloc.c \ + ../src/common/die.c \ + ../src/common/strbuf.c \ ../src/common/path.c \ ../src/notify/event.c \ ../src/notify/tree.c \ @@ -27,18 +40,27 @@ inotify : t_inotify.c -o test_inotify tree : - $(CC) $(CFLAGS) ../src/common/path.c ../src/notify/tree.c t_tree.c -o test_tree + $(CC) $(CFLAGS) \ + ../src/common/path.c \ + ../src/notify/tree.c \ + ../src/common/xalloc.c \ + ../src/common/die.c \ + ../src/common/strbuf.c \ + t_tree.c -o test_tree indexer : $(CC) $(CFLAGS) $(LDFLAGS) \ ../src/common/rbtree.c \ ../src/common/path.c \ + ../src/common/xalloc.c \ + ../src/common/die.c \ + ../src/common/strbuf.c \ ../src/notify/tree.c \ ../src/notify/inotify.c \ ../src/notify/event.c \ ../src/arch/mysql.c \ ../src/indexer.c \ - t_indexer.c -o test_indexer + t_indexer.c -o test_indexer mysql : $(CC) -D DB_DEBUG $(CFLAGS) $(LDFLAGS) ../src/arch/mysql.c t_mysql.c -o test_mysql