D-Bus: dbus-mempool.c Source File

D-Bus 1.13.18

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
56 typedef struct DBusFreedElement DBusFreedElement;
57
63 struct DBusFreedElement
64 {
65 DBusFreedElement *next;
66 };
67
72 #define ELEMENT_PADDING 4
73
78 typedef struct DBusMemBlock DBusMemBlock;
79
84 struct DBusMemBlock
85 {
86 DBusMemBlock *next;
91 /* this is a long so that "elements" is aligned */
92 long used_so_far;
94 unsigned char elements[ELEMENT_PADDING];
95 };
96
100 struct DBusMemPool
101 {
102 int element_size;
103 int block_size;
104 unsigned int zero_elements : 1;
106 DBusFreedElement *free_elements;
107 DBusMemBlock *blocks;
108 int allocated_elements;
109 };
110
139 DBusMemPool*
140 _dbus_mem_pool_new (int element_size,
141 dbus_bool_t zero_elements)
142 {
143 DBusMemPool *pool;
144
145 pool = dbus_new0 (DBusMemPool, 1);
146 if (pool == NULL)
147 return NULL;
148
149 /* Make the element size at least 8 bytes. */
150 if (element_size < 8)
151 element_size = 8;
152
153 /* these assertions are equivalent but the first is more clear
154 * to programmers that see it fail.
155 */
156 _dbus_assert (element_size >= (int) sizeof (void*));
157 _dbus_assert (element_size >= (int) sizeof (DBusFreedElement));
158
159 /* align the element size to a pointer boundary so we won't get bus
160 * errors under other architectures.
161 */
162 pool->element_size = _DBUS_ALIGN_VALUE (element_size, sizeof (void *));
163
164 pool->zero_elements = zero_elements != FALSE;
165
166 pool->allocated_elements = 0;
167
168 /* pick a size for the first block; it increases
169 * for each block we need to allocate. This is
170 * actually half the initial block size
171 * since _dbus_mem_pool_alloc() unconditionally
172 * doubles it prior to creating a new block. */
173 pool->block_size = pool->element_size * 8;
174
175 _dbus_assert ((pool->block_size %
176 pool->element_size) == 0);
177
178 VALGRIND_CREATE_MEMPOOL (pool, 0, zero_elements);
179
180 return pool;
181 }
182
188 void
189 _dbus_mem_pool_free (DBusMemPool *pool)
190 {
191 DBusMemBlock *block;
192
193 VALGRIND_DESTROY_MEMPOOL (pool);
194
195 block = pool->blocks;
196 while (block != NULL)
197 {
198 DBusMemBlock *next = block->next;
199
200 dbus_free (block);
201
202 block = next;
203 }
204
205 dbus_free (pool);
206 }
207
215 void*
216 _dbus_mem_pool_alloc (DBusMemPool *pool)
217 {
218 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
219 if (_dbus_disable_mem_pools ())
220 {
221 DBusMemBlock *block;
222 int alloc_size;
223
224 /* This is obviously really silly, but it's
225 * debug-mode-only code that is compiled out
226 * when tests are disabled (_dbus_disable_mem_pools()
227 * is a constant expression FALSE so this block
228 * should vanish)
229 */
230
231 alloc_size = sizeof (DBusMemBlock) - ELEMENT_PADDING +
232 pool->element_size;
233
234 if (pool->zero_elements)
235 block = dbus_malloc0 (alloc_size);
236 else
237 block = dbus_malloc (alloc_size);
238
239 if (block != NULL)
240 {
241 block->next = pool->blocks;
242 pool->blocks = block;
243 pool->allocated_elements += 1;
244
245 VALGRIND_MEMPOOL_ALLOC (pool, (void *) &block->elements[0],
246 pool->element_size);
247 return (void*) &block->elements[0];
248 }
249 else
250 return NULL;
251 }
252 else
253 #endif
254 {
255 if (_dbus_decrement_fail_alloc_counter ())
256 {
257 _dbus_verbose (" FAILING mempool alloc\n");
258 return NULL;
259 }
260 else if (pool->free_elements)
261 {
262 DBusFreedElement *element = pool->free_elements;
263
264 pool->free_elements = pool->free_elements->next;
265
266 VALGRIND_MEMPOOL_ALLOC (pool, element, pool->element_size);
267
268 if (pool->zero_elements)
269 memset (element, '\0', pool->element_size);
270
271 pool->allocated_elements += 1;
272
273 return element;
274 }
275 else
276 {
277 void *element;
278
279 if (pool->blocks == NULL ||
280 pool->blocks->used_so_far == pool->block_size)
281 {
282 /* Need a new block */
283 DBusMemBlock *block;
284 int alloc_size;
285 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
286 int saved_counter;
287 #endif
288
289 if (pool->block_size <= _DBUS_INT_MAX / 4) /* avoid overflow */
290 {
291 /* use a larger block size for our next block */
292 pool->block_size *= 2;
293 _dbus_assert ((pool->block_size %
294 pool->element_size) == 0);
295 }
296
297 alloc_size = sizeof (DBusMemBlock) - ELEMENT_PADDING + pool->block_size;
298
299 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
300 /* We save/restore the counter, so that memory pools won't
301 * cause a given function to have different number of
302 * allocations on different invocations. i.e. when testing
303 * we want consistent alloc patterns. So we skip our
304 * malloc here for purposes of failed alloc simulation.
305 */
306 saved_counter = _dbus_get_fail_alloc_counter ();
307 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
308 #endif
309
310 if (pool->zero_elements)
311 block = dbus_malloc0 (alloc_size);
312 else
313 block = dbus_malloc (alloc_size);
314
315 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
316 _dbus_set_fail_alloc_counter (saved_counter);
317 _dbus_assert (saved_counter == _dbus_get_fail_alloc_counter ());
318 #endif
319
320 if (block == NULL)
321 return NULL;
322
323 block->used_so_far = 0;
324 block->next = pool->blocks;
325 pool->blocks = block;
326 }
327
328 element = &pool->blocks->elements[pool->blocks->used_so_far];
329
330 pool->blocks->used_so_far += pool->element_size;
331
332 pool->allocated_elements += 1;
333
334 VALGRIND_MEMPOOL_ALLOC (pool, element, pool->element_size);
335 return element;
336 }
337 }
338 }
339
348 dbus_bool_t
349 _dbus_mem_pool_dealloc (DBusMemPool *pool,
350 void *element)
351 {
352 VALGRIND_MEMPOOL_FREE (pool, element);
353
354 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
355 if (_dbus_disable_mem_pools ())
356 {
357 DBusMemBlock *block;
358 DBusMemBlock *prev;
359
360 /* mmm, fast. ;-) debug-only code, so doesn't matter. */
361
362 prev = NULL;
363 block = pool->blocks;
364
365 while (block != NULL)
366 {
367 if (block->elements == (unsigned char*) element)
368 {
369 if (prev)
370 prev->next = block->next;
371 else
372 pool->blocks = block->next;
373
374 dbus_free (block);
375
376 _dbus_assert (pool->allocated_elements > 0);
377 pool->allocated_elements -= 1;
378
379 if (pool->allocated_elements == 0)
380 _dbus_assert (pool->blocks == NULL);
381
382 return pool->blocks == NULL;
383 }
384 prev = block;
385 block = block->next;
386 }
387
388 _dbus_assert_not_reached ("freed nonexistent block");
389 return FALSE;
390 }
391 else
392 #endif
393 {
394 DBusFreedElement *freed;
395
396 freed = element;
397 /* used for internal mempool administration */
398 VALGRIND_MAKE_MEM_UNDEFINED (freed, sizeof (*freed));
399
400 freed->next = pool->free_elements;
401 pool->free_elements = freed;
402
403 _dbus_assert (pool->allocated_elements > 0);
404 pool->allocated_elements -= 1;
405
406 return pool->allocated_elements == 0;
407 }
408 }
409
410 #ifdef DBUS_ENABLE_STATS
411 void
412 _dbus_mem_pool_get_stats (DBusMemPool *pool,
413 dbus_uint32_t *in_use_p,
414 dbus_uint32_t *in_free_list_p,
415 dbus_uint32_t *allocated_p)
416 {
417 DBusMemBlock *block;
418 DBusFreedElement *freed;
419 dbus_uint32_t in_use = 0;
420 dbus_uint32_t in_free_list = 0;
421 dbus_uint32_t allocated = 0;
422
423 if (pool != NULL)
424 {
425 in_use = pool->element_size * pool->allocated_elements;
426
427 for (freed = pool->free_elements; freed != NULL; freed = freed->next)
428 {
429 in_free_list += pool->element_size;
430 }
431
432 for (block = pool->blocks; block != NULL; block = block->next)
433 {
434 if (block == pool->blocks)
435 allocated += pool->block_size;
436 else
437 allocated += block->used_so_far;
438 }
439 }
440
441 if (in_use_p != NULL)
442 *in_use_p = in_use;
443
444 if (in_free_list_p != NULL)
445 *in_free_list_p = in_free_list;
446
447 if (allocated_p != NULL)
448 *allocated_p = allocated;
449 }
450 #endif /* DBUS_ENABLE_STATS */
451
DBusMemBlock
DBusMemBlock object represents a single malloc()-returned block that gets chunked up into objects in ...
Definition: dbus-mempool.c:85
_dbus_mem_pool_new
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:140
DBusMemPool::zero_elements
unsigned int zero_elements
whether to zero-init allocated elements
Definition: dbus-mempool.c:104
DBusMemPool::free_elements
DBusFreedElement * free_elements
a free list of elements to recycle
Definition: dbus-mempool.c:106
_DBUS_INT_MAX
#define _DBUS_INT_MAX
Definition: dbus-internals.h:302
DBusMemPool
Internals fields of DBusMemPool.
Definition: dbus-mempool.c:101
DBusMemPool::element_size
int element_size
size of a single object in the pool
Definition: dbus-mempool.c:102
DBusMemBlock
struct DBusMemBlock DBusMemBlock
Typedef for DBusMemBlock so the struct can recursively point to itself.
Definition: dbus-mempool.c:78
ELEMENT_PADDING
#define ELEMENT_PADDING
The dummy size of the variable-length "elements" field in DBusMemBlock.
Definition: dbus-mempool.c:72
dbus_malloc0
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:534
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:704
dbus_malloc
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:464
dbus_uint32_t
unsigned int dbus_uint32_t
Definition: dbus-arch-deps.h:43
DBusMemPool::allocated_elements
int allocated_elements
Count of outstanding allocated elements.
Definition: dbus-mempool.c:108
FALSE
#define FALSE
_dbus_mem_pool_dealloc
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:349
_dbus_mem_pool_free
void _dbus_mem_pool_free(DBusMemPool *pool)
Frees a memory pool (and all elements allocated from it).
Definition: dbus-mempool.c:189
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Definition: dbus-internals.h:164
DBusFreedElement::next
DBusFreedElement * next
next element of the free list
Definition: dbus-mempool.c:65
DBusMemBlock::elements
unsigned char elements[ELEMENT_PADDING]
the block data, actually allocated to required size
Definition: dbus-mempool.c:94
_dbus_assert
#define _dbus_assert(condition)
Definition: dbus-internals.h:153
DBusFreedElement
struct representing an element on the free list.
Definition: dbus-mempool.c:64
DBusMemPool::block_size
int block_size
size of most recently allocated block
Definition: dbus-mempool.c:103
DBusMemPool::blocks
DBusMemBlock * blocks
blocks of memory from malloc()
Definition: dbus-mempool.c:107
_dbus_mem_pool_alloc
void * _dbus_mem_pool_alloc(DBusMemPool *pool)
Allocates an object from the memory pool.
Definition: dbus-mempool.c:216
DBusMemBlock::next
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:86
dbus_new0
#define dbus_new0(type, count)
Definition: dbus-memory.h:58
dbus_bool_t
dbus_uint32_t dbus_bool_t
Definition: dbus-types.h:35
NULL
#define NULL
DBusMemBlock::used_so_far
long used_so_far
bytes of this block already allocated as elements.
Definition: dbus-mempool.c:92