1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/mempool.c Wed Jun 28 20:07:52 2023 +0200 1.3 @@ -0,0 +1,232 @@ 1.4 +/* 1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 + * 1.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions are met: 1.11 + * 1.12 + * 1. Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * 2. Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 + * POSSIBILITY OF SUCH DAMAGE. 1.30 + */ 1.31 + 1.32 +#include "cx/mempool.h" 1.33 +#include "cx/utils.h" 1.34 +#include <string.h> 1.35 + 1.36 +struct cx_mempool_memory_s { 1.37 + /** The destructor. */ 1.38 + cx_destructor_func destructor; 1.39 + /** The actual memory. */ 1.40 + char c[]; 1.41 +}; 1.42 + 1.43 +static void *cx_mempool_malloc( 1.44 + void *p, 1.45 + size_t n 1.46 +) { 1.47 + struct cx_mempool_s *pool = p; 1.48 + 1.49 + if (pool->size >= pool->capacity) { 1.50 + size_t newcap = pool->capacity - (pool->capacity % 16) + 16; 1.51 + struct cx_mempool_memory_s **newdata = realloc(pool->data, newcap*sizeof(struct cx_mempool_memory_s*)); 1.52 + if (newdata == NULL) { 1.53 + return NULL; 1.54 + } 1.55 + pool->data = newdata; 1.56 + pool->capacity = newcap; 1.57 + } 1.58 + 1.59 + struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n); 1.60 + if (mem == NULL) { 1.61 + return NULL; 1.62 + } 1.63 + 1.64 + mem->destructor = pool->auto_destr; 1.65 + pool->data[pool->size] = mem; 1.66 + pool->size++; 1.67 + 1.68 + return mem->c; 1.69 +} 1.70 + 1.71 +static void *cx_mempool_calloc( 1.72 + void *p, 1.73 + size_t nelem, 1.74 + size_t elsize 1.75 +) { 1.76 + size_t msz; 1.77 + if (cx_szmul(nelem, elsize, &msz)) { 1.78 + return NULL; 1.79 + } 1.80 + void *ptr = cx_mempool_malloc(p, msz); 1.81 + if (ptr == NULL) { 1.82 + return NULL; 1.83 + } 1.84 + memset(ptr, 0, nelem * elsize); 1.85 + return ptr; 1.86 +} 1.87 + 1.88 +static void *cx_mempool_realloc( 1.89 + void *p, 1.90 + void *ptr, 1.91 + size_t n 1.92 +) { 1.93 + struct cx_mempool_s *pool = p; 1.94 + 1.95 + struct cx_mempool_memory_s *mem, *newm; 1.96 + mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func)); 1.97 + newm = realloc(mem, n + sizeof(cx_destructor_func)); 1.98 + 1.99 + if (newm == NULL) { 1.100 + return NULL; 1.101 + } 1.102 + if (mem != newm) { 1.103 + cx_for_n(i, pool->size) { 1.104 + if (pool->data[i] == mem) { 1.105 + pool->data[i] = newm; 1.106 + return ((char*)newm) + sizeof(cx_destructor_func); 1.107 + } 1.108 + } 1.109 + abort(); 1.110 + } else { 1.111 + return ptr; 1.112 + } 1.113 +} 1.114 + 1.115 +static void cx_mempool_free( 1.116 + void *p, 1.117 + void *ptr 1.118 +) { 1.119 + struct cx_mempool_s *pool = p; 1.120 + 1.121 + struct cx_mempool_memory_s *mem = (struct cx_mempool_memory_s *) 1.122 + ((char *) ptr - sizeof(cx_destructor_func)); 1.123 + 1.124 + cx_for_n(i, pool->size) { 1.125 + if (mem == pool->data[i]) { 1.126 + if (mem->destructor) { 1.127 + mem->destructor(mem->c); 1.128 + } 1.129 + free(mem); 1.130 + size_t last_index = pool->size - 1; 1.131 + if (i != last_index) { 1.132 + pool->data[i] = pool->data[last_index]; 1.133 + pool->data[last_index] = NULL; 1.134 + } 1.135 + pool->size--; 1.136 + return; 1.137 + } 1.138 + } 1.139 + abort(); 1.140 +} 1.141 + 1.142 +void cxMempoolDestroy(CxMempool *pool) { 1.143 + struct cx_mempool_memory_s *mem; 1.144 + cx_for_n(i, pool->size) { 1.145 + mem = pool->data[i]; 1.146 + if (mem->destructor) { 1.147 + mem->destructor(mem->c); 1.148 + } 1.149 + free(mem); 1.150 + } 1.151 + free(pool->data); 1.152 + free((void*) pool->allocator); 1.153 + free(pool); 1.154 +} 1.155 + 1.156 +void cxMempoolSetDestructor( 1.157 + void *ptr, 1.158 + cx_destructor_func func 1.159 +) { 1.160 + *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func; 1.161 +} 1.162 + 1.163 +struct cx_mempool_foreign_mem_s { 1.164 + cx_destructor_func destr; 1.165 + void* mem; 1.166 +}; 1.167 + 1.168 +static void cx_mempool_destr_foreign_mem(void* ptr) { 1.169 + struct cx_mempool_foreign_mem_s *fm = ptr; 1.170 + fm->destr(fm->mem); 1.171 +} 1.172 + 1.173 +int cxMempoolRegister( 1.174 + CxMempool *pool, 1.175 + void *memory, 1.176 + cx_destructor_func destr 1.177 +) { 1.178 + struct cx_mempool_foreign_mem_s *fm = cx_mempool_malloc( 1.179 + pool, 1.180 + sizeof(struct cx_mempool_foreign_mem_s) 1.181 + ); 1.182 + if (fm == NULL) return 1; 1.183 + 1.184 + fm->mem = memory; 1.185 + fm->destr = destr; 1.186 + *(cx_destructor_func *) ((char *) fm - sizeof(cx_destructor_func)) = cx_mempool_destr_foreign_mem; 1.187 + 1.188 + return 0; 1.189 +} 1.190 + 1.191 +static cx_allocator_class cx_mempool_allocator_class = { 1.192 + cx_mempool_malloc, 1.193 + cx_mempool_realloc, 1.194 + cx_mempool_calloc, 1.195 + cx_mempool_free 1.196 +}; 1.197 + 1.198 +CxMempool *cxMempoolCreate( 1.199 + size_t capacity, 1.200 + cx_destructor_func destr 1.201 +) { 1.202 + size_t poolsize; 1.203 + if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) { 1.204 + return NULL; 1.205 + } 1.206 + 1.207 + struct cx_mempool_s *pool = 1.208 + malloc(sizeof(struct cx_mempool_s)); 1.209 + if (pool == NULL) { 1.210 + return NULL; 1.211 + } 1.212 + 1.213 + CxAllocator *provided_allocator = malloc(sizeof(CxAllocator)); 1.214 + if (provided_allocator == NULL) { 1.215 + free(pool); 1.216 + return NULL; 1.217 + } 1.218 + provided_allocator->cl = &cx_mempool_allocator_class; 1.219 + provided_allocator->data = pool; 1.220 + 1.221 + pool->allocator = provided_allocator; 1.222 + 1.223 + pool->data = malloc(poolsize); 1.224 + if (pool->data == NULL) { 1.225 + free(provided_allocator); 1.226 + free(pool); 1.227 + return NULL; 1.228 + } 1.229 + 1.230 + pool->size = 0; 1.231 + pool->capacity = capacity; 1.232 + pool->auto_destr = destr; 1.233 + 1.234 + return (CxMempool *) pool; 1.235 +}