src/ucx/map.c

changeset 99
b9a6af0ae41a
parent 91
fac51f87def0
child 135
471e28cca288
--- a/src/ucx/map.c	Mon Apr 06 16:36:25 2015 +0200
+++ b/src/ucx/map.c	Fri Oct 16 19:23:49 2015 +0200
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2013 Olaf Wintermann. All rights reserved.
+ * Copyright 2015 Olaf Wintermann. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -44,18 +44,16 @@
         allocator = ucx_default_allocator();
     }
     
-    UcxMap *map = (UcxMap*)allocator->malloc(allocator->pool, sizeof(UcxMap));
+    UcxMap *map = (UcxMap*)almalloc(allocator, sizeof(UcxMap));
     if (!map) {
         return NULL;
     }
     
     map->allocator = allocator;
-    map->map = (UcxMapElement**)allocator->calloc(
-            allocator->pool,
-            size,
-            sizeof(UcxMapElement*));
+    map->map = (UcxMapElement**)alcalloc(
+            allocator, size, sizeof(UcxMapElement*));
     if(map->map == NULL) {
-        allocator->free(allocator->pool, map);
+        alfree(allocator, map);
         return NULL;
     }
     map->size = size;
@@ -64,24 +62,41 @@
     return map;
 }
 
-static void ucx_map_free_elmlist(UcxMap *map) {
+static void ucx_map_free_elmlist_contents(UcxMap *map) {
     for (size_t n = 0 ; n < map->size ; n++) {
         UcxMapElement *elem = map->map[n];
         if (elem != NULL) {
             do {
                 UcxMapElement *next = elem->next;
-                map->allocator->free(map->allocator->pool, elem->key.data);
-                map->allocator->free(map->allocator->pool, elem);
+                alfree(map->allocator, elem->key.data);
+                alfree(map->allocator, elem);
                 elem = next;
             } while (elem != NULL);
         }
     }
-    map->allocator->free(map->allocator->pool, map->map);
 }
 
 void ucx_map_free(UcxMap *map) {
-    ucx_map_free_elmlist(map);
-    map->allocator->free(map->allocator->pool, map);
+    ucx_map_free_elmlist_contents(map);
+    alfree(map->allocator, map->map);
+    alfree(map->allocator, map);
+}
+
+void ucx_map_free_content(UcxMap *map, ucx_destructor destr) {
+    UcxMapIterator iter = ucx_map_iterator(map);
+    void *val;
+    UCX_MAP_FOREACH(key, val, iter) {
+        destr(val);
+    }
+}
+
+void ucx_map_clear(UcxMap *map) {
+    if (map->count == 0) {
+        return; // nothing to do
+    }
+    ucx_map_free_elmlist_contents(map);
+    memset(map->map, 0, map->size*sizeof(UcxMapElement*));
+    map->count = 0;
 }
 
 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
@@ -116,10 +131,8 @@
         oldmap.allocator = map->allocator;
         
         map->size = (map->count * 5) >> 1;
-        map->map = (UcxMapElement**)map->allocator->calloc(
-                map->allocator->pool,
-                map->size,
-                sizeof(UcxMapElement*));
+        map->map = (UcxMapElement**)alcalloc(
+                map->allocator, map->size, sizeof(UcxMapElement*));
         if (!map->map) {
             *map = oldmap;
             return 1;
@@ -128,7 +141,8 @@
         ucx_map_copy(&oldmap, map, NULL, NULL);
         
         /* free the UcxMapElement list of oldmap */
-        ucx_map_free_elmlist(&oldmap);
+        ucx_map_free_elmlist_contents(&oldmap);
+        alfree(map->allocator, oldmap.map);
     }
     return 0;
 }
@@ -150,9 +164,8 @@
     }
     
     if (!elm || elm->key.hash != key.hash) {
-        UcxMapElement *e = (UcxMapElement*)allocator->malloc(
-                allocator->pool,
-                sizeof(UcxMapElement));
+        UcxMapElement *e = (UcxMapElement*)almalloc(
+                allocator, sizeof(UcxMapElement));
         if (!e) {
             return -1;
         }
@@ -167,7 +180,7 @@
     }
     
     if (!elm->key.data) {
-        void *kd = allocator->malloc(allocator->pool, key.len);
+        void *kd = almalloc(allocator, key.len);
         if (!kd) {
             return -1;
         }
@@ -200,8 +213,8 @@
                     } else {
                         map->map[slot] = elm->next;
                     }
-                    map->allocator->free(map->allocator->pool, elm->key.data);
-                    map->allocator->free(map->allocator->pool, elm);
+                    alfree(map->allocator, elm->key.data);
+                    alfree(map->allocator, elm);
                     map->count--;
                 }
 

mercurial