1.1 --- a/src/mempool.c Mon Dec 30 09:54:10 2019 +0100 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,237 +0,0 @@ 1.4 -/* 1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 - * 1.7 - * Copyright 2017 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 "ucx/mempool.h" 1.33 - 1.34 -#include <stdlib.h> 1.35 -#include <string.h> 1.36 -#include <stdio.h> 1.37 -#ifdef __cplusplus 1.38 -#define __STDC_FORMAT_MACROS 1.39 -#endif 1.40 -#include <inttypes.h> 1.41 - 1.42 -/** Capsule for destructible memory chunks. */ 1.43 -typedef struct { 1.44 - /** The destructor for the memory chunk. */ 1.45 - ucx_destructor destructor; 1.46 - /** 1.47 - * First byte of the memory chunk. 1.48 - * Note, that the address <code>&c</code> is also the address 1.49 - * of the whole memory chunk. 1.50 - */ 1.51 - char c; 1.52 -} ucx_memchunk; 1.53 - 1.54 -/** Capsule for data and its destructor. */ 1.55 -typedef struct { 1.56 - /** The destructor for the data. */ 1.57 - ucx_destructor destructor; 1.58 - /** A pointer to the data. */ 1.59 - void *ptr; 1.60 -} ucx_regdestr; 1.61 - 1.62 -#ifdef __cplusplus 1.63 -extern "C" 1.64 -#endif 1.65 -void ucx_mempool_shared_destr(void* ptr) { 1.66 - ucx_regdestr *rd = (ucx_regdestr*)ptr; 1.67 - rd->destructor(rd->ptr); 1.68 -} 1.69 - 1.70 -UcxMempool *ucx_mempool_new(size_t n) { 1.71 - size_t poolsz; 1.72 - if(ucx_szmul(n, sizeof(void*), &poolsz)) { 1.73 - return NULL; 1.74 - } 1.75 - 1.76 - UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); 1.77 - if (!pool) { 1.78 - return NULL; 1.79 - } 1.80 - 1.81 - pool->data = (void**) malloc(poolsz); 1.82 - if (pool->data == NULL) { 1.83 - free(pool); 1.84 - return NULL; 1.85 - } 1.86 - 1.87 - pool->ndata = 0; 1.88 - pool->size = n; 1.89 - 1.90 - UcxAllocator *allocator = (UcxAllocator*)malloc(sizeof(UcxAllocator)); 1.91 - if(!allocator) { 1.92 - free(pool->data); 1.93 - free(pool); 1.94 - return NULL; 1.95 - } 1.96 - allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc; 1.97 - allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc; 1.98 - allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc; 1.99 - allocator->free = (ucx_allocator_free)ucx_mempool_free; 1.100 - allocator->pool = pool; 1.101 - pool->allocator = allocator; 1.102 - 1.103 - return pool; 1.104 -} 1.105 - 1.106 -int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { 1.107 - if (newcap < pool->ndata) { 1.108 - return 1; 1.109 - } 1.110 - 1.111 - size_t newcapsz; 1.112 - if(ucx_szmul(newcap, sizeof(void*), &newcapsz)) { 1.113 - return 1; 1.114 - } 1.115 - 1.116 - void **data = (void**) realloc(pool->data, newcapsz); 1.117 - if (data) { 1.118 - pool->data = data; 1.119 - pool->size = newcap; 1.120 - return 0; 1.121 - } else { 1.122 - return 1; 1.123 - } 1.124 -} 1.125 - 1.126 -void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { 1.127 - if(((size_t)-1) - sizeof(ucx_destructor) < n) { 1.128 - return NULL; 1.129 - } 1.130 - 1.131 - if (pool->ndata >= pool->size) { 1.132 - size_t newcap = pool->size*2; 1.133 - if (newcap < pool->size || ucx_mempool_chcap(pool, newcap)) { 1.134 - return NULL; 1.135 - } 1.136 - } 1.137 - 1.138 - void *p = malloc(sizeof(ucx_destructor) + n); 1.139 - ucx_memchunk *mem = (ucx_memchunk*)p; 1.140 - if (!mem) { 1.141 - return NULL; 1.142 - } 1.143 - 1.144 - mem->destructor = NULL; 1.145 - pool->data[pool->ndata] = mem; 1.146 - pool->ndata++; 1.147 - 1.148 - return &(mem->c); 1.149 -} 1.150 - 1.151 -void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { 1.152 - size_t msz; 1.153 - if(ucx_szmul(nelem, elsize, &msz)) { 1.154 - return NULL; 1.155 - } 1.156 - 1.157 - void *ptr = ucx_mempool_malloc(pool, msz); 1.158 - if (!ptr) { 1.159 - return NULL; 1.160 - } 1.161 - memset(ptr, 0, nelem * elsize); 1.162 - return ptr; 1.163 -} 1.164 - 1.165 -void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { 1.166 - if(((size_t)-1) - sizeof(ucx_destructor) < n) { 1.167 - return NULL; 1.168 - } 1.169 - 1.170 - char *mem = ((char*)ptr) - sizeof(ucx_destructor); 1.171 - char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); 1.172 - if (!newm) { 1.173 - return NULL; 1.174 - } 1.175 - if (mem != newm) { 1.176 - for(size_t i=0 ; i < pool->ndata ; i++) { 1.177 - if(pool->data[i] == mem) { 1.178 - pool->data[i] = newm; 1.179 - return newm + sizeof(ucx_destructor); 1.180 - } 1.181 - } 1.182 - fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", 1.183 - (intptr_t)ptr, (intptr_t)pool); 1.184 - abort(); 1.185 - } else { 1.186 - return newm + sizeof(ucx_destructor); 1.187 - } 1.188 -} 1.189 - 1.190 -void ucx_mempool_free(UcxMempool *pool, void *ptr) { 1.191 - ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor)); 1.192 - for(size_t i=0 ; i<pool->ndata ; i++) { 1.193 - if(chunk == pool->data[i]) { 1.194 - if(chunk->destructor != NULL) { 1.195 - chunk->destructor(&(chunk->c)); 1.196 - } 1.197 - free(chunk); 1.198 - size_t last_index = pool->ndata - 1; 1.199 - if(i != last_index) { 1.200 - pool->data[i] = pool->data[last_index]; 1.201 - pool->data[last_index] = NULL; 1.202 - } 1.203 - pool->ndata--; 1.204 - return; 1.205 - } 1.206 - } 1.207 - fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", 1.208 - (intptr_t)ptr, (intptr_t)pool); 1.209 - abort(); 1.210 -} 1.211 - 1.212 -void ucx_mempool_destroy(UcxMempool *pool) { 1.213 - ucx_memchunk *chunk; 1.214 - for(size_t i=0 ; i<pool->ndata ; i++) { 1.215 - chunk = (ucx_memchunk*) pool->data[i]; 1.216 - if(chunk) { 1.217 - if(chunk->destructor) { 1.218 - chunk->destructor(&(chunk->c)); 1.219 - } 1.220 - free(chunk); 1.221 - } 1.222 - } 1.223 - free(pool->data); 1.224 - free(pool->allocator); 1.225 - free(pool); 1.226 -} 1.227 - 1.228 -void ucx_mempool_set_destr(void *ptr, ucx_destructor func) { 1.229 - *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func; 1.230 -} 1.231 - 1.232 -void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) { 1.233 - ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc( 1.234 - pool, 1.235 - sizeof(ucx_regdestr)); 1.236 - rd->destructor = destr; 1.237 - rd->ptr = ptr; 1.238 - ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); 1.239 -} 1.240 -