Sat, 28 Oct 2017 15:59:16 +0200
rename LICENSE to COPYING to be distributed by autoconf
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
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.
15 *
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.
27 */
29 #include "ucx/mempool.h"
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #ifdef __cplusplus
35 #define __STDC_FORMAT_MACROS
36 #endif
37 #include <inttypes.h>
39 /** Capsule for destructible memory chunks. */
40 typedef struct {
41 /** The destructor for the memory chunk. */
42 ucx_destructor destructor;
43 /**
44 * First byte of the memory chunk.
45 * Note, that the address <code>&c</code> is also the address
46 * of the whole memory chunk.
47 */
48 char c;
49 } ucx_memchunk;
51 /** Capsule for data and its destructor. */
52 typedef struct {
53 /** The destructor for the data. */
54 ucx_destructor destructor;
55 /** A pointer to the data. */
56 void *ptr;
57 } ucx_regdestr;
59 #ifdef __cplusplus
60 extern "C"
61 #endif
62 void ucx_mempool_shared_destr(void* ptr) {
63 ucx_regdestr *rd = (ucx_regdestr*)ptr;
64 rd->destructor(rd->ptr);
65 }
67 UcxMempool *ucx_mempool_new(size_t n) {
68 UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
69 if (!pool) {
70 return NULL;
71 }
73 pool->data = (void**) malloc(n * sizeof(void*));
74 if (pool->data == NULL) {
75 free(pool);
76 return NULL;
77 }
79 pool->ndata = 0;
80 pool->size = n;
82 UcxAllocator *allocator = (UcxAllocator*)malloc(sizeof(UcxAllocator));
83 if(!allocator) {
84 free(pool->data);
85 free(pool);
86 return NULL;
87 }
88 allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc;
89 allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc;
90 allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc;
91 allocator->free = (ucx_allocator_free)ucx_mempool_free;
92 allocator->pool = pool;
93 pool->allocator = allocator;
95 return pool;
96 }
98 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
99 if (newcap < pool->ndata) {
100 return 1;
101 }
103 void **data = (void**) realloc(pool->data, newcap*sizeof(void*));
104 if (data) {
105 pool->data = data;
106 pool->size = newcap;
107 return 0;
108 } else {
109 return 1;
110 }
111 }
113 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
114 if (pool->ndata >= pool->size) {
115 size_t newcap = pool->size*2;
116 if (newcap < pool->size || ucx_mempool_chcap(pool, newcap)) {
117 return NULL;
118 }
119 }
121 void *p = malloc(sizeof(ucx_destructor) + n);
122 ucx_memchunk *mem = (ucx_memchunk*)p;
123 if (!mem) {
124 return NULL;
125 }
127 mem->destructor = NULL;
128 pool->data[pool->ndata] = mem;
129 pool->ndata++;
131 return &(mem->c);
132 }
134 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
135 void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
136 if (!ptr) {
137 return NULL;
138 }
139 memset(ptr, 0, nelem * elsize);
140 return ptr;
141 }
143 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
144 char *mem = ((char*)ptr) - sizeof(ucx_destructor);
145 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
146 if (!newm) {
147 return NULL;
148 }
149 if (mem != newm) {
150 for(size_t i=0 ; i < pool->ndata ; i++) {
151 if(pool->data[i] == mem) {
152 pool->data[i] = newm;
153 return newm + sizeof(ucx_destructor);
154 }
155 }
156 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
157 (intptr_t)ptr, (intptr_t)pool);
158 abort();
159 } else {
160 return newm + sizeof(ucx_destructor);
161 }
162 }
164 void ucx_mempool_free(UcxMempool *pool, void *ptr) {
165 ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor));
166 for(size_t i=0 ; i<pool->ndata ; i++) {
167 if(chunk == pool->data[i]) {
168 if(chunk->destructor != NULL) {
169 chunk->destructor(&(chunk->c));
170 }
171 free(chunk);
172 size_t last_index = pool->ndata - 1;
173 if(i != last_index) {
174 pool->data[i] = pool->data[last_index];
175 pool->data[last_index] = NULL;
176 }
177 pool->ndata--;
178 return;
179 }
180 }
181 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
182 (intptr_t)ptr, (intptr_t)pool);
183 abort();
184 }
186 void ucx_mempool_destroy(UcxMempool *pool) {
187 ucx_memchunk *chunk;
188 for(size_t i=0 ; i<pool->ndata ; i++) {
189 chunk = (ucx_memchunk*) pool->data[i];
190 if(chunk) {
191 if(chunk->destructor) {
192 chunk->destructor(&(chunk->c));
193 }
194 free(chunk);
195 }
196 }
197 free(pool->data);
198 free(pool->allocator);
199 free(pool);
200 }
202 void ucx_mempool_set_destr(void *ptr, ucx_destructor func) {
203 *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func;
204 }
206 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) {
207 ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc(
208 pool,
209 sizeof(ucx_regdestr));
210 rd->destructor = destr;
211 rd->ptr = ptr;
212 ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
213 }