From a653e1e86eb5af1621de97603c33222315d2d2c3 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sun, 23 Feb 2020 15:12:37 +0000 Subject: utils: Add a generic hashmap and tests for it In order to be able to use a generic hashmap in things such as the fs_backing_store we want one to exist. Here it is, along with some moderately comprehensive tests. Current limits: 1. All keys and values are owned by the hashmap 2. The hashmap, while capable of different bucket counts only has a single fixed count for now Signed-off-by: Daniel Silverstone --- utils/hashmap.c | 215 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils/hashmap.h | 137 ++++++++++++++++++++++++++++++++++++ 2 files changed, 352 insertions(+) create mode 100644 utils/hashmap.c create mode 100644 utils/hashmap.h (limited to 'utils') diff --git a/utils/hashmap.c b/utils/hashmap.c new file mode 100644 index 000000000..7ed19946b --- /dev/null +++ b/utils/hashmap.c @@ -0,0 +1,215 @@ +/* + * Copyright 2020 Daniel Silverstone + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "utils/hashmap.h" + +/** + * The default number of buckets in the hashmaps we create. + */ +#define DEFAULT_HASHMAP_BUCKETS (4091) + +/** + * Hashmaps have chains of entries in buckets. + */ +typedef struct hashmap_entry_s { + struct hashmap_entry_s **prevptr; + struct hashmap_entry_s *next; + void *key; + void *value; + uint32_t key_hash; +} hashmap_entry_t; + +/** + * The content of a hashmap + */ +struct hashmap_s { + /** + * The parameters to be used for this hashmap + */ + hashmap_parameters_t *params; + + /** + * The buckets for the hash chains + */ + hashmap_entry_t **buckets; + + /** + * The number of buckets in this map + */ + uint32_t bucket_count; +}; + +/* Exported function, documented in hashmap.h */ +hashmap_t * +hashmap_create(hashmap_parameters_t *params) +{ + hashmap_t *ret = malloc(sizeof(hashmap_t)); + + ret->params = params; + ret->bucket_count = DEFAULT_HASHMAP_BUCKETS; + ret->buckets = malloc(ret->bucket_count * sizeof(hashmap_entry_t *)); + memset(ret->buckets, 0, ret->bucket_count * sizeof(hashmap_entry_t *)); + + if (ret->buckets == NULL) { + free(ret); + return NULL; + } + + return ret; +} + +/* Exported function, documented in hashmap.h */ +void +hashmap_destroy(hashmap_t *hashmap) +{ + uint32_t bucket; + hashmap_entry_t *entry; + + for (bucket = 0; bucket < hashmap->bucket_count; bucket++) { + for (entry = hashmap->buckets[bucket]; + entry != NULL; + entry = entry->next) { + hashmap->params->value_destroy(entry->value); + hashmap->params->key_destroy(entry->key); + free(entry); + } + } + + free(hashmap->buckets); + free(hashmap); +} + +/* Exported function, documented in hashmap.h */ +void * +hashmap_lookup(hashmap_t *hashmap, void *key) +{ + uint32_t hash = hashmap->params->key_hash(key); + hashmap_entry_t *entry = hashmap->buckets[hash % hashmap->bucket_count]; + + for(;entry != NULL; entry = entry->next) { + if (entry->key_hash == hash) { + if (hashmap->params->key_eq(key, entry->key)) { + return entry->value; + } + } + } + + return NULL; +} + +/* Exported function, documented in hashmap.h */ +void * +hashmap_insert(hashmap_t *hashmap, void *key) +{ + uint32_t hash = hashmap->params->key_hash(key); + uint32_t bucket = hash % hashmap->bucket_count; + hashmap_entry_t *entry = hashmap->buckets[bucket]; + void *new_key, *new_value; + + for(;entry != NULL; entry = entry->next) { + if (entry->key_hash == hash) { + if (hashmap->params->key_eq(key, entry->key)) { + /* This key is already here */ + new_key = hashmap->params->key_clone(key); + if (new_key == NULL) { + /* Allocation failed */ + return NULL; + } + new_value = hashmap->params->value_alloc(entry->key); + if (new_value == NULL) { + /* Allocation failed */ + hashmap->params->key_destroy(new_key); + return NULL; + } + hashmap->params->value_destroy(entry->value); + hashmap->params->key_destroy(entry->key); + entry->value = new_value; + entry->key = new_key; + return entry->value; + } + } + } + + /* The key was not found in the map, so allocate a new entry */ + entry = malloc(sizeof(*entry)); + + if (entry == NULL) { + return NULL; + } + + memset(entry, 0, sizeof(*entry)); + + entry->key = hashmap->params->key_clone(key); + if (entry->key == NULL) { + goto err; + } + entry->key_hash = hash; + + entry->value = hashmap->params->value_alloc(entry->key); + if (entry->value == NULL) { + goto err; + } + + entry->prevptr = &(hashmap->buckets[bucket]); + entry->next = hashmap->buckets[bucket]; + if (entry->next != NULL) { + entry->next->prevptr = &entry->next; + } + + hashmap->buckets[bucket] = entry; + + return entry->value; + +err: + if (entry->value != NULL) + hashmap->params->value_destroy(entry->value); + if (entry->key != NULL) + hashmap->params->key_destroy(entry->key); + free(entry); + + return NULL; +} + +/* Exported function, documented in hashmap.h */ +bool +hashmap_remove(hashmap_t *hashmap, void *key) +{ + uint32_t hash = hashmap->params->key_hash(key); + + hashmap_entry_t *entry = hashmap->buckets[hash % hashmap->bucket_count]; + + for(;entry != NULL; entry = entry->next) { + if (entry->key_hash == hash) { + if (hashmap->params->key_eq(key, entry->key)) { + hashmap->params->value_destroy(entry->value); + hashmap->params->key_destroy(entry->key); + if (entry->next != NULL) { + entry->next->prevptr = entry->prevptr; + } + *entry->prevptr = entry->next; + free(entry); + return true; + } + } + } + + return false; +} diff --git a/utils/hashmap.h b/utils/hashmap.h new file mode 100644 index 000000000..4e1237ae9 --- /dev/null +++ b/utils/hashmap.h @@ -0,0 +1,137 @@ +/* + * Copyright 2020 Daniel Silverstone + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef NETSURF_HASHMAP_H +#define NETSURF_HASHMAP_H + +#include +#include + +/** + * Generic hashmap. + * + * Hashmaps take ownership of the keys inserted into them by means of a + * clone function in their parameters. They also manage the value memory + * directly. + */ +typedef struct hashmap_s hashmap_t; + +/** + * Parameters for hashmaps + */ +typedef struct { + /** + * A function which when called will clone a key and give + * ownership of the returned object to the hashmap + */ + void * (*key_clone)(void *key); + + /** + * A function which when given a key will return its hash. + */ + uint32_t (*key_hash)(void *key); + + /** + * A function to compare two keys and return if they are equal. + * Note: identity is not necessary, nor strict equality, so long + * as the function is a full equality model. + * (i.e. key1 == key2 => key2 == key1) + */ + bool (*key_eq)(void *key1, void *key2); + + /** + * A function which when called will destroy a key object + */ + void (*key_destroy)(void *key); + + /** + * A function which when called will allocate a value object + */ + void * (*value_alloc)(void *key); + + /** + * A function which when called will destroy a value object + */ + void (*value_destroy)(void *value); +} hashmap_parameters_t; + + +/** + * Create a hashmap + * + * The provided hashmap parameter table will be used for map operations + * which need to allocate/free etc. + * + * \param params The hashmap parameters for this map + */ +hashmap_t* hashmap_create(hashmap_parameters_t *params); + +/** + * Destroy a hashmap + * + * After this, all keys and values will have been destroyed and all memory + * associated with this hashmap will be invalidated. + * + * \param hashmap The hashmap to destroy + */ +void hashmap_destroy(hashmap_t *hashmap); + +/** + * Look up a key in a hashmap + * + * If the key has an associated value in the hashmap then the pointer to it + * is returned, otherwise NULL. + * + * \param hashmap The hashmap to look up the key inside + * \param key The key to look up in the hashmap + * \return A pointer to the value if found, NULL otherwise + */ +void* hashmap_lookup(hashmap_t *hashmap, void *key); + +/** + * Create an entry in a hashmap + * + * This creates a blank value using the parameters and then associates it with + * a clone of the given key, inserting it into the hashmap. If a value was + * present for the given key already, then it is destroyed first. + * + * NOTE: If allocation of the new value object fails, then any existing entry + * will be left alone, but NULL will be returned. + * + * \param hashmap The hashmap to insert into + * \param key The key to insert an entry for + * \return The value pointer for that key, or NULL if allocation failed. + */ +void *hashmap_insert(hashmap_t *hashmap, void *key); + +/** + * Remove an entry from the hashmap + * + * This will remove the entry for the given key from the hashmap + * If there is no such entry, this will safely do nothing. + * The value associated with the entry will be destroyed and so should not + * be used beyond calling this function. + * + * \param hashmap The hashmap to remove the entry from + * \param key The key to remove the entry for + * \return true if an entry was removed, false otherwise + */ +bool hashmap_remove(hashmap_t *hashmap, void *key); + + +#endif -- cgit v1.2.3