7 array_init(struct array *array)
9 memset(array, 0, sizeof *array);
13 array_release(struct array *array)
19 array_add(struct array *array, int size)
29 while (alloc < array->size + size)
32 if (array->alloc < alloc) {
33 data = realloc(array->data, alloc);
40 p = array->data + array->size;
46 #define RAZOR_ENTRY_LAST 0x80000000ul
47 #define RAZOR_IMMEDIATE 0x80000000ul
48 #define RAZOR_ENTRY_MASK 0x00fffffful
51 list_init(uint32_t *list)
57 list_set(uint32_t *list, struct array *pool, struct array *items)
61 if (items->size == 0) {
63 } else if (items->size == sizeof (uint32_t)) {
64 *list = *(uint32_t *) items->data | RAZOR_IMMEDIATE;
66 p = array_add(pool, items->size);
67 memcpy(p, items->data, items->size);
68 p[items->size / sizeof *p - 1] |= RAZOR_ENTRY_LAST;
69 *list = p - (uint32_t *) pool->data;
74 list_first(uint32_t *list, struct array *pool)
78 else if (*list & RAZOR_IMMEDIATE)
81 return (uint32_t *) pool->data + (*list & RAZOR_ENTRY_MASK);
85 list_next(uint32_t *list)
87 if (*list & ~RAZOR_ENTRY_MASK)
93 list_remap_pool(struct array *pool, uint32_t *map)
97 end = pool->data + pool->size;
98 for (p = pool->data; p < end; p++)
99 *p = map[LIST_VALUE(p)] | LIST_FLAGS(p);
103 list_remap_if_immediate(uint32_t *list, uint32_t *map)
105 if ((*list & ~RAZOR_ENTRY_MASK) == RAZOR_IMMEDIATE)
106 *list = map[LIST_VALUE(list)] | LIST_FLAGS(list);
111 hashtable_init(struct hashtable *table, struct array *pool)
113 array_init(&table->buckets);
118 hashtable_release(struct hashtable *table)
120 array_release(&table->buckets);
124 hash_string(const char *key)
127 unsigned int hash = 0;
129 for (p = key; *p; p++)
130 hash = (hash * 617) ^ *p;
136 hashtable_lookup(struct hashtable *table, const char *key)
138 unsigned int mask, start, i;
142 pool = table->pool->data;
143 mask = table->buckets.alloc - 1;
144 start = hash_string(key) * sizeof(uint32_t);
146 for (i = 0; i < table->buckets.alloc; i += sizeof *b) {
147 b = table->buckets.data + ((start + i) & mask);
152 if (strcmp(key, &pool[*b]) == 0)
160 do_insert(struct hashtable *table, uint32_t value)
162 unsigned int mask, start, i;
166 key = (char *) table->pool->data + value;
167 mask = table->buckets.alloc - 1;
168 start = hash_string(key) * sizeof(uint32_t);
170 for (i = 0; i < table->buckets.alloc; i += sizeof *b) {
171 b = table->buckets.data + ((start + i) & mask);
180 add_to_string_pool(struct hashtable *table, const char *key)
185 len = strlen(key) + 1;
186 p = array_add(table->pool, len);
189 return p - (char *) table->pool->data;
193 hashtable_insert(struct hashtable *table, const char *key)
195 uint32_t value, *buckets, *b, *end;
198 alloc = table->buckets.alloc;
199 array_add(&table->buckets, 4 * sizeof *buckets);
200 if (alloc != table->buckets.alloc) {
201 end = table->buckets.data + alloc;
202 memset(end, 0, table->buckets.alloc - alloc);
203 for (b = table->buckets.data; b < end; b++) {
207 do_insert(table, value);
212 value = add_to_string_pool(table, key);
213 do_insert (table, value);
219 hashtable_tokenize(struct hashtable *table, const char *string)
226 token = hashtable_lookup(table, string);
230 return hashtable_insert(table, string);