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 | /** | |
30 | * @file mempool.h | |
31 | * | |
32 | * Memory pool implementation. | |
33 | * | |
34 | * @author Mike Becker | |
35 | * @author Olaf Wintermann | |
36 | */ | |
37 | ||
38 | #ifndef UCX_MEMPOOL_H | |
39 | #define UCX_MEMPOOL_H | |
40 | ||
41 | #include "ucx.h" | |
42 | #include <stddef.h> | |
43 | #include "allocator.h" | |
44 | ||
45 | #ifdef __cplusplus | |
46 | extern "C" { | |
47 | #endif | |
48 | ||
49 | /** | |
50 | * UCX mempool structure. | |
51 | */ | |
52 | typedef struct { | |
2
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
53 | /** UcxAllocator based on this pool */ |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
54 | UcxAllocator *allocator; |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
55 | |
0 | 56 | /** List of pointers to pooled memory. */ |
2
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
57 | void **data; |
0 | 58 | |
59 | /** Count of pooled memory items. */ | |
2
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
60 | size_t ndata; |
0 | 61 | |
62 | /** Memory pool size. */ | |
2
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
63 | size_t size; |
0 | 64 | } UcxMempool; |
65 | ||
66 | /** Shorthand for a new default memory pool with a capacity of 16 elements. */ | |
67 | #define ucx_mempool_new_default() ucx_mempool_new(16) | |
68 | ||
69 | ||
70 | /** | |
71 | * Creates a memory pool with the specified initial size. | |
72 | * | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
73 | * As the created memory pool automatically grows in size by factor two when |
0 | 74 | * trying to allocate memory on a full pool, it is recommended that you use |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
75 | * a power of two for the initial size. |
0 | 76 | * |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
77 | * @param n initial pool size (should be a power of two, e.g. 16) |
0 | 78 | * @return a pointer to the new memory pool |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
79 | * @see ucx_mempool_new_default() |
0 | 80 | */ |
81 | UcxMempool *ucx_mempool_new(size_t n); | |
82 | ||
83 | /** | |
84 | * Resizes a memory pool. | |
85 | * | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
86 | * This function will fail if the new capacity is not sufficient for the |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
87 | * present data. |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
88 | * |
0 | 89 | * @param pool the pool to resize |
90 | * @param newcap the new capacity | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
91 | * @return zero on success or non-zero on failure |
0 | 92 | */ |
93 | int ucx_mempool_chcap(UcxMempool *pool, size_t newcap); | |
94 | ||
95 | /** | |
96 | * Allocates pooled memory. | |
97 | * | |
98 | * @param pool the memory pool | |
99 | * @param n amount of memory to allocate | |
100 | * @return a pointer to the allocated memory | |
101 | * @see ucx_allocator_malloc() | |
102 | */ | |
103 | void *ucx_mempool_malloc(UcxMempool *pool, size_t n); | |
104 | /** | |
105 | * Allocates a pooled memory array. | |
106 | * | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
107 | * The content of the allocated memory is set to zero. |
0 | 108 | * |
109 | * @param pool the memory pool | |
110 | * @param nelem amount of elements to allocate | |
111 | * @param elsize amount of memory per element | |
112 | * @return a pointer to the allocated memory | |
113 | * @see ucx_allocator_calloc() | |
114 | */ | |
115 | void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize); | |
116 | ||
117 | /** | |
118 | * Reallocates pooled memory. | |
119 | * | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
120 | * If the memory to be reallocated is not contained by the specified pool, the |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
121 | * behavior is undefined. |
0 | 122 | * |
123 | * @param pool the memory pool | |
124 | * @param ptr a pointer to the memory that shall be reallocated | |
125 | * @param n the new size of the memory | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
126 | * @return a pointer to the new location of the memory |
0 | 127 | * @see ucx_allocator_realloc() |
128 | */ | |
129 | void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n); | |
130 | ||
131 | /** | |
132 | * Frees pooled memory. | |
133 | * | |
134 | * Before freeing the memory, the specified destructor function (if any) | |
135 | * is called. | |
136 | * | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
137 | * If you specify memory, that is not pooled by the specified memory pool, the |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
138 | * program will terminate with a call to <code>abort()</code>. |
0 | 139 | * |
140 | * @param pool the memory pool | |
141 | * @param ptr a pointer to the memory that shall be freed | |
142 | * @see ucx_mempool_set_destr() | |
143 | */ | |
144 | void ucx_mempool_free(UcxMempool *pool, void *ptr); | |
145 | ||
146 | /** | |
147 | * Destroys a memory pool. | |
148 | * | |
149 | * For each element the destructor function (if any) is called and the element | |
150 | * is freed. | |
151 | * | |
152 | * Each of the registered destructor function that has no corresponding element | |
153 | * within the pool (namely those registered by ucx_mempool_reg_destr) is | |
154 | * called interleaving with the element destruction, but with guarantee to the | |
155 | * order in which they were registered (FIFO order). | |
156 | * | |
157 | * | |
158 | * @param pool the mempool to destroy | |
159 | */ | |
160 | void ucx_mempool_destroy(UcxMempool *pool); | |
161 | ||
162 | /** | |
163 | * Sets a destructor function for the specified memory. | |
164 | * | |
165 | * The destructor is automatically called when the memory is freed or the | |
166 | * pool is destroyed. | |
167 | * | |
168 | * The only requirement for the specified memory is, that it <b>MUST</b> be | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
169 | * pooled memory by a UcxMempool or an element-compatible mempool. The pointer |
0 | 170 | * to the destructor function is saved in a reserved area before the actual |
171 | * memory. | |
172 | * | |
173 | * @param ptr pooled memory | |
174 | * @param func a pointer to the destructor function | |
175 | * @see ucx_mempool_free() | |
176 | * @see ucx_mempool_destroy() | |
177 | */ | |
178 | void ucx_mempool_set_destr(void *ptr, ucx_destructor func); | |
179 | ||
180 | /** | |
181 | * Registers a destructor function for the specified (non-pooled) memory. | |
182 | * | |
183 | * This is useful, if you have memory that has not been allocated by a mempool, | |
184 | * but shall be managed by a mempool. | |
185 | * | |
186 | * This function creates an entry in the specified mempool and the memory will | |
187 | * therefore (logically) convert to pooled memory. | |
188 | * | |
189 | * @param pool the memory pool | |
190 | * @param ptr data the destructor is registered for | |
191 | * @param destr a pointer to the destructor function | |
192 | */ | |
193 | void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr); | |
194 | ||
195 | #ifdef __cplusplus | |
196 | } | |
197 | #endif | |
198 | ||
199 | #endif /* UCX_MEMPOOL_H */ | |
200 |