| 103 ) { |
103 ) { |
| 104 struct cx_mempool_s *pool = p; |
104 struct cx_mempool_s *pool = p; |
| 105 |
105 |
| 106 struct cx_mempool_memory_s *mem, *newm; |
106 struct cx_mempool_memory_s *mem, *newm; |
| 107 mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func)); |
107 mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func)); |
| 108 newm = cxRealloc(cxDefaultAllocator, mem, n + sizeof(cx_destructor_func)); |
108 newm = cxReallocDefault(mem, n + sizeof(cx_destructor_func)); |
| 109 |
109 |
| 110 if (newm == NULL) return NULL; |
110 if (newm == NULL) return NULL; |
| 111 if (mem != newm) { |
111 if (mem != newm) { |
| 112 for (size_t i = 0; i < pool->size; i++) { |
112 for (size_t i = 0; i < pool->size; i++) { |
| 113 if (pool->data[i] == mem) { |
113 if (pool->data[i] == mem) { |
| 134 for (size_t i = 0; i < pool->size; i++) { |
134 for (size_t i = 0; i < pool->size; i++) { |
| 135 if (mem == pool->data[i]) { |
135 if (mem == pool->data[i]) { |
| 136 if (mem->destructor) { |
136 if (mem->destructor) { |
| 137 mem->destructor(mem->c); |
137 mem->destructor(mem->c); |
| 138 } |
138 } |
| 139 cxFree(cxDefaultAllocator, mem); |
139 cxFreeDefault(mem); |
| 140 size_t last_index = pool->size - 1; |
140 size_t last_index = pool->size - 1; |
| 141 if (i != last_index) { |
141 if (i != last_index) { |
| 142 pool->data[i] = pool->data[last_index]; |
142 pool->data[i] = pool->data[last_index]; |
| 143 pool->data[last_index] = NULL; |
143 pool->data[last_index] = NULL; |
| 144 } |
144 } |
| 155 for (size_t i = 0; i < pool->size; i++) { |
155 for (size_t i = 0; i < pool->size; i++) { |
| 156 mem = pool->data[i]; |
156 mem = pool->data[i]; |
| 157 if (mem->destructor) { |
157 if (mem->destructor) { |
| 158 mem->destructor(mem->c); |
158 mem->destructor(mem->c); |
| 159 } |
159 } |
| 160 cxFree(cxDefaultAllocator, mem); |
160 cxFreeDefault(mem); |
| 161 } |
161 } |
| 162 cxFree(cxDefaultAllocator, pool->data); |
162 cxFreeDefault(pool->data); |
| 163 cxFree(cxDefaultAllocator, (void*) pool->allocator); |
163 cxFreeDefault((void*) pool->allocator); |
| 164 cxFree(cxDefaultAllocator, pool); |
164 cxFreeDefault(pool); |
| 165 } |
165 } |
| 166 |
166 |
| 167 void cxMempoolSetDestructor( |
167 void cxMempoolSetDestructor( |
| 168 void *ptr, |
168 void *ptr, |
| 169 cx_destructor_func func |
169 cx_destructor_func func |
| 219 errno = EOVERFLOW; |
219 errno = EOVERFLOW; |
| 220 return NULL; |
220 return NULL; |
| 221 } |
221 } |
| 222 |
222 |
| 223 struct cx_mempool_s *pool = |
223 struct cx_mempool_s *pool = |
| 224 cxMalloc(cxDefaultAllocator, sizeof(struct cx_mempool_s)); |
224 cxMallocDefault(sizeof(struct cx_mempool_s)); |
| 225 if (pool == NULL) return NULL; |
225 if (pool == NULL) return NULL; |
| 226 |
226 |
| 227 CxAllocator *provided_allocator = cxMalloc(cxDefaultAllocator, sizeof(CxAllocator)); |
227 CxAllocator *provided_allocator = cxMallocDefault(sizeof(CxAllocator)); |
| 228 if (provided_allocator == NULL) { // LCOV_EXCL_START |
228 if (provided_allocator == NULL) { // LCOV_EXCL_START |
| 229 cxFree(cxDefaultAllocator, pool); |
229 cxFreeDefault(pool); |
| 230 return NULL; |
230 return NULL; |
| 231 } // LCOV_EXCL_STOP |
231 } // LCOV_EXCL_STOP |
| 232 provided_allocator->cl = &cx_mempool_allocator_class; |
232 provided_allocator->cl = &cx_mempool_allocator_class; |
| 233 provided_allocator->data = pool; |
233 provided_allocator->data = pool; |
| 234 |
234 |
| 235 pool->allocator = provided_allocator; |
235 pool->allocator = provided_allocator; |
| 236 |
236 |
| 237 pool->data = cxMalloc(cxDefaultAllocator, poolsize); |
237 pool->data = cxMallocDefault(poolsize); |
| 238 if (pool->data == NULL) { // LCOV_EXCL_START |
238 if (pool->data == NULL) { // LCOV_EXCL_START |
| 239 cxFree(cxDefaultAllocator, provided_allocator); |
239 cxFreeDefault(provided_allocator); |
| 240 cxFree(cxDefaultAllocator, pool); |
240 cxFreeDefault(pool); |
| 241 return NULL; |
241 return NULL; |
| 242 } // LCOV_EXCL_STOP |
242 } // LCOV_EXCL_STOP |
| 243 |
243 |
| 244 pool->size = 0; |
244 pool->size = 0; |
| 245 pool->capacity = capacity; |
245 pool->capacity = capacity; |
| 263 if (cx_mempool_ensure_capacity(dest, dest->size + source->size + 1)) { |
263 if (cx_mempool_ensure_capacity(dest, dest->size + source->size + 1)) { |
| 264 return 1; // LCOV_EXCL_LINE |
264 return 1; // LCOV_EXCL_LINE |
| 265 } |
265 } |
| 266 |
266 |
| 267 // allocate a replacement allocator for the source pool |
267 // allocate a replacement allocator for the source pool |
| 268 CxAllocator *new_source_allocator = cxMalloc(cxDefaultAllocator, sizeof(CxAllocator)); |
268 CxAllocator *new_source_allocator = cxMallocDefault(sizeof(CxAllocator)); |
| 269 if (new_source_allocator == NULL) { // LCOV_EXCL_START |
269 if (new_source_allocator == NULL) { // LCOV_EXCL_START |
| 270 return 1; |
270 return 1; |
| 271 } // LCOV_EXCL_STOP |
271 } // LCOV_EXCL_STOP |
| 272 new_source_allocator->cl = &cx_mempool_allocator_class; |
272 new_source_allocator->cl = &cx_mempool_allocator_class; |
| 273 new_source_allocator->data = source; |
273 new_source_allocator->data = source; |