From f0e8dcb77a97d72cd21d76108fb6efeb4076f987 Mon Sep 17 00:00:00 2001 From: Taekyun Kim Date: Mon, 10 Aug 2015 18:15:19 +0900 Subject: [PATCH] pepper: Expose pepper_map_t structure and Add pepper_map_init() Change-Id: I3b0d19f1739aea1f30363cfbb000e15bebfc182c --- src/lib/pepper/pepper-utils.h | 27 +++++++++++++-- src/lib/pepper/utils-map.c | 81 ++++++++++++++++++++++--------------------- 2 files changed, 67 insertions(+), 41 deletions(-) diff --git a/src/lib/pepper/pepper-utils.h b/src/lib/pepper/pepper-utils.h index 75bfa07..2f81889 100644 --- a/src/lib/pepper/pepper-utils.h +++ b/src/lib/pepper/pepper-utils.h @@ -252,6 +252,29 @@ typedef int (*pepper_key_length_func_t)(const void *key); typedef int (*pepper_key_compare_func_t)(const void *key0, int key0_length, const void *key1, int key1_length); +struct pepper_map +{ + pepper_hash_func_t hash_func; + pepper_key_length_func_t key_length_func; + pepper_key_compare_func_t key_compare_func; + + int bucket_bits; + int bucket_size; + int bucket_mask; + pepper_map_entry_t **buckets; +}; + +PEPPER_API void +pepper_map_init(pepper_map_t *map, + int bucket_bits, + pepper_hash_func_t hash_func, + pepper_key_length_func_t key_length_func, + pepper_key_compare_func_t key_compare_func, + void *buckets); + +PEPPER_API void +pepper_map_fini(pepper_map_t *map); + PEPPER_API pepper_map_t * pepper_map_create(int bucket_bits, pepper_hash_func_t hash_func, @@ -259,10 +282,10 @@ pepper_map_create(int bucket_bits, pepper_key_compare_func_t key_compare_func); PEPPER_API void -pepper_map_clear(pepper_map_t *map); +pepper_map_destroy(pepper_map_t *map); PEPPER_API void -pepper_map_destroy(pepper_map_t *map); +pepper_map_clear(pepper_map_t *map); PEPPER_API void * pepper_map_get(pepper_map_t *map, const void *key); diff --git a/src/lib/pepper/utils-map.c b/src/lib/pepper/utils-map.c index 7f01461..5ee9320 100644 --- a/src/lib/pepper/utils-map.c +++ b/src/lib/pepper/utils-map.c @@ -9,18 +9,6 @@ struct pepper_map_entry pepper_map_entry_t *next; }; -struct pepper_map -{ - pepper_hash_func_t hash_func; - pepper_key_length_func_t key_length_func; - pepper_key_compare_func_t key_compare_func; - - int bucket_bits; - int bucket_size; - int bucket_mask; - pepper_map_entry_t **buckets; -}; - static inline int get_bucket_index(pepper_map_t *map, const void *key) { @@ -40,18 +28,14 @@ get_bucket(pepper_map_t *map, const void *key) return &map->buckets[get_bucket_index(map, key)]; } -pepper_map_t * -pepper_map_create(int bucket_bits, - pepper_hash_func_t hash_func, - pepper_key_length_func_t key_length_func, - pepper_key_compare_func_t key_compare_func) +PEPPER_API void +pepper_map_init(pepper_map_t *map, + int bucket_bits, + pepper_hash_func_t hash_func, + pepper_key_length_func_t key_length_func, + pepper_key_compare_func_t key_compare_func, + void *buckets) { - pepper_map_t *map; - - map = pepper_calloc(1, sizeof(pepper_map_t)); - if (!map) - return NULL; - map->hash_func = hash_func; map->key_length_func = key_length_func; map->key_compare_func = key_compare_func; @@ -60,21 +44,48 @@ pepper_map_create(int bucket_bits, map->bucket_size = 1 << bucket_bits; map->bucket_mask = map->bucket_size - 1; - map->buckets = pepper_calloc(map->bucket_size, sizeof(pepper_map_entry_t *)); - if (!map->buckets) - { - pepper_free(map); + map->buckets = buckets; +} + +PEPPER_API void +pepper_map_fini(pepper_map_t *map) +{ + pepper_map_clear(map); +} + +PEPPER_API pepper_map_t * +pepper_map_create(int bucket_bits, + pepper_hash_func_t hash_func, + pepper_key_length_func_t key_length_func, + pepper_key_compare_func_t key_compare_func) +{ + pepper_map_t *map; + int bucket_size = 1 << bucket_bits; + + map = pepper_calloc(1, sizeof(pepper_map_t) + bucket_size * sizeof(pepper_map_entry_t *)); + if (!map) return NULL; - } + pepper_map_init(map, bucket_bits, hash_func, key_length_func, key_compare_func, map + 1); return map; } -void +PEPPER_API void +pepper_map_destroy(pepper_map_t *map) +{ + pepper_map_fini(map); + pepper_free(map->buckets); + pepper_free(map); +} + +PEPPER_API void pepper_map_clear(pepper_map_t *map) { int i; + if (!map->buckets) + return; + for (i = 0; i < map->bucket_size; i++) { pepper_map_entry_t *curr = map->buckets[i]; @@ -94,15 +105,7 @@ pepper_map_clear(pepper_map_t *map) memset(map->buckets, 0x00, map->bucket_size * sizeof(pepper_map_entry_t *)); } -void -pepper_map_destroy(pepper_map_t *map) -{ - pepper_map_clear(map); - pepper_free(map->buckets); - pepper_free(map); -} - -void * +PEPPER_API void * pepper_map_get(pepper_map_t *map, const void *key) { pepper_map_entry_t *curr = *get_bucket(map, key); @@ -127,7 +130,7 @@ pepper_map_get(pepper_map_t *map, const void *key) return NULL; } -void +PEPPER_API void pepper_map_set(pepper_map_t *map, const void *key, void *data, pepper_free_func_t free_func) { pepper_map_entry_t **bucket = get_bucket(map, key); -- 2.7.4