From cc754bc6e3be0f37ca0eaca4b6b90f033433fb1a Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Mon, 29 Oct 2007 01:00:52 -0500 Subject: 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 --- lib/libalpm/deps.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) (limited to 'lib/libalpm/deps.c') diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c index b459ca6c..f399083c 100644 --- a/lib/libalpm/deps.c +++ b/lib/libalpm/deps.c @@ -71,11 +71,7 @@ pmdepmissing_t *_alpm_depmiss_new(const char *target, pmdeptype_t type, ALPM_LOG_FUNC; - miss = malloc(sizeof(pmdepmissing_t)); - if(miss == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes\n"), sizeof(pmdepmissing_t)); - RET_ERR(PM_ERR_MEMORY, NULL); - } + MALLOC(miss, sizeof(pmdepmissing_t), RET_ERR(PM_ERR_MEMORY, NULL)); strncpy(miss->target, target, PKG_NAME_LEN); miss->type = type; @@ -524,11 +520,7 @@ pmdepend_t SYMEXPORT *alpm_splitdep(const char *depstring) } newstr = strdup(depstring); - depend = malloc(sizeof(pmdepend_t)); - if(depend == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes\n"), sizeof(pmdepend_t)); - return(NULL); - } + MALLOC(depend, sizeof(pmdepend_t), return(NULL)); /* Find a version comparator if one exists. If it does, set the type and * increment the ptr accordingly so we can copy the right strings. */ @@ -855,12 +847,11 @@ char SYMEXPORT *alpm_dep_get_string(pmdepend_t *dep) ASSERT(handle != NULL, return(NULL)); ASSERT(dep != NULL, return(NULL)); + /* TODO redo the sprintf, change to snprintf and + * make it less hacky and dependent on sizeof, etc */ char *ptr; - char *depstring = malloc(sizeof(pmdepend_t)); - if(depstring == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes\n"), sizeof(pmdepend_t)); - return NULL; - } + char *depstring; + MALLOC(depstring, sizeof(pmdepend_t), RET_ERR(PM_ERR_MEMORY, NULL)); strcpy(depstring, dep->name); ptr = depstring + strlen(depstring); -- cgit v1.2.3-24-g4f1b