Debian Codegraph @

libmowgli-2/src/libmowgli/core/heap.enabled.c

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * heap.c: Heap allocation.
4 *
5 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
6 * Copyright (c) 2005-2006 Theo Julienne <terminal -at- atheme.org>
7 * Copyright (c) 2011 Andrew Wilcox <awilcox -at- wilcox-tech.com>
8 *
9 * Permission to use, copy, modify, and/or distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice is present in all copies.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
17 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
22 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 * POSSIBILITY OF SUCH DAMAGE.
24 *
25 * Legal note: code devised from claro.base.block module r288 (Pre MPL)
26 */
27
28 #include "mowgli.h"
29
30 #ifdef HAVE_MMAP
31 # include <sys/mman.h>
32 # if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
33 # define MAP_ANON MAP_ANONYMOUS
34 # endif
35 #endif
36
37 /* A block of memory allocated to the allocator */
38 struct mowgli_block_
39 {
40 mowgli_node_t node;
41
42 /* link back to our heap */
43 mowgli_heap_t *heap;
44
45 /* pointer to the first item */
46 void *data;
47
48 /* singly linked list of free items */
49 void *first_free;
50
51 size_t num_allocated;
52 };
53
54 /* A pile of blocks */
55 struct mowgli_heap_
56 {
57 size_t elem_size;
58 size_t mowgli_heap_elems;
59 size_t free_elems;
60 size_t alloc_size;
61
62 unsigned int flags;
63
64 mowgli_list_t blocks; /* list of non-empty blocks */
65
66 mowgli_allocation_policy_t *allocator;
67 mowgli_boolean_t use_mmap;
68
69 mowgli_mutex_t mutex;
70
71 mowgli_block_t *empty_block; /* a single entirely free block, or NULL */
72 };
73
74 typedef struct mowgli_heap_elem_header_ mowgli_heap_elem_header_t;
75
76 struct mowgli_heap_elem_header_
77 {
78 union
79 {
80 mowgli_block_t *block; /* for allocated elems: block ptr */
81 mowgli_heap_elem_header_t *next;/* for free elems: next free */
82 } un;
83 };
84
85 /* expands a mowgli_heap_t by 1 block */
86 static void
87 mowgli_heap_expand(mowgli_heap_t *bh)
88 {
89 return_if_fail(bh != NULL);
90 return_if_fail(bh->allocator != NULL);
91 return_if_fail(bh->allocator->allocate != NULL);
92
93 mowgli_block_t *block = NULL;
94 void *blp = NULL;
95 mowgli_heap_elem_header_t *node, *prev;
96 char *offset;
97 size_t a;
98 size_t blp_size;
99
100 blp_size = sizeof(mowgli_block_t) + (bh->alloc_size * bh->mowgli_heap_elems);
101
102 if (bh->empty_block != NULL)
103 return;
104
105 #if defined(HAVE_MMAP) && defined(MAP_ANON)
106
107 if (bh->use_mmap)
108 blp = mmap(NULL, sizeof(mowgli_block_t) + (bh->alloc_size * bh->mowgli_heap_elems),
109 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
110 else
111 #elif defined(_WIN32)
112
113 if (bh->use_mmap)
114 blp = VirtualAlloc(NULL, sizeof(mowgli_block_t) + (bh->alloc_size * bh->mowgli_heap_elems),
115 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
116 else
117 #endif
118 blp = bh->allocator->allocate(blp_size);
119
120 block = (mowgli_block_t *) blp;
121
122 offset = (char *) blp + sizeof(mowgli_block_t);
123 block->data = offset;
124 block->heap = bh;
125
126 prev = NULL;
127
128 for (a = 0; a < bh->mowgli_heap_elems; a++)
129 {
130 node = (mowgli_heap_elem_header_t *) offset;
131 node->un.next = prev;
132 offset += bh->alloc_size;
133 prev = node;
134 }
135
136 block->first_free = prev;
137
138 bh->empty_block = block;
139 bh->free_elems += bh->mowgli_heap_elems;
140 }
141
142 /* shrinks a mowgli_heap_t by 1 block. */
143 static void
144 mowgli_heap_shrink(mowgli_heap_t *heap, mowgli_block_t *b)
145 {
146 return_if_fail(b != NULL);
147 return_if_fail(heap != NULL);
148 return_if_fail(heap->allocator != NULL);
149 return_if_fail(heap->allocator->deallocate != NULL);
150
151 if (b == heap->empty_block)
152 heap->empty_block = NULL;
153 else
154 mowgli_node_delete(&b->node, &heap->blocks);
155
156 #if defined(HAVE_MMAP) && defined(MAP_ANON)
157
158 if (heap->use_mmap)
159 munmap(b, sizeof(mowgli_block_t) + (heap->alloc_size * heap->mowgli_heap_elems));
160 else
161 #elif defined(_WIN32)
162
163 if (heap->use_mmap)
164 VirtualFree(b, 0, MEM_RELEASE);
165 else
166 #endif
167 heap->allocator->deallocate(b);
168
169 heap->free_elems -= heap->mowgli_heap_elems;
170 }
171
172 /* creates a new mowgli_heap_t */
173 mowgli_heap_t *
174 mowgli_heap_create_full(size_t elem_size, size_t mowgli_heap_elems, unsigned int flags, mowgli_allocation_policy_t *allocator)
175 {
176 return_null_if_fail(elem_size != 0);
177
178 mowgli_allocation_policy_t *bhallocator = allocator;
179
180 if (bhallocator == NULL)
181 bhallocator = mowgli_allocator_get_policy();
182
183 return_null_if_fail(bhallocator != NULL);
184 return_null_if_fail(bhallocator->allocate != NULL);
185
186 mowgli_heap_t *bh = bhallocator->allocate(sizeof *bh);
187
188 if (! bh)
189 {
190 #ifdef HEAP_DEBUG
191 (void) mowgli_log_warning("cannot allocate memory for heap");
192 #endif
193 return NULL;
194 }
195
196 (void) memset(bh, 0x00, sizeof *bh);
197
198 int numpages, pagesize;
199
200 bh->elem_size = elem_size;
201 bh->mowgli_heap_elems = mowgli_heap_elems;
202
203 /* at least 2, this avoids some silly special cases */
204 if (bh->mowgli_heap_elems < 2)
205 bh->mowgli_heap_elems = 2;
206
207 bh->free_elems = 0;
208
209 bh->alloc_size = bh->elem_size + sizeof(mowgli_heap_elem_header_t);
210
211 /* don't waste part of a page */
212 if (allocator == NULL)
213 {
214 #ifdef HAVE_MMAP
215 pagesize = getpagesize();
216 #else
217 pagesize = 4096;
218 #endif
219 numpages = (sizeof(mowgli_block_t) + (bh->alloc_size * bh->mowgli_heap_elems) + pagesize - 1) / pagesize;
220 bh->mowgli_heap_elems = (numpages * pagesize - sizeof(mowgli_block_t)) / bh->alloc_size;
221 }
222
223 bh->flags = flags;
224 bh->allocator = bhallocator;
225
226 #if (defined(HAVE_MMAP) && defined(MAP_ANON)) || defined(_WIN32)
227 bh->use_mmap = allocator != NULL ? FALSE : TRUE;
228 #endif
229
230 if (mowgli_mutex_init(&bh->mutex) != 0)
231 mowgli_log_fatal("heap mutex can't be created");
232
233 if (flags & BH_NOW)
234 {
235 mowgli_mutex_lock(&bh->mutex);
236 mowgli_heap_expand(bh);
237 mowgli_mutex_unlock(&bh->mutex);
238 }
239
240 #ifdef HEAP_DEBUG
241 (void) mowgli_log("heap@%p: created (elem_size %zu, num_elems %zu, flags %u)",
242 bh, bh->elem_size, bh->mowgli_heap_elems, bh->flags);
243 #endif
244
245 return bh;
246 }
247
248 mowgli_heap_t *
249 mowgli_heap_create(size_t elem_size, size_t mowgli_heap_elems, unsigned int flags)
250 {
251 return mowgli_heap_create_full(elem_size, mowgli_heap_elems, flags, NULL);
252 }
253
254 /* completely frees a mowgli_heap_t and all blocks */
255 void
256 mowgli_heap_destroy(mowgli_heap_t *heap)
257 {
258 return_if_fail(heap != NULL);
259 return_if_fail(heap->allocator != NULL);
260 return_if_fail(heap->allocator->deallocate != NULL);
261
262 const size_t elem_size = heap->elem_size;
263 mowgli_node_t *n, *tn;
264
265 MOWGLI_LIST_FOREACH_SAFE(n, tn, heap->blocks.head)
266 {
267 mowgli_heap_shrink(heap, n->data);
268 }
269
270 if (heap->empty_block)
271 mowgli_heap_shrink(heap, heap->empty_block);
272
273 mowgli_mutex_uninit(&heap->mutex);
274
275 /* everything related to heap has gone, time for itself */
276 heap->allocator->deallocate(heap);
277
278 #ifdef HEAP_DEBUG
279 (void) mowgli_log("heap@%p: destroyed (elem_size %zu)", heap, elem_size);
280 #endif
281 }
282
283 /* allocates a new item from a mowgli_heap_t */
284 void *
285 mowgli_heap_alloc(mowgli_heap_t *heap)
286 {
287 return_null_if_fail(heap != NULL);
288
289 mowgli_node_t *n;
290 mowgli_block_t *b;
291 mowgli_heap_elem_header_t *h;
292
293 if (mowgli_mutex_lock(&heap->mutex) != 0)
294 mowgli_log_fatal("heap mutex can't be locked");
295
296 /* no free space? */
297 if (heap->free_elems == 0)
298 {
299 mowgli_heap_expand(heap);
300
301 if (heap->free_elems == 0)
302 {
303 mowgli_mutex_unlock(&heap->mutex);
304 return NULL;
305 }
306 }
307
308 /* try a partially used block before using a fully free block */
309 n = heap->blocks.head;
310 b = n != NULL ? n->data : NULL;
311
312 if ((b == NULL) || (b->first_free == NULL))
313 b = heap->empty_block;
314
315 /* due to above check */
316 return_val_if_fail(b != NULL, NULL);
317
318 /* pull the first free node from the list */
319 h = b->first_free;
320 return_val_if_fail(h != NULL, NULL);
321
322 /* mark it as used */
323 b->first_free = h->un.next;
324 h->un.block = b;
325
326 /* keep count */
327 heap->free_elems--;
328 b->num_allocated++;
329
330 /* move it between the lists if needed */
331
332 /* note that a block has at least two items in it, so these cases
333 * cannot both occur in the same allocation */
334 if (b->num_allocated == 1)
335 {
336 heap->empty_block = NULL;
337 mowgli_node_add_head(b, &b->node, &heap->blocks);
338 }
339 else if (b->first_free == NULL)
340 {
341 /* move full blocks to the end of the list */
342 mowgli_node_delete(&b->node, &heap->blocks);
343 mowgli_node_add(b, &b->node, &heap->blocks);
344 }
345
346 void *const result = (char *) h + sizeof(mowgli_heap_elem_header_t);
347
348 #ifdef HEAP_DEBUG
349 (void) mowgli_log("heap@%p: allocated ptr %p (elem_size %zu)", heap, result, heap->elem_size);
350 #endif
351
352 mowgli_mutex_unlock(&heap->mutex);
353
354 return memset(result, 0x00, heap->elem_size);
355 }
356
357 /* frees an item back to the mowgli_heap_t */
358 void
359 mowgli_heap_free(mowgli_heap_t *heap, void *data)
360 {
361 return_if_fail(heap != NULL);
362
363 mowgli_block_t *b;
364 mowgli_heap_elem_header_t *h;
365
366 if (mowgli_mutex_lock(&heap->mutex) != 0)
367 mowgli_log_fatal("heap mutex can't be locked");
368
369 h = (mowgli_heap_elem_header_t *) ((char *) data - sizeof(mowgli_heap_elem_header_t));
370 b = h->un.block;
371
372 return_if_fail(b->heap == heap);
373 return_if_fail(b->num_allocated > 0);
374
375 #ifdef HEAP_DEBUG
376 (void) mowgli_log("pseudoheap@%p: freeing ptr %p (elem_size %zu)", heap, data, heap->elem_size);
377 #endif
378
379 /* memset the element before returning it to the heap. */
380 memset(data, 0, b->heap->elem_size);
381
382 /* mark it as free */
383 h->un.next = b->first_free;
384 b->first_free = h;
385
386 /* keep count */
387 heap->free_elems++;
388 b->num_allocated--;
389
390 /* move it between the lists if needed */
391 if (b->num_allocated == 0)
392 {
393 if (heap->empty_block != NULL)
394 mowgli_heap_shrink(heap, heap->empty_block);
395
396 mowgli_node_delete(&b->node, &heap->blocks);
397 heap->empty_block = b;
398 }
399 else if (b->num_allocated == heap->mowgli_heap_elems - 1)
400 {
401 mowgli_node_delete(&b->node, &heap->blocks);
402 mowgli_node_add_head(b, &b->node, &heap->blocks);
403 }
404
405 mowgli_mutex_unlock(&heap->mutex);
406 }