src/MMagicST.c

filter: Statementstmt 31.2% 55 / 176 Branchbran 15.7% 18 / 114 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 25.1% 73 / 290 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/File/MMagic/benchmark.pl ↑ Summary src/perl-mmagic-xs.c »
1 #ifndef __MMAGIC_ST_C__
2 #define __MMAGIC_ST_C__
3  
4 /* This is a public domain general purpose hash table package written by Peter Moore @ UCB. */
5  
6 /* static char sccsid[] = "@(#) st.c 5.1 89/12/14 Crucible"; */
7  
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "MMagicST.h"
12  
13 #ifdef _WIN32
14 #include <malloc.h>
15 #endif
16  
17 typedef struct st_table_entry st_table_entry;
18  
19 struct st_table_entry {
20 unsigned int hash;
21 st_data_t key;
22 st_data_t record;
23 st_table_entry *next;
24 };
25  
26 #define ST_DEFAULT_MAX_DENSITY 5
27 #define ST_DEFAULT_INIT_TABLE_SIZE 11
28  
29 /*
30 * DEFAULT_MAX_DENSITY is the default for the largest we allow the
31 * average number of items per bin before increasing the number of
32 * bins
33 *
34 * DEFAULT_INIT_TABLE_SIZE is the default for the number of bins
35 * allocated initially
36 *
37 */
38 static int numcmp(long, long);
39 static int numhash(long);
40 static struct st_hash_type type_numhash = {
41 numcmp,
42 numhash,
43 };
44  
45 /* extern int strcmp(const char *, const char *); */
46 static int strhash(const char *);
47 static struct st_hash_type type_strhash = {
48 strcmp,
49 strhash,
50 };
51  
52 #ifdef RUBY_PLATFORM
53 #define xmalloc ruby_xmalloc
54 #define xcalloc ruby_xcalloc
55 #define xrealloc ruby_xrealloc
56 #define xfree ruby_xfree
57  
58 void *xmalloc(long);
59 void *xcalloc(long, long);
60 void *xrealloc(void *, long);
61 void xfree(void *);
62 #else
63 #define xmalloc malloc
64 #define xcalloc calloc
65 #endif
66  
67 static void rehash(st_table *);
68  
69 #define alloc(type) (type*)xmalloc((unsigned)sizeof(type))
70 #define Calloc(n,s) (char*)xcalloc((n),(s))
71  
72 #define EQUAL(table,x,y) ((x)==(y) || (*table->type->compare)((x),(y)) == 0)
73  
74 #define do_hash(key,table) (unsigned int)(*(table)->type->hash)((key))
75 #define do_hash_bin(key,table) (do_hash(key, table)%(table)->num_bins)
76  
77 /*
78 * MINSIZE is the minimum size of a dictionary.
79 */
80  
81 #define MINSIZE 8
82  
83 /*
84 Table of prime numbers 2^n+a, 2<=n<=30.
85 */
86 static long primes[] = {
87 8 + 3,
88 16 + 3,
89 32 + 5,
90 64 + 3,
91 128 + 3,
92 256 + 27,
93 512 + 9,
94 1024 + 9,
95 2048 + 5,
96 4096 + 3,
97 8192 + 27,
98 16384 + 43,
99 32768 + 3,
100 65536 + 45,
101 131072 + 29,
102 262144 + 3,
103 524288 + 21,
104 1048576 + 7,
105 2097152 + 17,
106 4194304 + 15,
107 8388608 + 9,
108 16777216 + 43,
109 33554432 + 35,
110 67108864 + 15,
111 134217728 + 29,
112 268435456 + 3,
113 536870912 + 11,
114 1073741824 + 85,
115 0
116 };
117  
118 static int
119 9 new_size(size)
120 int size;
121 {
122 int i;
123  
124 #if 0
125 for (i=3; i<31; i++) {
126 if ((1<<i) > size) return 1<<i;
127 }
128 return -1;
129 #else
130 int newsize;
131  
132 9 for (i = 0, newsize = MINSIZE;
133 9 ▶ i < sizeof(primes)/sizeof(primes[0]);
Branch: 1 branch
BranchTrueFalseTotal
i < sizeof(primes)/sizeof(primes[0]); 9 0 9
134 0 i++, newsize <<= 1)
135 {
136 9 ▶ if (newsize > size) return primes[i];
Branch: 1 branch
BranchTrueFalseTotal
if (newsize > size) return primes[i]; 9 0 9
137 }
138 /* Ran out of polynomials */
139 0 return -1; /* should raise exception */
140 #endif
141 }
142  
143 #ifdef HASH_LOG
144 static int collision = 0;
145 static int init_st = 0;
146  
147 static void
148 stat_col()
149 {
150 FILE *f = fopen("/tmp/col", "w");
151 fprintf(f, "collision: %d\n", collision);
152 fclose(f);
153 }
154 #endif
155  
156 st_table*
157 9 st_init_table_with_size(type, size)
158 struct st_hash_type *type;
159 int size;
160 {
161 st_table *tbl;
162  
163 #ifdef HASH_LOG
164 if (init_st == 0) {
165 init_st = 1;
166 atexit(stat_col);
167 }
168 #endif
169  
170 9 size = new_size(size); /* round up to prime number */
171  
172 9 tbl = alloc(st_table);
173 9 tbl->type = type;
174 9 tbl->num_entries = 0;
175 9 tbl->num_bins = size;
176 9 tbl->bins = (st_table_entry **)Calloc(size, sizeof(st_table_entry*));
177  
178 9 return tbl;
179 }
180  
181 st_table*
182 9 st_init_table(type)
183 struct st_hash_type *type;
184 {
185 9 return st_init_table_with_size(type, 0);
186 }
187  
188 st_table*
189 0 st_init_numtable(void)
190 {
191 0 return st_init_table(&type_numhash);
192 }
193  
194 st_table*
195 0 st_init_numtable_with_size(size)
196 int size;
197 {
198 0 return st_init_table_with_size(&type_numhash, size);
199 }
200  
201 st_table*
202 9 st_init_strtable(void)
203 {
204 9 return st_init_table(&type_strhash);
205 }
206  
207 st_table*
208 0 st_init_strtable_with_size(size)
209 int size;
210 {
211 0 return st_init_table_with_size(&type_strhash, size);
212 }
213  
214 void
215 10 st_free_table(table)
216 st_table *table;
217 {
218 register st_table_entry *ptr, *next;
219 int i;
220  
221 120 ▶ for(i = 0; i < table->num_bins; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < table->num_bins; i++) { 110 10 120
222 110 ptr = table->bins[i];
223 112 ▶ while (ptr != 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (ptr != 0) { 2 110 112
224 2 next = ptr->next;
225 2 free(ptr);
226 2 ptr = next;
227 }
228 }
229 10 free(table->bins);
230 10 free(table);
231 10 }
232  
233 #define PTR_NOT_EQUAL(table, ptr, hash_val, key) \
234 ((ptr) != 0 && (ptr->hash != (hash_val) || !EQUAL((table), (key), (ptr)->key)))
235  
236 #ifdef HASH_LOG
237 #define COLLISION collision++
238 #else
239 #define COLLISION
240 #endif
241  
242 #define FIND_ENTRY(table, ptr, hash_val, bin_pos) do {\
243 bin_pos = hash_val%(table)->num_bins;\
244 ptr = (table)->bins[bin_pos];\
245 if (PTR_NOT_EQUAL(table, ptr, hash_val, key)) {\
246 COLLISION;\
247 while (PTR_NOT_EQUAL(table, ptr->next, hash_val, key)) {\
248 ptr = ptr->next;\
249 }\
250 ptr = ptr->next;\
251 }\
252 } while (0)
253  
254 int
255 2 st_lookup(table, key, value)
256 st_table *table;
257 register st_data_t key;
258 st_data_t *value;
259 {
260 unsigned int hash_val, bin_pos;
261 register st_table_entry *ptr;
262  
263 2 hash_val = do_hash(key, table);
264 2 ▶ FIND_ENTRY(table, ptr, hash_val, bin_pos);
Branch: 8 branches
BranchTrueFalseTotal
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 2 2
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
265  
266 2 ▶ if (ptr == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ptr == 0) { 2 0 2
267 2 return 0;
268 }
269 else {
270 0 ▶ if (value != 0) *value = ptr->record;
Branch: 1 branch
BranchTrueFalseTotal
if (value != 0) *value = ptr->record; 0 0 0
271 0 return 1;
272 }
273 }
274  
275 #define ADD_DIRECT(table, key, value, hash_val, bin_pos)\
276 do {\
277 st_table_entry *entry;\
278 if (table->num_entries/(table->num_bins) > ST_DEFAULT_MAX_DENSITY) {\
279 rehash(table);\
280 bin_pos = hash_val % table->num_bins;\
281 }\
282 \
283 entry = alloc(st_table_entry);\
284 \
285 entry->hash = hash_val;\
286 entry->key = key;\
287 entry->record = value;\
288 entry->next = table->bins[bin_pos];\
289 table->bins[bin_pos] = entry;\
290 table->num_entries++;\
291 } while (0)
292  
293 int
294 2 st_insert(table, key, value)
295 register st_table *table;
296 register st_data_t key;
297 st_data_t value;
298 {
299 unsigned int hash_val, bin_pos;
300 register st_table_entry *ptr;
301  
302 2 hash_val = do_hash(key, table);
303 2 ▶ FIND_ENTRY(table, ptr, hash_val, bin_pos);
Branch: 8 branches
BranchTrueFalseTotal
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 2 2
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
FIND_ENTRY(table, ptr, hash_val, bin_pos); 0 0 0
304  
305 2 ▶ if (ptr == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ptr == 0) { 2 0 2
306 2 ▶ ADD_DIRECT(table, key, value, hash_val, bin_pos);
Branch: 1 branch
BranchTrueFalseTotal
ADD_DIRECT(table, key, value, hash_val, bin_pos); 0 2 2
307 2 return 0;
308 }
309 else {
310 0 ptr->record = value;
311 0 return 1;
312 }
313 }
314  
315 void
316 0 st_add_direct(table, key, value)
317 st_table *table;
318 st_data_t key;
319 st_data_t value;
320 {
321 unsigned int hash_val, bin_pos;
322  
323 0 hash_val = do_hash(key, table);
324 0 bin_pos = hash_val % table->num_bins;
325 0 ▶ ADD_DIRECT(table, key, value, hash_val, bin_pos);
Branch: 1 branch
BranchTrueFalseTotal
ADD_DIRECT(table, key, value, hash_val, bin_pos); 0 0 0
326 0 }
327  
328 static void
329 0 rehash(table)
330 register st_table *table;
331 {
332 register st_table_entry *ptr, *next, **new_bins;
333 0 int i, old_num_bins = table->num_bins, new_num_bins;
334 unsigned int hash_val;
335  
336 0 new_num_bins = new_size(old_num_bins+1);
337 0 new_bins = (st_table_entry**)Calloc(new_num_bins, sizeof(st_table_entry*));
338  
339 0 ▶ for(i = 0; i < old_num_bins; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < old_num_bins; i++) { 0 0 0
340 0 ptr = table->bins[i];
341 0 ▶ while (ptr != 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (ptr != 0) { 0 0 0
342 0 next = ptr->next;
343 0 hash_val = ptr->hash % new_num_bins;
344 0 ptr->next = new_bins[hash_val];
345 0 new_bins[hash_val] = ptr;
346 0 ptr = next;
347 }
348 }
349 0 free(table->bins);
350 0 table->num_bins = new_num_bins;
351 0 table->bins = new_bins;
352 0 }
353  
354 st_table*
355 1 st_copy(old_table)
356 st_table *old_table;
357 {
358 st_table *new_table;
359 st_table_entry *ptr, *entry;
360 1 int i, num_bins = old_table->num_bins;
361  
362 1 new_table = alloc(st_table);
363 1 ▶ if (new_table == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_table == 0) { 0 1 1
364 0 return 0;
365 }
366  
367 1 *new_table = *old_table;
368 1 new_table->bins = (st_table_entry**)
369 1 Calloc((unsigned)num_bins, sizeof(st_table_entry*));
370  
371 1 ▶ if (new_table->bins == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_table->bins == 0) { 0 1 1
372 0 free(new_table);
373 0 return 0;
374 }
375  
376 12 ▶ for(i = 0; i < num_bins; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < num_bins; i++) { 11 1 12
377 11 new_table->bins[i] = 0;
378 11 ptr = old_table->bins[i];
379 11 ▶ while (ptr != 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (ptr != 0) { 0 11 11
380 0 entry = alloc(st_table_entry);
381 0 ▶ if (entry == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry == 0) { 0 0 0
382 0 free(new_table->bins);
383 0 free(new_table);
384 0 return 0;
385 }
386 0 *entry = *ptr;
387 0 entry->next = new_table->bins[i];
388 0 new_table->bins[i] = entry;
389 0 ptr = ptr->next;
390 }
391 }
392 1 return new_table;
393 }
394  
395 int
396 0 st_delete(table, key, value)
397 register st_table *table;
398 register st_data_t *key;
399 st_data_t *value;
400 {
401 unsigned int hash_val;
402 st_table_entry *tmp;
403 register st_table_entry *ptr;
404  
405 0 hash_val = do_hash_bin(*key, table);
406 0 ptr = table->bins[hash_val];
407  
408 0 ▶ if (ptr == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ptr == 0) { 0 0 0
409 0 ▶ if (value != 0) *value = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value != 0) *value = 0; 0 0 0
410 0 return 0;
411 }
412  
413 0 ▶ if (EQUAL(table, *key, ptr->key)) {
Branch: 2 branches
BranchTrueFalseTotal
if (EQUAL(table, *key, ptr->key)) { 0 0 0
if (EQUAL(table, *key, ptr->key)) { 0 0 0
414 0 table->bins[hash_val] = ptr->next;
415 0 table->num_entries--;
416 0 ▶ if (value != 0) *value = ptr->record;
Branch: 1 branch
BranchTrueFalseTotal
if (value != 0) *value = ptr->record; 0 0 0
417 0 *key = ptr->key;
418 0 free(ptr);
419 0 return 1;
420 }
421  
422 0 ▶ for(; ptr->next != 0; ptr = ptr->next) {
Branch: 1 branch
BranchTrueFalseTotal
for(; ptr->next != 0; ptr = ptr->next) { 0 0 0
423 0 ▶ if (EQUAL(table, ptr->next->key, *key)) {
Branch: 2 branches
BranchTrueFalseTotal
if (EQUAL(table, ptr->next->key, *key)) { 0 0 0
if (EQUAL(table, ptr->next->key, *key)) { 0 0 0
424 0 tmp = ptr->next;
425 0 ptr->next = ptr->next->next;
426 0 table->num_entries--;
427 0 ▶ if (value != 0) *value = tmp->record;
Branch: 1 branch
BranchTrueFalseTotal
if (value != 0) *value = tmp->record; 0 0 0
428 0 *key = tmp->key;
429 0 free(tmp);
430 0 return 1;
431 }
432 }
433  
434 0 return 0;
435 }
436  
437 int
438 0 st_delete_safe(table, key, value, never)
439 register st_table *table;
440 register st_data_t *key;
441 st_data_t *value;
442 st_data_t never;
443 {
444 unsigned int hash_val;
445 register st_table_entry *ptr;
446  
447 0 hash_val = do_hash_bin(*key, table);
448 0 ptr = table->bins[hash_val];
449  
450 0 ▶ if (ptr == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ptr == 0) { 0 0 0
451 0 ▶ if (value != 0) *value = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value != 0) *value = 0; 0 0 0
452 0 return 0;
453 }
454  
455 0 ▶ for(; ptr != 0; ptr = ptr->next) {
Branch: 1 branch
BranchTrueFalseTotal
for(; ptr != 0; ptr = ptr->next) { 0 0 0
456 0 ▶ if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) {
Branch: 3 branches
BranchTrueFalseTotal
if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) { 0 0 0
if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) { 0 0 0
if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) { 0 0 0
457 0 table->num_entries--;
458 0 *key = ptr->key;
459 0 ▶ if (value != 0) *value = ptr->record;
Branch: 1 branch
BranchTrueFalseTotal
if (value != 0) *value = ptr->record; 0 0 0
460 0 ptr->key = ptr->record = never;
461 0 return 1;
462 }
463 }
464  
465 0 return 0;
466 }
467  
468 static int
469 0 delete_never(key, value, never)
470 st_data_t key, value, never;
471 {
472 0 ▶ if (value == never) return ST_DELETE;
Branch: 1 branch
BranchTrueFalseTotal
if (value == never) return ST_DELETE; 0 0 0
473 0 return ST_CONTINUE;
474 }
475  
476 void
477 0 st_cleanup_safe(table, never)
478 st_table *table;
479 st_data_t never;
480 {
481 0 int num_entries = table->num_entries;
482  
483 0 st_foreach(table, delete_never, never);
484 0 table->num_entries = num_entries;
485 0 }
486  
487 void
488 0 st_foreach(table, func, arg)
489 st_table *table;
490 int (*func)();
491 st_data_t arg;
492 {
493 st_table_entry *ptr, *last, *tmp;
494 enum st_retval retval;
495 int i;
496  
497 0 ▶ for(i = 0; i < table->num_bins; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < table->num_bins; i++) { 0 0 0
498 0 last = 0;
499 0 ▶ for(ptr = table->bins[i]; ptr != 0;) {
Branch: 1 branch
BranchTrueFalseTotal
for(ptr = table->bins[i]; ptr != 0;) { 0 0 0
500 0 retval = (*func)(ptr->key, ptr->record, arg, 0);
501 0 switch (retval) {
502 0 case ST_CHECK: /* check if hash is modified during iteration */
503 0 tmp = 0;
504 0 ▶ if (i < table->num_bins) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < table->num_bins) { 0 0 0
505 0 ▶ for (tmp = table->bins[i]; tmp; tmp=tmp->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (tmp = table->bins[i]; tmp; tmp=tmp->next) { 0 0 0
506 0 ▶ if (tmp == ptr) break;
Branch: 1 branch
BranchTrueFalseTotal
if (tmp == ptr) break; 0 0 0
507 }
508 }
509 0 ▶ if (!tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tmp) { 0 0 0
510 /* call func with error notice */
511 0 retval = (*func)(0, 0, arg, 1);
512 0 return;
513 }
514 /* fall through */
515 case ST_CONTINUE:
516 0 last = ptr;
517 0 ptr = ptr->next;
518 0 break;
519 0 case ST_STOP:
520 0 return;
521 0 case ST_DELETE:
522 0 tmp = ptr;
523 0 ▶ if (last == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (last == 0) { 0 0 0
524 0 table->bins[i] = ptr->next;
525 }
526 else {
527 0 last->next = ptr->next;
528 }
529 0 ptr = ptr->next;
530 0 free(tmp);
531 0 table->num_entries--;
532 }
533 }
534 }
535 }
536  
537 static int
538 4 strhash(string)
539 register const char *string;
540 {
541 register int c;
542  
543 #ifdef HASH_ELFHASH
544 register unsigned int h = 0, g;
545  
546 while ((c = *string++) != '\0') {
547 h = ( h << 4 ) + c;
548 if ( g = h & 0xF0000000 )
549 h ^= g >> 24;
550 h &= ~g;
551 }
552 return h;
553 #elif HASH_PERL
554 register int val = 0;
555  
556 while ((c = *string++) != '\0') {
557 val += c;
558 val += (val << 10);
559 val ^= (val >> 6);
560 }
561 val += (val << 3);
562 val ^= (val >> 11);
563  
564 return val + (val << 15);
565 #else
566 4 register int val = 0;
567  
568 8 ▶ while ((c = *string++) != '\0') {
Branch: 1 branch
BranchTrueFalseTotal
while ((c = *string++) != '\0') { 4 4 8
569 4 val = val*997 + c;
570 }
571  
572 4 return val + (val>>5);
573 #endif
574 }
575  
576 static int
577 0 numcmp(x, y)
578 long x, y;
579 {
580 0 return x != y;
581 }
582  
583 static int
584 0 numhash(n)
585 long n;
586 {
587 0 return n;
588 }
589  
590 #endif /* __MMAGIC_ST_C__ */
« blib/lib/File/MMagic/benchmark.pl ↑ Summary src/perl-mmagic-xs.c »