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
     1.1 --- a/test/dlist_tests.c	Tue Feb 21 01:13:17 2012 +0100
     1.2 +++ b/test/dlist_tests.c	Fri Feb 24 15:53:50 2012 +0100
     1.3 @@ -60,7 +60,6 @@
     1.4      UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
     1.5      UCX_TEST_ASSERT(list->next->next == NULL, "failed")
     1.6      
     1.7 -    ucx_dlist_free(list2);
     1.8      ucx_dlist_free(list);
     1.9      
    1.10      UCX_TEST_END
    1.11 @@ -160,8 +159,8 @@
    1.12  
    1.13      free(world);
    1.14      free(hello);
    1.15 -    free(list);
    1.16 -    free(copy);
    1.17 +    ucx_dlist_free(list);
    1.18 +    ucx_dlist_free(copy);
    1.19      
    1.20      UCX_TEST_END
    1.21  }
     2.1 --- a/test/list_tests.c	Tue Feb 21 01:13:17 2012 +0100
     2.2 +++ b/test/list_tests.c	Fri Feb 24 15:53:50 2012 +0100
     2.3 @@ -60,7 +60,6 @@
     2.4      UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
     2.5      UCX_TEST_ASSERT(list->next->next == NULL, "failed")
     2.6      
     2.7 -    ucx_list_free(list2);
     2.8      ucx_list_free(list);
     2.9      
    2.10      UCX_TEST_END
    2.11 @@ -140,14 +139,14 @@
    2.12      UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed")
    2.13      UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
    2.14      UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
    2.15 -
    2.16 +            
    2.17      free(copy->next->data);
    2.18      free(copy->data);
    2.19  
    2.20      free(world);
    2.21      free(hello);
    2.22 -    free(list);
    2.23 -    free(copy);
    2.24 +    ucx_list_free(list);
    2.25 +    ucx_list_free(copy);
    2.26      
    2.27      UCX_TEST_END
    2.28  }
     3.1 --- a/test/mpool_tests.c	Tue Feb 21 01:13:17 2012 +0100
     3.2 +++ b/test/mpool_tests.c	Fri Feb 24 15:53:50 2012 +0100
     3.3 @@ -2,6 +2,8 @@
     3.4   *
     3.5   */
     3.6  
     3.7 +#include <inttypes.h>
     3.8 +
     3.9  #include "mpool_tests.h"
    3.10  
    3.11  UCX_TEST_BEGIN(test_ucx_mempool_new) {
    3.12 @@ -70,7 +72,7 @@
    3.13  }
    3.14  
    3.15  void test_setdestr(void* elem) {
    3.16 -    int *cb = (int*) ((int*) elem)[1];
    3.17 +    intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
    3.18      *cb = 42;
    3.19  }
    3.20  
    3.21 @@ -79,20 +81,19 @@
    3.22      UcxMempool *pool = ucx_mempool_new(2);
    3.23      
    3.24      ucx_mempool_malloc(pool, sizeof(int));
    3.25 -    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
    3.26 +    intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
    3.27      
    3.28 -    int *cb = (int*) malloc(sizeof(int));
    3.29 +    int *cb = (intptr_t*) malloc(sizeof(intptr_t));
    3.30      UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
    3.31      
    3.32 -    test[0] = 5; test[1] = (int) cb;
    3.33 +    test[0] = 5; test[1] = (intptr_t) cb;
    3.34      *cb = 13;
    3.35      
    3.36      ucx_mempool_set_destr(test, test_setdestr);
    3.37 -    
    3.38      UCX_TEST_ASSERT(
    3.39              *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
    3.40      UCX_TEST_ASSERT(
    3.41 -            test[0] == 5 && test[1] == (int) cb, "setdestr destroyed data")
    3.42 +            test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
    3.43      
    3.44      ucx_mempool_free(pool);
    3.45      
    3.46 @@ -108,22 +109,23 @@
    3.47      
    3.48      UcxMempool *pool = ucx_mempool_new(1);
    3.49      
    3.50 -    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
    3.51 +    intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
    3.52      
    3.53 -    int *cb = (int*) malloc(sizeof(int));
    3.54 +    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
    3.55      UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
    3.56      
    3.57 -    test[0] = 5; test[1] = (int) cb;
    3.58 +    test[0] = 5; test[1] = (intptr_t) cb;
    3.59      *cb = 13;
    3.60      
    3.61      ucx_mempool_reg_destr(pool, test, test_setdestr);
    3.62      
    3.63      ucx_destructor *pooladdr = (ucx_destructor*)
    3.64 -            ((char*)pool->data[1] + sizeof(ucx_destructor));
    3.65 +            ((char*)pool->data[0] + sizeof(ucx_destructor));
    3.66      
    3.67      UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed")
    3.68      
    3.69      ucx_mempool_free(pool);
    3.70 +    free(test);
    3.71      
    3.72      UCX_TEST_ASSERT(*cb == 42, "destructor not called")
    3.73  
    3.74 @@ -137,12 +139,12 @@
    3.75      UcxMempool *pool = ucx_mempool_new(2);
    3.76      
    3.77      ucx_mempool_malloc(pool, sizeof(int));
    3.78 -    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
    3.79 +    intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
    3.80      
    3.81 -    int *cb = (int*) malloc(sizeof(int));
    3.82 +    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
    3.83      UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
    3.84      
    3.85 -    test[0] = 5; test[1] = (int) cb;
    3.86 +    test[0] = 5; test[1] = (intptr_t) cb;
    3.87      *cb = 13;
    3.88      
    3.89      ucx_mempool_set_destr(test, test_setdestr);
    3.90 @@ -151,14 +153,14 @@
    3.91      do {
    3.92          n *= 2;
    3.93          UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc")
    3.94 -        rtest = ucx_mempool_realloc(pool, test, n*sizeof(int));
    3.95 +        rtest = ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
    3.96      } while (rtest == test);
    3.97      test = rtest;
    3.98      
    3.99      UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
   3.100              "realloc killed destructor")
   3.101      UCX_TEST_ASSERT(
   3.102 -            test[0] == 5 && test[1] == (int) cb, "realloc destroyed data")
   3.103 +            test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
   3.104      
   3.105      ucx_mempool_free(pool);
   3.106      
     4.1 --- a/ucx/map.c	Tue Feb 21 01:13:17 2012 +0100
     4.2 +++ b/ucx/map.c	Fri Feb 24 15:53:50 2012 +0100
     4.3 @@ -29,11 +29,13 @@
     4.4          if (elem != NULL) {
     4.5              do {
     4.6                  UcxMapElement *next = elem->next;
     4.7 +                free(elem->key.data);
     4.8                  free(elem);
     4.9                  elem = next;
    4.10              } while (elem != NULL);
    4.11          }
    4.12      }
    4.13 +    free(map->map);
    4.14      free(map);
    4.15  }
    4.16  
    4.17 @@ -41,12 +43,6 @@
    4.18      if(key.hash == 0) {
    4.19          key.hash = ucx_hash((char*)key.data, key.len);
    4.20      }
    4.21 -    void *kd = malloc(key.len);
    4.22 -    if (kd == NULL) {
    4.23 -        return -1;
    4.24 -    }
    4.25 -    memcpy(kd, key.data, key.len);
    4.26 -    key.data = kd;
    4.27  
    4.28      size_t slot = key.hash%map->size;
    4.29      UcxMapElement *elm = map->map[slot];
    4.30 @@ -62,6 +58,7 @@
    4.31          if(e == NULL) {
    4.32              return -1;
    4.33          }
    4.34 +        e->key.data = NULL;
    4.35          if (prev == NULL) {
    4.36              map->map[slot] = e;
    4.37          } else {
    4.38 @@ -71,7 +68,15 @@
    4.39          elm = e;
    4.40      }
    4.41      
    4.42 -    elm->key = key;
    4.43 +    if(elm->key.data == NULL) {
    4.44 +        void *kd = malloc(key.len);
    4.45 +        if (kd == NULL) {
    4.46 +            return -1;
    4.47 +        }
    4.48 +        memcpy(kd, key.data, key.len);
    4.49 +        key.data = kd;
    4.50 +        elm->key = key;
    4.51 +    }
    4.52      elm->data = data;
    4.53  
    4.54      return 0;
     5.1 --- a/ucx/map.h	Tue Feb 21 01:13:17 2012 +0100
     5.2 +++ b/ucx/map.h	Fri Feb 24 15:53:50 2012 +0100
     5.3 @@ -40,9 +40,9 @@
     5.4  int ucx_map_put(UcxMap *map, UcxKey key, void *data);
     5.5  void* ucx_map_get(UcxMap *map, UcxKey key);
     5.6  
     5.7 -#define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, 1+s.length), d)
     5.8 +#define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d)
     5.9  #define ucx_map_cstr_put(m, s, d) ucx_map_put(m, ucx_key(s, 1+strlen(s)), d)
    5.10 -#define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, 1+s.length))
    5.11 +#define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, s.length))
    5.12  #define ucx_map_cstr_get(m, s) ucx_map_get(m, ucx_key(s, 1+strlen(s)))
    5.13  
    5.14  UcxKey ucx_key(void *data, size_t len);
     6.1 --- a/ucx/string.c	Tue Feb 21 01:13:17 2012 +0100
     6.2 +++ b/ucx/string.c	Fri Feb 24 15:53:50 2012 +0100
     6.3 @@ -6,7 +6,7 @@
     6.4   */
     6.5  
     6.6  #include <stdlib.h>
     6.7 -#include <string.h>
     6.8 +#include <strings.h>
     6.9  #include <stdarg.h>
    6.10  
    6.11  #include "string.h"

mercurial