ucx/mempool.c

changeset 431
bb7da585debc
parent 187
24ce2c326d85
child 440
7c4b9cba09ca
equal deleted inserted replaced
169:fe49cff3c571 431:bb7da585debc
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "ucx/mempool.h" 29 #include "cx/mempool.h"
30 30 #include "cx/utils.h"
31 #include <stdlib.h>
32 #include <string.h> 31 #include <string.h>
33 #include <stdio.h> 32
34 #ifdef __cplusplus 33 struct cx_mempool_memory_s {
35 #define __STDC_FORMAT_MACROS 34 /** The destructor. */
36 #endif 35 cx_destructor_func destructor;
37 #include <inttypes.h> 36 /** The actual memory. */
38 37 char c[];
39 /** Capsule for destructible memory chunks. */ 38 };
40 typedef struct { 39
41 /** The destructor for the memory chunk. */ 40 static void *cx_mempool_malloc(
42 ucx_destructor destructor; 41 void *p,
43 /** 42 size_t n
44 * First byte of the memory chunk. 43 ) {
45 * Note, that the address <code>&amp;c</code> is also the address 44 struct cx_mempool_s *pool = p;
46 * of the whole memory chunk. 45
47 */ 46 if (pool->size >= pool->capacity) {
48 char c; 47 size_t newcap = pool->capacity - (pool->capacity % 16) + 16;
49 } ucx_memchunk; 48 struct cx_mempool_memory_s **newdata = realloc(pool->data, newcap*sizeof(struct cx_mempool_memory_s*));
50 49 if (newdata == NULL) {
51 /** Capsule for data and its destructor. */
52 typedef struct {
53 /** The destructor for the data. */
54 ucx_destructor destructor;
55 /** A pointer to the data. */
56 void *ptr;
57 } ucx_regdestr;
58
59 #ifdef __cplusplus
60 extern "C"
61 #endif
62 void ucx_mempool_shared_destr(void* ptr) {
63 ucx_regdestr *rd = (ucx_regdestr*)ptr;
64 rd->destructor(rd->ptr);
65 }
66
67 UcxMempool *ucx_mempool_new(size_t n) {
68 size_t poolsz;
69 if(ucx_szmul(n, sizeof(void*), &poolsz)) {
70 return NULL;
71 }
72
73 UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
74 if (!pool) {
75 return NULL;
76 }
77
78 pool->data = (void**) malloc(poolsz);
79 if (pool->data == NULL) {
80 free(pool);
81 return NULL;
82 }
83
84 pool->ndata = 0;
85 pool->size = n;
86
87 UcxAllocator *allocator = (UcxAllocator*)malloc(sizeof(UcxAllocator));
88 if(!allocator) {
89 free(pool->data);
90 free(pool);
91 return NULL;
92 }
93 allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc;
94 allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc;
95 allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc;
96 allocator->free = (ucx_allocator_free)ucx_mempool_free;
97 allocator->pool = pool;
98 pool->allocator = allocator;
99
100 return pool;
101 }
102
103 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
104 if (newcap < pool->ndata) {
105 return 1;
106 }
107
108 size_t newcapsz;
109 if(ucx_szmul(newcap, sizeof(void*), &newcapsz)) {
110 return 1;
111 }
112
113 void **data = (void**) realloc(pool->data, newcapsz);
114 if (data) {
115 pool->data = data;
116 pool->size = newcap;
117 return 0;
118 } else {
119 return 1;
120 }
121 }
122
123 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
124 if(((size_t)-1) - sizeof(ucx_destructor) < n) {
125 return NULL;
126 }
127
128 if (pool->ndata >= pool->size) {
129 size_t newcap = pool->size*2;
130 if (newcap < pool->size || ucx_mempool_chcap(pool, newcap)) {
131 return NULL; 50 return NULL;
132 } 51 }
133 } 52 pool->data = newdata;
134 53 pool->capacity = newcap;
135 void *p = malloc(sizeof(ucx_destructor) + n); 54 }
136 ucx_memchunk *mem = (ucx_memchunk*)p; 55
137 if (!mem) { 56 struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n);
138 return NULL; 57 if (mem == NULL) {
139 } 58 return NULL;
140 59 }
141 mem->destructor = NULL; 60
142 pool->data[pool->ndata] = mem; 61 mem->destructor = pool->auto_destr;
143 pool->ndata++; 62 pool->data[pool->size] = mem;
144 63 pool->size++;
145 return &(mem->c); 64
146 } 65 return mem->c;
147 66 }
148 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { 67
68 static void *cx_mempool_calloc(
69 void *p,
70 size_t nelem,
71 size_t elsize
72 ) {
149 size_t msz; 73 size_t msz;
150 if(ucx_szmul(nelem, elsize, &msz)) { 74 if (cx_szmul(nelem, elsize, &msz)) {
151 return NULL; 75 return NULL;
152 } 76 }
153 77 void *ptr = cx_mempool_malloc(p, msz);
154 void *ptr = ucx_mempool_malloc(pool, msz); 78 if (ptr == NULL) {
155 if (!ptr) {
156 return NULL; 79 return NULL;
157 } 80 }
158 memset(ptr, 0, nelem * elsize); 81 memset(ptr, 0, nelem * elsize);
159 return ptr; 82 return ptr;
160 } 83 }
161 84
162 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { 85 static void *cx_mempool_realloc(
163 if(((size_t)-1) - sizeof(ucx_destructor) < n) { 86 void *p,
164 return NULL; 87 void *ptr,
165 } 88 size_t n
166 89 ) {
167 char *mem = ((char*)ptr) - sizeof(ucx_destructor); 90 struct cx_mempool_s *pool = p;
168 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); 91
169 if (!newm) { 92 struct cx_mempool_memory_s *mem, *newm;
93 mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func));
94 newm = realloc(mem, n + sizeof(cx_destructor_func));
95
96 if (newm == NULL) {
170 return NULL; 97 return NULL;
171 } 98 }
172 if (mem != newm) { 99 if (mem != newm) {
173 for(size_t i=0 ; i < pool->ndata ; i++) { 100 cx_for_n(i, pool->size) {
174 if(pool->data[i] == mem) { 101 if (pool->data[i] == mem) {
175 pool->data[i] = newm; 102 pool->data[i] = newm;
176 return newm + sizeof(ucx_destructor); 103 return ((char*)newm) + sizeof(cx_destructor_func);
177 } 104 }
178 } 105 }
179 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
180 (intptr_t)ptr, (intptr_t)pool);
181 abort(); 106 abort();
182 } else { 107 } else {
183 return newm + sizeof(ucx_destructor); 108 return ptr;
184 } 109 }
185 } 110 }
186 111
187 void ucx_mempool_free(UcxMempool *pool, void *ptr) { 112 static void cx_mempool_free(
188 ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor)); 113 void *p,
189 for(size_t i=0 ; i<pool->ndata ; i++) { 114 void *ptr
190 if(chunk == pool->data[i]) { 115 ) {
191 if(chunk->destructor != NULL) { 116 struct cx_mempool_s *pool = p;
192 chunk->destructor(&(chunk->c)); 117
118 struct cx_mempool_memory_s *mem = (struct cx_mempool_memory_s *)
119 ((char *) ptr - sizeof(cx_destructor_func));
120
121 cx_for_n(i, pool->size) {
122 if (mem == pool->data[i]) {
123 if (mem->destructor) {
124 mem->destructor(mem->c);
193 } 125 }
194 free(chunk); 126 free(mem);
195 size_t last_index = pool->ndata - 1; 127 size_t last_index = pool->size - 1;
196 if(i != last_index) { 128 if (i != last_index) {
197 pool->data[i] = pool->data[last_index]; 129 pool->data[i] = pool->data[last_index];
198 pool->data[last_index] = NULL; 130 pool->data[last_index] = NULL;
199 } 131 }
200 pool->ndata--; 132 pool->size--;
201 return; 133 return;
202 } 134 }
203 } 135 }
204 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
205 (intptr_t)ptr, (intptr_t)pool);
206 abort(); 136 abort();
207 } 137 }
208 138
209 void ucx_mempool_destroy(UcxMempool *pool) { 139 void cxMempoolDestroy(CxMempool *pool) {
210 ucx_memchunk *chunk; 140 struct cx_mempool_memory_s *mem;
211 for(size_t i=0 ; i<pool->ndata ; i++) { 141 cx_for_n(i, pool->size) {
212 chunk = (ucx_memchunk*) pool->data[i]; 142 mem = pool->data[i];
213 if(chunk) { 143 if (mem->destructor) {
214 if(chunk->destructor) { 144 mem->destructor(mem->c);
215 chunk->destructor(&(chunk->c)); 145 }
216 } 146 free(mem);
217 free(chunk);
218 }
219 } 147 }
220 free(pool->data); 148 free(pool->data);
221 free(pool->allocator); 149 free((void*) pool->allocator);
222 free(pool); 150 free(pool);
223 } 151 }
224 152
225 void ucx_mempool_set_destr(void *ptr, ucx_destructor func) { 153 void cxMempoolSetDestructor(
226 *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func; 154 void *ptr,
227 } 155 cx_destructor_func func
228 156 ) {
229 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) { 157 *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func;
230 ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc( 158 }
159
160 struct cx_mempool_foreign_mem_s {
161 cx_destructor_func destr;
162 void* mem;
163 };
164
165 static void cx_mempool_destr_foreign_mem(void* ptr) {
166 struct cx_mempool_foreign_mem_s *fm = ptr;
167 fm->destr(fm->mem);
168 }
169
170 int cxMempoolRegister(
171 CxMempool *pool,
172 void *memory,
173 cx_destructor_func destr
174 ) {
175 struct cx_mempool_foreign_mem_s *fm = cx_mempool_malloc(
231 pool, 176 pool,
232 sizeof(ucx_regdestr)); 177 sizeof(struct cx_mempool_foreign_mem_s)
233 rd->destructor = destr; 178 );
234 rd->ptr = ptr; 179 if (fm == NULL) return 1;
235 ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); 180
236 } 181 fm->mem = memory;
237 182 fm->destr = destr;
183 *(cx_destructor_func *) ((char *) fm - sizeof(cx_destructor_func)) = cx_mempool_destr_foreign_mem;
184
185 return 0;
186 }
187
188 static cx_allocator_class cx_mempool_allocator_class = {
189 cx_mempool_malloc,
190 cx_mempool_realloc,
191 cx_mempool_calloc,
192 cx_mempool_free
193 };
194
195 CxMempool *cxMempoolCreate(
196 size_t capacity,
197 cx_destructor_func destr
198 ) {
199 size_t poolsize;
200 if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) {
201 return NULL;
202 }
203
204 struct cx_mempool_s *pool =
205 malloc(sizeof(struct cx_mempool_s));
206 if (pool == NULL) {
207 return NULL;
208 }
209
210 CxAllocator *provided_allocator = malloc(sizeof(CxAllocator));
211 if (provided_allocator == NULL) {
212 free(pool);
213 return NULL;
214 }
215 provided_allocator->cl = &cx_mempool_allocator_class;
216 provided_allocator->data = pool;
217
218 pool->allocator = provided_allocator;
219
220 pool->data = malloc(poolsize);
221 if (pool->data == NULL) {
222 free(provided_allocator);
223 free(pool);
224 return NULL;
225 }
226
227 pool->size = 0;
228 pool->capacity = capacity;
229 pool->auto_destr = destr;
230
231 return (CxMempool *) pool;
232 }

mercurial