Fri, 20 Dec 2019 14:29:41 +0100
adds proper cmake build targets
olaf@13 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
olaf@13 | 3 | * |
universe@259 | 4 | * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
olaf@13 | 27 | */ |
olaf@13 | 28 | |
universe@251 | 29 | #include "ucx/mempool.h" |
universe@251 | 30 | |
olaf@13 | 31 | #include <stdlib.h> |
olaf@13 | 32 | #include <string.h> |
olaf@13 | 33 | #include <stdio.h> |
universe@95 | 34 | #ifdef __cplusplus |
universe@95 | 35 | #define __STDC_FORMAT_MACROS |
universe@95 | 36 | #endif |
universe@75 | 37 | #include <inttypes.h> |
olaf@13 | 38 | |
universe@138 | 39 | /** Capsule for destructible memory chunks. */ |
olaf@13 | 40 | typedef struct { |
universe@138 | 41 | /** The destructor for the memory chunk. */ |
olaf@13 | 42 | ucx_destructor destructor; |
universe@138 | 43 | /** |
universe@138 | 44 | * First byte of the memory chunk. |
universe@138 | 45 | * Note, that the address <code>&c</code> is also the address |
universe@138 | 46 | * of the whole memory chunk. |
universe@138 | 47 | */ |
olaf@13 | 48 | char c; |
olaf@13 | 49 | } ucx_memchunk; |
olaf@13 | 50 | |
universe@138 | 51 | /** Capsule for data and its destructor. */ |
olaf@13 | 52 | typedef struct { |
universe@138 | 53 | /** The destructor for the data. */ |
olaf@13 | 54 | ucx_destructor destructor; |
universe@138 | 55 | /** A pointer to the data. */ |
olaf@13 | 56 | void *ptr; |
olaf@13 | 57 | } ucx_regdestr; |
olaf@13 | 58 | |
universe@253 | 59 | #ifdef __cplusplus |
universe@253 | 60 | extern "C" |
universe@253 | 61 | #endif |
universe@253 | 62 | void ucx_mempool_shared_destr(void* ptr) { |
olaf@13 | 63 | ucx_regdestr *rd = (ucx_regdestr*)ptr; |
olaf@13 | 64 | rd->destructor(rd->ptr); |
olaf@13 | 65 | } |
olaf@13 | 66 | |
olaf@13 | 67 | UcxMempool *ucx_mempool_new(size_t n) { |
olaf@270 | 68 | size_t poolsz; |
olaf@270 | 69 | if(ucx_szmul(n, sizeof(void*), &poolsz)) { |
olaf@270 | 70 | return NULL; |
olaf@270 | 71 | } |
olaf@270 | 72 | |
olaf@14 | 73 | UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); |
universe@135 | 74 | if (!pool) { |
universe@135 | 75 | return NULL; |
universe@135 | 76 | } |
universe@15 | 77 | |
olaf@270 | 78 | pool->data = (void**) malloc(poolsz); |
universe@15 | 79 | if (pool->data == NULL) { |
universe@15 | 80 | free(pool); |
universe@15 | 81 | return NULL; |
universe@15 | 82 | } |
universe@15 | 83 | |
olaf@13 | 84 | pool->ndata = 0; |
olaf@13 | 85 | pool->size = n; |
olaf@158 | 86 | |
olaf@158 | 87 | UcxAllocator *allocator = (UcxAllocator*)malloc(sizeof(UcxAllocator)); |
olaf@158 | 88 | if(!allocator) { |
olaf@158 | 89 | free(pool->data); |
olaf@158 | 90 | free(pool); |
olaf@158 | 91 | return NULL; |
olaf@158 | 92 | } |
olaf@158 | 93 | allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc; |
olaf@158 | 94 | allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc; |
olaf@158 | 95 | allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc; |
olaf@158 | 96 | allocator->free = (ucx_allocator_free)ucx_mempool_free; |
olaf@158 | 97 | allocator->pool = pool; |
olaf@158 | 98 | pool->allocator = allocator; |
olaf@158 | 99 | |
olaf@13 | 100 | return pool; |
olaf@13 | 101 | } |
olaf@13 | 102 | |
universe@15 | 103 | int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { |
universe@241 | 104 | if (newcap < pool->ndata) { |
universe@241 | 105 | return 1; |
universe@241 | 106 | } |
universe@241 | 107 | |
olaf@270 | 108 | size_t newcapsz; |
olaf@270 | 109 | if(ucx_szmul(newcap, sizeof(void*), &newcapsz)) { |
olaf@270 | 110 | return 1; |
olaf@270 | 111 | } |
olaf@270 | 112 | |
olaf@270 | 113 | void **data = (void**) realloc(pool->data, newcapsz); |
universe@135 | 114 | if (data) { |
universe@15 | 115 | pool->data = data; |
universe@15 | 116 | pool->size = newcap; |
universe@241 | 117 | return 0; |
universe@135 | 118 | } else { |
universe@241 | 119 | return 1; |
universe@15 | 120 | } |
olaf@13 | 121 | } |
olaf@13 | 122 | |
olaf@13 | 123 | void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { |
olaf@270 | 124 | if(((size_t)-1) - sizeof(ucx_destructor) < n) { |
olaf@270 | 125 | return NULL; |
olaf@270 | 126 | } |
olaf@270 | 127 | |
universe@141 | 128 | if (pool->ndata >= pool->size) { |
universe@242 | 129 | size_t newcap = pool->size*2; |
universe@242 | 130 | if (newcap < pool->size || ucx_mempool_chcap(pool, newcap)) { |
universe@141 | 131 | return NULL; |
universe@141 | 132 | } |
universe@141 | 133 | } |
universe@141 | 134 | |
universe@248 | 135 | void *p = malloc(sizeof(ucx_destructor) + n); |
universe@248 | 136 | ucx_memchunk *mem = (ucx_memchunk*)p; |
universe@135 | 137 | if (!mem) { |
universe@135 | 138 | return NULL; |
universe@135 | 139 | } |
olaf@13 | 140 | |
olaf@13 | 141 | mem->destructor = NULL; |
olaf@13 | 142 | pool->data[pool->ndata] = mem; |
olaf@13 | 143 | pool->ndata++; |
olaf@13 | 144 | |
universe@135 | 145 | return &(mem->c); |
olaf@13 | 146 | } |
olaf@13 | 147 | |
olaf@13 | 148 | void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { |
olaf@270 | 149 | size_t msz; |
olaf@270 | 150 | if(ucx_szmul(nelem, elsize, &msz)) { |
olaf@270 | 151 | return NULL; |
olaf@270 | 152 | } |
olaf@270 | 153 | |
olaf@270 | 154 | void *ptr = ucx_mempool_malloc(pool, msz); |
universe@135 | 155 | if (!ptr) { |
olaf@13 | 156 | return NULL; |
olaf@13 | 157 | } |
olaf@13 | 158 | memset(ptr, 0, nelem * elsize); |
olaf@13 | 159 | return ptr; |
olaf@13 | 160 | } |
olaf@13 | 161 | |
olaf@13 | 162 | void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { |
olaf@270 | 163 | if(((size_t)-1) - sizeof(ucx_destructor) < n) { |
olaf@270 | 164 | return NULL; |
olaf@270 | 165 | } |
olaf@270 | 166 | |
universe@28 | 167 | char *mem = ((char*)ptr) - sizeof(ucx_destructor); |
universe@15 | 168 | char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); |
universe@135 | 169 | if (!newm) { |
universe@135 | 170 | return NULL; |
universe@135 | 171 | } |
universe@15 | 172 | if (mem != newm) { |
universe@95 | 173 | for(size_t i=0 ; i < pool->ndata ; i++) { |
olaf@14 | 174 | if(pool->data[i] == mem) { |
olaf@14 | 175 | pool->data[i] = newm; |
universe@28 | 176 | return newm + sizeof(ucx_destructor); |
olaf@14 | 177 | } |
olaf@13 | 178 | } |
universe@116 | 179 | fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
universe@75 | 180 | (intptr_t)ptr, (intptr_t)pool); |
universe@241 | 181 | abort(); |
universe@16 | 182 | } else { |
universe@28 | 183 | return newm + sizeof(ucx_destructor); |
olaf@13 | 184 | } |
olaf@13 | 185 | } |
olaf@13 | 186 | |
olaf@113 | 187 | void ucx_mempool_free(UcxMempool *pool, void *ptr) { |
olaf@113 | 188 | ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor)); |
olaf@113 | 189 | for(size_t i=0 ; i<pool->ndata ; i++) { |
olaf@113 | 190 | if(chunk == pool->data[i]) { |
olaf@113 | 191 | if(chunk->destructor != NULL) { |
universe@141 | 192 | chunk->destructor(&(chunk->c)); |
olaf@113 | 193 | } |
olaf@113 | 194 | free(chunk); |
olaf@113 | 195 | size_t last_index = pool->ndata - 1; |
olaf@113 | 196 | if(i != last_index) { |
olaf@113 | 197 | pool->data[i] = pool->data[last_index]; |
universe@141 | 198 | pool->data[last_index] = NULL; |
olaf@113 | 199 | } |
olaf@113 | 200 | pool->ndata--; |
olaf@113 | 201 | return; |
olaf@113 | 202 | } |
olaf@113 | 203 | } |
universe@116 | 204 | fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
olaf@113 | 205 | (intptr_t)ptr, (intptr_t)pool); |
universe@240 | 206 | abort(); |
olaf@113 | 207 | } |
olaf@113 | 208 | |
olaf@113 | 209 | void ucx_mempool_destroy(UcxMempool *pool) { |
olaf@13 | 210 | ucx_memchunk *chunk; |
universe@95 | 211 | for(size_t i=0 ; i<pool->ndata ; i++) { |
olaf@13 | 212 | chunk = (ucx_memchunk*) pool->data[i]; |
olaf@113 | 213 | if(chunk) { |
universe@135 | 214 | if(chunk->destructor) { |
universe@135 | 215 | chunk->destructor(&(chunk->c)); |
olaf@113 | 216 | } |
olaf@113 | 217 | free(chunk); |
olaf@13 | 218 | } |
olaf@13 | 219 | } |
olaf@13 | 220 | free(pool->data); |
olaf@158 | 221 | free(pool->allocator); |
olaf@13 | 222 | free(pool); |
olaf@13 | 223 | } |
olaf@13 | 224 | |
olaf@13 | 225 | void ucx_mempool_set_destr(void *ptr, ucx_destructor func) { |
olaf@13 | 226 | *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func; |
olaf@13 | 227 | } |
olaf@13 | 228 | |
olaf@13 | 229 | void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) { |
olaf@13 | 230 | ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc( |
olaf@13 | 231 | pool, |
olaf@13 | 232 | sizeof(ucx_regdestr)); |
olaf@13 | 233 | rd->destructor = destr; |
olaf@14 | 234 | rd->ptr = ptr; |
olaf@13 | 235 | ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); |
olaf@13 | 236 | } |
olaf@113 | 237 |