2003-02-26 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 = dbus_mutex_new ();
50   if (allocator->lock == NULL)
51     return FALSE;
52   else
53     return TRUE;
54 }
55
56 /**
57  * Allocates an integer ID to be used for storing data
58  * in a #DBusDataSlotList.
59  *
60  * @param allocator the allocator
61  * @returns the integer ID, or -1 on failure
62  */
63 int
64 _dbus_data_slot_allocator_alloc (DBusDataSlotAllocator *allocator)
65 {
66   int slot;
67   
68   if (!dbus_mutex_lock (allocator->lock))
69     return -1;
70
71   if (allocator->n_used_slots < allocator->n_allocated_slots)
72     {
73       slot = 0;
74       while (slot < allocator->n_allocated_slots)
75         {
76           if (allocator->allocated_slots[slot] < 0)
77             {
78               allocator->allocated_slots[slot] = slot;
79               allocator->n_used_slots += 1;
80               break;
81             }
82           ++slot;
83         }
84
85       _dbus_assert (slot < allocator->n_allocated_slots);
86     }
87   else
88     {
89       int *tmp;
90       
91       slot = -1;
92       tmp = dbus_realloc (allocator->allocated_slots,
93                           sizeof (int) * (allocator->n_allocated_slots + 1));
94       if (tmp == NULL)
95         goto out;
96
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;
102     }
103
104   _dbus_assert (slot >= 0);
105   _dbus_assert (slot < allocator->n_allocated_slots);
106   
107  out:
108   dbus_mutex_unlock (allocator->lock);
109   return slot;
110 }
111
112 /**
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).
118  *
119  * @param allocator the allocator
120  * @param slot the slot to deallocate
121  */
122 void
123 _dbus_data_slot_allocator_free (DBusDataSlotAllocator *allocator,
124                                 int slot)
125 {
126   dbus_mutex_lock (allocator->lock);
127
128   _dbus_assert (slot < allocator->n_allocated_slots);
129   _dbus_assert (allocator->allocated_slots[slot] == slot);
130   
131   allocator->allocated_slots[slot] = -1;
132   allocator->n_used_slots -= 1;
133
134   if (allocator->n_used_slots == 0)
135     {
136       dbus_free (allocator->allocated_slots);
137       allocator->allocated_slots = NULL;
138       allocator->n_allocated_slots = 0;
139     }
140   
141   dbus_mutex_unlock (allocator->lock);
142 }
143
144 /**
145  * Initializes a slot list.
146  * @param list the list to initialize.
147  */
148 void
149 _dbus_data_slot_list_init (DBusDataSlotList *list)
150 {
151   list->slots = NULL;
152   list->n_slots = 0;
153 }
154
155 /**
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.
162  *
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
171  */
172 dbus_bool_t
173 _dbus_data_slot_list_set  (DBusDataSlotAllocator *allocator,
174                            DBusDataSlotList      *list,
175                            int                    slot,
176                            void                  *data,
177                            DBusFreeFunction       free_data_func,
178                            DBusFreeFunction      *old_free_func,
179                            void                 **old_data)
180 {  
181   _dbus_assert (slot < allocator->n_allocated_slots);
182   _dbus_assert (allocator->allocated_slots[slot] == slot);
183   
184   if (slot >= list->n_slots)
185     {
186       DBusDataSlot *tmp;
187       int i;
188       
189       tmp = dbus_realloc (list->slots,
190                           sizeof (DBusDataSlot) * (slot + 1));
191       if (tmp == NULL)
192         return FALSE;
193       
194       list->slots = tmp;
195       i = list->n_slots;
196       list->n_slots = slot + 1;
197       while (i < list->n_slots)
198         {
199           list->slots[i].data = NULL;
200           list->slots[i].free_data_func = NULL;
201           ++i;
202         }
203     }
204
205   _dbus_assert (slot < list->n_slots);
206
207   *old_data = list->slots[slot].data;
208   *old_free_func = list->slots[slot].free_data_func;
209
210   list->slots[slot].data = data;
211   list->slots[slot].free_data_func = free_data_func;
212
213   return TRUE;
214 }
215
216 /**
217  * Retrieves data previously set with _dbus_data_slot_list_set_data().
218  * The slot must still be allocated (must not have been freed).
219  *
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
224  */
225 void*
226 _dbus_data_slot_list_get  (DBusDataSlotAllocator *allocator,
227                            DBusDataSlotList      *list,
228                            int                    slot)
229 {
230   _dbus_assert (slot < allocator->n_allocated_slots);
231   _dbus_assert (allocator->allocated_slots[slot] == slot);
232
233   if (slot >= list->n_slots)
234     return NULL;
235   else
236     return list->slots[slot].data;
237 }
238
239 /**
240  * Frees the data slot list and all data slots contained
241  * in it, calling application-provided free functions
242  * if they exist.
243  *
244  * @param list the list to free
245  */
246 void
247 _dbus_data_slot_list_free (DBusDataSlotList *list)
248 {
249   int i;
250
251   i = 0;
252   while (i < list->n_slots)
253     {
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;
258       ++i;
259     }
260
261   dbus_free (list->slots);
262   list->slots = NULL;
263   list->n_slots = 0;
264 }
265
266 /** @} */
267
268 #ifdef DBUS_BUILD_TESTS
269 #include "dbus-test.h"
270 #include <stdio.h>
271
272 static int free_counter;
273
274 static void
275 test_free_slot_data_func (void *data)
276 {
277   int i = _DBUS_POINTER_TO_INT (data);
278
279   _dbus_assert (free_counter == i);
280   ++free_counter;
281 }
282
283 /**
284  * Test function for data slots
285  */
286 dbus_bool_t
287 _dbus_data_slot_test (void)
288 {
289   DBusDataSlotAllocator allocator;
290   DBusDataSlotList list;
291   int i;
292   DBusFreeFunction old_free_func;
293   void *old_data;
294   
295   if (!_dbus_data_slot_allocator_init (&allocator))
296     _dbus_assert_not_reached ("no memory for allocator");
297
298   _dbus_data_slot_list_init (&list);
299
300 #define N_SLOTS 100
301
302   i = 0;
303   while (i < N_SLOTS)
304     {
305       /* we don't really want apps to rely on this ordered
306        * allocation, but it simplifies things to rely on it
307        * here.
308        */
309       if (_dbus_data_slot_allocator_alloc (&allocator) != i)
310         _dbus_assert_not_reached ("did not allocate slots in numeric order\n");
311
312       ++i;
313     }
314
315   i = 0;
316   while (i < N_SLOTS)
317     {
318       if (!_dbus_data_slot_list_set (&allocator, &list,
319                                      i,
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");
324
325       _dbus_assert (old_free_func == NULL);
326       _dbus_assert (old_data == NULL);
327
328       _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
329                     _DBUS_INT_TO_POINTER (i));
330       
331       ++i;
332     }
333
334   free_counter = 0;
335   i = 0;
336   while (i < N_SLOTS)
337     {
338       if (!_dbus_data_slot_list_set (&allocator, &list,
339                                      i,
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");
344
345       _dbus_assert (old_free_func == test_free_slot_data_func);
346       _dbus_assert (_DBUS_POINTER_TO_INT (old_data) == i);
347
348       (* old_free_func) (old_data);
349       _dbus_assert (i == (free_counter - 1));
350
351       _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
352                     _DBUS_INT_TO_POINTER (i));
353       
354       ++i;
355     }
356
357   free_counter = 0;
358   _dbus_data_slot_list_free (&list);
359
360   _dbus_assert (N_SLOTS == free_counter);
361
362   i = 0;
363   while (i < N_SLOTS)
364     {
365       _dbus_data_slot_allocator_free (&allocator, i);
366       ++i;
367     }
368   
369   return TRUE;
370 }
371
372 #endif /* DBUS_BUILD_TESTS */