diff options
author | Dan McGee <dan@archlinux.org> | 2007-10-29 07:00:52 +0100 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2007-10-29 07:00:52 +0100 |
commit | cc754bc6e3be0f37ca0eaca4b6b90f033433fb1a (patch) | |
tree | a9940053625e4b4e9b5d345eb1ac441a911f6efa /lib/libalpm | |
parent | fe3a461703a5d90937c0c6f1ce0c3d802c0f8630 (diff) | |
download | pacman-cc754bc6e3be0f37ca0eaca4b6b90f033433fb1a.tar.gz pacman-cc754bc6e3be0f37ca0eaca4b6b90f033433fb1a.tar.xz |
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')
-rw-r--r-- | lib/libalpm/conflict.c | 9 | ||||
-rw-r--r-- | lib/libalpm/db.c | 13 | ||||
-rw-r--r-- | lib/libalpm/delta.c | 3 | ||||
-rw-r--r-- | lib/libalpm/deps.c | 21 | ||||
-rw-r--r-- | lib/libalpm/group.c | 7 | ||||
-rw-r--r-- | lib/libalpm/handle.c | 7 | ||||
-rw-r--r-- | lib/libalpm/package.c | 19 | ||||
-rw-r--r-- | lib/libalpm/server.c | 12 | ||||
-rw-r--r-- | lib/libalpm/sync.c | 5 | ||||
-rw-r--r-- | lib/libalpm/trans.c | 5 | ||||
-rw-r--r-- | lib/libalpm/util.h | 7 |
11 files changed, 30 insertions, 78 deletions
diff --git a/lib/libalpm/conflict.c b/lib/libalpm/conflict.c index d09c996d..0baef8d1 100644 --- a/lib/libalpm/conflict.c +++ b/lib/libalpm/conflict.c @@ -239,12 +239,9 @@ static alpm_list_t *add_fileconflict(alpm_list_t *conflicts, pmconflicttype_t type, const char *filestr, const char* name1, const char* name2) { - pmconflict_t *conflict = malloc(sizeof(pmconflict_t)); - if(conflict == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes\n"), - sizeof(pmconflict_t)); - return(conflicts); - } + pmconflict_t *conflict; + MALLOC(conflict, sizeof(pmconflict_t), return(conflicts)); + conflict->type = type; strncpy(conflict->target, name1, PKG_NAME_LEN); strncpy(conflict->file, filestr, CONFLICT_FILE_LEN); diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c index 1052840f..8c36c310 100644 --- a/lib/libalpm/db.c +++ b/lib/libalpm/db.c @@ -582,18 +582,9 @@ pmdb_t *_alpm_db_new(const char *dbpath, const char *treename) ALPM_LOG_FUNC; - db = calloc(1, sizeof(pmdb_t)); - if(db == NULL) { - _alpm_log(PM_LOG_ERROR, "calloc : %s\n", strerror(errno)); - RET_ERR(PM_ERR_MEMORY, NULL); - } + CALLOC(db, 1, sizeof(pmdb_t), RET_ERR(PM_ERR_MEMORY, NULL)); + CALLOC(db->path, 1, pathsize, RET_ERR(PM_ERR_MEMORY, NULL)); - db->path = calloc(1, pathsize); - if(db->path == NULL) { - _alpm_log(PM_LOG_ERROR, "calloc : %s\n", strerror(errno)); - FREE(db); - RET_ERR(PM_ERR_MEMORY, NULL); - } sprintf(db->path, "%s%s/", dbpath, treename); strncpy(db->treename, treename, PATH_MAX); diff --git a/lib/libalpm/delta.c b/lib/libalpm/delta.c index 00ed46cb..626ffb31 100644 --- a/lib/libalpm/delta.c +++ b/lib/libalpm/delta.c @@ -26,6 +26,7 @@ /* libalpm */ #include "delta.h" +#include "error.h" #include "util.h" #include "log.h" #include "alpm_list.h" @@ -235,7 +236,7 @@ pmdelta_t *_alpm_delta_parse(char *line) pmdelta_t *delta; char *tmp = line, *tmp2; - delta = malloc(sizeof(pmdelta_t)); + CALLOC(delta, 1, sizeof(pmdelta_t), RET_ERR(PM_ERR_MEMORY, NULL)); tmp2 = tmp; tmp = strchr(tmp, ' '); 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); diff --git a/lib/libalpm/group.c b/lib/libalpm/group.c index 7a6db163..4ad39d6d 100644 --- a/lib/libalpm/group.c +++ b/lib/libalpm/group.c @@ -39,12 +39,7 @@ pmgrp_t *_alpm_grp_new() ALPM_LOG_FUNC; - grp = calloc(1, sizeof(pmgrp_t)); - if(grp == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes\n"), - sizeof(pmgrp_t)); - RET_ERR(PM_ERR_MEMORY, NULL); - } + CALLOC(grp, 1, sizeof(pmgrp_t), RET_ERR(PM_ERR_MEMORY, NULL)); return(grp); } diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c index 242bbe51..5cbb0157 100644 --- a/lib/libalpm/handle.c +++ b/lib/libalpm/handle.c @@ -47,13 +47,8 @@ pmhandle_t *_alpm_handle_new() { pmhandle_t *handle; - handle = malloc(sizeof(pmhandle_t)); - if(handle == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes\n"), sizeof(pmhandle_t)); - RET_ERR(PM_ERR_MEMORY, NULL); - } + CALLOC(handle, 1, sizeof(pmhandle_t), RET_ERR(PM_ERR_MEMORY, NULL)); - memset(handle, 0, sizeof(pmhandle_t)); handle->lckfd = -1; handle->logstream = NULL; diff --git a/lib/libalpm/package.c b/lib/libalpm/package.c index 4f6f5a9e..9c166a50 100644 --- a/lib/libalpm/package.c +++ b/lib/libalpm/package.c @@ -668,9 +668,7 @@ pmpkg_t *_alpm_pkg_new(const char *name, const char *version) ALPM_LOG_FUNC; - if((pkg = calloc(1,sizeof(pmpkg_t))) == NULL) { - RET_ERR(PM_ERR_MEMORY, NULL); - } + CALLOC(pkg, 1, sizeof(pmpkg_t), RET_ERR(PM_ERR_MEMORY, NULL)); if(name && name[0] != 0) { strncpy(pkg->name, name, PKG_NAME_LEN); @@ -692,10 +690,7 @@ pmpkg_t *_alpm_pkg_dup(pmpkg_t *pkg) ALPM_LOG_FUNC; - if((newpkg = calloc(1, sizeof(pmpkg_t))) == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes\n"), sizeof(pmpkg_t)); - RET_ERR(PM_ERR_MEMORY, NULL); - } + CALLOC(newpkg, 1, sizeof(pmpkg_t), RET_ERR(PM_ERR_MEMORY, NULL)); memcpy(newpkg, pkg, sizeof(pmpkg_t)); newpkg->licenses = alpm_list_strdup(alpm_pkg_get_licenses(pkg)); @@ -951,14 +946,14 @@ pmpkg_t *_alpm_pkg_load(const char *pkgfile, unsigned short full) /* If full is false, only read through the archive until we find our needed * metadata. If it is true, read through the entire archive, which serves * as a verfication of integrity. */ - while((ret = archive_read_next_header (archive, &entry)) == ARCHIVE_OK) { + while((ret = archive_read_next_header(archive, &entry)) == ARCHIVE_OK) { const char *entry_name = archive_entry_pathname(entry); if(strcmp(entry_name, ".PKGINFO") == 0) { /* extract this file into /tmp. it has info for us */ descfile = strdup("/tmp/alpm_XXXXXX"); fd = mkstemp(descfile); - archive_read_data_into_fd (archive, fd); + archive_read_data_into_fd(archive, fd); /* parse the info file */ if(parse_descfile(descfile, info) == -1) { _alpm_log(PM_LOG_ERROR, _("could not parse package description file in %s\n"), @@ -984,12 +979,9 @@ pmpkg_t *_alpm_pkg_load(const char *pkgfile, unsigned short full) /* Build info->files from the filelist */ FILE *fp; char *fn; - char *str; + char str[PATH_MAX+1]; int fd; - if((str = malloc(PATH_MAX)) == NULL) { - RET_ERR(PM_ERR_MEMORY, (pmpkg_t *)-1); - } fn = strdup("/tmp/alpm_XXXXXX"); fd = mkstemp(fn); archive_read_data_into_fd(archive,fd); @@ -1001,7 +993,6 @@ pmpkg_t *_alpm_pkg_load(const char *pkgfile, unsigned short full) _alpm_strtrim(str); info->files = alpm_list_add(info->files, strdup(str)); } - FREE(str); fclose(fp); if(unlink(fn)) { _alpm_log(PM_LOG_WARNING, _("could not remove tempfile %s\n"), fn); diff --git a/lib/libalpm/server.c b/lib/libalpm/server.c index 11a92033..d4c0a05a 100644 --- a/lib/libalpm/server.c +++ b/lib/libalpm/server.c @@ -49,13 +49,8 @@ pmserver_t *_alpm_server_new(const char *url) ALPM_LOG_FUNC; - server = malloc(sizeof(pmserver_t)); - if(server == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes\n"), sizeof(pmserver_t)); - RET_ERR(PM_ERR_MEMORY, NULL); - } + CALLOC(server, 1, sizeof(pmserver_t), RET_ERR(PM_ERR_MEMORY, NULL)); - memset(server, 0, sizeof(pmserver_t)); u = downloadParseURL(url); if(!u) { _alpm_log(PM_LOG_ERROR, _("url '%s' is invalid, ignoring\n"), url); @@ -122,10 +117,7 @@ static struct url *url_for_file(pmserver_t *server, const char *filename) int doclen = 0; doclen = strlen(server->s_url->doc) + strlen(filename) + 2; - doc = calloc(doclen, sizeof(char)); - if(!doc) { - RET_ERR(PM_ERR_MEMORY, NULL); - } + CALLOC(doc, doclen, sizeof(char), RET_ERR(PM_ERR_MEMORY, NULL)); snprintf(doc, doclen, "%s/%s", server->s_url->doc, filename); ret = downloadMakeURL(server->s_url->scheme, diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c index 5aa0beae..4900bda4 100644 --- a/lib/libalpm/sync.c +++ b/lib/libalpm/sync.c @@ -56,10 +56,7 @@ pmsyncpkg_t *_alpm_sync_new(int type, pmpkg_t *spkg, void *data) ALPM_LOG_FUNC; - if((sync = malloc(sizeof(pmsyncpkg_t))) == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes\n"), sizeof(pmsyncpkg_t)); - return(NULL); - } + CALLOC(sync, 1, sizeof(pmsyncpkg_t), RET_ERR(PM_ERR_MEMORY, NULL)); sync->type = type; sync->pkg = spkg; diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c index b8f87371..1f9f225a 100644 --- a/lib/libalpm/trans.c +++ b/lib/libalpm/trans.c @@ -229,10 +229,7 @@ pmtrans_t *_alpm_trans_new() ALPM_LOG_FUNC; - if((trans = malloc(sizeof(pmtrans_t))) == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes\n"), sizeof(pmtrans_t)); - return(NULL); - } + CALLOC(trans, 1, sizeof(pmtrans_t), RET_ERR(PM_ERR_MEMORY, NULL)); trans->targets = NULL; trans->packages = NULL; 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) |