2003-06-22 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / dbus / dbus-dataslot.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-dataslot.c  storing data on objects
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23 #include "dbus-dataslot.h"
24 #include "dbus-threads.h"
25
26 /**
27  * @defgroup DBusDataSlot Data slots
28  * @ingroup  DBusInternals
29  * @brief Storing data by ID
30  *
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. 
34  * @{
35  */
36
37 /**
38  * Initializes a data slot allocator object, used to assign
39  * integer IDs for data slots.
40  *
41  * @param allocator the allocator to initialize
42  */
43 dbus_bool_t
44 _dbus_data_slot_allocator_init (DBusDataSlotAllocator *allocator)
45 {
46   allocator->allocated_slots = NULL;
47   allocator->n_allocated_slots = 0;
48   allocator->n_used_slots = 0;
49   allocator->lock = NULL;
50   
51   return TRUE;
52 }
53
54 /**
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.
60  * 
61  * @param allocator the allocator
62  * @param mutex the lock for this allocator
63  * @param slot_id_p address to fill with the slot ID
64  * @returns #TRUE on success
65  */
66 dbus_bool_t
67 _dbus_data_slot_allocator_alloc (DBusDataSlotAllocator *allocator,
68                                  DBusMutex             *mutex,
69                                  dbus_int32_t          *slot_id_p)
70 {
71   dbus_int32_t slot;
72
73   if (!dbus_mutex_lock (mutex))
74     return FALSE;
75
76   if (allocator->n_allocated_slots == 0)
77     {
78       _dbus_assert (allocator->lock == NULL);
79       allocator->lock = mutex;
80     }
81   else
82     _dbus_assert (allocator->lock == mutex);
83
84   if (*slot_id_p >= 0)
85     {
86       slot = *slot_id_p;
87       
88       _dbus_assert (slot < allocator->n_allocated_slots);
89       _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
90       
91       allocator->allocated_slots[slot].refcount += 1;
92
93       goto out;
94     }
95
96   _dbus_assert (*slot_id_p < 0);
97   
98   if (allocator->n_used_slots < allocator->n_allocated_slots)
99     {
100       slot = 0;
101       while (slot < allocator->n_allocated_slots)
102         {
103           if (allocator->allocated_slots[slot].slot_id < 0)
104             {
105               allocator->allocated_slots[slot].slot_id = slot;
106               allocator->allocated_slots[slot].refcount = 1;
107               allocator->n_used_slots += 1;
108               break;
109             }
110           ++slot;
111         }
112
113       _dbus_assert (slot < allocator->n_allocated_slots);
114     }
115   else
116     {
117       DBusAllocatedSlot *tmp;
118       
119       slot = -1;
120       tmp = dbus_realloc (allocator->allocated_slots,
121                           sizeof (DBusAllocatedSlot) * (allocator->n_allocated_slots + 1));
122       if (tmp == NULL)
123         goto out;
124
125       allocator->allocated_slots = tmp;
126       slot = allocator->n_allocated_slots;
127       allocator->n_allocated_slots += 1;
128       allocator->n_used_slots += 1;
129       allocator->allocated_slots[slot].slot_id = slot;
130       allocator->allocated_slots[slot].refcount = 1;
131     }
132
133   _dbus_assert (slot >= 0);
134   _dbus_assert (slot < allocator->n_allocated_slots);
135   _dbus_assert (*slot_id_p < 0);
136   _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
137   _dbus_assert (allocator->allocated_slots[slot].refcount == 1);
138   
139   *slot_id_p = slot;
140   
141   _dbus_verbose ("Allocated slot %d on allocator %p total %d slots allocated %d used\n",
142                  slot, allocator, allocator->n_allocated_slots, allocator->n_used_slots);
143   
144  out:
145   dbus_mutex_unlock (allocator->lock);
146   return slot >= 0;
147 }
148
149 /**
150  * Deallocates an ID previously allocated with
151  * _dbus_data_slot_allocator_alloc().  Existing data stored on
152  * existing #DBusDataList objects with this ID will be freed when the
153  * data list is finalized, but may not be retrieved (and may only be
154  * replaced if someone else reallocates the slot).
155  * The slot value is reset to -1 if this is the last unref.
156  *
157  * @param allocator the allocator
158  * @param slot_id_p address where we store the slot
159  */
160 void
161 _dbus_data_slot_allocator_free (DBusDataSlotAllocator *allocator,
162                                 dbus_int32_t          *slot_id_p)
163 {
164   dbus_mutex_lock (allocator->lock);
165   
166   _dbus_assert (*slot_id_p < allocator->n_allocated_slots);
167   _dbus_assert (allocator->allocated_slots[*slot_id_p].slot_id == *slot_id_p);
168   _dbus_assert (allocator->allocated_slots[*slot_id_p].refcount > 0);
169
170   allocator->allocated_slots[*slot_id_p].refcount -= 1;
171
172   if (allocator->allocated_slots[*slot_id_p].refcount > 0)
173     {
174       dbus_mutex_unlock (allocator->lock);
175       return;
176     }
177
178   /* refcount is 0, free the slot */
179   _dbus_verbose ("Freeing slot %d on allocator %p total %d allocated %d used\n",
180                  *slot_id_p, allocator, allocator->n_allocated_slots, allocator->n_used_slots);
181   
182   allocator->allocated_slots[*slot_id_p].slot_id = -1;
183   *slot_id_p = -1;
184   
185   allocator->n_used_slots -= 1;
186   
187   if (allocator->n_used_slots == 0)
188     {
189       DBusMutex *mutex = allocator->lock;
190       
191       dbus_free (allocator->allocated_slots);
192       allocator->allocated_slots = NULL;
193       allocator->n_allocated_slots = 0;
194       allocator->lock = NULL;
195
196       dbus_mutex_unlock (mutex);
197     }
198   else
199     {
200       dbus_mutex_unlock (allocator->lock);
201     }
202 }
203
204 /**
205  * Initializes a slot list.
206  * @param list the list to initialize.
207  */
208 void
209 _dbus_data_slot_list_init (DBusDataSlotList *list)
210 {
211   list->slots = NULL;
212   list->n_slots = 0;
213 }
214
215 /**
216  * Stores a pointer in the data slot list, along with an optional
217  * function to be used for freeing the data when the data is set
218  * again, or when the slot list is finalized. The slot number must
219  * have been allocated with _dbus_data_slot_allocator_alloc() for the
220  * same allocator passed in here. The same allocator has to be used
221  * with the slot list every time.
222  *
223  * @param allocator the allocator to use
224  * @param list the data slot list
225  * @param slot the slot number
226  * @param data the data to store
227  * @param free_data_func finalizer function for the data
228  * @param old_free_func free function for any previously-existing data
229  * @param old_data previously-existing data, should be freed with old_free_func
230  * @returns #TRUE if there was enough memory to store the data
231  */
232 dbus_bool_t
233 _dbus_data_slot_list_set  (DBusDataSlotAllocator *allocator,
234                            DBusDataSlotList      *list,
235                            int                    slot,
236                            void                  *data,
237                            DBusFreeFunction       free_data_func,
238                            DBusFreeFunction      *old_free_func,
239                            void                 **old_data)
240 {
241 #ifndef DBUS_DISABLE_ASSERT
242   /* We need to take the allocator lock here, because the allocator could
243    * be e.g. realloc()ing allocated_slots. We avoid doing this if asserts
244    * are disabled, since then the asserts are empty.
245    */
246   if (!dbus_mutex_lock (allocator->lock))
247     return FALSE;
248   _dbus_assert (slot < allocator->n_allocated_slots);
249   _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
250   dbus_mutex_unlock (allocator->lock);
251 #endif
252   
253   if (slot >= list->n_slots)
254     {
255       DBusDataSlot *tmp;
256       int i;
257       
258       tmp = dbus_realloc (list->slots,
259                           sizeof (DBusDataSlot) * (slot + 1));
260       if (tmp == NULL)
261         return FALSE;
262       
263       list->slots = tmp;
264       i = list->n_slots;
265       list->n_slots = slot + 1;
266       while (i < list->n_slots)
267         {
268           list->slots[i].data = NULL;
269           list->slots[i].free_data_func = NULL;
270           ++i;
271         }
272     }
273
274   _dbus_assert (slot < list->n_slots);
275
276   *old_data = list->slots[slot].data;
277   *old_free_func = list->slots[slot].free_data_func;
278
279   list->slots[slot].data = data;
280   list->slots[slot].free_data_func = free_data_func;
281
282   return TRUE;
283 }
284
285 /**
286  * Retrieves data previously set with _dbus_data_slot_list_set_data().
287  * The slot must still be allocated (must not have been freed).
288  *
289  * @param allocator the allocator slot was allocated from
290  * @param list the data slot list
291  * @param slot the slot to get data from
292  * @returns the data, or #NULL if not found
293  */
294 void*
295 _dbus_data_slot_list_get  (DBusDataSlotAllocator *allocator,
296                            DBusDataSlotList      *list,
297                            int                    slot)
298 {
299 #ifndef DBUS_DISABLE_ASSERT
300   /* We need to take the allocator lock here, because the allocator could
301    * be e.g. realloc()ing allocated_slots. We avoid doing this if asserts
302    * are disabled, since then the asserts are empty.
303    */
304   if (!dbus_mutex_lock (allocator->lock))
305     return FALSE;
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);
310 #endif
311
312   if (slot >= list->n_slots)
313     return NULL;
314   else
315     return list->slots[slot].data;
316 }
317
318 /**
319  * Frees the data slot list and all data slots contained
320  * in it, calling application-provided free functions
321  * if they exist.
322  *
323  * @param list the list to free
324  */
325 void
326 _dbus_data_slot_list_free (DBusDataSlotList *list)
327 {
328   int i;
329
330   i = 0;
331   while (i < list->n_slots)
332     {
333       if (list->slots[i].free_data_func)
334         (* list->slots[i].free_data_func) (list->slots[i].data);
335       list->slots[i].data = NULL;
336       list->slots[i].free_data_func = NULL;
337       ++i;
338     }
339
340   dbus_free (list->slots);
341   list->slots = NULL;
342   list->n_slots = 0;
343 }
344
345 /** @} */
346
347 #ifdef DBUS_BUILD_TESTS
348 #include "dbus-test.h"
349 #include <stdio.h>
350
351 static int free_counter;
352
353 static void
354 test_free_slot_data_func (void *data)
355 {
356   int i = _DBUS_POINTER_TO_INT (data);
357
358   _dbus_assert (free_counter == i);
359   ++free_counter;
360 }
361
362 /**
363  * Test function for data slots
364  */
365 dbus_bool_t
366 _dbus_data_slot_test (void)
367 {
368   DBusDataSlotAllocator allocator;
369   DBusDataSlotList list;
370   int i;
371   DBusFreeFunction old_free_func;
372   void *old_data;
373   DBusMutex *mutex;
374   
375   if (!_dbus_data_slot_allocator_init (&allocator))
376     _dbus_assert_not_reached ("no memory for allocator");
377
378   _dbus_data_slot_list_init (&list);
379
380   mutex = dbus_mutex_new ();
381   if (mutex == NULL)
382     _dbus_assert_not_reached ("failed to alloc mutex");
383   
384 #define N_SLOTS 100
385
386   i = 0;
387   while (i < N_SLOTS)
388     {
389       /* we don't really want apps to rely on this ordered
390        * allocation, but it simplifies things to rely on it
391        * here.
392        */
393       dbus_int32_t tmp = -1;
394       
395       _dbus_data_slot_allocator_alloc (&allocator, mutex, &tmp);
396
397       if (tmp != i)
398         _dbus_assert_not_reached ("did not allocate slots in numeric order\n");
399
400       ++i;
401     }
402
403   i = 0;
404   while (i < N_SLOTS)
405     {
406       if (!_dbus_data_slot_list_set (&allocator, &list,
407                                      i,
408                                      _DBUS_INT_TO_POINTER (i), 
409                                      test_free_slot_data_func,
410                                      &old_free_func, &old_data))
411         _dbus_assert_not_reached ("no memory to set data");
412
413       _dbus_assert (old_free_func == NULL);
414       _dbus_assert (old_data == NULL);
415
416       _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
417                     _DBUS_INT_TO_POINTER (i));
418       
419       ++i;
420     }
421
422   free_counter = 0;
423   i = 0;
424   while (i < N_SLOTS)
425     {
426       if (!_dbus_data_slot_list_set (&allocator, &list,
427                                      i,
428                                      _DBUS_INT_TO_POINTER (i), 
429                                      test_free_slot_data_func,
430                                      &old_free_func, &old_data))
431         _dbus_assert_not_reached ("no memory to set data");
432
433       _dbus_assert (old_free_func == test_free_slot_data_func);
434       _dbus_assert (_DBUS_POINTER_TO_INT (old_data) == i);
435
436       (* old_free_func) (old_data);
437       _dbus_assert (i == (free_counter - 1));
438
439       _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
440                     _DBUS_INT_TO_POINTER (i));
441       
442       ++i;
443     }
444
445   free_counter = 0;
446   _dbus_data_slot_list_free (&list);
447
448   _dbus_assert (N_SLOTS == free_counter);
449
450   i = 0;
451   while (i < N_SLOTS)
452     {
453       dbus_int32_t tmp = i;
454       
455       _dbus_data_slot_allocator_free (&allocator, &tmp);
456       _dbus_assert (tmp == -1);
457       ++i;
458     }
459
460   dbus_mutex_free (mutex);
461   
462   return TRUE;
463 }
464
465 #endif /* DBUS_BUILD_TESTS */