dbus-mempool.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-mempool.h Memory pools
*
* Copyright (C) 2002, 2003 Red Hat, Inc.
* Copyright (C) 2003 CodeFactory AB
* Copyright (C) 2011-2012 Collabora Ltd.
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include "dbus-mempool.h"
#include "dbus-internals.h"
#include "dbus-valgrind-internal.h"
typedef struct DBusFreedElement DBusFreedElement;
struct DBusFreedElement
{
DBusFreedElement *next;
};
typedef struct DBusMemBlock DBusMemBlock;
struct DBusMemBlock
{
DBusMemBlock *next;
size_t used_so_far;
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
/*
* Ensure that elements is aligned correctly. For all supported pre-C11
* targets, the size_t above should ensure that the elements array is
* sufficiently aligned (this is checked in the static assert below).
*/
_Alignas (dbus_max_align_t)
#endif
unsigned char elements[];
};
_DBUS_STATIC_ASSERT (_DBUS_IS_ALIGNED (sizeof (struct DBusMemBlock),
_DBUS_ALIGNOF (dbus_max_align_t)));
_DBUS_STATIC_ASSERT (_DBUS_IS_ALIGNED (offsetof (struct DBusMemBlock,
elements),
_DBUS_ALIGNOF (dbus_max_align_t)));
struct DBusMemPool
{
size_t element_size;
size_t block_size;
unsigned int zero_elements : 1;
DBusFreedElement *free_elements;
DBusMemBlock *blocks;
int allocated_elements;
};
DBusMemPool*
_dbus_mem_pool_new (int element_size,
dbus_bool_t zero_elements)
{
DBusMemPool *pool;
pool = dbus_new0 (DBusMemPool, 1);
if (pool == NULL)
return NULL;
/* Make the element size at least 8 bytes. */
if (element_size < 8)
element_size = 8;
if (element_size < (int) sizeof (void *))
element_size = sizeof (void *);
/* these assertions are equivalent but the first is more clear
* to programmers that see it fail.
*/
_dbus_assert (element_size >= (int) sizeof (void*));
_dbus_assert (element_size >= (int) sizeof (DBusFreedElement));
/* align the element size to be suitable for the most-aligned type
* that we care about (in practice usually a pointer).
*/
pool->element_size =
_DBUS_ALIGN_VALUE (element_size, _DBUS_ALIGNOF (dbus_max_align_t));
pool->zero_elements = zero_elements != FALSE;
pool->allocated_elements = 0;
/* pick a size for the first block; it increases
* for each block we need to allocate. This is
* actually half the initial block size
* since _dbus_mem_pool_alloc() unconditionally
* doubles it prior to creating a new block. */
pool->block_size = pool->element_size * 8;
_dbus_assert ((pool->block_size %
pool->element_size) == 0);
VALGRIND_CREATE_MEMPOOL (pool, 0, zero_elements);
return pool;
}
void
_dbus_mem_pool_free (DBusMemPool *pool)
{
DBusMemBlock *block;
VALGRIND_DESTROY_MEMPOOL (pool);
block = pool->blocks;
while (block != NULL)
{
DBusMemBlock *next = block->next;
dbus_free (block);
block = next;
}
dbus_free (pool);
}
void*
_dbus_mem_pool_alloc (DBusMemPool *pool)
{
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
if (_dbus_disable_mem_pools ())
{
DBusMemBlock *block;
size_t alloc_size;
/* This is obviously really silly, but it's
* debug-mode-only code that is compiled out
* when tests are disabled (_dbus_disable_mem_pools()
* is a constant expression FALSE so this block
* should vanish)
*/
alloc_size = sizeof (DBusMemBlock) + pool->element_size;
if (pool->zero_elements)
block = dbus_malloc0 (alloc_size);
else
block = dbus_malloc (alloc_size);
if (block != NULL)
{
block->next = pool->blocks;
pool->blocks = block;
pool->allocated_elements += 1;
VALGRIND_MEMPOOL_ALLOC (pool, (void *) &block->elements[0],
pool->element_size);
_dbus_assert (_DBUS_IS_ALIGNED (&block->elements[0],
_DBUS_ALIGNOF (dbus_max_align_t)));
return (void*) &block->elements[0];
}
else
return NULL;
}
else
#endif
{
if (_dbus_decrement_fail_alloc_counter ())
{
_dbus_verbose (" FAILING mempool alloc\n");
return NULL;
}
else if (pool->free_elements)
{
DBusFreedElement *element = pool->free_elements;
pool->free_elements = pool->free_elements->next;
VALGRIND_MEMPOOL_ALLOC (pool, element, pool->element_size);
if (pool->zero_elements)
memset (element, '\0', pool->element_size);
pool->allocated_elements += 1;
_dbus_assert (
_DBUS_IS_ALIGNED (element, _DBUS_ALIGNOF (dbus_max_align_t)));
return element;
}
else
{
void *element;
if (pool->blocks == NULL ||
pool->blocks->used_so_far == pool->block_size)
{
/* Need a new block */
DBusMemBlock *block;
size_t alloc_size;
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
int saved_counter;
#endif
if (pool->block_size <= _DBUS_INT_MAX / 4) /* avoid overflow */
{
/* use a larger block size for our next block */
pool->block_size *= 2;
_dbus_assert ((pool->block_size %
pool->element_size) == 0);
}
alloc_size = sizeof (DBusMemBlock) + pool->block_size;
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
/* We save/restore the counter, so that memory pools won't
* cause a given function to have different number of
* allocations on different invocations. i.e. when testing
* we want consistent alloc patterns. So we skip our
* malloc here for purposes of failed alloc simulation.
*/
saved_counter = _dbus_get_fail_alloc_counter ();
_dbus_set_fail_alloc_counter (-1);
#endif
if (pool->zero_elements)
block = dbus_malloc0 (alloc_size);
else
block = dbus_malloc (alloc_size);
_dbus_assert (
_DBUS_IS_ALIGNED (block, _DBUS_ALIGNOF (dbus_max_align_t)));
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
_dbus_set_fail_alloc_counter (saved_counter);
_dbus_assert (saved_counter == _dbus_get_fail_alloc_counter ());
#endif
if (block == NULL)
return NULL;
block->used_so_far = 0;
block->next = pool->blocks;
pool->blocks = block;
}
element = &pool->blocks->elements[pool->blocks->used_so_far];
pool->blocks->used_so_far += pool->element_size;
pool->allocated_elements += 1;
VALGRIND_MEMPOOL_ALLOC (pool, element, pool->element_size);
_dbus_assert (
_DBUS_IS_ALIGNED (element, _DBUS_ALIGNOF (dbus_max_align_t)));
return element;
}
}
}
dbus_bool_t
_dbus_mem_pool_dealloc (DBusMemPool *pool,
void *element)
{
VALGRIND_MEMPOOL_FREE (pool, element);
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
if (_dbus_disable_mem_pools ())
{
DBusMemBlock *block;
DBusMemBlock *prev;
/* mmm, fast. ;-) debug-only code, so doesn't matter. */
prev = NULL;
block = pool->blocks;
while (block != NULL)
{
if (block->elements == (unsigned char*) element)
{
if (prev)
prev->next = block->next;
else
pool->blocks = block->next;
dbus_free (block);
_dbus_assert (pool->allocated_elements > 0);
pool->allocated_elements -= 1;
if (pool->allocated_elements == 0)
_dbus_assert (pool->blocks == NULL);
return pool->blocks == NULL;
}
prev = block;
block = block->next;
}
_dbus_assert_not_reached ("freed nonexistent block");
return FALSE;
}
else
#endif
{
DBusFreedElement *freed;
freed = element;
/* used for internal mempool administration */
VALGRIND_MAKE_MEM_UNDEFINED (freed, sizeof (*freed));
freed->next = pool->free_elements;
pool->free_elements = freed;
_dbus_assert (pool->allocated_elements > 0);
pool->allocated_elements -= 1;
return pool->allocated_elements == 0;
}
}
#ifdef DBUS_ENABLE_STATS
void
_dbus_mem_pool_get_stats (DBusMemPool *pool,
dbus_uint32_t *in_use_p,
dbus_uint32_t *in_free_list_p,
dbus_uint32_t *allocated_p)
{
DBusMemBlock *block;
DBusFreedElement *freed;
dbus_uint32_t in_use = 0;
dbus_uint32_t in_free_list = 0;
dbus_uint32_t allocated = 0;
if (pool != NULL)
{
in_use = pool->element_size * pool->allocated_elements;
for (freed = pool->free_elements; freed != NULL; freed = freed->next)
{
in_free_list += pool->element_size;
}
for (block = pool->blocks; block != NULL; block = block->next)
{
if (block == pool->blocks)
allocated += pool->block_size;
else
allocated += block->used_so_far;
}
}
if (in_use_p != NULL)
*in_use_p = in_use;
if (in_free_list_p != NULL)
*in_free_list_p = in_free_list;
if (allocated_p != NULL)
*allocated_p = allocated;
}
#endif /* DBUS_ENABLE_STATS */
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
Definition dbus-internals.h:164
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
_DBUS_INT_MAX
#define _DBUS_INT_MAX
Maximum value of type "int".
Definition dbus-internals.h:327
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
_dbus_mem_pool_alloc
void * _dbus_mem_pool_alloc(DBusMemPool *pool)
Allocates an object from the memory pool.
Definition dbus-mempool.c:227
_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:366
_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:200
_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:148
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:710
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:540
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:60
dbus_malloc
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition dbus-memory.c:470
dbus_uint32_t
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
Definition dbus-arch-deps.h:45
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBusFreedElement
struct representing an element on the free list.
Definition dbus-mempool.c:66
DBusFreedElement::next
DBusFreedElement * next
next element of the free list
Definition dbus-mempool.c:67
DBusMemBlock
DBusMemBlock object represents a single malloc()-returned block that gets chunked up into objects in ...
Definition dbus-mempool.c:81
DBusMemBlock::used_so_far
size_t used_so_far
bytes of this block already allocated as elements.
Definition dbus-mempool.c:87
DBusMemBlock::elements
unsigned char elements[]
the block data, actually allocated to required size
Definition dbus-mempool.c:96
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:82
DBusMemPool
Internals fields of DBusMemPool.
Definition dbus-mempool.c:109
DBusMemPool::allocated_elements
int allocated_elements
Count of outstanding allocated elements.
Definition dbus-mempool.c:116
DBusMemPool::zero_elements
unsigned int zero_elements
whether to zero-init allocated elements
Definition dbus-mempool.c:112
DBusMemPool::block_size
size_t block_size
size of most recently allocated block
Definition dbus-mempool.c:111
DBusMemPool::blocks
DBusMemBlock * blocks
blocks of memory from malloc()
Definition dbus-mempool.c:115
DBusMemPool::element_size
size_t element_size
size of a single object in the pool
Definition dbus-mempool.c:110
DBusMemPool::free_elements
DBusFreedElement * free_elements
a free list of elements to recycle
Definition dbus-mempool.c:114