ucx/map.c

Thu, 04 Oct 2012 16:03:18 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 04 Oct 2012 16:03:18 +0200
changeset 42
ff3dd1ee7dee
parent 38
35f67a8ef875
child 43
02f38adea013
permissions
-rw-r--r--

(broken-commit) - added load and store functions, tests failing

some evil crash happens when executing the test - remove the strcmp calls in the test case for the store and load function and everything "works"

the error must be somewhere else - maybe something that should not be freed is freed during the test

olaf@20 1 /*
olaf@20 2 *
olaf@20 3 */
olaf@2 4
olaf@20 5 #include <stdlib.h>
olaf@20 6 #include <string.h>
olaf@20 7
olaf@20 8 #include "map.h"
olaf@20 9
olaf@20 10 UcxMap *ucx_map_new(size_t size) {
olaf@20 11 UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap));
olaf@20 12 if(map == NULL) {
olaf@20 13 return NULL;
olaf@20 14 }
olaf@20 15
universe@29 16 map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*));
olaf@20 17 if(map->map == NULL) {
olaf@20 18 free(map);
olaf@20 19 return NULL;
olaf@20 20 }
olaf@20 21 map->size = size;
olaf@20 22
olaf@20 23 return map;
olaf@20 24 }
olaf@20 25
universe@29 26 void ucx_map_free(UcxMap *map) {
universe@29 27 for (size_t n = 0 ; n < map->size ; n++) {
universe@29 28 UcxMapElement *elem = map->map[n];
universe@29 29 if (elem != NULL) {
universe@29 30 do {
universe@29 31 UcxMapElement *next = elem->next;
olaf@30 32 free(elem->key.data);
universe@29 33 free(elem);
universe@29 34 elem = next;
universe@29 35 } while (elem != NULL);
universe@29 36 }
universe@29 37 }
olaf@30 38 free(map->map);
universe@29 39 free(map);
universe@29 40 }
universe@29 41
olaf@20 42 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
olaf@20 43 if(key.hash == 0) {
olaf@20 44 key.hash = ucx_hash((char*)key.data, key.len);
olaf@20 45 }
olaf@20 46
universe@29 47 size_t slot = key.hash%map->size;
universe@29 48 UcxMapElement *elm = map->map[slot];
universe@29 49 UcxMapElement *prev = NULL;
universe@29 50
universe@29 51 while (elm != NULL && elm->key.hash < key.hash) {
universe@29 52 prev = elm;
universe@29 53 elm = elm->next;
universe@29 54 }
universe@29 55
universe@29 56 if (elm == NULL || elm->key.hash != key.hash) {
olaf@20 57 UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement));
olaf@20 58 if(e == NULL) {
olaf@20 59 return -1;
olaf@20 60 }
olaf@30 61 e->key.data = NULL;
universe@29 62 if (prev == NULL) {
universe@29 63 map->map[slot] = e;
universe@29 64 } else {
universe@29 65 prev->next = e;
universe@29 66 }
universe@29 67 e->next = elm;
olaf@20 68 elm = e;
olaf@20 69 }
universe@29 70
olaf@30 71 if(elm->key.data == NULL) {
olaf@30 72 void *kd = malloc(key.len);
olaf@30 73 if (kd == NULL) {
olaf@30 74 return -1;
olaf@30 75 }
olaf@30 76 memcpy(kd, key.data, key.len);
olaf@30 77 key.data = kd;
olaf@30 78 elm->key = key;
olaf@30 79 }
olaf@20 80 elm->data = data;
olaf@20 81
olaf@20 82 return 0;
olaf@20 83 }
olaf@20 84
olaf@20 85 void* ucx_map_get(UcxMap *map, UcxKey key) {
olaf@20 86 if(key.hash == 0) {
olaf@20 87 key.hash = ucx_hash((char*)key.data, key.len);
olaf@20 88 }
olaf@20 89
universe@29 90 UcxMapElement *elm = map->map[key.hash%map->size];
universe@29 91 while (elm != NULL && elm->key.hash <= key.hash) {
olaf@20 92 if(elm->key.hash == key.hash) {
olaf@20 93 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
universe@29 94 if (memcmp(elm->key.data, key.data, n) == 0) {
olaf@20 95 return elm->data;
olaf@20 96 }
olaf@20 97 }
olaf@20 98 elm = elm->next;
olaf@20 99 }
olaf@20 100
olaf@20 101 return NULL;
olaf@20 102 }
olaf@20 103
olaf@20 104 UcxKey ucx_key(void *data, size_t len) {
olaf@20 105 UcxKey key;
olaf@20 106 key.data = data;
olaf@20 107 key.len = len;
olaf@20 108 key.hash = ucx_hash(data, len);
olaf@20 109 return key;
olaf@20 110 }
olaf@20 111
olaf@20 112
olaf@20 113 int ucx_hash(char *data, size_t len) {
olaf@20 114 /* murmur hash 2 */
olaf@20 115
olaf@20 116 int m = 0x5bd1e995;
olaf@20 117 int r = 24;
olaf@20 118
olaf@20 119 int h = 25 ^ len;
olaf@20 120
olaf@20 121 int i = 0;
olaf@20 122 while (len >= 4) {
olaf@20 123 int k = data[i + 0] & 0xFF;
olaf@20 124 k |= (data[i + 1] & 0xFF) << 8;
olaf@20 125 k |= (data[i + 2] & 0xFF) << 16;
olaf@20 126 k |= (data[i + 3] & 0xFF) << 24;
olaf@20 127
olaf@20 128 k *= m;
olaf@20 129 k ^= k >> r;
olaf@20 130 k *= m;
olaf@20 131
olaf@20 132 h *= m;
olaf@20 133 h ^= k;
olaf@20 134
olaf@20 135 i += 4;
olaf@20 136 len -= 4;
olaf@20 137 }
olaf@20 138
olaf@20 139 switch (len) {
olaf@20 140 case 3: h ^= (data[i + 2] & 0xFF) << 16;
universe@38 141 /* no break */
olaf@20 142 case 2: h ^= (data[i + 1] & 0xFF) << 8;
universe@38 143 /* no break */
olaf@20 144 case 1: h ^= (data[i + 0] & 0xFF); h *= m;
universe@38 145 /* no break */
olaf@20 146 }
olaf@20 147
olaf@20 148 h ^= h >> 13;
olaf@20 149 h *= m;
olaf@20 150 h ^= h >> 15;
olaf@20 151
olaf@20 152 return h;
olaf@20 153 }
olaf@31 154
olaf@31 155 UcxMapIterator ucx_map_iterator(UcxMap *map) {
olaf@31 156 UcxMapIterator i;
olaf@31 157 i.map = map;
olaf@31 158 i.cur = NULL;
olaf@31 159 i.index = 0;
olaf@31 160 return i;
olaf@31 161 }
olaf@31 162
olaf@31 163 int ucx_map_iter_next(UcxMapIterator *i, void **elm) {
olaf@31 164 UcxMapElement *e = i->cur;
olaf@31 165
olaf@31 166 if(e == NULL) {
olaf@31 167 e = i->map->map[0];
olaf@31 168 } else {
olaf@31 169 e = e->next;
olaf@31 170 }
olaf@31 171
olaf@31 172 while(i->index < i->map->size) {
olaf@31 173 if(e != NULL) {
olaf@31 174 if(e->data != NULL) {
olaf@31 175 i->cur = e;
olaf@31 176 *elm = e->data;
olaf@31 177 return 0;
olaf@31 178 }
olaf@31 179
olaf@31 180 e = e->next;
olaf@31 181 } else {
olaf@31 182 i->index++;
olaf@31 183
olaf@31 184 if(i->index < i->map->size) {
olaf@31 185 e = i->map->map[i->index];
olaf@31 186 }
olaf@31 187 }
olaf@31 188 }
olaf@31 189
olaf@31 190 return 1;
olaf@31 191 }
universe@42 192
universe@42 193 int ucx_map_load(UcxMap *map, FILE *f) {
universe@42 194
universe@42 195 char c; int r, n;
universe@42 196
universe@42 197 char *key, *value;
universe@42 198
universe@42 199 while ((c = (char) fgetc(f)) > 0) {
universe@42 200 /* Discard leading spaces and comments */
universe@42 201 if (c == ' ') continue;
universe@42 202 if (c == '#' || c == '!') {
universe@42 203 while ((c = (char) fgetc(f)) > 0) {
universe@42 204 if (c == '\n') break;
universe@42 205 }
universe@42 206 continue;
universe@42 207 }
universe@42 208
universe@42 209 /* read into key buffer */
universe@42 210 n = 16;
universe@42 211 key = malloc(n);
universe@42 212 r = 0;
universe@42 213 do {
universe@42 214 if (c == '=') break;
universe@42 215 if (r > n - 2) {
universe@42 216 n *= 2;
universe@42 217 key = realloc(key, n);
universe@42 218 }
universe@42 219 key[r] = c;
universe@42 220 r++;
universe@42 221 } while ((c = (char) fgetc(f)) > 0);
universe@42 222 if (c == 0) {
universe@42 223 free(key);
universe@42 224 return 1;
universe@42 225 }
universe@42 226 key[r] = 0;
universe@42 227
universe@42 228 /* read into value buffer */
universe@42 229 n = 64;
universe@42 230 value = malloc(n);
universe@42 231 r = 0;
universe@42 232 while ((c = (char) fgetc(f)) > 0) {
universe@42 233 if (c == '\n') break;
universe@42 234 if (r > n - 1) {
universe@42 235 n *= 2;
universe@42 236 value = realloc(value, n);
universe@42 237 }
universe@42 238 value[r] = c;
universe@42 239 r++;
universe@42 240 }
universe@42 241 value = realloc(value, r+1);
universe@42 242 value[r] = 0;
universe@42 243
universe@42 244 ucx_map_cstr_put(map, key, value);
universe@42 245 free(key);
universe@42 246 }
universe@42 247
universe@42 248 return 0;
universe@42 249 }
universe@42 250
universe@42 251 int ucx_map_store(UcxMap *map, FILE *f) {
universe@42 252 UcxMapIterator iter = ucx_map_iterator(map);
universe@42 253 char *k, *v;
universe@42 254 sstr_t key, value;
universe@42 255 int written;
universe@42 256
universe@42 257 UCX_MAP_FOREACH(v, iter) {
universe@42 258 k = (char*) iter.cur->key.data;
universe@42 259 key = sstr(k); value = sstr(v);
universe@42 260
universe@42 261 written = 0;
universe@42 262 written += fwrite(key.ptr, 1, key.length, f);
universe@42 263 written += fwrite(" = ", 1, 3, f);
universe@42 264 written += fwrite(value.ptr, 1, value.length, f);
universe@42 265 written += fwrite("\n", 1, 1, f);
universe@42 266
universe@42 267 if (written != key.length + value.length + 4) return 1;
universe@42 268 }
universe@42 269
universe@42 270 return 0;
universe@42 271 }

mercurial