1.1 --- a/razor.c Wed Feb 06 13:02:50 2008 -0500
1.2 +++ b/razor.c Thu Feb 07 09:58:48 2008 -0500
1.3 @@ -16,11 +16,7 @@
1.4
1.5 #include "razor.h"
1.6 #include "razor-internal.h"
1.7 -
1.8 -struct array {
1.9 - void *data;
1.10 - int size, alloc;
1.11 -};
1.12 +#include "types.h"
1.13
1.14 struct razor_set_section {
1.15 uint32_t type;
1.16 @@ -92,11 +88,6 @@
1.17 struct import_directory *last;
1.18 };
1.19
1.20 -struct hashtable {
1.21 - struct array buckets;
1.22 - struct array *pool;
1.23 -};
1.24 -
1.25 struct razor_importer {
1.26 struct razor_set *set;
1.27 struct hashtable table;
1.28 @@ -105,46 +96,6 @@
1.29 struct array files;
1.30 };
1.31
1.32 -static void
1.33 -array_init(struct array *array)
1.34 -{
1.35 - memset(array, 0, sizeof *array);
1.36 -}
1.37 -
1.38 -static void
1.39 -array_release(struct array *array)
1.40 -{
1.41 - free(array->data);
1.42 -}
1.43 -
1.44 -static void *
1.45 -array_add(struct array *array, int size)
1.46 -{
1.47 - int alloc;
1.48 - void *data, *p;
1.49 -
1.50 - if (array->alloc > 0)
1.51 - alloc = array->alloc;
1.52 - else
1.53 - alloc = 16;
1.54 -
1.55 - while (alloc < array->size + size)
1.56 - alloc *= 2;
1.57 -
1.58 - if (array->alloc < alloc) {
1.59 - data = realloc(array->data, alloc);
1.60 - if (data == NULL)
1.61 - return 0;
1.62 - array->data = data;
1.63 - array->alloc = alloc;
1.64 - }
1.65 -
1.66 - p = array->data + array->size;
1.67 - array->size += size;
1.68 -
1.69 - return p;
1.70 -}
1.71 -
1.72 static void *
1.73 zalloc(size_t size)
1.74 {
1.75 @@ -275,55 +226,6 @@
1.76 return 0;
1.77 }
1.78
1.79 -static unsigned int
1.80 -hash_string(const char *key)
1.81 -{
1.82 - const char *p;
1.83 - unsigned int hash = 0;
1.84 -
1.85 - for (p = key; *p; p++)
1.86 - hash = (hash * 617) ^ *p;
1.87 -
1.88 - return hash;
1.89 -}
1.90 -
1.91 -static uint32_t
1.92 -hashtable_lookup(struct hashtable *table, const char *key)
1.93 -{
1.94 - unsigned int mask, start, i;
1.95 - uint32_t *b;
1.96 - char *pool;
1.97 -
1.98 - pool = table->pool->data;
1.99 - mask = table->buckets.alloc - 1;
1.100 - start = hash_string(key) * sizeof(uint32_t);
1.101 -
1.102 - for (i = 0; i < table->buckets.alloc; i += sizeof *b) {
1.103 - b = table->buckets.data + ((start + i) & mask);
1.104 -
1.105 - if (*b == 0)
1.106 - return 0;
1.107 -
1.108 - if (strcmp(key, &pool[*b]) == 0)
1.109 - return *b;
1.110 - }
1.111 -
1.112 - return 0;
1.113 -}
1.114 -
1.115 -static uint32_t
1.116 -add_to_string_pool(struct hashtable *table, const char *key)
1.117 -{
1.118 - int len;
1.119 - char *p;
1.120 -
1.121 - len = strlen(key) + 1;
1.122 - p = array_add(table->pool, len);
1.123 - memcpy(p, key, len);
1.124 -
1.125 - return p - (char *) table->pool->data;
1.126 -}
1.127 -
1.128 static uint32_t
1.129 add_to_property_pool(struct array *pool, struct array *properties)
1.130 {
1.131 @@ -341,80 +243,6 @@
1.132 return p - (uint32_t *) pool->data;
1.133 }
1.134
1.135 -static void
1.136 -do_insert(struct hashtable *table, uint32_t value)
1.137 -{
1.138 - unsigned int mask, start, i;
1.139 - uint32_t *b;
1.140 - const char *key;
1.141 -
1.142 - key = (char *) table->pool->data + value;
1.143 - mask = table->buckets.alloc - 1;
1.144 - start = hash_string(key) * sizeof(uint32_t);
1.145 -
1.146 - for (i = 0; i < table->buckets.alloc; i += sizeof *b) {
1.147 - b = table->buckets.data + ((start + i) & mask);
1.148 - if (*b == 0) {
1.149 - *b = value;
1.150 - break;
1.151 - }
1.152 - }
1.153 -}
1.154 -
1.155 -static uint32_t
1.156 -hashtable_insert(struct hashtable *table, const char *key)
1.157 -{
1.158 - uint32_t value, *buckets, *b, *end;
1.159 - int alloc;
1.160 -
1.161 - alloc = table->buckets.alloc;
1.162 - array_add(&table->buckets, 4 * sizeof *buckets);
1.163 - if (alloc != table->buckets.alloc) {
1.164 - end = table->buckets.data + alloc;
1.165 - memset(end, 0, table->buckets.alloc - alloc);
1.166 - for (b = table->buckets.data; b < end; b++) {
1.167 - value = *b;
1.168 - if (value != 0) {
1.169 - *b = 0;
1.170 - do_insert(table, value);
1.171 - }
1.172 - }
1.173 - }
1.174 -
1.175 - value = add_to_string_pool(table, key);
1.176 - do_insert (table, value);
1.177 -
1.178 - return value;
1.179 -}
1.180 -
1.181 -static void
1.182 -hashtable_init(struct hashtable *table, struct array *pool)
1.183 -{
1.184 - array_init(&table->buckets);
1.185 - table->pool = pool;
1.186 -}
1.187 -
1.188 -static void
1.189 -hashtable_release(struct hashtable *table)
1.190 -{
1.191 - array_release(&table->buckets);
1.192 -}
1.193 -
1.194 -static uint32_t
1.195 -hashtable_tokenize(struct hashtable *table, const char *string)
1.196 -{
1.197 - uint32_t token;
1.198 -
1.199 - if (string == NULL)
1.200 - string = "";
1.201 -
1.202 - token = hashtable_lookup(table, string);
1.203 - if (token != 0)
1.204 - return token;
1.205 -
1.206 - return hashtable_insert(table, string);
1.207 -}
1.208 -
1.209 void
1.210 razor_importer_begin_package(struct razor_importer *importer,
1.211 const char *name, const char *version)