Sat, 31 Dec 2011 22:41:16 +0100
fixed mpool compiler warnings
ucx/mpool.c | file | annotate | diff | comparison | revisions | |
ucx/mpool.h | file | annotate | diff | comparison | revisions |
1.1 --- a/ucx/mpool.c Sat Dec 31 22:21:45 2011 +0100 1.2 +++ b/ucx/mpool.c Sat Dec 31 22:41:16 2011 +0100 1.3 @@ -5,6 +5,7 @@ 1.4 #include <stdlib.h> 1.5 #include <string.h> 1.6 #include <stdio.h> 1.7 +#include <errno.h> 1.8 1.9 #include "mpool.h" 1.10 1.11 @@ -25,27 +26,37 @@ 1.12 1.13 UcxMempool *ucx_mempool_new(size_t n) { 1.14 UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); 1.15 + if (pool == NULL) return NULL; 1.16 + 1.17 pool->data = malloc(n * sizeof(void*)); 1.18 + if (pool->data == NULL) { 1.19 + free(pool); 1.20 + return NULL; 1.21 + } 1.22 + 1.23 pool->ndata = 0; 1.24 pool->size = n; 1.25 return pool; 1.26 } 1.27 1.28 -void ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { 1.29 +int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { 1.30 void **data = realloc(pool->data, newcap*sizeof(void*)); 1.31 - pool->data = data; 1.32 - pool->size = newcap; 1.33 + if (data == NULL) { 1.34 + return ENOMEM; 1.35 + } else { 1.36 + pool->data = data; 1.37 + pool->size = newcap; 1.38 + return EXIT_SUCCESS; 1.39 + } 1.40 } 1.41 1.42 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { 1.43 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); 1.44 - if(mem == NULL) { 1.45 - return NULL; 1.46 - } 1.47 + if (mem == NULL) return NULL; 1.48 1.49 if (pool->ndata >= pool->size) { 1.50 ucx_mempool_chcap(pool, pool->size + 16); 1.51 - } 1.52 + } 1.53 1.54 mem->destructor = NULL; 1.55 pool->data[pool->ndata] = mem; 1.56 @@ -65,18 +76,19 @@ 1.57 1.58 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { 1.59 void *mem = ((char*)ptr) - sizeof(ucx_destructor); 1.60 - void *newm = realloc(mem, n + sizeof(ucx_destructor)); 1.61 + char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); 1.62 if (newm == NULL) return NULL; 1.63 - if(mem != newm) { 1.64 + if (mem != newm) { 1.65 for(int i=0;i<pool->ndata;i++) { 1.66 if(pool->data[i] == mem) { 1.67 pool->data[i] = newm; 1.68 break; 1.69 } 1.70 } 1.71 - /* TODO: exit or kill stultus programmer */ 1.72 + fprintf(stderr, "FATAL: %8x not in mpool %8x\n", mem, pool); 1.73 + exit(1); 1.74 } 1.75 - return newm + sizeof(ucx_destructor); 1.76 + return ((char*) newm) + sizeof(ucx_destructor); 1.77 } 1.78 1.79 void ucx_mempool_free(UcxMempool *pool) {
2.1 --- a/ucx/mpool.h Sat Dec 31 22:21:45 2011 +0100 2.2 +++ b/ucx/mpool.h Sat Dec 31 22:41:16 2011 +0100 2.3 @@ -19,7 +19,7 @@ 2.4 2.5 #define ucx_mempool_new_default() ucx_mempool_new(16) 2.6 UcxMempool *ucx_mempool_new(size_t n); 2.7 -void ucx_mempool_chcap(UcxMempool *pool, size_t newcap); 2.8 +int ucx_mempool_chcap(UcxMempool *pool, size_t newcap); 2.9 2.10 void *ucx_mempool_malloc(UcxMempool *pool, size_t n); 2.11 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize);