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 1.2
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.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 = dbus_mutex_new ();
50 if (allocator->lock == NULL)
57 * Allocates an integer ID to be used for storing data
58 * in a #DBusDataSlotList.
60 * @param allocator the allocator
61 * @returns the integer ID, or -1 on failure
64 _dbus_data_slot_allocator_alloc (DBusDataSlotAllocator *allocator)
68 if (!dbus_mutex_lock (allocator->lock))
71 if (allocator->n_used_slots < allocator->n_allocated_slots)
74 while (slot < allocator->n_allocated_slots)
76 if (allocator->allocated_slots[slot] < 0)
78 allocator->allocated_slots[slot] = slot;
79 allocator->n_used_slots += 1;
85 _dbus_assert (slot < allocator->n_allocated_slots);
92 tmp = dbus_realloc (allocator->allocated_slots,
93 sizeof (int) * (allocator->n_allocated_slots + 1));
97 allocator->allocated_slots = tmp;
98 slot = allocator->n_allocated_slots;
99 allocator->n_allocated_slots += 1;
100 allocator->n_used_slots += 1;
101 allocator->allocated_slots[slot] = slot;
104 _dbus_assert (slot >= 0);
105 _dbus_assert (slot < allocator->n_allocated_slots);
108 dbus_mutex_unlock (allocator->lock);
113 * Deallocates an ID previously allocated with
114 * _dbus_data_slot_allocator_alloc(). Existing data stored on
115 * existing #DBusDataList objects with this ID will be freed when the
116 * data list is finalized, but may not be retrieved (and may only be
117 * replaced if someone else reallocates the slot).
119 * @param allocator the allocator
120 * @param slot the slot to deallocate
123 _dbus_data_slot_allocator_free (DBusDataSlotAllocator *allocator,
126 dbus_mutex_lock (allocator->lock);
128 _dbus_assert (slot < allocator->n_allocated_slots);
129 _dbus_assert (allocator->allocated_slots[slot] == slot);
131 allocator->allocated_slots[slot] = -1;
132 allocator->n_used_slots -= 1;
134 if (allocator->n_used_slots == 0)
136 dbus_free (allocator->allocated_slots);
137 allocator->allocated_slots = NULL;
138 allocator->n_allocated_slots = 0;
141 dbus_mutex_unlock (allocator->lock);
145 * Initializes a slot list.
146 * @param list the list to initialize.
149 _dbus_data_slot_list_init (DBusDataSlotList *list)
156 * Stores a pointer in the data slot list, along with an optional
157 * function to be used for freeing the data when the data is set
158 * again, or when the slot list is finalized. The slot number must
159 * have been allocated with _dbus_data_slot_allocator_alloc() for the
160 * same allocator passed in here. The same allocator has to be used
161 * with the slot list every time.
163 * @param allocator the allocator to use
164 * @param list the data slot list
165 * @param slot the slot number
166 * @param data the data to store
167 * @param free_data_func finalizer function for the data
168 * @param old_free_func free function for any previously-existing data
169 * @param old_data previously-existing data, should be freed with old_free_func
170 * @returns #TRUE if there was enough memory to store the data
173 _dbus_data_slot_list_set (DBusDataSlotAllocator *allocator,
174 DBusDataSlotList *list,
177 DBusFreeFunction free_data_func,
178 DBusFreeFunction *old_free_func,
181 #ifndef DBUS_DISABLE_ASSERT
182 /* We need to take the allocator lock here, because the allocator could
183 * be e.g. realloc()ing allocated_slots. We avoid doing this if asserts
184 * are disabled, since then the asserts are empty.
186 if (!dbus_mutex_lock (allocator->lock))
188 _dbus_assert (slot < allocator->n_allocated_slots);
189 _dbus_assert (allocator->allocated_slots[slot] == slot);
190 dbus_mutex_unlock (allocator->lock);
193 if (slot >= list->n_slots)
198 tmp = dbus_realloc (list->slots,
199 sizeof (DBusDataSlot) * (slot + 1));
205 list->n_slots = slot + 1;
206 while (i < list->n_slots)
208 list->slots[i].data = NULL;
209 list->slots[i].free_data_func = NULL;
214 _dbus_assert (slot < list->n_slots);
216 *old_data = list->slots[slot].data;
217 *old_free_func = list->slots[slot].free_data_func;
219 list->slots[slot].data = data;
220 list->slots[slot].free_data_func = free_data_func;
226 * Retrieves data previously set with _dbus_data_slot_list_set_data().
227 * The slot must still be allocated (must not have been freed).
229 * @param allocator the allocator slot was allocated from
230 * @param list the data slot list
231 * @param slot the slot to get data from
232 * @returns the data, or #NULL if not found
235 _dbus_data_slot_list_get (DBusDataSlotAllocator *allocator,
236 DBusDataSlotList *list,
239 #ifndef DBUS_DISABLE_ASSERT
240 /* We need to take the allocator lock here, because the allocator could
241 * be e.g. realloc()ing allocated_slots. We avoid doing this if asserts
242 * are disabled, since then the asserts are empty.
244 if (!dbus_mutex_lock (allocator->lock))
246 _dbus_assert (slot < allocator->n_allocated_slots);
247 _dbus_assert (allocator->allocated_slots[slot] == slot);
248 dbus_mutex_unlock (allocator->lock);
251 if (slot >= list->n_slots)
254 return list->slots[slot].data;
258 * Frees the data slot list and all data slots contained
259 * in it, calling application-provided free functions
262 * @param list the list to free
265 _dbus_data_slot_list_free (DBusDataSlotList *list)
270 while (i < list->n_slots)
272 if (list->slots[i].free_data_func)
273 (* list->slots[i].free_data_func) (list->slots[i].data);
274 list->slots[i].data = NULL;
275 list->slots[i].free_data_func = NULL;
279 dbus_free (list->slots);
286 #ifdef DBUS_BUILD_TESTS
287 #include "dbus-test.h"
290 static int free_counter;
293 test_free_slot_data_func (void *data)
295 int i = _DBUS_POINTER_TO_INT (data);
297 _dbus_assert (free_counter == i);
302 * Test function for data slots
305 _dbus_data_slot_test (void)
307 DBusDataSlotAllocator allocator;
308 DBusDataSlotList list;
310 DBusFreeFunction old_free_func;
313 if (!_dbus_data_slot_allocator_init (&allocator))
314 _dbus_assert_not_reached ("no memory for allocator");
316 _dbus_data_slot_list_init (&list);
323 /* we don't really want apps to rely on this ordered
324 * allocation, but it simplifies things to rely on it
327 if (_dbus_data_slot_allocator_alloc (&allocator) != i)
328 _dbus_assert_not_reached ("did not allocate slots in numeric order\n");
336 if (!_dbus_data_slot_list_set (&allocator, &list,
338 _DBUS_INT_TO_POINTER (i),
339 test_free_slot_data_func,
340 &old_free_func, &old_data))
341 _dbus_assert_not_reached ("no memory to set data");
343 _dbus_assert (old_free_func == NULL);
344 _dbus_assert (old_data == NULL);
346 _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
347 _DBUS_INT_TO_POINTER (i));
356 if (!_dbus_data_slot_list_set (&allocator, &list,
358 _DBUS_INT_TO_POINTER (i),
359 test_free_slot_data_func,
360 &old_free_func, &old_data))
361 _dbus_assert_not_reached ("no memory to set data");
363 _dbus_assert (old_free_func == test_free_slot_data_func);
364 _dbus_assert (_DBUS_POINTER_TO_INT (old_data) == i);
366 (* old_free_func) (old_data);
367 _dbus_assert (i == (free_counter - 1));
369 _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
370 _DBUS_INT_TO_POINTER (i));
376 _dbus_data_slot_list_free (&list);
378 _dbus_assert (N_SLOTS == free_counter);
383 _dbus_data_slot_allocator_free (&allocator, i);
390 #endif /* DBUS_BUILD_TESTS */