ucx/mpool.c

changeset 15
2dc4c688c262
parent 14
b78e174b6814
equal deleted inserted replaced
14:b78e174b6814 15:2dc4c688c262
3 */ 3 */
4 4
5 #include <stdlib.h> 5 #include <stdlib.h>
6 #include <string.h> 6 #include <string.h>
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <errno.h>
8 9
9 #include "mpool.h" 10 #include "mpool.h"
10 11
11 typedef struct { 12 typedef struct {
12 ucx_destructor destructor; 13 ucx_destructor destructor;
23 rd->destructor(rd->ptr); 24 rd->destructor(rd->ptr);
24 } 25 }
25 26
26 UcxMempool *ucx_mempool_new(size_t n) { 27 UcxMempool *ucx_mempool_new(size_t n) {
27 UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); 28 UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
29 if (pool == NULL) return NULL;
30
28 pool->data = malloc(n * sizeof(void*)); 31 pool->data = malloc(n * sizeof(void*));
32 if (pool->data == NULL) {
33 free(pool);
34 return NULL;
35 }
36
29 pool->ndata = 0; 37 pool->ndata = 0;
30 pool->size = n; 38 pool->size = n;
31 return pool; 39 return pool;
32 } 40 }
33 41
34 void ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { 42 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
35 void **data = realloc(pool->data, newcap*sizeof(void*)); 43 void **data = realloc(pool->data, newcap*sizeof(void*));
36 pool->data = data; 44 if (data == NULL) {
37 pool->size = newcap; 45 return ENOMEM;
46 } else {
47 pool->data = data;
48 pool->size = newcap;
49 return EXIT_SUCCESS;
50 }
38 } 51 }
39 52
40 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { 53 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
41 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); 54 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
42 if(mem == NULL) { 55 if (mem == NULL) return NULL;
43 return NULL;
44 }
45 56
46 if (pool->ndata >= pool->size) { 57 if (pool->ndata >= pool->size) {
47 ucx_mempool_chcap(pool, pool->size + 16); 58 ucx_mempool_chcap(pool, pool->size + 16);
48 } 59 }
49 60
50 mem->destructor = NULL; 61 mem->destructor = NULL;
51 pool->data[pool->ndata] = mem; 62 pool->data[pool->ndata] = mem;
52 pool->ndata++; 63 pool->ndata++;
53 64
63 return ptr; 74 return ptr;
64 } 75 }
65 76
66 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { 77 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
67 void *mem = ((char*)ptr) - sizeof(ucx_destructor); 78 void *mem = ((char*)ptr) - sizeof(ucx_destructor);
68 void *newm = realloc(mem, n + sizeof(ucx_destructor)); 79 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
69 if (newm == NULL) return NULL; 80 if (newm == NULL) return NULL;
70 if(mem != newm) { 81 if (mem != newm) {
71 for(int i=0;i<pool->ndata;i++) { 82 for(int i=0;i<pool->ndata;i++) {
72 if(pool->data[i] == mem) { 83 if(pool->data[i] == mem) {
73 pool->data[i] = newm; 84 pool->data[i] = newm;
74 break; 85 break;
75 } 86 }
76 } 87 }
77 /* TODO: exit or kill stultus programmer */ 88 fprintf(stderr, "FATAL: %8x not in mpool %8x\n", mem, pool);
89 exit(1);
78 } 90 }
79 return newm + sizeof(ucx_destructor); 91 return ((char*) newm) + sizeof(ucx_destructor);
80 } 92 }
81 93
82 void ucx_mempool_free(UcxMempool *pool) { 94 void ucx_mempool_free(UcxMempool *pool) {
83 ucx_memchunk *chunk; 95 ucx_memchunk *chunk;
84 for(int i=0;i<pool->ndata;i++) { 96 for(int i=0;i<pool->ndata;i++) {

mercurial