Mon, 04 Feb 2019 14:46:11 +0100
new build system
0 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
4 | * Copyright 2016 Olaf Wintermann. All rights reserved. |
0 | 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 | */ | |
28 | ||
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <stdio.h> | |
32 | #ifdef __cplusplus | |
33 | #define __STDC_FORMAT_MACROS | |
34 | #endif | |
35 | #include <inttypes.h> | |
36 | ||
37 | #include "mempool.h" | |
38 | ||
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; | |
50 | ||
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; | |
58 | ||
59 | UCX_EXTERN void ucx_mempool_shared_destr(void* ptr) { | |
60 | ucx_regdestr *rd = (ucx_regdestr*)ptr; | |
61 | rd->destructor(rd->ptr); | |
62 | } | |
63 | ||
64 | UcxMempool *ucx_mempool_new(size_t n) { | |
65 | UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); | |
66 | if (!pool) { | |
67 | return NULL; | |
68 | } | |
69 | ||
70 | pool->data = (void**) malloc(n * sizeof(void*)); | |
71 | if (pool->data == NULL) { | |
72 | free(pool); | |
73 | return NULL; | |
74 | } | |
75 | ||
76 | pool->ndata = 0; | |
77 | pool->size = n; | |
2
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
78 | |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
79 | UcxAllocator *allocator = (UcxAllocator*)malloc(sizeof(UcxAllocator)); |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
80 | if(!allocator) { |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
81 | free(pool->data); |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
82 | free(pool); |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
83 | return NULL; |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
84 | } |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
85 | allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc; |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
86 | allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc; |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
87 | allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc; |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
88 | allocator->free = (ucx_allocator_free)ucx_mempool_free; |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
89 | allocator->pool = pool; |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
90 | pool->allocator = allocator; |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
91 | |
0 | 92 | return pool; |
93 | } | |
94 | ||
95 | int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
96 | if (newcap < pool->ndata) { |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
97 | return 1; |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
98 | } |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
99 | |
0 | 100 | void **data = (void**) realloc(pool->data, newcap*sizeof(void*)); |
101 | if (data) { | |
102 | pool->data = data; | |
103 | pool->size = newcap; | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
104 | return 0; |
0 | 105 | } else { |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
106 | return 1; |
0 | 107 | } |
108 | } | |
109 | ||
110 | void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { | |
111 | if (pool->ndata >= pool->size) { | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
112 | size_t newcap = pool->size*2; |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
113 | if (newcap < pool->size || ucx_mempool_chcap(pool, newcap)) { |
0 | 114 | return NULL; |
115 | } | |
116 | } | |
117 | ||
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
118 | void *p = malloc(sizeof(ucx_destructor) + n); |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
119 | ucx_memchunk *mem = (ucx_memchunk*)p; |
0 | 120 | if (!mem) { |
121 | return NULL; | |
122 | } | |
123 | ||
124 | mem->destructor = NULL; | |
125 | pool->data[pool->ndata] = mem; | |
126 | pool->ndata++; | |
127 | ||
128 | return &(mem->c); | |
129 | } | |
130 | ||
131 | void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { | |
132 | void *ptr = ucx_mempool_malloc(pool, nelem*elsize); | |
133 | if (!ptr) { | |
134 | return NULL; | |
135 | } | |
136 | memset(ptr, 0, nelem * elsize); | |
137 | return ptr; | |
138 | } | |
139 | ||
140 | void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { | |
141 | char *mem = ((char*)ptr) - sizeof(ucx_destructor); | |
142 | char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); | |
143 | if (!newm) { | |
144 | return NULL; | |
145 | } | |
146 | if (mem != newm) { | |
147 | for(size_t i=0 ; i < pool->ndata ; i++) { | |
148 | if(pool->data[i] == mem) { | |
149 | pool->data[i] = newm; | |
150 | return newm + sizeof(ucx_destructor); | |
151 | } | |
152 | } | |
153 | fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", | |
154 | (intptr_t)ptr, (intptr_t)pool); | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
155 | abort(); |
0 | 156 | } else { |
157 | return newm + sizeof(ucx_destructor); | |
158 | } | |
159 | } | |
160 | ||
161 | void ucx_mempool_free(UcxMempool *pool, void *ptr) { | |
162 | ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor)); | |
163 | for(size_t i=0 ; i<pool->ndata ; i++) { | |
164 | if(chunk == pool->data[i]) { | |
165 | if(chunk->destructor != NULL) { | |
166 | chunk->destructor(&(chunk->c)); | |
167 | } | |
168 | free(chunk); | |
169 | size_t last_index = pool->ndata - 1; | |
170 | if(i != last_index) { | |
171 | pool->data[i] = pool->data[last_index]; | |
172 | pool->data[last_index] = NULL; | |
173 | } | |
174 | pool->ndata--; | |
175 | return; | |
176 | } | |
177 | } | |
178 | fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", | |
179 | (intptr_t)ptr, (intptr_t)pool); | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
180 | abort(); |
0 | 181 | } |
182 | ||
183 | void ucx_mempool_destroy(UcxMempool *pool) { | |
184 | ucx_memchunk *chunk; | |
185 | for(size_t i=0 ; i<pool->ndata ; i++) { | |
186 | chunk = (ucx_memchunk*) pool->data[i]; | |
187 | if(chunk) { | |
188 | if(chunk->destructor) { | |
189 | chunk->destructor(&(chunk->c)); | |
190 | } | |
191 | free(chunk); | |
192 | } | |
193 | } | |
194 | free(pool->data); | |
2
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
195 | free(pool->allocator); |
0 | 196 | free(pool); |
197 | } | |
198 | ||
199 | void ucx_mempool_set_destr(void *ptr, ucx_destructor func) { | |
200 | *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func; | |
201 | } | |
202 | ||
203 | void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) { | |
204 | ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc( | |
205 | pool, | |
206 | sizeof(ucx_regdestr)); | |
207 | rd->destructor = destr; | |
208 | rd->ptr = ptr; | |
209 | ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); | |
210 | } | |
211 |