ucx/map.c

changeset 30
23bb65cbf7a4
parent 29
bce0d7f2912b
child 31
91ac86557290
     1.1 --- a/ucx/map.c	Tue Feb 21 01:13:17 2012 +0100
     1.2 +++ b/ucx/map.c	Fri Feb 24 15:53:50 2012 +0100
     1.3 @@ -29,11 +29,13 @@
     1.4          if (elem != NULL) {
     1.5              do {
     1.6                  UcxMapElement *next = elem->next;
     1.7 +                free(elem->key.data);
     1.8                  free(elem);
     1.9                  elem = next;
    1.10              } while (elem != NULL);
    1.11          }
    1.12      }
    1.13 +    free(map->map);
    1.14      free(map);
    1.15  }
    1.16  
    1.17 @@ -41,12 +43,6 @@
    1.18      if(key.hash == 0) {
    1.19          key.hash = ucx_hash((char*)key.data, key.len);
    1.20      }
    1.21 -    void *kd = malloc(key.len);
    1.22 -    if (kd == NULL) {
    1.23 -        return -1;
    1.24 -    }
    1.25 -    memcpy(kd, key.data, key.len);
    1.26 -    key.data = kd;
    1.27  
    1.28      size_t slot = key.hash%map->size;
    1.29      UcxMapElement *elm = map->map[slot];
    1.30 @@ -62,6 +58,7 @@
    1.31          if(e == NULL) {
    1.32              return -1;
    1.33          }
    1.34 +        e->key.data = NULL;
    1.35          if (prev == NULL) {
    1.36              map->map[slot] = e;
    1.37          } else {
    1.38 @@ -71,7 +68,15 @@
    1.39          elm = e;
    1.40      }
    1.41      
    1.42 -    elm->key = key;
    1.43 +    if(elm->key.data == NULL) {
    1.44 +        void *kd = malloc(key.len);
    1.45 +        if (kd == NULL) {
    1.46 +            return -1;
    1.47 +        }
    1.48 +        memcpy(kd, key.data, key.len);
    1.49 +        key.data = kd;
    1.50 +        elm->key = key;
    1.51 +    }
    1.52      elm->data = data;
    1.53  
    1.54      return 0;

mercurial