fixed mpool compiler warnings

Sat, 31 Dec 2011 22:41:16 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 31 Dec 2011 22:41:16 +0100
changeset 15
2dc4c688c262
parent 14
b78e174b6814
child 16
b4769e4eb4d1

fixed mpool compiler warnings

ucx/mpool.c file | annotate | diff | comparison | revisions
ucx/mpool.h file | annotate | diff | comparison | revisions
--- a/ucx/mpool.c	Sat Dec 31 22:21:45 2011 +0100
+++ b/ucx/mpool.c	Sat Dec 31 22:41:16 2011 +0100
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <errno.h>
 
 #include "mpool.h"
 
@@ -25,27 +26,37 @@
 
 UcxMempool *ucx_mempool_new(size_t n) {
     UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
+    if (pool == NULL) return NULL;
+    
     pool->data = malloc(n * sizeof(void*));
+    if (pool->data == NULL) {
+        free(pool);
+        return NULL;
+    }
+    
     pool->ndata = 0;
     pool->size = n;
     return pool;
 }
 
-void ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {    
+int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
     void **data = realloc(pool->data, newcap*sizeof(void*));
-    pool->data = data; 
-    pool->size = newcap;
+    if (data == NULL) {
+        return ENOMEM;
+    } else {
+        pool->data = data; 
+        pool->size = newcap;
+        return EXIT_SUCCESS;
+    }
 }
 
 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
     ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
-    if(mem == NULL) {
-        return NULL;
-    }
+    if (mem == NULL) return NULL;
 
     if (pool->ndata >= pool->size) {
         ucx_mempool_chcap(pool, pool->size + 16);
-    }
+     }
 
     mem->destructor = NULL;
     pool->data[pool->ndata] = mem;
@@ -65,18 +76,19 @@
 
 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
     void *mem = ((char*)ptr) - sizeof(ucx_destructor);
-    void *newm = realloc(mem, n + sizeof(ucx_destructor));
+    char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
     if (newm == NULL) return NULL;
-    if(mem != newm) {
+    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 */
+        fprintf(stderr, "FATAL: %8x not in mpool %8x\n", mem, pool);
+        exit(1);
     }
-    return newm + sizeof(ucx_destructor);
+    return ((char*) newm) + sizeof(ucx_destructor);
 }
 
 void ucx_mempool_free(UcxMempool *pool) {
--- a/ucx/mpool.h	Sat Dec 31 22:21:45 2011 +0100
+++ b/ucx/mpool.h	Sat Dec 31 22:41:16 2011 +0100
@@ -19,7 +19,7 @@
 
 #define ucx_mempool_new_default() ucx_mempool_new(16)
 UcxMempool *ucx_mempool_new(size_t n);
-void ucx_mempool_chcap(UcxMempool *pool, size_t newcap);
+int ucx_mempool_chcap(UcxMempool *pool, size_t newcap);
 
 void *ucx_mempool_malloc(UcxMempool *pool, size_t n);
 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize);

mercurial