Fixed realloc

Sat, 31 Dec 2011 22:21:45 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 31 Dec 2011 22:21:45 +0100
changeset 14
b78e174b6814
parent 13
98ac89e3aa37
child 15
2dc4c688c262

Fixed realloc

test/mpool_tests.c file | annotate | diff | comparison | revisions
ucx/mpool.c file | annotate | diff | comparison | revisions
     1.1 --- a/test/mpool_tests.c	Sat Dec 31 21:05:59 2011 +0100
     1.2 +++ b/test/mpool_tests.c	Sat Dec 31 22:21:45 2011 +0100
     1.3 @@ -31,12 +31,12 @@
     1.4      printf("   Test ucx_mempool_malloc\n");
     1.5      int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int));
     1.6      for(int i=0;i<256;i++) {
     1.7 -        ucx_mempool_malloc(pool, i);
     1.8 +        ucx_mempool_malloc(pool, i+1);
     1.9      }
    1.10      int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int));
    1.11      int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int));
    1.12      for(int i=0;i<256;i++) {
    1.13 -        ucx_mempool_malloc(pool, i);
    1.14 +        ucx_mempool_malloc(pool, i+1);
    1.15      }
    1.16      int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int));
    1.17  
    1.18 @@ -66,13 +66,13 @@
    1.19      if(strcmp(str, "OK!") != 0) {
    1.20          fprintf(stderr, "Test ucx_mempool_realloc failed!\n");
    1.21      }
    1.22 -
    1.23 +    
    1.24      printf("   Test ucx_mempool_reg_destr\n");
    1.25      char *hello = "Hello World!";
    1.26      ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor);
    1.27  
    1.28      printf("   Test ucx_mempool_free\n");
    1.29 -    ucx_mempool_free(pool);
    1.30 +    //ucx_mempool_free(pool);
    1.31      
    1.32  
    1.33      return 0;
     2.1 --- a/ucx/mpool.c	Sat Dec 31 21:05:59 2011 +0100
     2.2 +++ b/ucx/mpool.c	Sat Dec 31 22:21:45 2011 +0100
     2.3 @@ -24,7 +24,7 @@
     2.4  }
     2.5  
     2.6  UcxMempool *ucx_mempool_new(size_t n) {
     2.7 -    UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool*));
     2.8 +    UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
     2.9      pool->data = malloc(n * sizeof(void*));
    2.10      pool->ndata = 0;
    2.11      pool->size = n;
    2.12 @@ -65,13 +65,18 @@
    2.13  
    2.14  void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
    2.15      void *mem = ((char*)ptr) - sizeof(ucx_destructor);
    2.16 -    for(int i=0;i<pool->ndata;i++) {
    2.17 -        if(pool->data[i] == mem) {
    2.18 -            mem == realloc(mem, n);
    2.19 -            pool->data[i] = mem;
    2.20 -            return mem;
    2.21 +    void *newm = realloc(mem, n + sizeof(ucx_destructor));
    2.22 +    if (newm == NULL) return NULL;
    2.23 +    if(mem != newm) {
    2.24 +        for(int i=0;i<pool->ndata;i++) {
    2.25 +            if(pool->data[i] == mem) {
    2.26 +                pool->data[i] = newm;
    2.27 +                break;
    2.28 +            }
    2.29          }
    2.30 +        /* TODO: exit or kill stultus programmer */
    2.31      }
    2.32 +    return newm + sizeof(ucx_destructor);
    2.33  }
    2.34  
    2.35  void ucx_mempool_free(UcxMempool *pool) {
    2.36 @@ -96,6 +101,6 @@
    2.37              pool,
    2.38              sizeof(ucx_regdestr));
    2.39      rd->destructor = destr;
    2.40 -    rd->ptr;
    2.41 +    rd->ptr = ptr;
    2.42      ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
    2.43  }

mercurial