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 _dbus_assert (slot < allocator->n_allocated_slots);
182 _dbus_assert (allocator->allocated_slots[slot] == slot);
184 if (slot >= list->n_slots)
189 tmp = dbus_realloc (list->slots,
190 sizeof (DBusDataSlot) * (slot + 1));
196 list->n_slots = slot + 1;
197 while (i < list->n_slots)
199 list->slots[i].data = NULL;
200 list->slots[i].free_data_func = NULL;
205 _dbus_assert (slot < list->n_slots);
207 *old_data = list->slots[slot].data;
208 *old_free_func = list->slots[slot].free_data_func;
210 list->slots[slot].data = data;
211 list->slots[slot].free_data_func = free_data_func;
217 * Retrieves data previously set with _dbus_data_slot_list_set_data().
218 * The slot must still be allocated (must not have been freed).
220 * @param allocator the allocator slot was allocated from
221 * @param list the data slot list
222 * @param slot the slot to get data from
223 * @returns the data, or #NULL if not found
226 _dbus_data_slot_list_get (DBusDataSlotAllocator *allocator,
227 DBusDataSlotList *list,
230 _dbus_assert (slot < allocator->n_allocated_slots);
231 _dbus_assert (allocator->allocated_slots[slot] == slot);
233 if (slot >= list->n_slots)
236 return list->slots[slot].data;
240 * Frees the data slot list and all data slots contained
241 * in it, calling application-provided free functions
244 * @param list the list to free
247 _dbus_data_slot_list_free (DBusDataSlotList *list)
252 while (i < list->n_slots)
254 if (list->slots[i].free_data_func)
255 (* list->slots[i].free_data_func) (list->slots[i].data);
256 list->slots[i].data = NULL;
257 list->slots[i].free_data_func = NULL;
261 dbus_free (list->slots);
268 #ifdef DBUS_BUILD_TESTS
269 #include "dbus-test.h"
272 static int free_counter;
275 test_free_slot_data_func (void *data)
277 int i = _DBUS_POINTER_TO_INT (data);
279 _dbus_assert (free_counter == i);
284 * Test function for data slots
287 _dbus_data_slot_test (void)
289 DBusDataSlotAllocator allocator;
290 DBusDataSlotList list;
292 DBusFreeFunction old_free_func;
295 if (!_dbus_data_slot_allocator_init (&allocator))
296 _dbus_assert_not_reached ("no memory for allocator");
298 _dbus_data_slot_list_init (&list);
305 /* we don't really want apps to rely on this ordered
306 * allocation, but it simplifies things to rely on it
309 if (_dbus_data_slot_allocator_alloc (&allocator) != i)
310 _dbus_assert_not_reached ("did not allocate slots in numeric order\n");
318 if (!_dbus_data_slot_list_set (&allocator, &list,
320 _DBUS_INT_TO_POINTER (i),
321 test_free_slot_data_func,
322 &old_free_func, &old_data))
323 _dbus_assert_not_reached ("no memory to set data");
325 _dbus_assert (old_free_func == NULL);
326 _dbus_assert (old_data == NULL);
328 _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
329 _DBUS_INT_TO_POINTER (i));
338 if (!_dbus_data_slot_list_set (&allocator, &list,
340 _DBUS_INT_TO_POINTER (i),
341 test_free_slot_data_func,
342 &old_free_func, &old_data))
343 _dbus_assert_not_reached ("no memory to set data");
345 _dbus_assert (old_free_func == test_free_slot_data_func);
346 _dbus_assert (_DBUS_POINTER_TO_INT (old_data) == i);
348 (* old_free_func) (old_data);
349 _dbus_assert (i == (free_counter - 1));
351 _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
352 _DBUS_INT_TO_POINTER (i));
358 _dbus_data_slot_list_free (&list);
360 _dbus_assert (N_SLOTS == free_counter);
365 _dbus_data_slot_allocator_free (&allocator, i);
372 #endif /* DBUS_BUILD_TESTS */