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 /* this is a long so that "elements" is aligned */
88 long used_so_far; /**< bytes of this block already allocated as elements. */
90 unsigned char elements[ELEMENT_PADDING]; /**< the block data, actually allocated to required size */
94 * Internals fields of DBusMemPool
98 int element_size; /**< size of a single object in the pool */
99 int block_size; /**< size of most recently allocated block */
100 unsigned int zero_elements : 1; /**< whether to zero-init allocated elements */
102 DBusFreedElement *free_elements; /**< a free list of elements to recycle */
103 DBusMemBlock *blocks; /**< blocks of memory from malloc() */
104 int allocated_elements; /**< Count of outstanding allocated elements */
110 * @addtogroup DBusMemPool
116 * @typedef DBusMemPool
118 * Opaque object representing a memory pool. Memory pools allow
119 * avoiding per-malloc-block memory overhead when allocating a lot of
120 * small objects that are all the same size. They are slightly
121 * faster than calling malloc() also.
125 * Creates a new memory pool, or returns #NULL on failure. Objects in
126 * the pool must be at least sizeof(void*) bytes each, due to the way
127 * memory pools work. To avoid creating 64 bit problems, this means at
128 * least 8 bytes on all platforms, unless you are 4 bytes on 32-bit
129 * and 8 bytes on 64-bit.
131 * @param element_size size of an element allocated from the pool.
132 * @param zero_elements whether to zero-initialize elements
133 * @returns the new pool or #NULL
136 _dbus_mem_pool_new (int element_size,
137 dbus_bool_t zero_elements)
141 pool = dbus_new0 (DBusMemPool, 1);
145 /* Make the element size at least 8 bytes. */
146 if (element_size < 8)
149 /* these assertions are equivalent but the first is more clear
150 * to programmers that see it fail.
152 _dbus_assert (element_size >= (int) sizeof (void*));
153 _dbus_assert (element_size >= (int) sizeof (DBusFreedElement));
155 /* align the element size to a pointer boundary so we won't get bus
156 * errors under other architectures.
158 pool->element_size = _DBUS_ALIGN_VALUE (element_size, sizeof (void *));
160 pool->zero_elements = zero_elements != FALSE;
162 pool->allocated_elements = 0;
164 /* pick a size for the first block; it increases
165 * for each block we need to allocate. This is
166 * actually half the initial block size
167 * since _dbus_mem_pool_alloc() unconditionally
168 * doubles it prior to creating a new block. */
169 pool->block_size = pool->element_size * 8;
171 _dbus_assert ((pool->block_size %
172 pool->element_size) == 0);
178 * Frees a memory pool (and all elements allocated from it).
180 * @param pool the memory pool.
183 _dbus_mem_pool_free (DBusMemPool *pool)
187 block = pool->blocks;
188 while (block != NULL)
190 DBusMemBlock *next = block->next;
201 * Allocates an object from the memory pool.
202 * The object must be freed with _dbus_mem_pool_dealloc().
204 * @param pool the memory pool
205 * @returns the allocated object or #NULL if no memory.
208 _dbus_mem_pool_alloc (DBusMemPool *pool)
210 if (_dbus_disable_mem_pools ())
215 /* This is obviously really silly, but it's
216 * debug-mode-only code that is compiled out
217 * when tests are disabled (_dbus_disable_mem_pools()
218 * is a constant expression FALSE so this block
222 alloc_size = sizeof (DBusMemBlock) - ELEMENT_PADDING +
225 if (pool->zero_elements)
226 block = dbus_malloc0 (alloc_size);
228 block = dbus_malloc (alloc_size);
232 block->next = pool->blocks;
233 pool->blocks = block;
234 pool->allocated_elements += 1;
236 return (void*) &block->elements[0];
243 if (_dbus_decrement_fail_alloc_counter ())
245 _dbus_verbose (" FAILING mempool alloc\n");
248 else if (pool->free_elements)
250 DBusFreedElement *element = pool->free_elements;
252 pool->free_elements = pool->free_elements->next;
254 if (pool->zero_elements)
255 memset (element, '\0', pool->element_size);
257 pool->allocated_elements += 1;
265 if (pool->blocks == NULL ||
266 pool->blocks->used_so_far == pool->block_size)
268 /* Need a new block */
271 #ifdef DBUS_BUILD_TESTS
275 if (pool->block_size <= _DBUS_INT_MAX / 4) /* avoid overflow */
277 /* use a larger block size for our next block */
278 pool->block_size *= 2;
279 _dbus_assert ((pool->block_size %
280 pool->element_size) == 0);
283 alloc_size = sizeof (DBusMemBlock) - ELEMENT_PADDING + pool->block_size;
285 #ifdef DBUS_BUILD_TESTS
286 /* We save/restore the counter, so that memory pools won't
287 * cause a given function to have different number of
288 * allocations on different invocations. i.e. when testing
289 * we want consistent alloc patterns. So we skip our
290 * malloc here for purposes of failed alloc simulation.
292 saved_counter = _dbus_get_fail_alloc_counter ();
293 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
296 if (pool->zero_elements)
297 block = dbus_malloc0 (alloc_size);
299 block = dbus_malloc (alloc_size);
301 #ifdef DBUS_BUILD_TESTS
302 _dbus_set_fail_alloc_counter (saved_counter);
303 _dbus_assert (saved_counter == _dbus_get_fail_alloc_counter ());
309 block->used_so_far = 0;
310 block->next = pool->blocks;
311 pool->blocks = block;
314 element = &pool->blocks->elements[pool->blocks->used_so_far];
316 pool->blocks->used_so_far += pool->element_size;
318 pool->allocated_elements += 1;
326 * Deallocates an object previously created with
327 * _dbus_mem_pool_alloc(). The previous object
328 * must have come from this same pool.
329 * @param pool the memory pool
330 * @param element the element earlier allocated.
331 * @returns #TRUE if there are no remaining allocated elements
334 _dbus_mem_pool_dealloc (DBusMemPool *pool,
337 if (_dbus_disable_mem_pools ())
342 /* mmm, fast. ;-) debug-only code, so doesn't matter. */
345 block = pool->blocks;
347 while (block != NULL)
349 if (block->elements == (unsigned char*) element)
352 prev->next = block->next;
354 pool->blocks = block->next;
358 _dbus_assert (pool->allocated_elements > 0);
359 pool->allocated_elements -= 1;
361 if (pool->allocated_elements == 0)
362 _dbus_assert (pool->blocks == NULL);
364 return pool->blocks == NULL;
370 _dbus_assert_not_reached ("freed nonexistent block");
375 DBusFreedElement *freed;
378 freed->next = pool->free_elements;
379 pool->free_elements = freed;
381 _dbus_assert (pool->allocated_elements > 0);
382 pool->allocated_elements -= 1;
384 return pool->allocated_elements == 0;
390 #ifdef DBUS_BUILD_TESTS
391 #include "dbus-test.h"
396 time_for_size (int size)
402 #define FREE_ARRAY_SIZE 512
403 #define N_ITERATIONS FREE_ARRAY_SIZE * 512
404 void *to_free[FREE_ARRAY_SIZE];
407 _dbus_verbose ("Timings for size %d\n", size);
409 _dbus_verbose (" malloc\n");
415 while (i < N_ITERATIONS)
417 to_free[j] = dbus_malloc (size);
418 _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */
422 if (j == FREE_ARRAY_SIZE)
425 while (j < FREE_ARRAY_SIZE)
427 dbus_free (to_free[j]);
439 _dbus_verbose (" created/destroyed %d elements in %g seconds\n",
440 N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC);
444 _dbus_verbose (" mempools\n");
448 pool = _dbus_mem_pool_new (size, FALSE);
452 while (i < N_ITERATIONS)
454 to_free[j] = _dbus_mem_pool_alloc (pool);
455 _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */
459 if (j == FREE_ARRAY_SIZE)
462 while (j < FREE_ARRAY_SIZE)
464 _dbus_mem_pool_dealloc (pool, to_free[j]);
474 _dbus_mem_pool_free (pool);
478 _dbus_verbose (" created/destroyed %d elements in %g seconds\n",
479 N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC);
481 _dbus_verbose (" zeroed malloc\n");
487 while (i < N_ITERATIONS)
489 to_free[j] = dbus_malloc0 (size);
490 _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */
494 if (j == FREE_ARRAY_SIZE)
497 while (j < FREE_ARRAY_SIZE)
499 dbus_free (to_free[j]);
511 _dbus_verbose (" created/destroyed %d elements in %g seconds\n",
512 N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC);
514 _dbus_verbose (" zeroed mempools\n");
518 pool = _dbus_mem_pool_new (size, TRUE);
522 while (i < N_ITERATIONS)
524 to_free[j] = _dbus_mem_pool_alloc (pool);
525 _dbus_assert (to_free[j] != NULL); /* in a real app of course this is wrong */
529 if (j == FREE_ARRAY_SIZE)
532 while (j < FREE_ARRAY_SIZE)
534 _dbus_mem_pool_dealloc (pool, to_free[j]);
544 _dbus_mem_pool_free (pool);
548 _dbus_verbose (" created/destroyed %d elements in %g seconds\n",
549 N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC);
553 * @ingroup DBusMemPoolInternals
554 * Unit test for DBusMemPool
555 * @returns #TRUE on success.
558 _dbus_mem_pool_test (void)
561 int element_sizes[] = { 4, 8, 16, 50, 124 };
564 while (i < _DBUS_N_ELEMENTS (element_sizes))
566 time_for_size (element_sizes[i]);
573 #endif /* DBUS_BUILD_TESTS */