1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <platform.h>
#include <hash_map_priv.h>
#include <alloc.h>
#include <misc_lib.h>
#define MAX_HASHMAP_BUCKETS (1 << 30)
#define MIN_HASHMAP_BUCKETS (1 << 5)
#define MAX_LOAD_FACTOR 0.75
#define MIN_LOAD_FACTOR 0.35
HashMap *HashMapNew(MapHashFn hash_fn, MapKeyEqualFn equal_fn,
MapDestroyDataFn destroy_key_fn,
MapDestroyDataFn destroy_value_fn,
size_t init_size)
{
HashMap *map = xcalloc(1, sizeof(HashMap));
map->hash_fn = hash_fn;
map->equal_fn = equal_fn;
map->destroy_key_fn = destroy_key_fn;
map->destroy_value_fn = destroy_value_fn;
/* make sure size is in the bounds */
init_size = MIN(MAX(init_size, MIN_HASHMAP_BUCKETS), MAX_HASHMAP_BUCKETS);
if (ISPOW2(init_size))
{
map->size = init_size;
}
else
{
map->size = UpperPowerOfTwo(init_size);
}
map->init_size = map->size;
map->buckets = xcalloc(map->size, sizeof(BucketListItem *));
map->load = 0;
map->max_threshold = (size_t) map->size * MAX_LOAD_FACTOR;
map->min_threshold = (size_t) map->size * MIN_LOAD_FACTOR;
return map;
}
static unsigned int HashMapGetBucket(const HashMap *map, const void *key)
{
assert(map != NULL);
unsigned int hash = map->hash_fn(key, 0);
assert (ISPOW2 (map->size));
return (hash & (map->size - 1));
}
static void HashMapResize(HashMap *map, size_t new_size)
{
size_t old_size;
BucketListItem **old_buckets;
old_size = map->size;
old_buckets = map->buckets;
map->size = new_size;
/* map->load stays the same */
map->max_threshold = (size_t) map->size * MAX_LOAD_FACTOR;
map->min_threshold = (size_t) map->size * MIN_LOAD_FACTOR;
map->buckets = xcalloc(map->size, sizeof(BucketListItem *));
for (size_t i = 0; i < old_size; ++i)
{
BucketListItem *item;
item = old_buckets[i];
old_buckets[i] = NULL;
while (item != NULL)
{
BucketListItem *next = item->next;
unsigned bucket = HashMapGetBucket(map, item->value.key);
item->next = map->buckets[bucket];
map->buckets[bucket] = item;
item = next;
}
}
free (old_buckets);
}
/**
* @retval true if value was preexisting in the map and got replaced.
*/
bool HashMapInsert(HashMap *map, void *key, void *value)
{
unsigned bucket = HashMapGetBucket(map, key);
for (BucketListItem *i = map->buckets[bucket]; i != NULL; i = i->next)
{
if (map->equal_fn(i->value.key, key))
{
/* Replace the key with the new one despite those two being the
* same, since the new key might be referenced somewhere inside
* the new value. */
map->destroy_key_fn(i->value.key);
map->destroy_value_fn(i->value.value);
i->value.key = key;
i->value.value = value;
return true;
}
}
BucketListItem *i = xcalloc(1, sizeof(BucketListItem));
i->value.key = key;
i->value.value = value;
i->next = map->buckets[bucket];
map->buckets[bucket] = i;
map->load++;
if ((map->load > map->max_threshold) && (map->size < MAX_HASHMAP_BUCKETS))
{
HashMapResize(map, map->size << 1);
}
return false;
}
bool HashMapRemove(HashMap *map, const void *key)
{
unsigned bucket = HashMapGetBucket(map, key);
/*
* prev points to a previous "next" pointer to rewrite it in case value need
* to be deleted
*/
for (BucketListItem **prev = &map->buckets[bucket];
*prev != NULL;
prev = &((*prev)->next))
{
BucketListItem *cur = *prev;
if (map->equal_fn(cur->value.key, key))
{
map->destroy_key_fn(cur->value.key);
map->destroy_value_fn(cur->value.value);
*prev = cur->next;
free(cur);
map->load--;
if ((map->load < map->min_threshold) && (map->size > map->init_size))
{
HashMapResize(map, map->size >> 1);
}
return true;
}
}
return false;
}
MapKeyValue *HashMapGet(const HashMap *map, const void *key)
{
unsigned bucket = HashMapGetBucket(map, key);
for (BucketListItem *cur = map->buckets[bucket];
cur != NULL;
cur = cur->next)
{
if (map->equal_fn(cur->value.key, key))
{
return &cur->value;
}
}
return NULL;
}
static void FreeBucketListItem(HashMap *map, BucketListItem *item)
{
if (item->next)
{
FreeBucketListItem(map, item->next);
}
map->destroy_key_fn(item->value.key);
map->destroy_value_fn(item->value.value);
free(item);
map->load--;
}
/* Do not destroy value item */
static void FreeBucketListItemSoft(HashMap *map, BucketListItem *item)
{
if (item->next)
{
FreeBucketListItemSoft(map, item->next);
}
map->destroy_key_fn(item->value.key);
free(item);
map->load--;
}
void HashMapClear(HashMap *map)
{
for (size_t i = 0; i < map->size; ++i)
{
if (map->buckets[i])
{
FreeBucketListItem(map, map->buckets[i]);
}
map->buckets[i] = NULL;
}
assert(map->load == 0);
}
void HashMapSoftDestroy(HashMap *map)
{
if (map)
{
for (size_t i = 0; i < map->size; ++i)
{
if (map->buckets[i])
{
FreeBucketListItemSoft(map, map->buckets[i]);
}
map->buckets[i] = NULL;
}
free(map->buckets);
free(map);
}
}
void HashMapDestroy(HashMap *map)
{
if (map)
{
HashMapClear(map);
free(map->buckets);
free(map);
}
}
void HashMapPrintStats(const HashMap *hmap, FILE *f)
{
size_t *bucket_lengths;
size_t num_el = 0;
size_t num_buckets = 0;
bucket_lengths = xcalloc(hmap->size, sizeof(size_t));
for (size_t i = 0; i < hmap->size; i++)
{
BucketListItem *b = hmap->buckets[i];
if (b != NULL)
{
num_buckets++;
}
while (b != NULL)
{
num_el++;
bucket_lengths[i]++;
b = b->next;
}
}
fprintf(f, "\tTotal number of buckets: %5zu\n", hmap->size);
fprintf(f, "\tNumber of non-empty buckets: %5zu\n", num_buckets);
fprintf(f, "\tTotal number of elements: %5zu\n", num_el);
fprintf(f, "\tAverage elements per non-empty bucket (load factor): %5.2f\n",
(float) num_el / num_buckets);
fprintf(f, "\tThe 10 longest buckets are: \n");
for (int j = 0; j < 10; j++)
{
/* Find the maximum 10 times, zeroing it after printing it. */
int longest_bucket_id = 0;
for (size_t i = 0; i < hmap->size; i++)
{
if (bucket_lengths[i] > bucket_lengths[longest_bucket_id])
{
longest_bucket_id = i;
}
}
fprintf(f, "\t\tbucket %5d with %zu elements\n",
longest_bucket_id, bucket_lengths[longest_bucket_id]);
bucket_lengths[longest_bucket_id] = 0;
}
free(bucket_lengths);
}
/******************************************************************************/
HashMapIterator HashMapIteratorInit(HashMap *map)
{
return (HashMapIterator) { map, map->buckets[0], 0 };
}
MapKeyValue *HashMapIteratorNext(HashMapIterator *i)
{
while (i->cur == NULL)
{
if (++i->bucket >= i->map->size)
{
return NULL;
}
i->cur = i->map->buckets[i->bucket];
}
MapKeyValue *ret = &i->cur->value;
i->cur = i->cur->next;
return ret;
}
|