| 43 | } |
| 44 | |
| 45 | gen_hash_t *hash_init_flags(unsigned int size, unsigned int flags) |
| 46 | { |
| 47 | unsigned int n; |
| 48 | gen_hash_t *h; |
| 49 | |
| 50 | /* initialized the hash table */ |
| 51 | for (n=0 ; n<(8*sizeof(n) - 1) ; n++) { |
| 52 | if (size==(1<<n)) |
| 53 | break; |
| 54 | if (size<(1<<n)) { |
| 55 | LM_WARN("hash_size is not a power " |
| 56 | "of 2 as it should be -> rounding from %d to %d\n", |
| 57 | size, 1<<(n-1)); |
| 58 | size = 1<<(n-1); |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (flags & HASH_MAP_PERSIST) |
| 64 | h = rpm_malloc(sizeof *h + size * sizeof(*h->entries)); |
| 65 | else |
| 66 | h = shm_malloc(sizeof *h + size * sizeof(*h->entries)); |
| 67 | if (!h) { |
| 68 | LM_ERR("could not alloc hash of %d elements!\n", size); |
| 69 | return NULL; |
| 70 | } |
| 71 | memset(h, 0, sizeof *h + size * sizeof(*h->entries)); |
| 72 | h->entries = (map_t *)(h + 1); |
| 73 | h->size = size; |
| 74 | h->flags = flags; |
| 75 | if (hash_init_locks(h) < 0) { |
| 76 | LM_ERR("could not alloc locks for hash!\n"); |
| 77 | goto error; |
| 78 | } |
| 79 | |
| 80 | for (n = 0; n < h->size; n++) { |
| 81 | h->entries[n] = map_create( |
| 82 | ((flags&HASH_MAP_PERSIST)?AVLMAP_PERSISTENT:AVLMAP_SHARED)); |
| 83 | if (!h->entries[n]) { |
| 84 | h->size = n; /* we only account for what we've already created */ |
| 85 | goto error; |
| 86 | } |
| 87 | } |
| 88 | return h; |
| 89 | error: |
| 90 | hash_destroy(h, NULL); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | void hash_destroy_locks(gen_hash_t *hash) |
| 95 | { |
no test coverage detected