1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-mempool.h Memory pools
4 * Copyright (C) 2002, 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-mempool.h"
25 #include "dbus-internals.h"
28 * @defgroup DBusMemPool memory pools
29 * @ingroup DBusInternals
30 * @brief DBusMemPool object
32 * Types and functions related to DBusMemPool. A memory pool is used
33 * to decrease memory fragmentation/overhead and increase speed for
34 * blocks of small uniformly-sized objects. The main point is to avoid
35 * the overhead of a malloc block for each small object, speed is
40 * @defgroup DBusMemPoolInternals Memory pool implementation details
41 * @ingroup DBusInternals
42 * @brief DBusMemPool implementation details
44 * The guts of DBusMemPool.
50 * typedef so DBusFreedElement struct can refer to itself.
52 typedef struct DBusFreedElement DBusFreedElement;
55 * struct representing an element on the free list.
56 * We just cast freed elements to this so we can
57 * make a list out of them.
59 struct DBusFreedElement
61 DBusFreedElement *next; /**< next element of the free list */
65 * The dummy size of the variable-length "elements"
66 * field in DBusMemBlock
68 #define ELEMENT_PADDING 4
71 * Typedef for DBusMemBlock so the struct can recursively
74 typedef struct DBusMemBlock DBusMemBlock;
77 * DBusMemBlock object represents a single malloc()-returned
78 * block that gets chunked up into objects in the memory pool.
82 DBusMemBlock *next; /**< next block in the list, which is already used up;
83 * only saved so we can free all the blocks
84 * when we free the mem pool.
87 int used_so_far; /**< bytes of this block already allocated as elements. */
89 unsigned char elements[ELEMENT_PADDING]; /**< the block data, actually allocated to required size */
93 * Internals fields of DBusMemPool
97 int element_size; /**< size of a single object in the pool */
98 int block_size; /**< size of most recently allocated block */
99 unsigned int zero_elements : 1; /**< whether to zero-init allocated elements */
101 DBusFreedElement *free_elements; /**< a free list of elements to recycle */
102 DBusMemBlock *blocks; /**< blocks of memory from malloc() */
103 int allocated_elements; /**< Count of outstanding allocated elements */
109 * @addtogroup DBusMemPool
115 * @typedef DBusMemPool
117 * Opaque object representing a memory pool. Memory pools allow
118 * avoiding per-malloc-block memory overhead when allocating a lot of
119 * small objects that are all the same size. They are slightly
120 * faster than calling malloc() also.
124 * Creates a new memory pool, or returns #NULL on failure. Objects in
125 * the pool must be at least sizeof(void*) bytes each, due to the way
126 * memory pools work. To avoid creating 64 bit problems, this means at
127 * least 8 bytes on all platforms, unless you are 4 bytes on 32-bit
128 * and 8 bytes on 64-bit.
130 * @param element_size size of an element allocated from the pool.
131 * @param zero_elements whether to zero-initialize elements
132 * @returns the new pool or #NULL
135 _dbus_mem_pool_new (int element_size,
136 dbus_bool_t zero_elements)
140 pool = dbus_new0 (DBusMemPool, 1);
144 /* Make the element size at least 8 bytes. */
145 if (element_size < 8)
148 /* these assertions are equivalent but the first is more clear
149 * to programmers that see it fail.
151 _dbus_assert (element_size >= (int) sizeof (void*));
152 _dbus_assert (element_size >= (int) sizeof (DBusFreedElement));
154 /* align the element size to a pointer boundary so we won't get bus
155 * errors under other architectures.
157 pool->element_size = _DBUS_ALIGN_VALUE (element_size, sizeof (void *));
159 pool->zero_elements = zero_elements != FALSE;
161 pool->allocated_elements = 0;
163 /* pick a size for the first block; it increases
164 * for each block we need to allocate. This is
165 * actually half the initial block size
166 * since _dbus_mem_pool_alloc() unconditionally
167 * doubles it prior to creating a new block. */
168 pool->block_size = pool->element_size * 8;
170 _dbus_assert ((pool->block_size %
171 pool->element_size) == 0);
177 * Frees a memory pool (and all elements allocated from it).
179 * @param pool the memory pool.
182 _dbus_mem_pool_free (DBusMemPool *pool)
186 block = pool->blocks;
187 while (block != NULL)
189 DBusMemBlock *next = block->next;
200 * Allocates an object from the memory pool.
201 * The object must be freed with _dbus_mem_pool_dealloc().
203 * @param pool the memory pool
204 * @returns the allocated object or #NULL if no memory.
207 _dbus_mem_pool_alloc (DBusMemPool *pool)
209 if (_dbus_disable_mem_pools ())
214 /* This is obviously really silly, but it's
215 * debug-mode-only code that is compiled out
216 * when tests are disabled (_dbus_disable_mem_pools()
217 * is a constant expression FALSE so this block
221 alloc_size = sizeof (DBusMemBlock) - ELEMENT_PADDING +
224 if (pool->zero_elements)
225 block = dbus_malloc0 (alloc_size);
227 block = dbus_malloc (alloc_size);
231 block->next = pool->blocks;
232 pool->blocks = block;
233 pool->allocated_elements += 1;
235 return (void*) &block->elements[0];
242 if (_dbus_decrement_fail_alloc_counter ())
244 _dbus_verbose (" FAILING mempool alloc\n");
247 else if (pool->free_elements)
249 DBusFreedElement *element = pool->free_elements;
251 pool->free_elements = pool->free_elements->next;
253 if (pool->zero_elements)
254 memset (element, '\0', pool->element_size);
256 pool->allocated_elements += 1;
264 if (pool->blocks == NULL ||
265 pool->blocks->used_so_far == pool->block_size)
267 /* Need a new block */
270 #ifdef DBUS_BUILD_TESTS
274 if (pool->block_size <= _DBUS_INT_MAX / 4) /* avoid overflow */
276 /* use a larger block size for our next block */
277 pool->block_size *= 2;
278 _dbus_assert ((pool->block_size %
279 pool->element_size) == 0);
282 alloc_size = sizeof (DBusMemBlock) - ELEMENT_PADDING + pool->block_size;
284 #ifdef DBUS_BUILD_TESTS
285 /* We save/restore the counter, so that memory pools won't
286 * cause a given function to have different number of
287 * allocations on different invocations. i.e. when testing
288 * we want consistent alloc patterns. So we skip our
289 * malloc here for purposes of failed alloc simulation.
291 saved_counter = _dbus_get_fail_alloc_counter ();
292 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
295 if (pool->zero_elements)
296 block = dbus_malloc0 (alloc_size);
298 block = dbus_malloc (alloc_size);
300 #ifdef DBUS_BUILD_TESTS
301 _dbus_set_fail_alloc_counter (saved_counter);
302 _dbus_assert (saved_counter == _dbus_get_fail_alloc_counter ());
308 block->used_so_far = 0;
309 block->next = pool->blocks;
310 pool->blocks = block;
313 element = &pool->blocks->elements[pool->blocks->used_so_far];
315 pool->blocks->used_so_far += pool->element_size;
317 pool->allocated_elements += 1;
325 * Deallocates an object previously created with
326 * _dbus_mem_pool_alloc(). The previous object
327 * must have come from this same pool.
328 * @param pool the memory pool
329 * @param element the element earlier allocated.
330 * @returns #TRUE if there are no remaining allocated elements
333 _dbus_mem_pool_dealloc (DBusMemPool *pool,
336 if (_dbus_disable_mem_pools ())
341 /* mmm, fast. ;-) debug-only code, so doesn't matter. */
344 block = pool->blocks;
346 while (block != NULL)
348 if (block->elements == (unsigned char*) element)
351 prev->next = block->next;
353 pool->blocks = block->next;
357 _dbus_assert (pool->allocated_elements > 0);
358 pool->allocated_elements -= 1;
360 if (pool->allocated_elements == 0)
361 _dbus_assert (pool->blocks == NULL);
363 return pool->blocks == NULL;
369 _dbus_assert_not_reached ("freed nonexistent block");
374 DBusFreedElement *freed;
377 freed->next = pool->free_elements;
378 pool->free_elements = freed;
380 _dbus_assert (pool->allocated_elements > 0);
381 pool->allocated_elements -= 1;
383 return pool->allocated_elements == 0;
389 #ifdef DBUS_BUILD_TESTS
390 #include "dbus-test.h"
395 time_for_size (int size)
401 #define FREE_ARRAY_SIZE 512
402 #define N_ITERATIONS FREE_ARRAY_SIZE * 512
403 void *to_free[FREE_ARRAY_SIZE];
406 _dbus_verbose ("Timings for size %d\n", size);
408 _dbus_verbose (" malloc\n");
414 while (i < N_ITERATIONS)
416 to_free[j] = dbus_malloc (size);
417 _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */
421 if (j == FREE_ARRAY_SIZE)
424 while (j < FREE_ARRAY_SIZE)
426 dbus_free (to_free[j]);
438 _dbus_verbose (" created/destroyed %d elements in %g seconds\n",
439 N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC);
443 _dbus_verbose (" mempools\n");
447 pool = _dbus_mem_pool_new (size, FALSE);
451 while (i < N_ITERATIONS)
453 to_free[j] = _dbus_mem_pool_alloc (pool);
454 _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */
458 if (j == FREE_ARRAY_SIZE)
461 while (j < FREE_ARRAY_SIZE)
463 _dbus_mem_pool_dealloc (pool, to_free[j]);
473 _dbus_mem_pool_free (pool);
477 _dbus_verbose (" created/destroyed %d elements in %g seconds\n",
478 N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC);
480 _dbus_verbose (" zeroed malloc\n");
486 while (i < N_ITERATIONS)
488 to_free[j] = dbus_malloc0 (size);
489 _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */
493 if (j == FREE_ARRAY_SIZE)
496 while (j < FREE_ARRAY_SIZE)
498 dbus_free (to_free[j]);
510 _dbus_verbose (" created/destroyed %d elements in %g seconds\n",
511 N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC);
513 _dbus_verbose (" zeroed mempools\n");
517 pool = _dbus_mem_pool_new (size, TRUE);
521 while (i < N_ITERATIONS)
523 to_free[j] = _dbus_mem_pool_alloc (pool);
524 _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */
528 if (j == FREE_ARRAY_SIZE)
531 while (j < FREE_ARRAY_SIZE)
533 _dbus_mem_pool_dealloc (pool, to_free[j]);
543 _dbus_mem_pool_free (pool);
547 _dbus_verbose (" created/destroyed %d elements in %g seconds\n",
548 N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC);
552 * @ingroup DBusMemPoolInternals
553 * Unit test for DBusMemPool
554 * @returns #TRUE on success.
557 _dbus_mem_pool_test (void)
560 int element_sizes[] = { 4, 8, 16, 50, 124 };
563 while (i < _DBUS_N_ELEMENTS (element_sizes))
565 time_for_size (element_sizes[i]);
572 #endif /* DBUS_BUILD_TESTS */