ucx/map.c

changeset 107
86b19c98b5fd
parent 103
08018864fb91
child 108
d2b1e67b2b48
     1.1 --- a/ucx/map.c	Fri Jun 21 11:18:24 2013 +0200
     1.2 +++ b/ucx/map.c	Thu Jul 11 17:32:48 2013 +0200
     1.3 @@ -32,18 +32,30 @@
     1.4  #include "map.h"
     1.5  
     1.6  UcxMap *ucx_map_new(size_t size) {
     1.7 +    return ucx_map_new_allocator(size, NULL);
     1.8 +}
     1.9 +
    1.10 +UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator) {
    1.11      if(size == 0) {
    1.12          size = 16;
    1.13      }
    1.14      
    1.15 -    UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap));
    1.16 +    if(!allocator) {
    1.17 +        allocator = ucx_default_allocator();
    1.18 +    }
    1.19 +    
    1.20 +    UcxMap *map = (UcxMap*)allocator->malloc(allocator->pool, sizeof(UcxMap));
    1.21      if(map == NULL) {
    1.22          return NULL;
    1.23      }
    1.24 -
    1.25 -    map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*));
    1.26 +    
    1.27 +    map->allocator = allocator;
    1.28 +    map->map = (UcxMapElement**)allocator->calloc(
    1.29 +            allocator->pool,
    1.30 +            size,
    1.31 +            sizeof(UcxMapElement*));
    1.32      if(map->map == NULL) {
    1.33 -        free(map);
    1.34 +        allocator->free(allocator->pool, map);
    1.35          return NULL;
    1.36      }
    1.37      map->size = size;
    1.38 @@ -58,18 +70,18 @@
    1.39          if (elem != NULL) {
    1.40              do {
    1.41                  UcxMapElement *next = elem->next;
    1.42 -                free(elem->key.data);
    1.43 +                map->allocator->free(map->allocator->pool, elem->key.data);
    1.44                  free(elem);
    1.45                  elem = next;
    1.46              } while (elem != NULL);
    1.47          }
    1.48      }
    1.49 -    free(map->map);
    1.50 +    map->allocator->free(map->allocator->pool, map->map);
    1.51  }
    1.52  
    1.53  void ucx_map_free(UcxMap *map) {
    1.54      ucx_map_free_elmlist(map);
    1.55 -    free(map);
    1.56 +    map->allocator->free(map->allocator->pool, map);
    1.57  }
    1.58  
    1.59  int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
    1.60 @@ -102,9 +114,13 @@
    1.61          oldmap.map = map->map;
    1.62          oldmap.size = map->size;
    1.63          oldmap.count = map->count;
    1.64 +        oldmap.allocator = map->allocator;
    1.65          
    1.66          map->size = (map->count * 5) >> 1;
    1.67 -        map->map = (UcxMapElement**)calloc(map->size, sizeof(UcxMapElement*));
    1.68 +        map->map = (UcxMapElement**)map->allocator->calloc(
    1.69 +                map->allocator->pool,
    1.70 +                map->size,
    1.71 +                sizeof(UcxMapElement*));
    1.72          if(map->map == NULL) {
    1.73              *map = oldmap;
    1.74              return 1;
    1.75 @@ -119,6 +135,8 @@
    1.76  }
    1.77  
    1.78  int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
    1.79 +    UcxAllocator *allocator = map->allocator;
    1.80 +    
    1.81      if(key.hash == 0) {
    1.82          key.hash = ucx_hash((char*)key.data, key.len);
    1.83      }
    1.84 @@ -133,7 +151,9 @@
    1.85      }
    1.86      
    1.87      if (elm == NULL || elm->key.hash != key.hash) {
    1.88 -        UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement));
    1.89 +        UcxMapElement *e = (UcxMapElement*)allocator->malloc(
    1.90 +                allocator->pool,
    1.91 +                sizeof(UcxMapElement));
    1.92          if(e == NULL) {
    1.93              return -1;
    1.94          }
    1.95 @@ -148,7 +168,7 @@
    1.96      }
    1.97      
    1.98      if(elm->key.data == NULL) {
    1.99 -        void *kd = malloc(key.len);
   1.100 +        void *kd = allocator->malloc(allocator->pool, key.len);
   1.101          if (kd == NULL) {
   1.102              return -1;
   1.103          }
   1.104 @@ -181,7 +201,7 @@
   1.105                      } else {
   1.106                          map->map[slot] = elm->next;
   1.107                      }
   1.108 -                    free(elm);
   1.109 +                    map->allocator->free(map->allocator->pool, elm);
   1.110                      map->count--;
   1.111                  }
   1.112  

mercurial