1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dbus-dataslot.h"
26 #include "dbus-threads-internal.h"
29 * @defgroup DBusDataSlot Data slots
30 * @ingroup DBusInternals
31 * @brief Storing data by ID
33 * Types and functions related to storing data by an
34 * allocated ID. This is used for dbus_connection_set_data(),
35 * dbus_server_set_data(), etc.
40 * Initializes a data slot allocator object, used to assign
41 * integer IDs for data slots.
43 * @param allocator the allocator to initialize
46 _dbus_data_slot_allocator_init (DBusDataSlotAllocator *allocator)
48 allocator->allocated_slots = NULL;
49 allocator->n_allocated_slots = 0;
50 allocator->n_used_slots = 0;
51 allocator->lock_loc = NULL;
57 * Allocates an integer ID to be used for storing data
58 * in a #DBusDataSlotList. If the value at *slot_id_p is
59 * not -1, this function just increments the refcount for
60 * the existing slot ID. If the value is -1, a new slot ID
61 * is allocated and stored at *slot_id_p.
63 * @param allocator the allocator
64 * @param mutex_loc the location lock for this allocator
65 * @param slot_id_p address to fill with the slot ID
66 * @returns #TRUE on success
69 _dbus_data_slot_allocator_alloc (DBusDataSlotAllocator *allocator,
70 DBusRMutex **mutex_loc,
71 dbus_int32_t *slot_id_p)
75 _dbus_rmutex_lock (*mutex_loc);
77 if (allocator->n_allocated_slots == 0)
79 _dbus_assert (allocator->lock_loc == NULL);
80 allocator->lock_loc = mutex_loc;
82 else if (allocator->lock_loc != mutex_loc)
84 _dbus_warn_check_failed ("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.\n");
85 _dbus_assert_not_reached ("exiting");
92 _dbus_assert (slot < allocator->n_allocated_slots);
93 _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
95 allocator->allocated_slots[slot].refcount += 1;
100 _dbus_assert (*slot_id_p < 0);
102 if (allocator->n_used_slots < allocator->n_allocated_slots)
105 while (slot < allocator->n_allocated_slots)
107 if (allocator->allocated_slots[slot].slot_id < 0)
109 allocator->allocated_slots[slot].slot_id = slot;
110 allocator->allocated_slots[slot].refcount = 1;
111 allocator->n_used_slots += 1;
117 _dbus_assert (slot < allocator->n_allocated_slots);
121 DBusAllocatedSlot *tmp;
124 tmp = dbus_realloc (allocator->allocated_slots,
125 sizeof (DBusAllocatedSlot) * (allocator->n_allocated_slots + 1));
129 allocator->allocated_slots = tmp;
130 slot = allocator->n_allocated_slots;
131 allocator->n_allocated_slots += 1;
132 allocator->n_used_slots += 1;
133 allocator->allocated_slots[slot].slot_id = slot;
134 allocator->allocated_slots[slot].refcount = 1;
137 _dbus_assert (slot >= 0);
138 _dbus_assert (slot < allocator->n_allocated_slots);
139 _dbus_assert (*slot_id_p < 0);
140 _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
141 _dbus_assert (allocator->allocated_slots[slot].refcount == 1);
145 _dbus_verbose ("Allocated slot %d on allocator %p total %d slots allocated %d used\n",
146 slot, allocator, allocator->n_allocated_slots, allocator->n_used_slots);
149 _dbus_rmutex_unlock (*(allocator->lock_loc));
154 * Deallocates an ID previously allocated with
155 * _dbus_data_slot_allocator_alloc(). Existing data stored on
156 * existing #DBusDataSlotList objects with this ID will be freed when the
157 * data list is finalized, but may not be retrieved (and may only be
158 * replaced if someone else reallocates the slot).
159 * The slot value is reset to -1 if this is the last unref.
161 * @param allocator the allocator
162 * @param slot_id_p address where we store the slot
165 _dbus_data_slot_allocator_free (DBusDataSlotAllocator *allocator,
166 dbus_int32_t *slot_id_p)
168 _dbus_rmutex_lock (*(allocator->lock_loc));
170 _dbus_assert (*slot_id_p < allocator->n_allocated_slots);
171 _dbus_assert (allocator->allocated_slots[*slot_id_p].slot_id == *slot_id_p);
172 _dbus_assert (allocator->allocated_slots[*slot_id_p].refcount > 0);
174 allocator->allocated_slots[*slot_id_p].refcount -= 1;
176 if (allocator->allocated_slots[*slot_id_p].refcount > 0)
178 _dbus_rmutex_unlock (*(allocator->lock_loc));
182 /* refcount is 0, free the slot */
183 _dbus_verbose ("Freeing slot %d on allocator %p total %d allocated %d used\n",
184 *slot_id_p, allocator, allocator->n_allocated_slots, allocator->n_used_slots);
186 allocator->allocated_slots[*slot_id_p].slot_id = -1;
189 allocator->n_used_slots -= 1;
191 if (allocator->n_used_slots == 0)
193 DBusRMutex **mutex_loc = allocator->lock_loc;
195 dbus_free (allocator->allocated_slots);
196 allocator->allocated_slots = NULL;
197 allocator->n_allocated_slots = 0;
198 allocator->lock_loc = NULL;
200 _dbus_rmutex_unlock (*mutex_loc);
204 _dbus_rmutex_unlock (*(allocator->lock_loc));
209 * Initializes a slot list.
210 * @param list the list to initialize.
213 _dbus_data_slot_list_init (DBusDataSlotList *list)
220 * Stores a pointer in the data slot list, along with an optional
221 * function to be used for freeing the data when the data is set
222 * again, or when the slot list is finalized. The slot number must
223 * have been allocated with _dbus_data_slot_allocator_alloc() for the
224 * same allocator passed in here. The same allocator has to be used
225 * with the slot list every time.
227 * @param allocator the allocator to use
228 * @param list the data slot list
229 * @param slot the slot number
230 * @param data the data to store
231 * @param free_data_func finalizer function for the data
232 * @param old_free_func free function for any previously-existing data
233 * @param old_data previously-existing data, should be freed with old_free_func
234 * @returns #TRUE if there was enough memory to store the data
237 _dbus_data_slot_list_set (DBusDataSlotAllocator *allocator,
238 DBusDataSlotList *list,
241 DBusFreeFunction free_data_func,
242 DBusFreeFunction *old_free_func,
245 #ifndef DBUS_DISABLE_ASSERT
246 /* We need to take the allocator lock here, because the allocator could
247 * be e.g. realloc()ing allocated_slots. We avoid doing this if asserts
248 * are disabled, since then the asserts are empty.
250 _dbus_rmutex_lock (*(allocator->lock_loc));
251 _dbus_assert (slot < allocator->n_allocated_slots);
252 _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
253 _dbus_rmutex_unlock (*(allocator->lock_loc));
256 if (slot >= list->n_slots)
261 tmp = dbus_realloc (list->slots,
262 sizeof (DBusDataSlot) * (slot + 1));
268 list->n_slots = slot + 1;
269 while (i < list->n_slots)
271 list->slots[i].data = NULL;
272 list->slots[i].free_data_func = NULL;
277 _dbus_assert (slot < list->n_slots);
279 *old_data = list->slots[slot].data;
280 *old_free_func = list->slots[slot].free_data_func;
282 list->slots[slot].data = data;
283 list->slots[slot].free_data_func = free_data_func;
289 * Retrieves data previously set with _dbus_data_slot_list_set_data().
290 * The slot must still be allocated (must not have been freed).
292 * @param allocator the allocator slot was allocated from
293 * @param list the data slot list
294 * @param slot the slot to get data from
295 * @returns the data, or #NULL if not found
298 _dbus_data_slot_list_get (DBusDataSlotAllocator *allocator,
299 DBusDataSlotList *list,
302 #ifndef DBUS_DISABLE_ASSERT
303 /* We need to take the allocator lock here, because the allocator could
304 * be e.g. realloc()ing allocated_slots. We avoid doing this if asserts
305 * are disabled, since then the asserts are empty.
307 _dbus_rmutex_lock (*(allocator->lock_loc));
308 _dbus_assert (slot >= 0);
309 _dbus_assert (slot < allocator->n_allocated_slots);
310 _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
311 _dbus_rmutex_unlock (*(allocator->lock_loc));
314 if (slot >= list->n_slots)
317 return list->slots[slot].data;
321 * Frees all data slots contained in the list, calling
322 * application-provided free functions if they exist.
324 * @param list the list to clear
327 _dbus_data_slot_list_clear (DBusDataSlotList *list)
332 while (i < list->n_slots)
334 if (list->slots[i].free_data_func)
335 (* list->slots[i].free_data_func) (list->slots[i].data);
336 list->slots[i].data = NULL;
337 list->slots[i].free_data_func = NULL;
343 * Frees the data slot list and all data slots contained
344 * in it, calling application-provided free functions
347 * @param list the list to free
350 _dbus_data_slot_list_free (DBusDataSlotList *list)
352 _dbus_data_slot_list_clear (list);
354 dbus_free (list->slots);
361 #ifdef DBUS_BUILD_TESTS
362 #include "dbus-test.h"
365 static int free_counter;
368 test_free_slot_data_func (void *data)
370 int i = _DBUS_POINTER_TO_INT (data);
372 _dbus_assert (free_counter == i);
377 * Test function for data slots
380 _dbus_data_slot_test (void)
382 DBusDataSlotAllocator allocator;
383 DBusDataSlotList list;
385 DBusFreeFunction old_free_func;
389 if (!_dbus_data_slot_allocator_init (&allocator))
390 _dbus_assert_not_reached ("no memory for allocator");
392 _dbus_data_slot_list_init (&list);
394 _dbus_rmutex_new_at_location (&mutex);
396 _dbus_assert_not_reached ("failed to alloc mutex");
403 /* we don't really want apps to rely on this ordered
404 * allocation, but it simplifies things to rely on it
407 dbus_int32_t tmp = -1;
409 _dbus_data_slot_allocator_alloc (&allocator, &mutex, &tmp);
412 _dbus_assert_not_reached ("did not allocate slots in numeric order\n");
420 if (!_dbus_data_slot_list_set (&allocator, &list,
422 _DBUS_INT_TO_POINTER (i),
423 test_free_slot_data_func,
424 &old_free_func, &old_data))
425 _dbus_assert_not_reached ("no memory to set data");
427 _dbus_assert (old_free_func == NULL);
428 _dbus_assert (old_data == NULL);
430 _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
431 _DBUS_INT_TO_POINTER (i));
440 if (!_dbus_data_slot_list_set (&allocator, &list,
442 _DBUS_INT_TO_POINTER (i),
443 test_free_slot_data_func,
444 &old_free_func, &old_data))
445 _dbus_assert_not_reached ("no memory to set data");
447 _dbus_assert (old_free_func == test_free_slot_data_func);
448 _dbus_assert (_DBUS_POINTER_TO_INT (old_data) == i);
450 (* old_free_func) (old_data);
451 _dbus_assert (i == (free_counter - 1));
453 _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
454 _DBUS_INT_TO_POINTER (i));
460 _dbus_data_slot_list_free (&list);
462 _dbus_assert (N_SLOTS == free_counter);
467 dbus_int32_t tmp = i;
469 _dbus_data_slot_allocator_free (&allocator, &tmp);
470 _dbus_assert (tmp == -1);
474 _dbus_rmutex_free_at_location (&mutex);
479 #endif /* DBUS_BUILD_TESTS */