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>
This commit is contained in:
Dan McGee 2007-10-29 01:00:52 -05:00
parent fe3a461703
commit cc754bc6e3
11 changed files with 30 additions and 78 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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, ' ');

View File

@ -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);

View File

@ -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);
}

View File

@ -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;

View File

@ -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);

View File

@ -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,

View File

@ -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;

View File

@ -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;

View File

@ -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)