Mon, 20 Nov 2017 18:06:32 +0100
removes man dir from clean target of doc makefile
13 | 1 | /* |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
13 | 3 | * |
259 | 4 | * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
5 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
8 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
11 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
15 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
13 | 27 | */ |
28 | ||
251
fae240d633fc
changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents:
250
diff
changeset
|
29 | #include "ucx/mempool.h" |
fae240d633fc
changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents:
250
diff
changeset
|
30 | |
13 | 31 | #include <stdlib.h> |
32 | #include <string.h> | |
33 | #include <stdio.h> | |
95 | 34 | #ifdef __cplusplus |
35 | #define __STDC_FORMAT_MACROS | |
36 | #endif | |
75
990734f548ef
fixed some compiler warnings
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
37 | #include <inttypes.h> |
13 | 38 | |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
39 | /** Capsule for destructible memory chunks. */ |
13 | 40 | typedef struct { |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
41 | /** The destructor for the memory chunk. */ |
13 | 42 | ucx_destructor destructor; |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
43 | /** |
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
44 | * First byte of the memory chunk. |
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
45 | * Note, that the address <code>&c</code> is also the address |
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
46 | * of the whole memory chunk. |
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
47 | */ |
13 | 48 | char c; |
49 | } ucx_memchunk; | |
50 | ||
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
51 | /** Capsule for data and its destructor. */ |
13 | 52 | typedef struct { |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
53 | /** The destructor for the data. */ |
13 | 54 | ucx_destructor destructor; |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
55 | /** A pointer to the data. */ |
13 | 56 | void *ptr; |
57 | } ucx_regdestr; | |
58 | ||
253
e19825a1430a
removes unnecessary macros from ucx.h + removes the usage of restrict and _Bool completely, instead of defining macros
Mike Becker <universe@uap-core.de>
parents:
251
diff
changeset
|
59 | #ifdef __cplusplus |
e19825a1430a
removes unnecessary macros from ucx.h + removes the usage of restrict and _Bool completely, instead of defining macros
Mike Becker <universe@uap-core.de>
parents:
251
diff
changeset
|
60 | extern "C" |
e19825a1430a
removes unnecessary macros from ucx.h + removes the usage of restrict and _Bool completely, instead of defining macros
Mike Becker <universe@uap-core.de>
parents:
251
diff
changeset
|
61 | #endif |
e19825a1430a
removes unnecessary macros from ucx.h + removes the usage of restrict and _Bool completely, instead of defining macros
Mike Becker <universe@uap-core.de>
parents:
251
diff
changeset
|
62 | void ucx_mempool_shared_destr(void* ptr) { |
13 | 63 | ucx_regdestr *rd = (ucx_regdestr*)ptr; |
64 | rd->destructor(rd->ptr); | |
65 | } | |
66 | ||
67 | UcxMempool *ucx_mempool_new(size_t n) { | |
14 | 68 | UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
69 | if (!pool) { |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
70 | return NULL; |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
71 | } |
15
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
72 | |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
73 | pool->data = (void**) malloc(n * sizeof(void*)); |
15
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
74 | if (pool->data == NULL) { |
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
75 | free(pool); |
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
76 | return NULL; |
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
77 | } |
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
78 | |
13 | 79 | pool->ndata = 0; |
80 | pool->size = n; | |
158
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
81 | |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
82 | UcxAllocator *allocator = (UcxAllocator*)malloc(sizeof(UcxAllocator)); |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
83 | if(!allocator) { |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
84 | free(pool->data); |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
85 | free(pool); |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
86 | return NULL; |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
87 | } |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
88 | allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc; |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
89 | allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc; |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
90 | allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc; |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
91 | allocator->free = (ucx_allocator_free)ucx_mempool_free; |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
92 | allocator->pool = pool; |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
93 | pool->allocator = allocator; |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
94 | |
13 | 95 | return pool; |
96 | } | |
97 | ||
15
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
98 | int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { |
241
661f33ef20d8
removes unused ucx_mempool_clamp() and changes storage behavior of mempool to increase the capacity by a factor of two
Mike Becker <universe@uap-core.de>
parents:
240
diff
changeset
|
99 | if (newcap < pool->ndata) { |
661f33ef20d8
removes unused ucx_mempool_clamp() and changes storage behavior of mempool to increase the capacity by a factor of two
Mike Becker <universe@uap-core.de>
parents:
240
diff
changeset
|
100 | return 1; |
661f33ef20d8
removes unused ucx_mempool_clamp() and changes storage behavior of mempool to increase the capacity by a factor of two
Mike Becker <universe@uap-core.de>
parents:
240
diff
changeset
|
101 | } |
661f33ef20d8
removes unused ucx_mempool_clamp() and changes storage behavior of mempool to increase the capacity by a factor of two
Mike Becker <universe@uap-core.de>
parents:
240
diff
changeset
|
102 | |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
103 | void **data = (void**) realloc(pool->data, newcap*sizeof(void*)); |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
104 | if (data) { |
15
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
105 | pool->data = data; |
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
106 | pool->size = newcap; |
241
661f33ef20d8
removes unused ucx_mempool_clamp() and changes storage behavior of mempool to increase the capacity by a factor of two
Mike Becker <universe@uap-core.de>
parents:
240
diff
changeset
|
107 | return 0; |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
108 | } else { |
241
661f33ef20d8
removes unused ucx_mempool_clamp() and changes storage behavior of mempool to increase the capacity by a factor of two
Mike Becker <universe@uap-core.de>
parents:
240
diff
changeset
|
109 | return 1; |
15
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
110 | } |
13 | 111 | } |
112 | ||
113 | void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { | |
141
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
138
diff
changeset
|
114 | if (pool->ndata >= pool->size) { |
242
a3597d704421
safe one multiplication in overflow check for mempool_chcap()
Mike Becker <universe@uap-core.de>
parents:
241
diff
changeset
|
115 | size_t newcap = pool->size*2; |
a3597d704421
safe one multiplication in overflow check for mempool_chcap()
Mike Becker <universe@uap-core.de>
parents:
241
diff
changeset
|
116 | if (newcap < pool->size || ucx_mempool_chcap(pool, newcap)) { |
141
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
138
diff
changeset
|
117 | return NULL; |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
138
diff
changeset
|
118 | } |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
138
diff
changeset
|
119 | } |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
138
diff
changeset
|
120 | |
248
dff45a2234ce
explicit cast from (void*) for (ucx_memchunk*) to silence a warning
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
121 | void *p = malloc(sizeof(ucx_destructor) + n); |
dff45a2234ce
explicit cast from (void*) for (ucx_memchunk*) to silence a warning
Mike Becker <universe@uap-core.de>
parents:
242
diff
changeset
|
122 | ucx_memchunk *mem = (ucx_memchunk*)p; |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
123 | if (!mem) { |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
124 | return NULL; |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
125 | } |
13 | 126 | |
127 | mem->destructor = NULL; | |
128 | pool->data[pool->ndata] = mem; | |
129 | pool->ndata++; | |
130 | ||
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
131 | return &(mem->c); |
13 | 132 | } |
133 | ||
134 | void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { | |
135 | void *ptr = ucx_mempool_malloc(pool, nelem*elsize); | |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
136 | if (!ptr) { |
13 | 137 | return NULL; |
138 | } | |
139 | memset(ptr, 0, nelem * elsize); | |
140 | return ptr; | |
141 | } | |
142 | ||
143 | void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { | |
28 | 144 | char *mem = ((char*)ptr) - sizeof(ucx_destructor); |
15
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
145 | char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
146 | if (!newm) { |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
147 | return NULL; |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
148 | } |
15
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
149 | if (mem != newm) { |
95 | 150 | for(size_t i=0 ; i < pool->ndata ; i++) { |
14 | 151 | if(pool->data[i] == mem) { |
152 | pool->data[i] = newm; | |
28 | 153 | return newm + sizeof(ucx_destructor); |
14 | 154 | } |
13 | 155 | } |
116
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
113
diff
changeset
|
156 | fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
75
990734f548ef
fixed some compiler warnings
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
157 | (intptr_t)ptr, (intptr_t)pool); |
241
661f33ef20d8
removes unused ucx_mempool_clamp() and changes storage behavior of mempool to increase the capacity by a factor of two
Mike Becker <universe@uap-core.de>
parents:
240
diff
changeset
|
158 | abort(); |
16
b4769e4eb4d1
fixed ultra fail + renamed files from mpool to mempool
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
159 | } else { |
28 | 160 | return newm + sizeof(ucx_destructor); |
13 | 161 | } |
162 | } | |
163 | ||
113
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
164 | void ucx_mempool_free(UcxMempool *pool, void *ptr) { |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
165 | ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor)); |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
166 | for(size_t i=0 ; i<pool->ndata ; i++) { |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
167 | if(chunk == pool->data[i]) { |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
168 | if(chunk->destructor != NULL) { |
141
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
138
diff
changeset
|
169 | chunk->destructor(&(chunk->c)); |
113
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
170 | } |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
171 | free(chunk); |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
172 | size_t last_index = pool->ndata - 1; |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
173 | if(i != last_index) { |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
174 | pool->data[i] = pool->data[last_index]; |
141
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
138
diff
changeset
|
175 | pool->data[last_index] = NULL; |
113
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
176 | } |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
177 | pool->ndata--; |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
178 | return; |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
179 | } |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
180 | } |
116
234920008754
some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents:
113
diff
changeset
|
181 | fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
113
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
182 | (intptr_t)ptr, (intptr_t)pool); |
240
8f937a3a6d11
ucx_mempool_free() aborts execution instead of just exiting on failure
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
183 | abort(); |
113
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
184 | } |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
185 | |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
186 | void ucx_mempool_destroy(UcxMempool *pool) { |
13 | 187 | ucx_memchunk *chunk; |
95 | 188 | for(size_t i=0 ; i<pool->ndata ; i++) { |
13 | 189 | chunk = (ucx_memchunk*) pool->data[i]; |
113
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
190 | if(chunk) { |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
191 | if(chunk->destructor) { |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
192 | chunk->destructor(&(chunk->c)); |
113
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
193 | } |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
194 | free(chunk); |
13 | 195 | } |
196 | } | |
197 | free(pool->data); | |
158
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
198 | free(pool->allocator); |
13 | 199 | free(pool); |
200 | } | |
201 | ||
202 | void ucx_mempool_set_destr(void *ptr, ucx_destructor func) { | |
203 | *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func; | |
204 | } | |
205 | ||
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; | |
14 | 211 | rd->ptr = ptr; |
13 | 212 | ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); |
213 | } | |
113
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
214 |