-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc_hash.c
More file actions
431 lines (343 loc) · 12.1 KB
/
malloc_hash.c
File metadata and controls
431 lines (343 loc) · 12.1 KB
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* Copyright (c) 2013, Mario Ivancic
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// malloc_hash.c
#include "malloc_hash.h"
#include <stdlib.h>
#include <string.h>
#define DEFAULT_POOL_SIZE 2048
#define DEFAULT_POOL_INCREMENT 2048
#define MAX_LOAD_FACTOR 4
// allocete memory block and copy string str to it.
// return allocetad block or 0 if no memory
static char* mystrdup(const char* str)
{
char* p;
size_t i;
if(!str) return 0;
i = strlen(str);
p = (char*) malloc(i + 1);
if(!p) return 0;
memcpy(p, str, i + 1);
return p;
}
// create new hash object, return 0 if unsuccessful
malloc_hash_t* mhash_create(void)
{
malloc_hash_t *p = malloc(sizeof(*p));
return p;
}
// Initialize a new hashtable and allocate memory for hash storage.
// arguments:
// hash pointer to malloc_hash_t object
// pool_size size of hash pool (this is hash capacity)
// if pool_size == 0 dafault value will be used
// pool_increment this is added to pool_size when hash become full
// if pool_increment == 0 default_value will be used
// returns:
// MHASH_INVARG if invalid argument
// MHASH_NOMEM if no enough memory
// MHASH_OK if success
int mhash_init(malloc_hash_t* hash, int pool_size, int pool_increment)
{
hashentry_t **t, *p; // table and pool
int table_size = 1;
if(!hash) return MHASH_INVARG; // invalid argument
if(pool_size == 0) pool_size = DEFAULT_POOL_SIZE;
if(pool_increment == 0) pool_increment = DEFAULT_POOL_INCREMENT;
while(table_size < pool_size) table_size <<= 1;
t = malloc(table_size * sizeof(*t));
if(!t) return MHASH_NOMEM; // no memory
p = malloc(pool_size * sizeof(*p));
if(!p)
{
free(t);
return MHASH_NOMEM; // no memory
}
hashtable_init(&hash->hash, t, table_size, p, pool_size);
hash->pool_size = pool_size;
hash->pool_increment = pool_increment;
return MHASH_OK;
}
// Retrieve a key-value pair from a hash table when value is void* pointer
// Returns:
// MHASH_INVARG if invalid argument
// MHASH_NOTFOUND if key is not found
// MHASH_OK if success
// val pointer may be 0 if we just want to test for existance
int mhash_getptr( malloc_hash_t *hashtable, const char *key, const void **val)
{
if(!hashtable) return MHASH_INVARG;
return hashtable_get(&hashtable->hash, key, val);
}
// Retrieve a key-value pair from a hash table when value is integer
// Returns:
// MHASH_INVARG if invalid argument
// MHASH_NOTFOUND if key is not found
// MHASH_OK if success
// val pointer may be 0 if we just want to test for existance
int mhash_getint( malloc_hash_t *hashtable, const char *key, int *val)
{
const void* p;
int i;
if(!hashtable) return MHASH_INVARG;
i = hashtable_get(&hashtable->hash, key, &p);
if(val) *val = (int) p;
return i;
}
// Retrieve a key-value pair from a hash table when value is malloced string
// Retrieved string is strduped
// Returns:
// MHASH_INVARG if invalid argument
// MHASH_NOTFOUND if key is not found
// MHASH_NOMEM if no memory for string copy
// MHASH_OK if success
// val pointer may be 0 if we just want to test for existance
int mhash_getstr( malloc_hash_t *hashtable, const char *key, char **val)
{
char *p, *p2;
int i;
if(!hashtable) return MHASH_INVARG;
i = hashtable_get(&hashtable->hash, key, (const void**)&p);
if(i < 0) return i;
if(val)
{
p2 = mystrdup(p);
if(!p2) return MHASH_NOMEM;
*val = p2;
}
return MHASH_OK;
}
// Insert a key-value pair into a hash table when value is void* pointer
// key string is strduped
// Returns:
// MHASH_INVARG if invalid argument
// MHASH_NOMEM if no memory
// MHASH_DUPL if key already exists
// MHASH_OK if successful
int mhash_setptr( malloc_hash_t *hashtable, const char *key, const void *value )
{
char* k;
int i;
if(!hashtable) return MHASH_INVARG;
if(!key) return MHASH_INVARG;
k = mystrdup(key);
if(!k) return MHASH_NOMEM;
i = hashtable_set(&hashtable->hash, k, value);
if(i == HASHTABLE_DUPL) free(k);
else if(i == HASHTABLE_FULL)
{
// table is full, we have to allocate more space
// and try again
i = mhash_resize(hashtable);
if(i) free(k);
else
{
i = hashtable_set(&hashtable->hash, k, value);
if(i) free(k);
}
}
else if(i) free(k);
return i;
}
// Replace a key-value pair into a hash table when value is void* pointer
// Returns:
// MHASH_INVARG if invalid argument
// MHASH_NOMEM if no memory
// MHASH_OK if successful
int mhash_replaceptr( malloc_hash_t *hashtable, const char *key, const void *value )
{
const char *k;
int i;
// TODO: iskoristiti value_out da se vidi je li kljuc postojao ranije
// i ako jeste odmah osloboditi ovu kopiju kljuca. Na taj nacin bi se
// izbjeglo koristenje hashtable_test() funkcije ali bi se zato uvijek
// pravila nova kopija kljuca funkcijom mystrdup().
if(!hashtable) return MHASH_INVARG;
if(!key) return MHASH_INVARG;
i = hashtable_test(&hashtable->hash, key);
if(i == HASHTABLE_INVARG) return MHASH_INVARG;
if(i == HASHTABLE_NOTFOUND) k = mystrdup(key);
else k = key;
if(!k) return MHASH_NOMEM;
i = hashtable_getset(&hashtable->hash, k, value, 0);
if(i == HASHTABLE_FULL)
{
// table is full, we have to allocate more space
// and try again
i = mhash_resize(hashtable);
if(i) free((void*)k);
else
{
i = hashtable_set(&hashtable->hash, k, value);
if(i) free((void*)k);
}
}
else if(i) free((void*)k);
return i;
}
// Insert a key-value pair into a hash table when value is malloced string
// key and value strings are strduped
// Returns:
// MHASH_INVARG if invalid argument
// MHASH_NOMEM if no memory
// MHASH_DUPL if key already exists
// MHASH_OK if successful
int mhash_setstr( malloc_hash_t *hashtable, const char *key, const char *value )
{
char *k, *v;
int i;
if(!hashtable) return MHASH_INVARG;
if(!key) return MHASH_INVARG;
if(!value) return MHASH_INVARG;
k = mystrdup(key);
if(!k) return MHASH_NOMEM;
v = mystrdup(value);
if(!v)
{
free((void*)k);
return MHASH_NOMEM;
}
i = hashtable_set(&hashtable->hash, k, v);
if(i == HASHTABLE_DUPL) free((void*)k), free((void*)v);
else if(i == HASHTABLE_FULL)
{
// table is full, we have to allocate more space
// and try again
i = mhash_resize(hashtable);
if(i) free((void*)k), free((void*)v);
else
{
i = hashtable_set(&hashtable->hash, k, v);
if(i) free((void*)k), free((void*)v);
}
}
else if(i) free((void*)k), free((void*)v);
return i;
}
// Replace a key-value pair into a hash table when value is malloced string
// if key exists old value is feeed and new is strduped. if key dos not exists
// key and value strings are strduped
// Returns:
// MHASH_INVARG if invalid argument
// MHASH_NOMEM if no memory
// MHASH_OK if successful
int mhash_replacestr( malloc_hash_t *hashtable, const char *key, const char *value )
{
char *k, *v, *oldv;
int i;
// TODO: iskoristiti value_out da se vidi je li kljuc postojao ranije
// i ako jeste odmah osloboditi ovu kopiju kljuca. Na taj nacin bi se
// izbjeglo koristenje hashtable_test() funkcije ali bi se zato uvijek
// pravila nova kopija kljuca funkcijom mystrdup().
if(!hashtable) return MHASH_INVARG;
if(!key) return MHASH_INVARG;
if(!value) return MHASH_INVARG;
k = mystrdup(key);
if(!k) return MHASH_NOMEM;
v = mystrdup(value);
if(!v)
{
free((void*)k);
return MHASH_NOMEM;
}
i = hashtable_getset(&hashtable->hash, k, v, (const void**)&oldv);
if(i == HASHTABLE_PARTIAL)
{
// SET OK, GET failed, so we know (k,v) is new pair in hash
// we have nothing to do, just set i to MHASH_OK.
i = MHASH_OK;
}
else if(i == HASHTABLE_OK)
{
// SET OK, GET ok, so we know (k,v) is not new pair in hash
// we have to deallocate k and oldv
free((void*)k);
free((void*)oldv);
}
else if(i == HASHTABLE_FULL)
{
// table is full, we have to allocate more space and try again
i = mhash_resize(hashtable);
if(i) free((void*)k), free((void*)v);
else
{
i = hashtable_set(&hashtable->hash, k, v);
if(i) free((void*)k), free((void*)v);
}
}
else if(i) free((void*)k), free((void*)v);
return i;
}
// Delete entry from hash
// Returns:
// MHASH_INVARG if invalid argument
// MHASH_NOTFOUND if key is not found
// MHASH_OK if success
// if deallocate_val is true value will be free'd
// key will always be free'd
int mhash_del( malloc_hash_t *hashtable, const char *key, int deallocate_val)
{
const char *v, *k;
int i;
if(!hashtable) return MHASH_INVARG;
if(!key) return MHASH_INVARG;
i = hashtable_delex(&hashtable->hash, key, &k, (const void**)&v);
if(i) return i;
free((void*)k);
if(deallocate_val) free((void*)v);
return MHASH_OK;
}
// function for walking through all entries in hash
// callback function fcn is called for every entry. If it returns 0
// walk will continue; if it returns != 0 walk will stop
void mhash_walk(malloc_hash_t *hashtable, int (*fcn)(const hashentry_t*))
{
hashtable_walk(&hashtable->hash, fcn);
}
// this function will resize hash table
int mhash_resize(malloc_hash_t *hashtable)
{
malloc_hash_t t;
int table_size, pool_size, i;
// we allow load_factor up to MAX_LOAD_FACTOR
if(hashtable->hash.pool_size < DEFAULT_POOL_SIZE)
{
// by default we addjust only pool size
table_size = hashtable->hash.table_size;
pool_size = hashtable->pool_size + hashtable->pool_increment;
// table size is addjusted only if load factor become >= MAX_LOAD_FACTOR
if(pool_size > MAX_LOAD_FACTOR * table_size) while(pool_size > table_size) table_size <<= 1;
i = mhash_init(&t, table_size, pool_size);
if(i) return i;
// now we have to walk the old hash and copy values to new hash
hashtable_copy(&hashtable->hash, &t.hash);
// now we cak free old hash table and pool
free(hashtable->hash.table);
free(hashtable->hash.pool);
// last step is to copy new hash struct to old one
hashtable->pool_size = pool_size;
hashtable->hash.table_size = table_size;
hashtable->hash.pool_size = t.hash.pool_size;
hashtable->hash.pool = t.hash.pool;
hashtable->hash.table = t.hash.table;
}
return 0;
}