Mon, 15 Jul 2013 16:59:52 +0200
added mempool allocator
olaf@13 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
olaf@13 | 3 | * |
universe@103 | 4 | * Copyright 2013 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 | |
olaf@13 | 29 | #include <stdlib.h> |
olaf@13 | 30 | #include <string.h> |
olaf@13 | 31 | #include <stdio.h> |
universe@95 | 32 | #ifdef __cplusplus |
universe@95 | 33 | #define __STDC_FORMAT_MACROS |
universe@95 | 34 | #endif |
universe@75 | 35 | #include <inttypes.h> |
olaf@13 | 36 | |
olaf@17 | 37 | #include "mempool.h" |
olaf@13 | 38 | |
olaf@13 | 39 | typedef struct { |
olaf@13 | 40 | ucx_destructor destructor; |
olaf@13 | 41 | char c; |
olaf@13 | 42 | } ucx_memchunk; |
olaf@13 | 43 | |
olaf@13 | 44 | typedef struct { |
olaf@13 | 45 | ucx_destructor destructor; |
olaf@13 | 46 | void *ptr; |
olaf@13 | 47 | } ucx_regdestr; |
olaf@13 | 48 | |
olaf@13 | 49 | void ucx_mempool_shared_destr(void* ptr) { |
olaf@13 | 50 | ucx_regdestr *rd = (ucx_regdestr*)ptr; |
olaf@13 | 51 | rd->destructor(rd->ptr); |
olaf@13 | 52 | } |
olaf@13 | 53 | |
olaf@13 | 54 | UcxMempool *ucx_mempool_new(size_t n) { |
olaf@14 | 55 | UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); |
universe@15 | 56 | if (pool == NULL) return NULL; |
universe@15 | 57 | |
universe@69 | 58 | pool->data = (void**) malloc(n * sizeof(void*)); |
universe@15 | 59 | if (pool->data == NULL) { |
universe@15 | 60 | free(pool); |
universe@15 | 61 | return NULL; |
universe@15 | 62 | } |
universe@15 | 63 | |
olaf@13 | 64 | pool->ndata = 0; |
olaf@13 | 65 | pool->size = n; |
olaf@13 | 66 | return pool; |
olaf@13 | 67 | } |
olaf@13 | 68 | |
universe@15 | 69 | int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { |
universe@69 | 70 | void **data = (void**) realloc(pool->data, newcap*sizeof(void*)); |
universe@15 | 71 | if (data == NULL) { |
olaf@57 | 72 | return 1; |
universe@15 | 73 | } else { |
universe@15 | 74 | pool->data = data; |
universe@15 | 75 | pool->size = newcap; |
universe@15 | 76 | return EXIT_SUCCESS; |
universe@15 | 77 | } |
olaf@13 | 78 | } |
olaf@13 | 79 | |
olaf@13 | 80 | void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { |
olaf@13 | 81 | ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); |
universe@15 | 82 | if (mem == NULL) return NULL; |
olaf@13 | 83 | |
olaf@13 | 84 | if (pool->ndata >= pool->size) { |
olaf@13 | 85 | ucx_mempool_chcap(pool, pool->size + 16); |
universe@15 | 86 | } |
olaf@13 | 87 | |
olaf@13 | 88 | mem->destructor = NULL; |
olaf@13 | 89 | pool->data[pool->ndata] = mem; |
olaf@13 | 90 | pool->ndata++; |
olaf@13 | 91 | |
olaf@13 | 92 | return &mem->c; |
olaf@13 | 93 | } |
olaf@13 | 94 | |
olaf@13 | 95 | void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { |
olaf@13 | 96 | void *ptr = ucx_mempool_malloc(pool, nelem*elsize); |
olaf@13 | 97 | if(ptr == NULL) { |
olaf@13 | 98 | return NULL; |
olaf@13 | 99 | } |
olaf@13 | 100 | memset(ptr, 0, nelem * elsize); |
olaf@13 | 101 | return ptr; |
olaf@13 | 102 | } |
olaf@13 | 103 | |
olaf@13 | 104 | void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { |
universe@28 | 105 | char *mem = ((char*)ptr) - sizeof(ucx_destructor); |
universe@15 | 106 | char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); |
olaf@14 | 107 | if (newm == NULL) return NULL; |
universe@15 | 108 | if (mem != newm) { |
universe@95 | 109 | for(size_t i=0 ; i < pool->ndata ; i++) { |
olaf@14 | 110 | if(pool->data[i] == mem) { |
olaf@14 | 111 | pool->data[i] = newm; |
universe@28 | 112 | return newm + sizeof(ucx_destructor); |
olaf@14 | 113 | } |
olaf@13 | 114 | } |
olaf@113 | 115 | fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
universe@75 | 116 | (intptr_t)ptr, (intptr_t)pool); |
universe@15 | 117 | exit(1); |
universe@16 | 118 | } else { |
universe@28 | 119 | return newm + sizeof(ucx_destructor); |
olaf@13 | 120 | } |
olaf@13 | 121 | } |
olaf@13 | 122 | |
olaf@113 | 123 | void ucx_mempool_free(UcxMempool *pool, void *ptr) { |
olaf@113 | 124 | ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor)); |
olaf@113 | 125 | for(size_t i=0 ; i<pool->ndata ; i++) { |
olaf@113 | 126 | if(chunk == pool->data[i]) { |
olaf@113 | 127 | if(chunk->destructor != NULL) { |
olaf@113 | 128 | chunk->destructor(&chunk->c); |
olaf@113 | 129 | } |
olaf@113 | 130 | free(chunk); |
olaf@113 | 131 | size_t last_index = pool->ndata - 1; |
olaf@113 | 132 | if(i != last_index) { |
olaf@113 | 133 | pool->data[i] = pool->data[last_index]; |
olaf@113 | 134 | } |
olaf@113 | 135 | pool->ndata--; |
olaf@113 | 136 | return; |
olaf@113 | 137 | } |
olaf@113 | 138 | } |
olaf@113 | 139 | fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
olaf@113 | 140 | (intptr_t)ptr, (intptr_t)pool); |
olaf@113 | 141 | exit(1); |
olaf@113 | 142 | } |
olaf@113 | 143 | |
olaf@113 | 144 | void ucx_mempool_destroy(UcxMempool *pool) { |
olaf@13 | 145 | ucx_memchunk *chunk; |
universe@95 | 146 | for(size_t i=0 ; i<pool->ndata ; i++) { |
olaf@13 | 147 | chunk = (ucx_memchunk*) pool->data[i]; |
olaf@113 | 148 | if(chunk) { |
olaf@113 | 149 | if(chunk->destructor != NULL) { |
olaf@113 | 150 | chunk->destructor(&chunk->c); |
olaf@113 | 151 | } |
olaf@113 | 152 | free(chunk); |
olaf@13 | 153 | } |
olaf@13 | 154 | } |
olaf@13 | 155 | free(pool->data); |
olaf@13 | 156 | free(pool); |
olaf@13 | 157 | } |
olaf@13 | 158 | |
olaf@13 | 159 | void ucx_mempool_set_destr(void *ptr, ucx_destructor func) { |
olaf@13 | 160 | *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func; |
olaf@13 | 161 | } |
olaf@13 | 162 | |
olaf@13 | 163 | void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) { |
olaf@13 | 164 | ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc( |
olaf@13 | 165 | pool, |
olaf@13 | 166 | sizeof(ucx_regdestr)); |
olaf@13 | 167 | rd->destructor = destr; |
olaf@14 | 168 | rd->ptr = ptr; |
olaf@13 | 169 | ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); |
olaf@13 | 170 | } |
olaf@113 | 171 | |
olaf@113 | 172 | UcxAllocator* ucx_mempool_allocator(UcxMempool *pool) { |
olaf@113 | 173 | UcxAllocator *allocator = (UcxAllocator*)ucx_mempool_malloc( |
olaf@113 | 174 | pool, sizeof(UcxAllocator)); |
olaf@113 | 175 | if(!allocator) { |
olaf@113 | 176 | return NULL; |
olaf@113 | 177 | } |
olaf@113 | 178 | allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc; |
olaf@113 | 179 | allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc; |
olaf@113 | 180 | allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc; |
olaf@113 | 181 | allocator->free = (ucx_allocator_free)ucx_mempool_free; |
olaf@113 | 182 | allocator->pool = pool; |
olaf@113 | 183 | return allocator; |
olaf@113 | 184 | } |