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
--- a/test/mpool_tests.c	Sat Dec 31 21:05:59 2011 +0100
+++ b/test/mpool_tests.c	Sat Dec 31 22:21:45 2011 +0100
@@ -31,12 +31,12 @@
     printf("   Test ucx_mempool_malloc\n");
     int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int));
     for(int i=0;i<256;i++) {
-        ucx_mempool_malloc(pool, i);
+        ucx_mempool_malloc(pool, i+1);
     }
     int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int));
     int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int));
     for(int i=0;i<256;i++) {
-        ucx_mempool_malloc(pool, i);
+        ucx_mempool_malloc(pool, i+1);
     }
     int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int));
 
@@ -66,13 +66,13 @@
     if(strcmp(str, "OK!") != 0) {
         fprintf(stderr, "Test ucx_mempool_realloc failed!\n");
     }
-
+    
     printf("   Test ucx_mempool_reg_destr\n");
     char *hello = "Hello World!";
     ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor);
 
     printf("   Test ucx_mempool_free\n");
-    ucx_mempool_free(pool);
+    //ucx_mempool_free(pool);
     
 
     return 0;
--- a/ucx/mpool.c	Sat Dec 31 21:05:59 2011 +0100
+++ b/ucx/mpool.c	Sat Dec 31 22:21:45 2011 +0100
@@ -24,7 +24,7 @@
 }
 
 UcxMempool *ucx_mempool_new(size_t n) {
-    UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool*));
+    UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
     pool->data = malloc(n * sizeof(void*));
     pool->ndata = 0;
     pool->size = n;
@@ -65,13 +65,18 @@
 
 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
     void *mem = ((char*)ptr) - sizeof(ucx_destructor);
-    for(int i=0;i<pool->ndata;i++) {
-        if(pool->data[i] == mem) {
-            mem == realloc(mem, n);
-            pool->data[i] = mem;
-            return mem;
+    void *newm = realloc(mem, n + sizeof(ucx_destructor));
+    if (newm == NULL) return NULL;
+    if(mem != newm) {
+        for(int i=0;i<pool->ndata;i++) {
+            if(pool->data[i] == mem) {
+                pool->data[i] = newm;
+                break;
+            }
         }
+        /* TODO: exit or kill stultus programmer */
     }
+    return newm + sizeof(ucx_destructor);
 }
 
 void ucx_mempool_free(UcxMempool *pool) {
@@ -96,6 +101,6 @@
             pool,
             sizeof(ucx_regdestr));
     rd->destructor = destr;
-    rd->ptr;
+    rd->ptr = ptr;
     ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
 }

mercurial