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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/**
* Copyright 2019-2020 DigitalOcean Inc.
*
* 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.
*/
#include <pthread.h>
#include <stdbool.h>
// Public
#include "prom_alloc.h"
// Private
#include "prom_assert.h"
#include "prom_errors.h"
#include "prom_linked_list_i.h"
#include "prom_linked_list_t.h"
#include "prom_log.h"
#include "prom_map_i.h"
#include "prom_map_t.h"
#define PROM_MAP_INITIAL_SIZE 32
static void destroy_map_node_value_no_op(void *value) {}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// prom_map_node
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
prom_map_node_t *prom_map_node_new(const char *key, void *value, prom_map_node_free_value_fn free_value_fn) {
prom_map_node_t *self = prom_malloc(sizeof(prom_map_node_t));
self->key = prom_strdup(key);
self->value = value;
self->free_value_fn = free_value_fn;
return self;
}
int prom_map_node_destroy(prom_map_node_t *self) {
PROM_ASSERT(self != NULL);
if (self == NULL) return 0;
prom_free((void *)self->key);
self->key = NULL;
if (self->value != NULL) (*self->free_value_fn)(self->value);
self->value = NULL;
prom_free(self);
self = NULL;
return 0;
}
void prom_map_node_free(void *item) {
prom_map_node_t *map_node = (prom_map_node_t *)item;
prom_map_node_destroy(map_node);
}
prom_linked_list_compare_t prom_map_node_compare(void *item_a, void *item_b) {
prom_map_node_t *map_node_a = (prom_map_node_t *)item_a;
prom_map_node_t *map_node_b = (prom_map_node_t *)item_b;
return strcmp(map_node_a->key, map_node_b->key);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// prom_map
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
prom_map_t *prom_map_new() {
int r = 0;
prom_map_t *self = (prom_map_t *)prom_malloc(sizeof(prom_map_t));
self->size = 0;
self->max_size = PROM_MAP_INITIAL_SIZE;
self->keys = prom_linked_list_new();
if (self->keys == NULL) return NULL;
// These each key will be allocated once by prom_map_node_new and used here as well to save memory. With that said
// we will only have to deallocate each key once. That will happen on prom_map_node_destroy.
r = prom_linked_list_set_free_fn(self->keys, prom_linked_list_no_op_free);
if (r) {
prom_map_destroy(self);
return NULL;
}
self->addrs = prom_malloc(sizeof(prom_linked_list_t) * self->max_size);
self->free_value_fn = destroy_map_node_value_no_op;
for (int i = 0; i < self->max_size; i++) {
self->addrs[i] = prom_linked_list_new();
r = prom_linked_list_set_free_fn(self->addrs[i], prom_map_node_free);
if (r) {
prom_map_destroy(self);
return NULL;
}
r = prom_linked_list_set_compare_fn(self->addrs[i], prom_map_node_compare);
if (r) {
prom_map_destroy(self);
return NULL;
}
}
self->rwlock = (pthread_rwlock_t *)prom_malloc(sizeof(pthread_rwlock_t));
r = pthread_rwlock_init(self->rwlock, NULL);
if (r) {
PROM_LOG(PROM_PTHREAD_RWLOCK_INIT_ERROR);
prom_map_destroy(self);
return NULL;
}
return self;
}
int prom_map_destroy(prom_map_t *self) {
PROM_ASSERT(self != NULL);
int r = 0;
int ret = 0;
r = prom_linked_list_destroy(self->keys);
if (r) ret = r;
self->keys = NULL;
for (size_t i = 0; i < self->max_size; i++) {
r = prom_linked_list_destroy(self->addrs[i]);
if (r) ret = r;
self->addrs[i] = NULL;
}
prom_free(self->addrs);
self->addrs = NULL;
r = pthread_rwlock_destroy(self->rwlock);
if (r) {
PROM_LOG(PROM_PTHREAD_RWLOCK_DESTROY_ERROR)
ret = r;
}
prom_free(self->rwlock);
self->rwlock = NULL;
prom_free(self);
self = NULL;
return ret;
}
static size_t prom_map_get_index_internal(const char *key, size_t *size, size_t *max_size) {
size_t index;
size_t a = 31415, b = 27183;
for (index = 0; *key != '\0'; key++, a = a * b % (*max_size - 1)) {
index = (a * index + *key) % *max_size;
}
return index;
}
/**
* @brief API PRIVATE hash function that returns an array index from the given key and prom_map.
*
* The algorithm is based off of Horner's method. In a simpler version, you set the return value to 0. Next, for each
* character in the string, you add the integer value of the current character to the product of the prime number and
* the current return value, set the result to the return value, then finally return the return value.
*
* In this version of the algorithm, we attempt to achieve a probabily of key to index conversion collisions to
* 1/M (with M being the max_size of the map). This optimizes dispersion and consequently, evens out the performance
* for gets and sets for each item. Instead of using a fixed prime number, we generate a coefficient for each iteration
* through the loop.
*
* Reference:
* * Algorithms in C: Third Edition by Robert Sedgewick, p579
*/
size_t prom_map_get_index(prom_map_t *self, const char *key) {
return prom_map_get_index_internal(key, &self->size, &self->max_size);
}
static void *prom_map_get_internal(const char *key, size_t *size, size_t *max_size, prom_linked_list_t *keys,
prom_linked_list_t **addrs, prom_map_node_free_value_fn free_value_fn) {
size_t index = prom_map_get_index_internal(key, size, max_size);
prom_linked_list_t *list = addrs[index];
prom_map_node_t *temp_map_node = prom_map_node_new(key, NULL, free_value_fn);
for (prom_linked_list_node_t *current_node = list->head; current_node != NULL; current_node = current_node->next) {
prom_map_node_t *current_map_node = (prom_map_node_t *)current_node->item;
prom_linked_list_compare_t result = prom_linked_list_compare(list, current_map_node, temp_map_node);
if (result == PROM_EQUAL) {
prom_map_node_destroy(temp_map_node);
temp_map_node = NULL;
return current_map_node->value;
}
}
prom_map_node_destroy(temp_map_node);
temp_map_node = NULL;
return NULL;
}
void *prom_map_get(prom_map_t *self, const char *key) {
PROM_ASSERT(self != NULL);
int r = 0;
r = pthread_rwlock_wrlock(self->rwlock);
if (r) {
PROM_LOG(PROM_PTHREAD_RWLOCK_LOCK_ERROR);
NULL;
}
void *payload =
prom_map_get_internal(key, &self->size, &self->max_size, self->keys, self->addrs, self->free_value_fn);
r = pthread_rwlock_unlock(self->rwlock);
if (r) {
PROM_LOG(PROM_PTHREAD_RWLOCK_UNLOCK_ERROR);
return NULL;
}
return payload;
}
static int prom_map_set_internal(const char *key, void *value, size_t *size, size_t *max_size, prom_linked_list_t *keys,
prom_linked_list_t **addrs, prom_map_node_free_value_fn free_value_fn,
bool destroy_current_value) {
prom_map_node_t *map_node = prom_map_node_new(key, value, free_value_fn);
if (map_node == NULL) return 1;
size_t index = prom_map_get_index_internal(key, size, max_size);
prom_linked_list_t *list = addrs[index];
for (prom_linked_list_node_t *current_node = list->head; current_node != NULL; current_node = current_node->next) {
prom_map_node_t *current_map_node = (prom_map_node_t *)current_node->item;
prom_linked_list_compare_t result = prom_linked_list_compare(list, current_map_node, map_node);
if (result == PROM_EQUAL) {
if (destroy_current_value) {
free_value_fn(current_map_node->value);
current_map_node->value = NULL;
}
prom_free((char *)current_map_node->key);
current_map_node->key = NULL;
prom_free(current_map_node);
current_map_node = NULL;
current_node->item = map_node;
return 0;
}
}
prom_linked_list_append(list, map_node);
prom_linked_list_append(keys, (char *)map_node->key);
(*size)++;
return 0;
}
int prom_map_ensure_space(prom_map_t *self) {
PROM_ASSERT(self != NULL);
int r = 0;
if (self->size <= self->max_size / 2) {
return 0;
}
// Increase the max size
size_t new_max = self->max_size * 2;
size_t new_size = 0;
// Create a new list of keys
prom_linked_list_t *new_keys = prom_linked_list_new();
if (new_keys == NULL) return 1;
r = prom_linked_list_set_free_fn(new_keys, prom_linked_list_no_op_free);
if (r) return r;
// Create a new array of addrs
prom_linked_list_t **new_addrs = prom_malloc(sizeof(prom_linked_list_t) * new_max);
// Initialize the new array
for (int i = 0; i < new_max; i++) {
new_addrs[i] = prom_linked_list_new();
r = prom_linked_list_set_free_fn(new_addrs[i], prom_map_node_free);
if (r) return r;
r = prom_linked_list_set_compare_fn(new_addrs[i], prom_map_node_compare);
if (r) return r;
}
// Iterate through each linked-list at each memory region in the map's backbone
for (int i = 0; i < self->max_size; i++) {
// Create a new map node for each node in the linked list and insert it into the new map. Afterwards, deallocate
// the old map node
prom_linked_list_t *list = self->addrs[i];
prom_linked_list_node_t *current_node = list->head;
while (current_node != NULL) {
prom_map_node_t *map_node = (prom_map_node_t *)current_node->item;
r = prom_map_set_internal(map_node->key, map_node->value, &new_size, &new_max, new_keys, new_addrs,
self->free_value_fn, false);
if (r) return r;
prom_linked_list_node_t *next = current_node->next;
prom_free(current_node);
current_node = NULL;
prom_free((void *)map_node->key);
map_node->key = NULL;
prom_free(map_node);
map_node = NULL;
current_node = next;
}
// We're done deallocating each map node in the linked list, so deallocate the linked-list object
prom_free(self->addrs[i]);
self->addrs[i] = NULL;
}
// Destroy the collection of keys in the map
prom_linked_list_destroy(self->keys);
self->keys = NULL;
// Deallocate the backbone of the map
prom_free(self->addrs);
self->addrs = NULL;
// Update the members of the current map
self->size = new_size;
self->max_size = new_max;
self->keys = new_keys;
self->addrs = new_addrs;
return 0;
}
int prom_map_set(prom_map_t *self, const char *key, void *value) {
PROM_ASSERT(self != NULL);
int r = 0;
r = pthread_rwlock_wrlock(self->rwlock);
if (r) {
PROM_LOG(PROM_PTHREAD_RWLOCK_LOCK_ERROR);
return r;
}
r = prom_map_ensure_space(self);
if (r) {
int rr = 0;
rr = pthread_rwlock_unlock(self->rwlock);
if (rr) {
PROM_LOG(PROM_PTHREAD_RWLOCK_UNLOCK_ERROR);
return rr;
} else {
return r;
}
}
r = prom_map_set_internal(key, value, &self->size, &self->max_size, self->keys, self->addrs, self->free_value_fn,
true);
if (r) {
int rr = 0;
rr = pthread_rwlock_unlock(self->rwlock);
if (rr) {
PROM_LOG(PROM_PTHREAD_RWLOCK_UNLOCK_ERROR);
return rr;
} else {
return r;
}
}
r = pthread_rwlock_unlock(self->rwlock);
if (r) {
PROM_LOG(PROM_PTHREAD_RWLOCK_UNLOCK_ERROR);
}
return r;
}
static int prom_map_delete_internal(const char *key, size_t *size, size_t *max_size, prom_linked_list_t *keys,
prom_linked_list_t **addrs, prom_map_node_free_value_fn free_value_fn) {
int r = 0;
size_t index = prom_map_get_index_internal(key, size, max_size);
prom_linked_list_t *list = addrs[index];
prom_map_node_t *temp_map_node = prom_map_node_new(key, NULL, free_value_fn);
for (prom_linked_list_node_t *current_node = list->head; current_node != NULL; current_node = current_node->next) {
prom_map_node_t *current_map_node = (prom_map_node_t *)current_node->item;
prom_linked_list_compare_t result = prom_linked_list_compare(list, current_map_node, temp_map_node);
if (result == PROM_EQUAL) {
r = prom_linked_list_remove(list, current_node);
if (r) return r;
r = prom_linked_list_remove(keys, (char *)current_map_node->key);
if (r) return r;
(*size)--;
break;
}
}
r = prom_map_node_destroy(temp_map_node);
temp_map_node = NULL;
return r;
}
int prom_map_delete(prom_map_t *self, const char *key) {
PROM_ASSERT(self != NULL);
int r = 0;
int ret = 0;
r = pthread_rwlock_wrlock(self->rwlock);
if (r) {
PROM_LOG(PROM_PTHREAD_RWLOCK_LOCK_ERROR);
ret = r;
}
r = prom_map_delete_internal(key, &self->size, &self->max_size, self->keys, self->addrs, self->free_value_fn);
if (r) ret = r;
r = pthread_rwlock_unlock(self->rwlock);
if (r) {
PROM_LOG(PROM_PTHREAD_RWLOCK_UNLOCK_ERROR);
ret = r;
}
return ret;
}
int prom_map_set_free_value_fn(prom_map_t *self, prom_map_node_free_value_fn free_value_fn) {
PROM_ASSERT(self != NULL);
self->free_value_fn = free_value_fn;
return 0;
}
size_t prom_map_size(prom_map_t *self) {
PROM_ASSERT(self != NULL);
return self->size;
}
|