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