return 0;
}
+/* similar to hash_add(), but fails if key already exists */
+int hash_add_unique(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 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 hash_entry);
+ struct hash_entry *tmp = realloc(bucket->entries, size);
+ if (tmp == NULL)
+ return -errno;
+ bucket->entries = tmp;
+ bucket->total = new_total;
+ }
+
+ entry = bucket->entries;
+ entry_end = entry + bucket->used;
+ for (; entry < entry_end; entry++) {
+ int c = strcmp(key, entry->key);
+ if (c == 0)
+ return -EEXIST;
+ else if (c < 0) {
+ memmove(entry + 1, entry,
+ (entry_end - entry) * sizeof(struct hash_entry));
+ break;
+ }
+ }
+
+ entry->key = key;
+ entry->value = value;
+ bucket->used++;
+ hash->count++;
+ return 0;
+}
+
static int hash_entry_cmp(const void *pa, const void *pb)
{
const struct hash_entry *a = pa;
return 0;
}
+
+unsigned int hash_get_count(const struct hash *hash)
+{
+ return hash->count;
+}
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_add_unique(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);
+unsigned int hash_get_count(const struct hash *hash);
/* libkmod-file.c */
struct kmod_file *kmod_file_open(const char *filename) __must_check __attribute__((nonnull(1)));