dbus-dataslot.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-dataslot.c storing data on objects
*
* Copyright (C) 2003 Red Hat, Inc.
*
* 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-dataslot.h"
#include "dbus-threads-internal.h"
#include <dbus/dbus-test-tap.h>
dbus_bool_t
_dbus_data_slot_allocator_init (DBusDataSlotAllocator *allocator,
DBusGlobalLock lock)
{
allocator->allocated_slots = NULL;
allocator->n_allocated_slots = 0;
allocator->n_used_slots = 0;
allocator->lock = lock;
return TRUE;
}
dbus_bool_t
_dbus_data_slot_allocator_alloc (DBusDataSlotAllocator *allocator,
dbus_int32_t *slot_id_p)
{
dbus_int32_t slot;
if (!_dbus_lock (allocator->lock))
return FALSE;
if (*slot_id_p >= 0)
{
slot = *slot_id_p;
_dbus_assert (slot < allocator->n_allocated_slots);
_dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
allocator->allocated_slots[slot].refcount += 1;
goto out;
}
_dbus_assert (*slot_id_p < 0);
if (allocator->n_used_slots < allocator->n_allocated_slots)
{
slot = 0;
while (slot < allocator->n_allocated_slots)
{
if (allocator->allocated_slots[slot].slot_id < 0)
{
allocator->allocated_slots[slot].slot_id = slot;
allocator->allocated_slots[slot].refcount = 1;
allocator->n_used_slots += 1;
break;
}
++slot;
}
_dbus_assert (slot < allocator->n_allocated_slots);
}
else
{
DBusAllocatedSlot *tmp;
slot = -1;
tmp = dbus_realloc (allocator->allocated_slots,
sizeof (DBusAllocatedSlot) * (allocator->n_allocated_slots + 1));
if (tmp == NULL)
goto out;
allocator->allocated_slots = tmp;
slot = allocator->n_allocated_slots;
allocator->n_allocated_slots += 1;
allocator->n_used_slots += 1;
allocator->allocated_slots[slot].slot_id = slot;
allocator->allocated_slots[slot].refcount = 1;
}
_dbus_assert (slot >= 0);
_dbus_assert (slot < allocator->n_allocated_slots);
_dbus_assert (*slot_id_p < 0);
_dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
_dbus_assert (allocator->allocated_slots[slot].refcount == 1);
*slot_id_p = slot;
_dbus_verbose ("Allocated slot %d on allocator %p total %d slots allocated %d used\n",
slot, allocator, allocator->n_allocated_slots, allocator->n_used_slots);
out:
_dbus_unlock (allocator->lock);
return slot >= 0;
}
void
_dbus_data_slot_allocator_free (DBusDataSlotAllocator *allocator,
dbus_int32_t *slot_id_p)
{
if (!_dbus_lock (allocator->lock))
_dbus_assert_not_reached ("we should have initialized global locks "
"before we allocated this slot");
_dbus_assert (*slot_id_p < allocator->n_allocated_slots);
_dbus_assert (allocator->allocated_slots[*slot_id_p].slot_id == *slot_id_p);
_dbus_assert (allocator->allocated_slots[*slot_id_p].refcount > 0);
allocator->allocated_slots[*slot_id_p].refcount -= 1;
if (allocator->allocated_slots[*slot_id_p].refcount > 0)
{
_dbus_unlock (allocator->lock);
return;
}
/* refcount is 0, free the slot */
_dbus_verbose ("Freeing slot %d on allocator %p total %d allocated %d used\n",
*slot_id_p, allocator, allocator->n_allocated_slots, allocator->n_used_slots);
allocator->allocated_slots[*slot_id_p].slot_id = -1;
*slot_id_p = -1;
allocator->n_used_slots -= 1;
if (allocator->n_used_slots == 0)
{
dbus_free (allocator->allocated_slots);
allocator->allocated_slots = NULL;
allocator->n_allocated_slots = 0;
}
_dbus_unlock (allocator->lock);
}
void
_dbus_data_slot_list_init (DBusDataSlotList *list)
{
list->slots = NULL;
list->n_slots = 0;
}
dbus_bool_t
_dbus_data_slot_list_set (DBusDataSlotAllocator *allocator,
DBusDataSlotList *list,
int slot,
void *data,
DBusFreeFunction free_data_func,
DBusFreeFunction *old_free_func,
void **old_data)
{
#ifndef DBUS_DISABLE_ASSERT
/* We need to take the allocator lock here, because the allocator could
* be e.g. realloc()ing allocated_slots. We avoid doing this if asserts
* are disabled, since then the asserts are empty.
*/
if (!_dbus_lock (allocator->lock))
_dbus_assert_not_reached ("we should have initialized global locks "
"before we allocated this slot");
_dbus_assert (slot < allocator->n_allocated_slots);
_dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
_dbus_unlock (allocator->lock);
#endif
if (slot >= list->n_slots)
{
DBusDataSlot *tmp;
int i;
tmp = dbus_realloc (list->slots,
sizeof (DBusDataSlot) * (slot + 1));
if (tmp == NULL)
return FALSE;
list->slots = tmp;
i = list->n_slots;
list->n_slots = slot + 1;
while (i < list->n_slots)
{
list->slots[i].data = NULL;
list->slots[i].free_data_func = NULL;
++i;
}
}
_dbus_assert (slot < list->n_slots);
*old_data = list->slots[slot].data;
*old_free_func = list->slots[slot].free_data_func;
list->slots[slot].data = data;
list->slots[slot].free_data_func = free_data_func;
return TRUE;
}
void*
_dbus_data_slot_list_get (DBusDataSlotAllocator *allocator,
DBusDataSlotList *list,
int slot)
{
#ifndef DBUS_DISABLE_ASSERT
/* We need to take the allocator lock here, because the allocator could
* be e.g. realloc()ing allocated_slots. We avoid doing this if asserts
* are disabled, since then the asserts are empty.
*/
if (!_dbus_lock (allocator->lock))
_dbus_assert_not_reached ("we should have initialized global locks "
"before we allocated this slot");
_dbus_assert (slot >= 0);
_dbus_assert (slot < allocator->n_allocated_slots);
_dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
_dbus_unlock (allocator->lock);
#endif
if (slot >= list->n_slots)
return NULL;
else
return list->slots[slot].data;
}
void
_dbus_data_slot_list_clear (DBusDataSlotList *list)
{
int i;
i = 0;
while (i < list->n_slots)
{
if (list->slots[i].free_data_func)
(* list->slots[i].free_data_func) (list->slots[i].data);
list->slots[i].data = NULL;
list->slots[i].free_data_func = NULL;
++i;
}
}
void
_dbus_data_slot_list_free (DBusDataSlotList *list)
{
_dbus_data_slot_list_clear (list);
dbus_free (list->slots);
list->slots = NULL;
list->n_slots = 0;
}
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
#include "dbus-test.h"
#include <stdio.h>
/* Test-only, does not need to be thread-safe */
static int free_counter;
static void
test_free_slot_data_func (void *data)
{
int i = _DBUS_POINTER_TO_INT (data);
_dbus_assert (free_counter == i);
++free_counter;
}
dbus_bool_t
_dbus_data_slot_test (const char *test_data_dir _DBUS_GNUC_UNUSED)
{
DBusDataSlotAllocator allocator;
DBusDataSlotList list;
int i;
DBusFreeFunction old_free_func;
void *old_data;
if (!_dbus_data_slot_allocator_init (&allocator, _DBUS_LOCK_server_slots))
_dbus_test_fatal ("no memory for allocator");
_dbus_data_slot_list_init (&list);
#define N_SLOTS 100
i = 0;
while (i < N_SLOTS)
{
/* we don't really want apps to rely on this ordered
* allocation, but it simplifies things to rely on it
* here.
*/
dbus_int32_t tmp = -1;
_dbus_data_slot_allocator_alloc (&allocator, &tmp);
if (tmp != i)
_dbus_test_fatal ("did not allocate slots in numeric order");
++i;
}
i = 0;
while (i < N_SLOTS)
{
if (!_dbus_data_slot_list_set (&allocator, &list,
i,
_DBUS_INT_TO_POINTER (i),
test_free_slot_data_func,
&old_free_func, &old_data))
_dbus_test_fatal ("no memory to set data");
_dbus_assert (old_free_func == NULL);
_dbus_assert (old_data == NULL);
_dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
_DBUS_INT_TO_POINTER (i));
++i;
}
free_counter = 0;
i = 0;
while (i < N_SLOTS)
{
if (!_dbus_data_slot_list_set (&allocator, &list,
i,
_DBUS_INT_TO_POINTER (i),
test_free_slot_data_func,
&old_free_func, &old_data))
_dbus_test_fatal ("no memory to set data");
_dbus_assert (old_free_func == test_free_slot_data_func);
_dbus_assert (_DBUS_POINTER_TO_INT (old_data) == i);
(* old_free_func) (old_data);
_dbus_assert (i == (free_counter - 1));
_dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
_DBUS_INT_TO_POINTER (i));
++i;
}
free_counter = 0;
_dbus_data_slot_list_free (&list);
_dbus_assert (N_SLOTS == free_counter);
i = 0;
while (i < N_SLOTS)
{
dbus_int32_t tmp = i;
_dbus_data_slot_allocator_free (&allocator, &tmp);
_dbus_assert (tmp == -1);
++i;
}
return TRUE;
}
#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
_dbus_data_slot_allocator_free
void _dbus_data_slot_allocator_free(DBusDataSlotAllocator *allocator, dbus_int32_t *slot_id_p)
Deallocates an ID previously allocated with _dbus_data_slot_allocator_alloc().
Definition dbus-dataslot.c:157
_dbus_data_slot_list_clear
void _dbus_data_slot_list_clear(DBusDataSlotList *list)
Frees all data slots contained in the list, calling application-provided free functions if they exist...
Definition dbus-dataslot.c:320
_dbus_data_slot_allocator_init
dbus_bool_t _dbus_data_slot_allocator_init(DBusDataSlotAllocator *allocator, DBusGlobalLock lock)
Initializes a data slot allocator object, used to assign integer IDs for data slots.
Definition dbus-dataslot.c:49
_dbus_data_slot_list_init
void _dbus_data_slot_list_init(DBusDataSlotList *list)
Initializes a slot list.
Definition dbus-dataslot.c:200
_dbus_data_slot_list_free
void _dbus_data_slot_list_free(DBusDataSlotList *list)
Frees the data slot list and all data slots contained in it, calling application-provided free functi...
Definition dbus-dataslot.c:343
_dbus_data_slot_list_get
void * _dbus_data_slot_list_get(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot)
Retrieves data previously set with _dbus_data_slot_list_set_data().
Definition dbus-dataslot.c:288
_dbus_data_slot_list_set
dbus_bool_t _dbus_data_slot_list_set(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot, void *data, DBusFreeFunction free_data_func, DBusFreeFunction *old_free_func, void **old_data)
Stores a pointer in the data slot list, along with an optional function to be used for freeing the da...
Definition dbus-dataslot.c:224
_dbus_data_slot_allocator_alloc
dbus_bool_t _dbus_data_slot_allocator_alloc(DBusDataSlotAllocator *allocator, dbus_int32_t *slot_id_p)
Allocates an integer ID to be used for storing data in a DBusDataSlotList.
Definition dbus-dataslot.c:72
_DBUS_INT_TO_POINTER
#define _DBUS_INT_TO_POINTER(integer)
Safely stuffs an integer into a pointer, to be extracted later with _DBUS_POINTER_TO_INT.
Definition dbus-internals.h:192
_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_POINTER_TO_INT
#define _DBUS_POINTER_TO_INT(pointer)
Safely casts a void* to an integer; should only be used on void* that actually contain integers,...
Definition dbus-internals.h:191
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
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_realloc
void * dbus_realloc(void *memory, size_t bytes)
Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:610
DBusFreeFunction
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition dbus-memory.h:65
dbus_int32_t
int dbus_int32_t
A 32-bit signed integer on all platforms.
Definition dbus-arch-deps.h:44
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBusAllocatedSlot
An allocated slot for storing data.
Definition dbus-dataslot.h:49
DBusAllocatedSlot::refcount
int refcount
Number of uses of the slot.
Definition dbus-dataslot.h:51
DBusAllocatedSlot::slot_id
dbus_int32_t slot_id
ID of this slot.
Definition dbus-dataslot.h:50
DBusDataSlotAllocator
An allocator that tracks a set of slot IDs.
Definition dbus-dataslot.h:58
DBusDataSlotAllocator::allocated_slots
DBusAllocatedSlot * allocated_slots
Allocated slots.
Definition dbus-dataslot.h:59
DBusDataSlotAllocator::n_allocated_slots
int n_allocated_slots
number of slots malloc'd
Definition dbus-dataslot.h:60
DBusDataSlotAllocator::lock
DBusGlobalLock lock
index of thread lock
Definition dbus-dataslot.h:62
DBusDataSlotAllocator::n_used_slots
int n_used_slots
number of slots used
Definition dbus-dataslot.h:61
DBusDataSlotList
Data structure that stores the actual user data set at a given slot.
Definition dbus-dataslot.h:72
DBusDataSlotList::n_slots
int n_slots
Slots we have storage for in data_slots.
Definition dbus-dataslot.h:74
DBusDataSlotList::slots
DBusDataSlot * slots
Data slots.
Definition dbus-dataslot.h:73
DBusDataSlot
DBusDataSlot is used to store application data on the connection.
Definition dbus-dataslot.h:39
DBusDataSlot::data
void * data
The application data.
Definition dbus-dataslot.h:40
DBusDataSlot::free_data_func
DBusFreeFunction free_data_func
Free the application data.
Definition dbus-dataslot.h:41