From: Lucas De Marchi Date: Tue, 27 Dec 2011 14:13:54 +0000 (-0200) Subject: Remove kmod_ prefix from hash implementation X-Git-Tag: v3~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=822913d74c0a3196803e948a4ec278606637232b;p=thirdparty%2Fkmod.git Remove kmod_ prefix from hash implementation In a future commit, hash implementation will be shared between libkmod and depmod. kmod_hash is not exported, so remove the namespace. --- diff --git a/libkmod/libkmod-hash.c b/libkmod/libkmod-hash.c index acdd8f89..0fd94100 100644 --- a/libkmod/libkmod-hash.c +++ b/libkmod/libkmod-hash.c @@ -25,30 +25,30 @@ #include #include -struct kmod_hash_entry { +struct hash_entry { const char *key; const void *value; }; -struct kmod_hash_bucket { - struct kmod_hash_entry *entries; +struct hash_bucket { + struct hash_entry *entries; unsigned int used; unsigned int total; }; -struct kmod_hash { +struct hash { unsigned int count; unsigned int step; unsigned int n_buckets; void (*free_value)(void *value); - struct kmod_hash_bucket buckets[]; + struct hash_bucket buckets[]; }; -struct kmod_hash *kmod_hash_new(unsigned int n_buckets, +struct hash *hash_new(unsigned int n_buckets, void (*free_value)(void *value)) { - struct kmod_hash *hash = calloc(1, sizeof(struct kmod_hash) + - n_buckets * sizeof(struct kmod_hash_bucket)); + struct hash *hash = calloc(1, sizeof(struct hash) + + n_buckets * sizeof(struct hash_bucket)); if (hash == NULL) return NULL; hash->n_buckets = n_buckets; @@ -61,14 +61,14 @@ struct kmod_hash *kmod_hash_new(unsigned int n_buckets, return hash; } -void kmod_hash_free(struct kmod_hash *hash) +void hash_free(struct hash *hash) { - struct kmod_hash_bucket *bucket, *bucket_end; + struct hash_bucket *bucket, *bucket_end; bucket = hash->buckets; bucket_end = bucket + hash->n_buckets; for (; bucket < bucket_end; bucket++) { if (hash->free_value) { - struct kmod_hash_entry *entry, *entry_end; + struct hash_entry *entry, *entry_end; entry = bucket->entries; entry_end = entry + bucket->used; for (; entry < entry_end; entry++) @@ -137,18 +137,18 @@ static inline unsigned int hash_superfast(const char *key, unsigned int len) * none of key or value are copied, just references are remembered as is, * make sure they are live while pair exists in hash! */ -int kmod_hash_add(struct kmod_hash *hash, const char *key, const void *value) +int hash_add(struct hash *hash, const char *key, const void *value) { unsigned int keylen = strlen(key); unsigned int hashval = hash_superfast(key, keylen); unsigned int pos = hashval % hash->n_buckets; - struct kmod_hash_bucket *bucket = hash->buckets + pos; - struct kmod_hash_entry *entry, *entry_end; + struct hash_bucket *bucket = hash->buckets + pos; + struct hash_entry *entry, *entry_end; if (bucket->used + 1 >= bucket->total) { unsigned new_total = bucket->total + hash->step; - size_t size = new_total * sizeof(struct kmod_hash_entry); - struct kmod_hash_entry *tmp = realloc(bucket->entries, size); + size_t size = new_total * sizeof(struct hash_entry); + struct hash_entry *tmp = realloc(bucket->entries, size); if (tmp == NULL) return -errno; bucket->entries = tmp; @@ -165,7 +165,7 @@ int kmod_hash_add(struct kmod_hash *hash, const char *key, const void *value) return 0; } else if (c < 0) { memmove(entry + 1, entry, - (entry_end - entry) * sizeof(struct kmod_hash_entry)); + (entry_end - entry) * sizeof(struct hash_entry)); break; } } @@ -177,52 +177,52 @@ int kmod_hash_add(struct kmod_hash *hash, const char *key, const void *value) return 0; } -static int kmod_hash_entry_cmp(const void *pa, const void *pb) +static int hash_entry_cmp(const void *pa, const void *pb) { - const struct kmod_hash_entry *a = pa; - const struct kmod_hash_entry *b = pb; + const struct hash_entry *a = pa; + const struct hash_entry *b = pb; return strcmp(a->key, b->key); } -void *kmod_hash_find(const struct kmod_hash *hash, const char *key) +void *hash_find(const struct hash *hash, const char *key) { unsigned int keylen = strlen(key); unsigned int hashval = hash_superfast(key, keylen); unsigned int pos = hashval % hash->n_buckets; - const struct kmod_hash_bucket *bucket = hash->buckets + pos; - const struct kmod_hash_entry se = { + const struct hash_bucket *bucket = hash->buckets + pos; + const struct hash_entry se = { .key = key, .value = NULL }; - const struct kmod_hash_entry *entry = bsearch( + const struct hash_entry *entry = bsearch( &se, bucket->entries, bucket->used, - sizeof(struct kmod_hash_entry), kmod_hash_entry_cmp); + sizeof(struct hash_entry), hash_entry_cmp); if (entry == NULL) return NULL; return (void *)entry->value; } -int kmod_hash_del(struct kmod_hash *hash, const char *key) +int hash_del(struct hash *hash, const char *key) { unsigned int keylen = strlen(key); unsigned int hashval = hash_superfast(key, keylen); unsigned int pos = hashval % hash->n_buckets; unsigned int steps_used, steps_total; - struct kmod_hash_bucket *bucket = hash->buckets + pos; - struct kmod_hash_entry *entry, *entry_end; - const struct kmod_hash_entry se = { + struct hash_bucket *bucket = hash->buckets + pos; + struct hash_entry *entry, *entry_end; + const struct hash_entry se = { .key = key, .value = NULL }; entry = bsearch(&se, bucket->entries, bucket->used, - sizeof(struct kmod_hash_entry), kmod_hash_entry_cmp); + sizeof(struct hash_entry), hash_entry_cmp); if (entry == NULL) return -ENOENT; entry_end = bucket->entries + bucket->used; memmove(entry, entry + 1, - (entry_end - entry) * sizeof(struct kmod_hash_entry)); + (entry_end - entry) * sizeof(struct hash_entry)); bucket->used--; hash->count--; @@ -231,8 +231,8 @@ int kmod_hash_del(struct kmod_hash *hash, const char *key) steps_total = bucket->total / hash->step; if (steps_used + 1 < steps_total) { size_t size = (steps_used + 1) * - hash->step * sizeof(struct kmod_hash_entry); - struct kmod_hash_entry *tmp = realloc(bucket->entries, size); + hash->step * sizeof(struct hash_entry); + struct hash_entry *tmp = realloc(bucket->entries, size); if (tmp) { bucket->entries = tmp; bucket->total = (steps_used + 1) * hash->step; diff --git a/libkmod/libkmod-private.h b/libkmod/libkmod-private.h index f2bc1f28..87c8df49 100644 --- a/libkmod/libkmod-private.h +++ b/libkmod/libkmod-private.h @@ -129,12 +129,12 @@ void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd) void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd) __attribute__((nonnull(1))); /* libkmod-hash.c */ -struct kmod_hash; -struct kmod_hash *kmod_hash_new(unsigned int n_buckets, void (*free_value)(void *value)); -void kmod_hash_free(struct kmod_hash *hash); -int kmod_hash_add(struct kmod_hash *hash, const char *key, const void *value); -int kmod_hash_del(struct kmod_hash *hash, const char *key); -void *kmod_hash_find(const struct kmod_hash *hash, const char *key); +struct hash; +struct hash *hash_new(unsigned int n_buckets, void (*free_value)(void *value)); +void hash_free(struct hash *hash); +int hash_add(struct hash *hash, const char *key, const void *value); +int hash_del(struct hash *hash, const char *key); +void *hash_find(const struct hash *hash, const char *key); /* libkmod-file.c */ struct kmod_file *kmod_file_open(const char *filename) __must_check __attribute__((nonnull(1))); diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c index 779e16bc..4f49ff54 100644 --- a/libkmod/libkmod.c +++ b/libkmod/libkmod.c @@ -80,7 +80,7 @@ struct kmod_ctx { const void *userdata; char *dirname; struct kmod_config *config; - struct kmod_hash *modules_by_name; + struct hash *modules_by_name; struct index_mm *indexes[_KMOD_INDEX_LAST]; }; @@ -229,7 +229,7 @@ KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname, goto fail; } - ctx->modules_by_name = kmod_hash_new(KMOD_HASH_SIZE, NULL); + ctx->modules_by_name = hash_new(KMOD_HASH_SIZE, NULL); if (ctx->modules_by_name == NULL) { ERR(ctx, "could not create by-name hash\n"); goto fail; @@ -281,7 +281,7 @@ KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx) INFO(ctx, "context %p released\n", ctx); kmod_unload_resources(ctx); - kmod_hash_free(ctx->modules_by_name); + hash_free(ctx->modules_by_name); free(ctx->dirname); if (ctx->config) kmod_config_free(ctx->config); @@ -346,7 +346,7 @@ struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx, { struct kmod_module *mod; - mod = kmod_hash_find(ctx->modules_by_name, key); + mod = hash_find(ctx->modules_by_name, key); DBG(ctx, "get module name='%s' found=%p\n", key, mod); @@ -358,7 +358,7 @@ void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod, { DBG(ctx, "add %p key='%s'\n", mod, key); - kmod_hash_add(ctx->modules_by_name, key, mod); + hash_add(ctx->modules_by_name, key, mod); } void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod, @@ -366,7 +366,7 @@ void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod, { DBG(ctx, "del %p key='%s'\n", mod, key); - kmod_hash_del(ctx->modules_by_name, key); + hash_del(ctx->modules_by_name, key); } static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx,