|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
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 |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "ucx/mempool.h" |
|
30 |
|
31 #include <stdlib.h> |
|
32 #include <string.h> |
|
33 #include <stdio.h> |
|
34 #ifdef __cplusplus |
|
35 #define __STDC_FORMAT_MACROS |
|
36 #endif |
|
37 #include <inttypes.h> |
|
38 |
|
39 /** Capsule for destructible memory chunks. */ |
|
40 typedef struct { |
|
41 /** The destructor for the memory chunk. */ |
|
42 ucx_destructor destructor; |
|
43 /** |
|
44 * First byte of the memory chunk. |
|
45 * Note, that the address <code>&c</code> is also the address |
|
46 * of the whole memory chunk. |
|
47 */ |
|
48 char c; |
|
49 } ucx_memchunk; |
|
50 |
|
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; |
|
132 } |
|
133 } |
|
134 |
|
135 void *p = malloc(sizeof(ucx_destructor) + n); |
|
136 ucx_memchunk *mem = (ucx_memchunk*)p; |
|
137 if (!mem) { |
|
138 return NULL; |
|
139 } |
|
140 |
|
141 mem->destructor = NULL; |
|
142 pool->data[pool->ndata] = mem; |
|
143 pool->ndata++; |
|
144 |
|
145 return &(mem->c); |
|
146 } |
|
147 |
|
148 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { |
|
149 size_t msz; |
|
150 if(ucx_szmul(nelem, elsize, &msz)) { |
|
151 return NULL; |
|
152 } |
|
153 |
|
154 void *ptr = ucx_mempool_malloc(pool, msz); |
|
155 if (!ptr) { |
|
156 return NULL; |
|
157 } |
|
158 memset(ptr, 0, nelem * elsize); |
|
159 return ptr; |
|
160 } |
|
161 |
|
162 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { |
|
163 if(((size_t)-1) - sizeof(ucx_destructor) < n) { |
|
164 return NULL; |
|
165 } |
|
166 |
|
167 char *mem = ((char*)ptr) - sizeof(ucx_destructor); |
|
168 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); |
|
169 if (!newm) { |
|
170 return NULL; |
|
171 } |
|
172 if (mem != newm) { |
|
173 for(size_t i=0 ; i < pool->ndata ; i++) { |
|
174 if(pool->data[i] == mem) { |
|
175 pool->data[i] = newm; |
|
176 return newm + sizeof(ucx_destructor); |
|
177 } |
|
178 } |
|
179 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
|
180 (intptr_t)ptr, (intptr_t)pool); |
|
181 abort(); |
|
182 } else { |
|
183 return newm + sizeof(ucx_destructor); |
|
184 } |
|
185 } |
|
186 |
|
187 void ucx_mempool_free(UcxMempool *pool, void *ptr) { |
|
188 ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor)); |
|
189 for(size_t i=0 ; i<pool->ndata ; i++) { |
|
190 if(chunk == pool->data[i]) { |
|
191 if(chunk->destructor != NULL) { |
|
192 chunk->destructor(&(chunk->c)); |
|
193 } |
|
194 free(chunk); |
|
195 size_t last_index = pool->ndata - 1; |
|
196 if(i != last_index) { |
|
197 pool->data[i] = pool->data[last_index]; |
|
198 pool->data[last_index] = NULL; |
|
199 } |
|
200 pool->ndata--; |
|
201 return; |
|
202 } |
|
203 } |
|
204 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
|
205 (intptr_t)ptr, (intptr_t)pool); |
|
206 abort(); |
|
207 } |
|
208 |
|
209 void ucx_mempool_destroy(UcxMempool *pool) { |
|
210 ucx_memchunk *chunk; |
|
211 for(size_t i=0 ; i<pool->ndata ; i++) { |
|
212 chunk = (ucx_memchunk*) pool->data[i]; |
|
213 if(chunk) { |
|
214 if(chunk->destructor) { |
|
215 chunk->destructor(&(chunk->c)); |
|
216 } |
|
217 free(chunk); |
|
218 } |
|
219 } |
|
220 free(pool->data); |
|
221 free(pool->allocator); |
|
222 free(pool); |
|
223 } |
|
224 |
|
225 void ucx_mempool_set_destr(void *ptr, ucx_destructor func) { |
|
226 *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func; |
|
227 } |
|
228 |
|
229 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) { |
|
230 ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc( |
|
231 pool, |
|
232 sizeof(ucx_regdestr)); |
|
233 rd->destructor = destr; |
|
234 rd->ptr = ptr; |
|
235 ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); |
|
236 } |
|
237 |