Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/lib/libalpm/util.h
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2007-10-29 01:00:52 -0500
committerDan McGee <dan@archlinux.org>2007-10-29 01:00:52 -0500
commitcc754bc6e3be0f37ca0eaca4b6b90f033433fb1a (patch)
treea9940053625e4b4e9b5d345eb1ac441a911f6efa /lib/libalpm/util.h
parentfe3a461703a5d90937c0c6f1ce0c3d802c0f8630 (diff)
libalpm: introduce MALLOC and CALLOC macros
These macros take the place of the common 4 or 5 line blocks of code we had in most places that called malloc or calloc. This should reduce some code duplication and make memory allocation more standard in libalpm. Highlights: * Note that the MALLOC macro actually uses calloc, this is just for safety so that memory is initialized to 0. This can be easily changed in one place. * One malloc call was completely eliminated- it made more sense to do it on the stack. * The use of RET_ERR in public functions (mainly the alpm_*_new functions) was standardized, this makes sense so pm_errno is set. Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'lib/libalpm/util.h')
-rw-r--r--lib/libalpm/util.h7
1 files changed, 6 insertions, 1 deletions
diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h
index d8e6cbd8..3b8fd83d 100644
--- a/lib/libalpm/util.h
+++ b/lib/libalpm/util.h
@@ -39,7 +39,12 @@
#define _(s) s
#endif
-#define FREE(p) do { if (p) { free(p); p = NULL; } } while(0)
+#define ALLOC_FAIL(s) do { _alpm_log(PM_LOG_ERROR, _("alloc failure: could not allocate %d bytes\n"), s); } while(0)
+
+#define MALLOC(p, s, action) do { p = calloc(1, s); if(p == NULL) { ALLOC_FAIL(s); action; } } while(0)
+#define CALLOC(p, l, s, action) do { p = calloc(l, s); if(p == NULL) { ALLOC_FAIL(s); action; } } while(0)
+
+#define FREE(p) do { if(p) { free(p); p = NULL; } } while(0)
#define ASSERT(cond, action) do { if(!(cond)) { action; } } while(0)