2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
29 #include "cx/mempool.h"
33 struct cx_mempool_memory_s {
34 /** The destructor. */
35 cx_destructor_func destructor;
36 /** The actual memory. */
40 static void *cx_mempool_malloc(
44 struct cx_mempool_s *pool = p;
46 if (pool->size >= pool->capacity) {
47 size_t newcap = pool->capacity - (pool->capacity % 16) + 16;
48 struct cx_mempool_memory_s **newdata = realloc(pool->data, newcap*sizeof(struct cx_mempool_memory_s*));
49 if (newdata == NULL) {
53 pool->capacity = newcap;
56 struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n);
61 mem->destructor = pool->auto_destr;
62 pool->data[pool->size] = mem;
68 static void *cx_mempool_calloc(
74 if (cx_szmul(nelem, elsize, &msz)) {
77 void *ptr = cx_mempool_malloc(p, msz);
81 memset(ptr, 0, nelem * elsize);
85 static void *cx_mempool_realloc(
90 struct cx_mempool_s *pool = p;
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));
100 cx_for_n(i, pool->size) {
101 if (pool->data[i] == mem) {
102 pool->data[i] = newm;
103 return ((char*)newm) + sizeof(cx_destructor_func);
112 static void cx_mempool_free(
116 struct cx_mempool_s *pool = p;
118 struct cx_mempool_memory_s *mem = (struct cx_mempool_memory_s *)
119 ((char *) ptr - sizeof(cx_destructor_func));
121 cx_for_n(i, pool->size) {
122 if (mem == pool->data[i]) {
123 if (mem->destructor) {
124 mem->destructor(mem->c);
127 size_t last_index = pool->size - 1;
128 if (i != last_index) {
129 pool->data[i] = pool->data[last_index];
130 pool->data[last_index] = NULL;
139 void cxMempoolDestroy(CxMempool *pool) {
140 struct cx_mempool_memory_s *mem;
141 cx_for_n(i, pool->size) {
143 if (mem->destructor) {
144 mem->destructor(mem->c);
149 free((void*) pool->allocator);
153 void cxMempoolSetDestructor(
155 cx_destructor_func func
157 *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func;
160 struct cx_mempool_foreign_mem_s {
161 cx_destructor_func destr;
165 static void cx_mempool_destr_foreign_mem(void* ptr) {
166 struct cx_mempool_foreign_mem_s *fm = ptr;
170 int cxMempoolRegister(
173 cx_destructor_func destr
175 struct cx_mempool_foreign_mem_s *fm = cx_mempool_malloc(
177 sizeof(struct cx_mempool_foreign_mem_s)
179 if (fm == NULL) return 1;
183 *(cx_destructor_func *) ((char *) fm - sizeof(cx_destructor_func)) = cx_mempool_destr_foreign_mem;
188 static cx_allocator_class cx_mempool_allocator_class = {
195 CxMempool *cxMempoolCreate(
197 cx_destructor_func destr
200 if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) {
204 struct cx_mempool_s *pool =
205 malloc(sizeof(struct cx_mempool_s));
210 CxAllocator *provided_allocator = malloc(sizeof(CxAllocator));
211 if (provided_allocator == NULL) {
215 provided_allocator->cl = &cx_mempool_allocator_class;
216 provided_allocator->data = pool;
218 pool->allocator = provided_allocator;
220 pool->data = malloc(poolsize);
221 if (pool->data == NULL) {
222 free(provided_allocator);
228 pool->capacity = capacity;
229 pool->auto_destr = destr;
231 return (CxMempool *) pool;