some fixes

Fri, 24 Feb 2012 15:53:50 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 24 Feb 2012 15:53:50 +0100
changeset 30
23bb65cbf7a4
parent 29
bce0d7f2912b
child 31
91ac86557290

some fixes

test/dlist_tests.c file | annotate | diff | comparison | revisions
test/list_tests.c file | annotate | diff | comparison | revisions
test/mpool_tests.c file | annotate | diff | comparison | revisions
ucx/map.c file | annotate | diff | comparison | revisions
ucx/map.h file | annotate | diff | comparison | revisions
ucx/string.c file | annotate | diff | comparison | revisions
--- a/test/dlist_tests.c	Tue Feb 21 01:13:17 2012 +0100
+++ b/test/dlist_tests.c	Fri Feb 24 15:53:50 2012 +0100
@@ -60,7 +60,6 @@
     UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
     UCX_TEST_ASSERT(list->next->next == NULL, "failed")
     
-    ucx_dlist_free(list2);
     ucx_dlist_free(list);
     
     UCX_TEST_END
@@ -160,8 +159,8 @@
 
     free(world);
     free(hello);
-    free(list);
-    free(copy);
+    ucx_dlist_free(list);
+    ucx_dlist_free(copy);
     
     UCX_TEST_END
 }
--- a/test/list_tests.c	Tue Feb 21 01:13:17 2012 +0100
+++ b/test/list_tests.c	Fri Feb 24 15:53:50 2012 +0100
@@ -60,7 +60,6 @@
     UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
     UCX_TEST_ASSERT(list->next->next == NULL, "failed")
     
-    ucx_list_free(list2);
     ucx_list_free(list);
     
     UCX_TEST_END
@@ -140,14 +139,14 @@
     UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed")
     UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
     UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
-
+            
     free(copy->next->data);
     free(copy->data);
 
     free(world);
     free(hello);
-    free(list);
-    free(copy);
+    ucx_list_free(list);
+    ucx_list_free(copy);
     
     UCX_TEST_END
 }
--- a/test/mpool_tests.c	Tue Feb 21 01:13:17 2012 +0100
+++ b/test/mpool_tests.c	Fri Feb 24 15:53:50 2012 +0100
@@ -2,6 +2,8 @@
  *
  */
 
+#include <inttypes.h>
+
 #include "mpool_tests.h"
 
 UCX_TEST_BEGIN(test_ucx_mempool_new) {
@@ -70,7 +72,7 @@
 }
 
 void test_setdestr(void* elem) {
-    int *cb = (int*) ((int*) elem)[1];
+    intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
     *cb = 42;
 }
 
@@ -79,20 +81,19 @@
     UcxMempool *pool = ucx_mempool_new(2);
     
     ucx_mempool_malloc(pool, sizeof(int));
-    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
+    intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
     
-    int *cb = (int*) malloc(sizeof(int));
+    int *cb = (intptr_t*) malloc(sizeof(intptr_t));
     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
     
-    test[0] = 5; test[1] = (int) cb;
+    test[0] = 5; test[1] = (intptr_t) cb;
     *cb = 13;
     
     ucx_mempool_set_destr(test, test_setdestr);
-    
     UCX_TEST_ASSERT(
             *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
     UCX_TEST_ASSERT(
-            test[0] == 5 && test[1] == (int) cb, "setdestr destroyed data")
+            test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
     
     ucx_mempool_free(pool);
     
@@ -108,22 +109,23 @@
     
     UcxMempool *pool = ucx_mempool_new(1);
     
-    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
+    intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
     
-    int *cb = (int*) malloc(sizeof(int));
+    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
     
-    test[0] = 5; test[1] = (int) cb;
+    test[0] = 5; test[1] = (intptr_t) cb;
     *cb = 13;
     
     ucx_mempool_reg_destr(pool, test, test_setdestr);
     
     ucx_destructor *pooladdr = (ucx_destructor*)
-            ((char*)pool->data[1] + sizeof(ucx_destructor));
+            ((char*)pool->data[0] + sizeof(ucx_destructor));
     
     UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed")
     
     ucx_mempool_free(pool);
+    free(test);
     
     UCX_TEST_ASSERT(*cb == 42, "destructor not called")
 
@@ -137,12 +139,12 @@
     UcxMempool *pool = ucx_mempool_new(2);
     
     ucx_mempool_malloc(pool, sizeof(int));
-    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
+    intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
     
-    int *cb = (int*) malloc(sizeof(int));
+    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
     
-    test[0] = 5; test[1] = (int) cb;
+    test[0] = 5; test[1] = (intptr_t) cb;
     *cb = 13;
     
     ucx_mempool_set_destr(test, test_setdestr);
@@ -151,14 +153,14 @@
     do {
         n *= 2;
         UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc")
-        rtest = ucx_mempool_realloc(pool, test, n*sizeof(int));
+        rtest = ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
     } while (rtest == test);
     test = rtest;
     
     UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
             "realloc killed destructor")
     UCX_TEST_ASSERT(
-            test[0] == 5 && test[1] == (int) cb, "realloc destroyed data")
+            test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
     
     ucx_mempool_free(pool);
     
--- a/ucx/map.c	Tue Feb 21 01:13:17 2012 +0100
+++ b/ucx/map.c	Fri Feb 24 15:53:50 2012 +0100
@@ -29,11 +29,13 @@
         if (elem != NULL) {
             do {
                 UcxMapElement *next = elem->next;
+                free(elem->key.data);
                 free(elem);
                 elem = next;
             } while (elem != NULL);
         }
     }
+    free(map->map);
     free(map);
 }
 
@@ -41,12 +43,6 @@
     if(key.hash == 0) {
         key.hash = ucx_hash((char*)key.data, key.len);
     }
-    void *kd = malloc(key.len);
-    if (kd == NULL) {
-        return -1;
-    }
-    memcpy(kd, key.data, key.len);
-    key.data = kd;
 
     size_t slot = key.hash%map->size;
     UcxMapElement *elm = map->map[slot];
@@ -62,6 +58,7 @@
         if(e == NULL) {
             return -1;
         }
+        e->key.data = NULL;
         if (prev == NULL) {
             map->map[slot] = e;
         } else {
@@ -71,7 +68,15 @@
         elm = e;
     }
     
-    elm->key = key;
+    if(elm->key.data == NULL) {
+        void *kd = malloc(key.len);
+        if (kd == NULL) {
+            return -1;
+        }
+        memcpy(kd, key.data, key.len);
+        key.data = kd;
+        elm->key = key;
+    }
     elm->data = data;
 
     return 0;
--- a/ucx/map.h	Tue Feb 21 01:13:17 2012 +0100
+++ b/ucx/map.h	Fri Feb 24 15:53:50 2012 +0100
@@ -40,9 +40,9 @@
 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
 void* ucx_map_get(UcxMap *map, UcxKey key);
 
-#define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, 1+s.length), d)
+#define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d)
 #define ucx_map_cstr_put(m, s, d) ucx_map_put(m, ucx_key(s, 1+strlen(s)), d)
-#define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, 1+s.length))
+#define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, s.length))
 #define ucx_map_cstr_get(m, s) ucx_map_get(m, ucx_key(s, 1+strlen(s)))
 
 UcxKey ucx_key(void *data, size_t len);
--- a/ucx/string.c	Tue Feb 21 01:13:17 2012 +0100
+++ b/ucx/string.c	Fri Feb 24 15:53:50 2012 +0100
@@ -6,7 +6,7 @@
  */
 
 #include <stdlib.h>
-#include <string.h>
+#include <strings.h>
 #include <stdarg.h>
 
 #include "string.h"

mercurial