D-Bus 1.15.0
dbus-mempool.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-mempool.h Memory pools
3 *
4 * Copyright (C) 2002, 2003 Red Hat, Inc.
5 * Copyright (C) 2003 CodeFactory AB
6 * Copyright (C) 2011-2012 Collabora Ltd.
7 *
8 * Licensed under the Academic Free License version 2.1
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 */
25
26#include <config.h>
27#include "dbus-mempool.h"
28#include "dbus-internals.h"
29#include "dbus-valgrind-internal.h"
30
57
64{
66};
67
73
79{
85 size_t used_so_far;
86#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
87 /*
88 * Ensure that elements is aligned correctly. For all supported pre-C11
89 * targets, the size_t above should ensure that the elements array is
90 * sufficiently aligned (this is checked in the static assert below).
91 */
92 _Alignas (dbus_max_align_t)
93#endif
94 unsigned char elements[];
95};
96
97_DBUS_STATIC_ASSERT (_DBUS_IS_ALIGNED (sizeof (struct DBusMemBlock),
98 _DBUS_ALIGNOF (dbus_max_align_t)));
99_DBUS_STATIC_ASSERT (_DBUS_IS_ALIGNED (offsetof (struct DBusMemBlock,
100 elements),
101 _DBUS_ALIGNOF (dbus_max_align_t)));
102
107{
109 size_t block_size;
110 unsigned int zero_elements : 1;
115};
116
146_dbus_mem_pool_new (int element_size,
147 dbus_bool_t zero_elements)
148{
149 DBusMemPool *pool;
150
151 pool = dbus_new0 (DBusMemPool, 1);
152 if (pool == NULL)
153 return NULL;
154
155 /* Make the element size at least 8 bytes. */
156 if (element_size < 8)
157 element_size = 8;
158 if (element_size < (int) sizeof (void *))
159 element_size = sizeof (void *);
160
161 /* these assertions are equivalent but the first is more clear
162 * to programmers that see it fail.
163 */
164 _dbus_assert (element_size >= (int) sizeof (void*));
165 _dbus_assert (element_size >= (int) sizeof (DBusFreedElement));
166
167 /* align the element size to be suitable for the most-aligned type
168 * that we care about (in practice usually a pointer).
169 */
170 pool->element_size =
171 _DBUS_ALIGN_VALUE (element_size, _DBUS_ALIGNOF (dbus_max_align_t));
172
173 pool->zero_elements = zero_elements != FALSE;
174
175 pool->allocated_elements = 0;
176
177 /* pick a size for the first block; it increases
178 * for each block we need to allocate. This is
179 * actually half the initial block size
180 * since _dbus_mem_pool_alloc() unconditionally
181 * doubles it prior to creating a new block. */
182 pool->block_size = pool->element_size * 8;
183
184 _dbus_assert ((pool->block_size %
185 pool->element_size) == 0);
186
187 VALGRIND_CREATE_MEMPOOL (pool, 0, zero_elements);
188
189 return pool;
190}
191
197void
199{
200 DBusMemBlock *block;
201
202 VALGRIND_DESTROY_MEMPOOL (pool);
203
204 block = pool->blocks;
205 while (block != NULL)
206 {
207 DBusMemBlock *next = block->next;
208
209 dbus_free (block);
210
211 block = next;
212 }
213
214 dbus_free (pool);
215}
216
224void*
226{
227#ifdef DBUS_ENABLE_EMBEDDED_TESTS
228 if (_dbus_disable_mem_pools ())
229 {
230 DBusMemBlock *block;
231 size_t alloc_size;
232
233 /* This is obviously really silly, but it's
234 * debug-mode-only code that is compiled out
235 * when tests are disabled (_dbus_disable_mem_pools()
236 * is a constant expression FALSE so this block
237 * should vanish)
238 */
239
240 alloc_size = sizeof (DBusMemBlock) + pool->element_size;
241
242 if (pool->zero_elements)
243 block = dbus_malloc0 (alloc_size);
244 else
245 block = dbus_malloc (alloc_size);
246
247 if (block != NULL)
248 {
249 block->next = pool->blocks;
250 pool->blocks = block;
251 pool->allocated_elements += 1;
252
253 VALGRIND_MEMPOOL_ALLOC (pool, (void *) &block->elements[0],
254 pool->element_size);
255 _dbus_assert (_DBUS_IS_ALIGNED (&block->elements[0],
256 _DBUS_ALIGNOF (dbus_max_align_t)));
257 return (void*) &block->elements[0];
258 }
259 else
260 return NULL;
261 }
262 else
263#endif
264 {
265 if (_dbus_decrement_fail_alloc_counter ())
266 {
267 _dbus_verbose (" FAILING mempool alloc\n");
268 return NULL;
269 }
270 else if (pool->free_elements)
271 {
272 DBusFreedElement *element = pool->free_elements;
273
274 pool->free_elements = pool->free_elements->next;
275
276 VALGRIND_MEMPOOL_ALLOC (pool, element, pool->element_size);
277
278 if (pool->zero_elements)
279 memset (element, '\0', pool->element_size);
280
281 pool->allocated_elements += 1;
283 _DBUS_IS_ALIGNED (element, _DBUS_ALIGNOF (dbus_max_align_t)));
284 return element;
285 }
286 else
287 {
288 void *element;
289
290 if (pool->blocks == NULL ||
291 pool->blocks->used_so_far == pool->block_size)
292 {
293 /* Need a new block */
294 DBusMemBlock *block;
295 size_t alloc_size;
296#ifdef DBUS_ENABLE_EMBEDDED_TESTS
297 int saved_counter;
298#endif
299
300 if (pool->block_size <= _DBUS_INT_MAX / 4) /* avoid overflow */
301 {
302 /* use a larger block size for our next block */
303 pool->block_size *= 2;
304 _dbus_assert ((pool->block_size %
305 pool->element_size) == 0);
306 }
307
308 alloc_size = sizeof (DBusMemBlock) + pool->block_size;
309
310#ifdef DBUS_ENABLE_EMBEDDED_TESTS
311 /* We save/restore the counter, so that memory pools won't
312 * cause a given function to have different number of
313 * allocations on different invocations. i.e. when testing
314 * we want consistent alloc patterns. So we skip our
315 * malloc here for purposes of failed alloc simulation.
316 */
317 saved_counter = _dbus_get_fail_alloc_counter ();
318 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
319#endif
320
321 if (pool->zero_elements)
322 block = dbus_malloc0 (alloc_size);
323 else
324 block = dbus_malloc (alloc_size);
326 _DBUS_IS_ALIGNED (block, _DBUS_ALIGNOF (dbus_max_align_t)));
327
328#ifdef DBUS_ENABLE_EMBEDDED_TESTS
329 _dbus_set_fail_alloc_counter (saved_counter);
330 _dbus_assert (saved_counter == _dbus_get_fail_alloc_counter ());
331#endif
332
333 if (block == NULL)
334 return NULL;
335
336 block->used_so_far = 0;
337 block->next = pool->blocks;
338 pool->blocks = block;
339 }
340
341 element = &pool->blocks->elements[pool->blocks->used_so_far];
342
343 pool->blocks->used_so_far += pool->element_size;
344
345 pool->allocated_elements += 1;
346
347 VALGRIND_MEMPOOL_ALLOC (pool, element, pool->element_size);
349 _DBUS_IS_ALIGNED (element, _DBUS_ALIGNOF (dbus_max_align_t)));
350 return element;
351 }
352 }
353}
354
365 void *element)
366{
367 VALGRIND_MEMPOOL_FREE (pool, element);
368
369#ifdef DBUS_ENABLE_EMBEDDED_TESTS
370 if (_dbus_disable_mem_pools ())
371 {
372 DBusMemBlock *block;
373 DBusMemBlock *prev;
374
375 /* mmm, fast. ;-) debug-only code, so doesn't matter. */
376
377 prev = NULL;
378 block = pool->blocks;
379
380 while (block != NULL)
381 {
382 if (block->elements == (unsigned char*) element)
383 {
384 if (prev)
385 prev->next = block->next;
386 else
387 pool->blocks = block->next;
388
389 dbus_free (block);
390
392 pool->allocated_elements -= 1;
393
394 if (pool->allocated_elements == 0)
395 _dbus_assert (pool->blocks == NULL);
396
397 return pool->blocks == NULL;
398 }
399 prev = block;
400 block = block->next;
401 }
402
403 _dbus_assert_not_reached ("freed nonexistent block");
404 return FALSE;
405 }
406 else
407#endif
408 {
409 DBusFreedElement *freed;
410
411 freed = element;
412 /* used for internal mempool administration */
413 VALGRIND_MAKE_MEM_UNDEFINED (freed, sizeof (*freed));
414
415 freed->next = pool->free_elements;
416 pool->free_elements = freed;
417
419 pool->allocated_elements -= 1;
420
421 return pool->allocated_elements == 0;
422 }
423}
424
425#ifdef DBUS_ENABLE_STATS
426void
427_dbus_mem_pool_get_stats (DBusMemPool *pool,
428 dbus_uint32_t *in_use_p,
429 dbus_uint32_t *in_free_list_p,
430 dbus_uint32_t *allocated_p)
431{
432 DBusMemBlock *block;
433 DBusFreedElement *freed;
434 dbus_uint32_t in_use = 0;
435 dbus_uint32_t in_free_list = 0;
436 dbus_uint32_t allocated = 0;
437
438 if (pool != NULL)
439 {
440 in_use = pool->element_size * pool->allocated_elements;
441
442 for (freed = pool->free_elements; freed != NULL; freed = freed->next)
443 {
444 in_free_list += pool->element_size;
445 }
446
447 for (block = pool->blocks; block != NULL; block = block->next)
448 {
449 if (block == pool->blocks)
450 allocated += pool->block_size;
451 else
452 allocated += block->used_so_far;
453 }
454 }
455
456 if (in_use_p != NULL)
457 *in_use_p = in_use;
458
459 if (in_free_list_p != NULL)
460 *in_free_list_p = in_free_list;
461
462 if (allocated_p != NULL)
463 *allocated_p = allocated;
464}
465#endif /* DBUS_ENABLE_STATS */
466
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define _DBUS_INT_MAX
Maximum value of type "int".
#define NULL
A null pointer, defined appropriately for C or C++.
#define FALSE
Expands to "0".
struct DBusMemBlock DBusMemBlock
Typedef for DBusMemBlock so the struct can recursively point to itself.
Definition: dbus-mempool.c:72
void * _dbus_mem_pool_alloc(DBusMemPool *pool)
Allocates an object from the memory pool.
Definition: dbus-mempool.c:225
dbus_bool_t _dbus_mem_pool_dealloc(DBusMemPool *pool, void *element)
Deallocates an object previously created with _dbus_mem_pool_alloc().
Definition: dbus-mempool.c:364
void _dbus_mem_pool_free(DBusMemPool *pool)
Frees a memory pool (and all elements allocated from it).
Definition: dbus-mempool.c:198
DBusMemPool * _dbus_mem_pool_new(int element_size, dbus_bool_t zero_elements)
Creates a new memory pool, or returns NULL on failure.
Definition: dbus-mempool.c:146
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:692
void * dbus_malloc0(size_t bytes)
Allocates the given number of bytes, as with standard malloc(), but all bytes are initialized to zero...
Definition: dbus-memory.c:522
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:452
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
struct representing an element on the free list.
Definition: dbus-mempool.c:64
DBusFreedElement * next
next element of the free list
Definition: dbus-mempool.c:65
DBusMemBlock object represents a single malloc()-returned block that gets chunked up into objects in ...
Definition: dbus-mempool.c:79
size_t used_so_far
bytes of this block already allocated as elements.
Definition: dbus-mempool.c:85
unsigned char elements[]
the block data, actually allocated to required size
Definition: dbus-mempool.c:94
DBusMemBlock * next
next block in the list, which is already used up; only saved so we can free all the blocks when we fr...
Definition: dbus-mempool.c:80
Internals fields of DBusMemPool.
Definition: dbus-mempool.c:107
int allocated_elements
Count of outstanding allocated elements.
Definition: dbus-mempool.c:114
unsigned int zero_elements
whether to zero-init allocated elements
Definition: dbus-mempool.c:110
size_t block_size
size of most recently allocated block
Definition: dbus-mempool.c:109
DBusMemBlock * blocks
blocks of memory from malloc()
Definition: dbus-mempool.c:113
size_t element_size
size of a single object in the pool
Definition: dbus-mempool.c:108
DBusFreedElement * free_elements
a free list of elements to recycle
Definition: dbus-mempool.c:112
A simple value union that lets you access bytes as if they were various types; useful when dealing wi...
Definition: dbus-types.h:159