summaryrefslogtreecommitdiffstats
path: root/lib/libalpm/pkghash.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libalpm/pkghash.c')
-rw-r--r--lib/libalpm/pkghash.c85
1 files changed, 40 insertions, 45 deletions
diff --git a/lib/libalpm/pkghash.c b/lib/libalpm/pkghash.c
index 6dc43243..f6207ada 100644
--- a/lib/libalpm/pkghash.c
+++ b/lib/libalpm/pkghash.c
@@ -17,9 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <errno.h>
+
#include "pkghash.h"
#include "util.h"
-#include "log.h"
/* List of primes for possible sizes of hash tables.
*
@@ -50,16 +51,12 @@ static const size_t prime_list[] =
};
/* Allocate a hash table with at least "size" buckets */
-pmpkghash_t *_alpm_pkghash_create(size_t size)
+alpm_pkghash_t *_alpm_pkghash_create(size_t size)
{
- pmpkghash_t *hash = NULL;
+ alpm_pkghash_t *hash = NULL;
size_t i, loopsize;
- MALLOC(hash, sizeof(pmpkghash_t), RET_ERR(PM_ERR_MEMORY, NULL));
-
- hash->list = NULL;
- hash->entries = 0;
- hash->buckets = 0;
+ CALLOC(hash, 1, sizeof(alpm_pkghash_t), return NULL);
loopsize = sizeof(prime_list) / sizeof(*prime_list);
for(i = 0; i < loopsize; i++) {
@@ -70,18 +67,18 @@ pmpkghash_t *_alpm_pkghash_create(size_t size)
}
if(hash->buckets < size) {
- _alpm_log(PM_LOG_ERROR, _("database larger than maximum size\n"));
+ errno = ERANGE;
free(hash);
- return(NULL);
+ return NULL;
}
- CALLOC(hash->hash_table, hash->buckets, sizeof(alpm_list_t*), \
- free(hash); RET_ERR(PM_ERR_MEMORY, NULL));
+ CALLOC(hash->hash_table, hash->buckets, sizeof(alpm_list_t *), \
+ free(hash); return NULL);
- return(hash);
+ return hash;
}
-static size_t get_hash_position(unsigned long name_hash, pmpkghash_t *hash)
+static size_t get_hash_position(unsigned long name_hash, alpm_pkghash_t *hash)
{
size_t position;
@@ -92,13 +89,13 @@ static size_t get_hash_position(unsigned long name_hash, pmpkghash_t *hash)
position = (position + 1) % hash->buckets;
}
- return(position);
+ return position;
}
/* Expand the hash table size to the next increment and rebin the entries */
-static pmpkghash_t *rehash(pmpkghash_t *oldhash)
+static alpm_pkghash_t *rehash(alpm_pkghash_t *oldhash)
{
- pmpkghash_t *newhash;
+ alpm_pkghash_t *newhash;
size_t newsize, position, i;
/* Hash tables will need resized in two cases:
@@ -123,7 +120,7 @@ static pmpkghash_t *rehash(pmpkghash_t *oldhash)
newhash = _alpm_pkghash_create(newsize);
if(newhash == NULL) {
/* creation of newhash failed, stick with old one... */
- return(oldhash);
+ return oldhash;
}
newhash->list = oldhash->list;
@@ -131,7 +128,7 @@ static pmpkghash_t *rehash(pmpkghash_t *oldhash)
for(i = 0; i < oldhash->buckets; i++) {
if(oldhash->hash_table[i] != NULL) {
- pmpkg_t *package = oldhash->hash_table[i]->data;
+ alpm_pkg_t *package = oldhash->hash_table[i]->data;
position = get_hash_position(package->name_hash, newhash);
@@ -144,16 +141,16 @@ static pmpkghash_t *rehash(pmpkghash_t *oldhash)
_alpm_pkghash_free(oldhash);
- return(newhash);
+ return newhash;
}
-static pmpkghash_t *pkghash_add_pkg(pmpkghash_t *hash, pmpkg_t *pkg, int sorted)
+static alpm_pkghash_t *pkghash_add_pkg(alpm_pkghash_t *hash, alpm_pkg_t *pkg, int sorted)
{
alpm_list_t *ptr;
size_t position;
if(pkg == NULL || hash == NULL) {
- return(hash);
+ return hash;
}
if((hash->entries + 1) / MAX_HASH_LOAD > hash->buckets) {
@@ -164,7 +161,7 @@ static pmpkghash_t *pkghash_add_pkg(pmpkghash_t *hash, pmpkg_t *pkg, int sorted)
ptr = calloc(1, sizeof(alpm_list_t));
if(ptr == NULL) {
- return(hash);
+ return hash;
}
ptr->data = pkg;
@@ -179,20 +176,20 @@ static pmpkghash_t *pkghash_add_pkg(pmpkghash_t *hash, pmpkg_t *pkg, int sorted)
}
hash->entries += 1;
- return(hash);
+ return hash;
}
-pmpkghash_t *_alpm_pkghash_add(pmpkghash_t *hash, pmpkg_t *pkg)
+alpm_pkghash_t *_alpm_pkghash_add(alpm_pkghash_t *hash, alpm_pkg_t *pkg)
{
- return(pkghash_add_pkg(hash, pkg, 0));
+ return pkghash_add_pkg(hash, pkg, 0);
}
-pmpkghash_t *_alpm_pkghash_add_sorted(pmpkghash_t *hash, pmpkg_t *pkg)
+alpm_pkghash_t *_alpm_pkghash_add_sorted(alpm_pkghash_t *hash, alpm_pkg_t *pkg)
{
- return(pkghash_add_pkg(hash, pkg, 1));
+ return pkghash_add_pkg(hash, pkg, 1);
}
-static size_t move_one_entry(pmpkghash_t *hash, size_t start, size_t end)
+static size_t move_one_entry(alpm_pkghash_t *hash, size_t start, size_t end)
{
/* Iterate backwards from 'end' to 'start', seeing if any of the items
* would hash to 'start'. If we find one, we move it there and break. If
@@ -203,7 +200,7 @@ static size_t move_one_entry(pmpkghash_t *hash, size_t start, size_t end)
* 'start' we can stop this madness. */
while(end != start) {
alpm_list_t *i = hash->hash_table[end];
- pmpkg_t *info = i->data;
+ alpm_pkg_t *info = i->data;
size_t new_position = get_hash_position(info->name_hash, hash);
if(new_position == start) {
@@ -217,7 +214,7 @@ static size_t move_one_entry(pmpkghash_t *hash, size_t start, size_t end)
* e.g. (47 + 0 - 1) % 47 == 46 */
end = (hash->buckets + end - 1) % hash->buckets;
}
- return(end);
+ return end;
}
/**
@@ -229,8 +226,8 @@ static size_t move_one_entry(pmpkghash_t *hash, size_t start, size_t end)
*
* @return the resultant hash
*/
-pmpkghash_t *_alpm_pkghash_remove(pmpkghash_t *hash, pmpkg_t *pkg,
- pmpkg_t **data)
+alpm_pkghash_t *_alpm_pkghash_remove(alpm_pkghash_t *hash, alpm_pkg_t *pkg,
+ alpm_pkg_t **data)
{
alpm_list_t *i;
size_t position;
@@ -240,12 +237,12 @@ pmpkghash_t *_alpm_pkghash_remove(pmpkghash_t *hash, pmpkg_t *pkg,
}
if(pkg == NULL || hash == NULL) {
- return(hash);
+ return hash;
}
position = pkg->name_hash % hash->buckets;
while((i = hash->hash_table[position]) != NULL) {
- pmpkg_t *info = i->data;
+ alpm_pkg_t *info = i->data;
if(info->name_hash == pkg->name_hash &&
strcmp(info->name, pkg->name) == 0) {
@@ -277,16 +274,16 @@ pmpkghash_t *_alpm_pkghash_remove(pmpkghash_t *hash, pmpkg_t *pkg,
position = prev;
}
- return(hash);
+ return hash;
}
position = (position + 1) % hash->buckets;
}
- return(hash);
+ return hash;
}
-void _alpm_pkghash_free(pmpkghash_t *hash)
+void _alpm_pkghash_free(alpm_pkghash_t *hash)
{
size_t i;
if(hash != NULL) {
@@ -298,16 +295,14 @@ void _alpm_pkghash_free(pmpkghash_t *hash)
free(hash);
}
-pmpkg_t *_alpm_pkghash_find(pmpkghash_t *hash, const char *name)
+alpm_pkg_t *_alpm_pkghash_find(alpm_pkghash_t *hash, const char *name)
{
alpm_list_t *lp;
unsigned long name_hash;
size_t position;
- ALPM_LOG_FUNC;
-
if(name == NULL || hash == NULL) {
- return(NULL);
+ return NULL;
}
name_hash = _alpm_hash_sdbm(name);
@@ -315,16 +310,16 @@ pmpkg_t *_alpm_pkghash_find(pmpkghash_t *hash, const char *name)
position = name_hash % hash->buckets;
while((lp = hash->hash_table[position]) != NULL) {
- pmpkg_t *info = lp->data;
+ alpm_pkg_t *info = lp->data;
if(info->name_hash == name_hash && strcmp(info->name, name) == 0) {
- return(info);
+ return info;
}
position = (position + 1) % hash->buckets;
}
- return(NULL);
+ return NULL;
}
/* vim: set ts=2 sw=2 noet: */