[pacman-dev] [PATCH 1/3] alpm_list: use malloc instead of calloc
In every case we were calling calloc, the struct we allocated (or the memory to be used) is fully specified later in the method. For alpm_list_t allocations, we always set all of data, next, and prev. For list copying and transforming to an array, we always copy the entire data element, so no need to zero it first. Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/alpm_list.c | 8 ++++---- lib/libalpm/pkghash.c | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/libalpm/alpm_list.c b/lib/libalpm/alpm_list.c index 274d3af..15286aa 100644 --- a/lib/libalpm/alpm_list.c +++ b/lib/libalpm/alpm_list.c @@ -89,7 +89,7 @@ alpm_list_t SYMEXPORT *alpm_list_add(alpm_list_t *list, void *data) { alpm_list_t *ptr, *lp; - ptr = calloc(1, sizeof(alpm_list_t)); + ptr = malloc(sizeof(alpm_list_t)); if(ptr == NULL) { return list; } @@ -127,7 +127,7 @@ alpm_list_t SYMEXPORT *alpm_list_add_sorted(alpm_list_t *list, void *data, alpm_ } else { alpm_list_t *add = NULL, *prev = NULL, *next = list; - add = calloc(1, sizeof(alpm_list_t)); + add = malloc(sizeof(alpm_list_t)); if(add == NULL) { return list; } @@ -470,7 +470,7 @@ alpm_list_t SYMEXPORT *alpm_list_copy_data(const alpm_list_t *list, const alpm_list_t *lp = list; alpm_list_t *newlist = NULL; while(lp) { - void *newdata = calloc(1, size); + void *newdata = malloc(size); if(newdata) { memcpy(newdata, lp->data, size); newlist = alpm_list_add(newlist, newdata); @@ -775,7 +775,7 @@ void SYMEXPORT *alpm_list_to_array(const alpm_list_t *list, size_t n, return NULL; } - array = calloc(n, size); + array = malloc(n * size); if(array == NULL) { return NULL; } diff --git a/lib/libalpm/pkghash.c b/lib/libalpm/pkghash.c index f6207ad..963ba25 100644 --- a/lib/libalpm/pkghash.c +++ b/lib/libalpm/pkghash.c @@ -159,19 +159,19 @@ static alpm_pkghash_t *pkghash_add_pkg(alpm_pkghash_t *hash, alpm_pkg_t *pkg, in position = get_hash_position(pkg->name_hash, hash); - ptr = calloc(1, sizeof(alpm_list_t)); + ptr = malloc(sizeof(alpm_list_t)); if(ptr == NULL) { return hash; } ptr->data = pkg; - ptr->next = NULL; ptr->prev = ptr; + ptr->next = NULL; hash->hash_table[position] = ptr; - if(!sorted){ + if(!sorted) { hash->list = alpm_list_join(hash->list, ptr); - }else{ + } else { hash->list = alpm_list_mmerge(hash->list, ptr, _alpm_pkg_cmp); } -- 1.7.6.4
This saves a lot of unnecessary work since we don't need any of the other fields in the stat struct. Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/be_sync.c | 7 ++----- 1 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/libalpm/be_sync.c b/lib/libalpm/be_sync.c index 4864616..53777d9 100644 --- a/lib/libalpm/be_sync.c +++ b/lib/libalpm/be_sync.c @@ -454,11 +454,8 @@ static int sync_db_populate(alpm_db_t *db) } while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) { - const struct stat *st; - - st = archive_entry_stat(entry); - - if(S_ISDIR(st->st_mode)) { + mode_t mode = archive_entry_mode(entry); + if(S_ISDIR(mode)) { continue; } else { /* we have desc, depends or deltas - parse it */ -- 1.7.6.4
There was only one simple to handle case where we left a field uninitialized; set it to NULL and use malloc() instead. Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/deps.c | 8 +++++--- 1 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c index c99701e..6040158 100644 --- a/lib/libalpm/deps.c +++ b/lib/libalpm/deps.c @@ -409,14 +409,14 @@ int _alpm_depcmp(alpm_pkg_t *pkg, alpm_depend_t *dep) alpm_depend_t *_alpm_splitdep(const char *depstring) { alpm_depend_t *depend; - const char *ptr, *version = NULL; + const char *ptr, *version; size_t deplen; if(depstring == NULL) { return NULL; } - CALLOC(depend, 1, sizeof(alpm_depend_t), return NULL); + MALLOC(depend, sizeof(alpm_depend_t), return NULL); deplen = strlen(depstring); /* Find a version comparator if one exists. If it does, set the type and @@ -442,8 +442,10 @@ alpm_depend_t *_alpm_splitdep(const char *depstring) depend->mod = ALPM_DEP_MOD_EQ; version = ptr + 1; } else { - /* no version specified, leave version and ptr NULL */ + /* no version specified, leave ptr NULL and set version to NULL */ depend->mod = ALPM_DEP_MOD_ANY; + depend->version = NULL; + version = NULL; } /* copy the right parts to the right places */ -- 1.7.6.4
participants (1)
-
Dan McGee