ucx/mempool.h

changeset 0
1f419bd32da1
child 2
eeb50c534497
equal deleted inserted replaced
-1:000000000000 0:1f419bd32da1
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 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 */
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 * A function pointer to a destructor function.
51 * @see ucx_mempool_setdestr()
52 * @see ucx_mempool_regdestr()
53 */
54 typedef void(*ucx_destructor)(void*);
55
56 /**
57 * UCX mempool structure.
58 */
59 typedef struct {
60 /** List of pointers to pooled memory. */
61 void **data;
62
63 /** Count of pooled memory items. */
64 size_t ndata;
65
66 /** Memory pool size. */
67 size_t size;
68 } UcxMempool;
69
70 /** Shorthand for a new default memory pool with a capacity of 16 elements. */
71 #define ucx_mempool_new_default() ucx_mempool_new(16)
72
73
74 /**
75 * Creates a memory pool with the specified initial size.
76 *
77 * As the created memory pool automatically grows in size by 16 elements, when
78 * trying to allocate memory on a full pool, it is recommended that you use
79 * a multiple of 16 for the initial size.
80 *
81 * @param n initial pool size (should be a multiple of 16)
82 * @return a pointer to the new memory pool
83 */
84 UcxMempool *ucx_mempool_new(size_t n);
85
86 /**
87 * Resizes a memory pool.
88 *
89 * @param pool the pool to resize
90 * @param newcap the new capacity
91 * @return <code>EXIT_SUCCESS</code> on success or
92 * <code>EXIT_FAILURE</code> on failure
93 */
94 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap);
95
96 /**
97 * Changes the pool size to the next smallest multiple of 16.
98 *
99 * You may use this macro, to reduce the pool size after freeing
100 * many pooled memory items.
101 *
102 * @param pool the pool to clamp
103 * @return <code>EXIT_SUCCESS</code> on success or
104 * <code>EXIT_FAILURE</code> on failure
105 */
106 #define ucx_mempool_clamp(pool) ucx_mempool_chcap(pool, \
107 (pool->ndata & ~0xF)+0x10)
108
109 /**
110 * Allocates pooled memory.
111 *
112 * @param pool the memory pool
113 * @param n amount of memory to allocate
114 * @return a pointer to the allocated memory
115 * @see ucx_allocator_malloc()
116 */
117 void *ucx_mempool_malloc(UcxMempool *pool, size_t n);
118 /**
119 * Allocates a pooled memory array.
120 *
121 * The contents of the allocated memory is set to zero.
122 *
123 * @param pool the memory pool
124 * @param nelem amount of elements to allocate
125 * @param elsize amount of memory per element
126 * @return a pointer to the allocated memory
127 * @see ucx_allocator_calloc()
128 */
129 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize);
130
131 /**
132 * Reallocates pooled memory.
133 *
134 * If the memory to be reallocated is not contained by the specified pool, this
135 * function will possibly fail. In case the memory had to be moved to another
136 * location, this function will print out a message to <code>stderr</code>
137 * and exit the program with error code <code>EXIT_FAILURE</code>.
138 *
139 * @param pool the memory pool
140 * @param ptr a pointer to the memory that shall be reallocated
141 * @param n the new size of the memory
142 * @return a pointer to the the location of the memory
143 * @see ucx_allocator_realloc()
144 */
145 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n);
146
147 /**
148 * Frees pooled memory.
149 *
150 * Before freeing the memory, the specified destructor function (if any)
151 * is called.
152 *
153 * If you specify memory, that is not pooled by the specified memory pool, this
154 * is considered as a severe error and an error message is written to
155 * <code>stderr</code> and the application is shut down with code
156 * <code>EXIT_FAILURE</code>.
157 *
158 * @param pool the memory pool
159 * @param ptr a pointer to the memory that shall be freed
160 * @see ucx_mempool_set_destr()
161 */
162 void ucx_mempool_free(UcxMempool *pool, void *ptr);
163
164 /**
165 * Destroys a memory pool.
166 *
167 * For each element the destructor function (if any) is called and the element
168 * is freed.
169 *
170 * Each of the registered destructor function that has no corresponding element
171 * within the pool (namely those registered by ucx_mempool_reg_destr) is
172 * called interleaving with the element destruction, but with guarantee to the
173 * order in which they were registered (FIFO order).
174 *
175 *
176 * @param pool the mempool to destroy
177 */
178 void ucx_mempool_destroy(UcxMempool *pool);
179
180 /**
181 * Sets a destructor function for the specified memory.
182 *
183 * The destructor is automatically called when the memory is freed or the
184 * pool is destroyed.
185 *
186 * The only requirement for the specified memory is, that it <b>MUST</b> be
187 * pooled memory by an UcxMempool or an element-compatible mempool. The pointer
188 * to the destructor function is saved in a reserved area before the actual
189 * memory.
190 *
191 * @param ptr pooled memory
192 * @param func a pointer to the destructor function
193 * @see ucx_mempool_free()
194 * @see ucx_mempool_destroy()
195 */
196 void ucx_mempool_set_destr(void *ptr, ucx_destructor func);
197
198 /**
199 * Registers a destructor function for the specified (non-pooled) memory.
200 *
201 * This is useful, if you have memory that has not been allocated by a mempool,
202 * but shall be managed by a mempool.
203 *
204 * This function creates an entry in the specified mempool and the memory will
205 * therefore (logically) convert to pooled memory.
206 *
207 * @param pool the memory pool
208 * @param ptr data the destructor is registered for
209 * @param destr a pointer to the destructor function
210 */
211 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr);
212
213 /**
214 * Creates an UcxAllocator based on an UcxMempool.
215 *
216 * @param pool the mempool to create the UcxAllocator for
217 * @return a new UcxAllocator based on the specified pool
218 */
219 UcxAllocator* ucx_mempool_allocator(UcxMempool *pool);
220
221 #ifdef __cplusplus
222 }
223 #endif
224
225 #endif /* UCX_MEMPOOL_H */
226

mercurial