1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-dataslot.c storing data on objects
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus-dataslot.h"
24 #include "dbus-threads-internal.h"
27 * @defgroup DBusDataSlot Data slots
28 * @ingroup DBusInternals
29 * @brief Storing data by ID
31 * Types and functions related to storing data by an
32 * allocated ID. This is used for dbus_connection_set_data(),
33 * dbus_server_set_data(), etc.
38 * Initializes a data slot allocator object, used to assign
39 * integer IDs for data slots.
41 * @param allocator the allocator to initialize
44 _dbus_data_slot_allocator_init (DBusDataSlotAllocator *allocator)
46 allocator->allocated_slots = NULL;
47 allocator->n_allocated_slots = 0;
48 allocator->n_used_slots = 0;
49 allocator->lock_loc = NULL;
55 * Allocates an integer ID to be used for storing data
56 * in a #DBusDataSlotList. If the value at *slot_id_p is
57 * not -1, this function just increments the refcount for
58 * the existing slot ID. If the value is -1, a new slot ID
59 * is allocated and stored at *slot_id_p.
61 * @param allocator the allocator
62 * @param mutex_loc the location lock for this allocator
63 * @param slot_id_p address to fill with the slot ID
64 * @returns #TRUE on success
67 _dbus_data_slot_allocator_alloc (DBusDataSlotAllocator *allocator,
68 DBusMutex **mutex_loc,
69 dbus_int32_t *slot_id_p)
73 _dbus_mutex_lock (*mutex_loc);
75 if (allocator->n_allocated_slots == 0)
77 _dbus_assert (allocator->lock_loc == NULL);
78 allocator->lock_loc = mutex_loc;
80 else if (allocator->lock_loc != mutex_loc)
82 _dbus_warn ("D-Bus threads were initialized after first using the D-Bus library. If your application does not directly initialize threads or use D-Bus, keep in mind that some library or plugin may have used D-Bus or initialized threads behind your back. You can often fix this problem by calling dbus_init_threads() or dbus_g_threads_init() early in your main() method, before D-Bus is used.");
83 _dbus_assert_not_reached ("exiting");
90 _dbus_assert (slot < allocator->n_allocated_slots);
91 _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
93 allocator->allocated_slots[slot].refcount += 1;
98 _dbus_assert (*slot_id_p < 0);
100 if (allocator->n_used_slots < allocator->n_allocated_slots)
103 while (slot < allocator->n_allocated_slots)
105 if (allocator->allocated_slots[slot].slot_id < 0)
107 allocator->allocated_slots[slot].slot_id = slot;
108 allocator->allocated_slots[slot].refcount = 1;
109 allocator->n_used_slots += 1;
115 _dbus_assert (slot < allocator->n_allocated_slots);
119 DBusAllocatedSlot *tmp;
122 tmp = dbus_realloc (allocator->allocated_slots,
123 sizeof (DBusAllocatedSlot) * (allocator->n_allocated_slots + 1));
127 allocator->allocated_slots = tmp;
128 slot = allocator->n_allocated_slots;
129 allocator->n_allocated_slots += 1;
130 allocator->n_used_slots += 1;
131 allocator->allocated_slots[slot].slot_id = slot;
132 allocator->allocated_slots[slot].refcount = 1;
135 _dbus_assert (slot >= 0);
136 _dbus_assert (slot < allocator->n_allocated_slots);
137 _dbus_assert (*slot_id_p < 0);
138 _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
139 _dbus_assert (allocator->allocated_slots[slot].refcount == 1);
143 _dbus_verbose ("Allocated slot %d on allocator %p total %d slots allocated %d used\n",
144 slot, allocator, allocator->n_allocated_slots, allocator->n_used_slots);
147 _dbus_mutex_unlock (*(allocator->lock_loc));
152 * Deallocates an ID previously allocated with
153 * _dbus_data_slot_allocator_alloc(). Existing data stored on
154 * existing #DBusDataList objects with this ID will be freed when the
155 * data list is finalized, but may not be retrieved (and may only be
156 * replaced if someone else reallocates the slot).
157 * The slot value is reset to -1 if this is the last unref.
159 * @param allocator the allocator
160 * @param slot_id_p address where we store the slot
163 _dbus_data_slot_allocator_free (DBusDataSlotAllocator *allocator,
164 dbus_int32_t *slot_id_p)
166 _dbus_mutex_lock (*(allocator->lock_loc));
168 _dbus_assert (*slot_id_p < allocator->n_allocated_slots);
169 _dbus_assert (allocator->allocated_slots[*slot_id_p].slot_id == *slot_id_p);
170 _dbus_assert (allocator->allocated_slots[*slot_id_p].refcount > 0);
172 allocator->allocated_slots[*slot_id_p].refcount -= 1;
174 if (allocator->allocated_slots[*slot_id_p].refcount > 0)
176 _dbus_mutex_unlock (*(allocator->lock_loc));
180 /* refcount is 0, free the slot */
181 _dbus_verbose ("Freeing slot %d on allocator %p total %d allocated %d used\n",
182 *slot_id_p, allocator, allocator->n_allocated_slots, allocator->n_used_slots);
184 allocator->allocated_slots[*slot_id_p].slot_id = -1;
187 allocator->n_used_slots -= 1;
189 if (allocator->n_used_slots == 0)
191 DBusMutex **mutex_loc = allocator->lock_loc;
193 dbus_free (allocator->allocated_slots);
194 allocator->allocated_slots = NULL;
195 allocator->n_allocated_slots = 0;
196 allocator->lock_loc = NULL;
198 _dbus_mutex_unlock (*mutex_loc);
202 _dbus_mutex_unlock (*(allocator->lock_loc));
207 * Initializes a slot list.
208 * @param list the list to initialize.
211 _dbus_data_slot_list_init (DBusDataSlotList *list)
218 * Stores a pointer in the data slot list, along with an optional
219 * function to be used for freeing the data when the data is set
220 * again, or when the slot list is finalized. The slot number must
221 * have been allocated with _dbus_data_slot_allocator_alloc() for the
222 * same allocator passed in here. The same allocator has to be used
223 * with the slot list every time.
225 * @param allocator the allocator to use
226 * @param list the data slot list
227 * @param slot the slot number
228 * @param data the data to store
229 * @param free_data_func finalizer function for the data
230 * @param old_free_func free function for any previously-existing data
231 * @param old_data previously-existing data, should be freed with old_free_func
232 * @returns #TRUE if there was enough memory to store the data
235 _dbus_data_slot_list_set (DBusDataSlotAllocator *allocator,
236 DBusDataSlotList *list,
239 DBusFreeFunction free_data_func,
240 DBusFreeFunction *old_free_func,
243 #ifndef DBUS_DISABLE_ASSERT
244 /* We need to take the allocator lock here, because the allocator could
245 * be e.g. realloc()ing allocated_slots. We avoid doing this if asserts
246 * are disabled, since then the asserts are empty.
248 _dbus_mutex_lock (*(allocator->lock_loc));
249 _dbus_assert (slot < allocator->n_allocated_slots);
250 _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
251 _dbus_mutex_unlock (*(allocator->lock_loc));
254 if (slot >= list->n_slots)
259 tmp = dbus_realloc (list->slots,
260 sizeof (DBusDataSlot) * (slot + 1));
266 list->n_slots = slot + 1;
267 while (i < list->n_slots)
269 list->slots[i].data = NULL;
270 list->slots[i].free_data_func = NULL;
275 _dbus_assert (slot < list->n_slots);
277 *old_data = list->slots[slot].data;
278 *old_free_func = list->slots[slot].free_data_func;
280 list->slots[slot].data = data;
281 list->slots[slot].free_data_func = free_data_func;
287 * Retrieves data previously set with _dbus_data_slot_list_set_data().
288 * The slot must still be allocated (must not have been freed).
290 * @param allocator the allocator slot was allocated from
291 * @param list the data slot list
292 * @param slot the slot to get data from
293 * @returns the data, or #NULL if not found
296 _dbus_data_slot_list_get (DBusDataSlotAllocator *allocator,
297 DBusDataSlotList *list,
300 #ifndef DBUS_DISABLE_ASSERT
301 /* We need to take the allocator lock here, because the allocator could
302 * be e.g. realloc()ing allocated_slots. We avoid doing this if asserts
303 * are disabled, since then the asserts are empty.
305 _dbus_mutex_lock (*(allocator->lock_loc));
306 _dbus_assert (slot >= 0);
307 _dbus_assert (slot < allocator->n_allocated_slots);
308 _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
309 _dbus_mutex_unlock (*(allocator->lock_loc));
312 if (slot >= list->n_slots)
315 return list->slots[slot].data;
319 * Frees all data slots contained in the list, calling
320 * application-provided free functions if they exist.
322 * @param list the list to clear
325 _dbus_data_slot_list_clear (DBusDataSlotList *list)
330 while (i < list->n_slots)
332 if (list->slots[i].free_data_func)
333 (* list->slots[i].free_data_func) (list->slots[i].data);
334 list->slots[i].data = NULL;
335 list->slots[i].free_data_func = NULL;
341 * Frees the data slot list and all data slots contained
342 * in it, calling application-provided free functions
345 * @param list the list to free
348 _dbus_data_slot_list_free (DBusDataSlotList *list)
350 _dbus_data_slot_list_clear (list);
352 dbus_free (list->slots);
359 #ifdef DBUS_BUILD_TESTS
360 #include "dbus-test.h"
363 static int free_counter;
366 test_free_slot_data_func (void *data)
368 int i = _DBUS_POINTER_TO_INT (data);
370 _dbus_assert (free_counter == i);
375 * Test function for data slots
378 _dbus_data_slot_test (void)
380 DBusDataSlotAllocator allocator;
381 DBusDataSlotList list;
383 DBusFreeFunction old_free_func;
387 if (!_dbus_data_slot_allocator_init (&allocator))
388 _dbus_assert_not_reached ("no memory for allocator");
390 _dbus_data_slot_list_init (&list);
392 _dbus_mutex_new_at_location (&mutex);
394 _dbus_assert_not_reached ("failed to alloc mutex");
401 /* we don't really want apps to rely on this ordered
402 * allocation, but it simplifies things to rely on it
405 dbus_int32_t tmp = -1;
407 _dbus_data_slot_allocator_alloc (&allocator, &mutex, &tmp);
410 _dbus_assert_not_reached ("did not allocate slots in numeric order\n");
418 if (!_dbus_data_slot_list_set (&allocator, &list,
420 _DBUS_INT_TO_POINTER (i),
421 test_free_slot_data_func,
422 &old_free_func, &old_data))
423 _dbus_assert_not_reached ("no memory to set data");
425 _dbus_assert (old_free_func == NULL);
426 _dbus_assert (old_data == NULL);
428 _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
429 _DBUS_INT_TO_POINTER (i));
438 if (!_dbus_data_slot_list_set (&allocator, &list,
440 _DBUS_INT_TO_POINTER (i),
441 test_free_slot_data_func,
442 &old_free_func, &old_data))
443 _dbus_assert_not_reached ("no memory to set data");
445 _dbus_assert (old_free_func == test_free_slot_data_func);
446 _dbus_assert (_DBUS_POINTER_TO_INT (old_data) == i);
448 (* old_free_func) (old_data);
449 _dbus_assert (i == (free_counter - 1));
451 _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
452 _DBUS_INT_TO_POINTER (i));
458 _dbus_data_slot_list_free (&list);
460 _dbus_assert (N_SLOTS == free_counter);
465 dbus_int32_t tmp = i;
467 _dbus_data_slot_allocator_free (&allocator, &tmp);
468 _dbus_assert (tmp == -1);
472 _dbus_mutex_free_at_location (&mutex);
477 #endif /* DBUS_BUILD_TESTS */