--- a/src/server/ucx/map.c Sat Jan 28 16:01:07 2012 +0100 +++ b/src/server/ucx/map.c Mon Feb 13 13:49:49 2012 +0100 @@ -2,6 +2,7 @@ * */ +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -27,21 +28,38 @@ if(key.hash == 0) { key.hash = ucx_hash((char*)key.data, key.len); } - void *kd = malloc(key.len); + void *kd = calloc(1, key.len); memcpy(kd, key.data, key.len); key.data = kd; UcxMapElement *elm = &map->map[key.hash%map->size]; - if(elm->next != NULL) { - while(elm->next != NULL) { - elm = elm->next; + if(elm->key.hash != 0) { + UcxMapElement *e = elm; + for(;;) { + /* check if the key is already in use */ + if(key.hash == e->key.hash) { + int n = (key.len > elm->key.len) ? elm->key.len : key.len; + if(memcmp(elm->key.data, key.data, n) == 0) { + /* use elm as storage place */ + elm = e; + break; + } + } + + /* check if this is the last element */ + if(e->next == NULL) { + /* create new element */ + UcxMapElement *en = (UcxMapElement*)malloc(sizeof(UcxMapElement)); + if(en == NULL) { + return -1; + } + e->next = en; + elm = en; + break; + } + + e = e->next; } - UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement)); - if(e == NULL) { - return -1; - } - elm->next = e; - elm = e; } elm->key = key; @@ -54,7 +72,7 @@ if(key.hash == 0) { key.hash = ucx_hash((char*)key.data, key.len); } - + UcxMapElement *elm = &map->map[key.hash%map->size]; while(elm != NULL) { if(elm->key.hash == key.hash) {