D-Bus  1.8.16
dbus-dataslot.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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 2.1
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include <config.h>
25 #include "dbus-dataslot.h"
26 #include "dbus-threads-internal.h"
27 
47  DBusGlobalLock lock)
48 {
49  allocator->allocated_slots = NULL;
50  allocator->n_allocated_slots = 0;
51  allocator->n_used_slots = 0;
52  allocator->lock = lock;
53 
54  return TRUE;
55 }
56 
70  dbus_int32_t *slot_id_p)
71 {
72  dbus_int32_t slot;
73 
74  if (!_dbus_lock (allocator->lock))
75  return FALSE;
76 
77  if (*slot_id_p >= 0)
78  {
79  slot = *slot_id_p;
80 
81  _dbus_assert (slot < allocator->n_allocated_slots);
82  _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
83 
84  allocator->allocated_slots[slot].refcount += 1;
85 
86  goto out;
87  }
88 
89  _dbus_assert (*slot_id_p < 0);
90 
91  if (allocator->n_used_slots < allocator->n_allocated_slots)
92  {
93  slot = 0;
94  while (slot < allocator->n_allocated_slots)
95  {
96  if (allocator->allocated_slots[slot].slot_id < 0)
97  {
98  allocator->allocated_slots[slot].slot_id = slot;
99  allocator->allocated_slots[slot].refcount = 1;
100  allocator->n_used_slots += 1;
101  break;
102  }
103  ++slot;
104  }
105 
106  _dbus_assert (slot < allocator->n_allocated_slots);
107  }
108  else
109  {
110  DBusAllocatedSlot *tmp;
111 
112  slot = -1;
113  tmp = dbus_realloc (allocator->allocated_slots,
114  sizeof (DBusAllocatedSlot) * (allocator->n_allocated_slots + 1));
115  if (tmp == NULL)
116  goto out;
117 
118  allocator->allocated_slots = tmp;
119  slot = allocator->n_allocated_slots;
120  allocator->n_allocated_slots += 1;
121  allocator->n_used_slots += 1;
122  allocator->allocated_slots[slot].slot_id = slot;
123  allocator->allocated_slots[slot].refcount = 1;
124  }
125 
126  _dbus_assert (slot >= 0);
127  _dbus_assert (slot < allocator->n_allocated_slots);
128  _dbus_assert (*slot_id_p < 0);
129  _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
130  _dbus_assert (allocator->allocated_slots[slot].refcount == 1);
131 
132  *slot_id_p = slot;
133 
134  _dbus_verbose ("Allocated slot %d on allocator %p total %d slots allocated %d used\n",
135  slot, allocator, allocator->n_allocated_slots, allocator->n_used_slots);
136 
137  out:
138  _dbus_unlock (allocator->lock);
139  return slot >= 0;
140 }
141 
153 void
155  dbus_int32_t *slot_id_p)
156 {
157  if (!_dbus_lock (allocator->lock))
158  _dbus_assert_not_reached ("we should have initialized global locks "
159  "before we allocated this slot");
160 
161  _dbus_assert (*slot_id_p < allocator->n_allocated_slots);
162  _dbus_assert (allocator->allocated_slots[*slot_id_p].slot_id == *slot_id_p);
163  _dbus_assert (allocator->allocated_slots[*slot_id_p].refcount > 0);
164 
165  allocator->allocated_slots[*slot_id_p].refcount -= 1;
166 
167  if (allocator->allocated_slots[*slot_id_p].refcount > 0)
168  {
169  _dbus_unlock (allocator->lock);
170  return;
171  }
172 
173  /* refcount is 0, free the slot */
174  _dbus_verbose ("Freeing slot %d on allocator %p total %d allocated %d used\n",
175  *slot_id_p, allocator, allocator->n_allocated_slots, allocator->n_used_slots);
176 
177  allocator->allocated_slots[*slot_id_p].slot_id = -1;
178  *slot_id_p = -1;
179 
180  allocator->n_used_slots -= 1;
181 
182  if (allocator->n_used_slots == 0)
183  {
184  dbus_free (allocator->allocated_slots);
185  allocator->allocated_slots = NULL;
186  allocator->n_allocated_slots = 0;
187  }
188 
189  _dbus_unlock (allocator->lock);
190 }
191 
196 void
198 {
199  list->slots = NULL;
200  list->n_slots = 0;
201 }
202 
222  DBusDataSlotList *list,
223  int slot,
224  void *data,
225  DBusFreeFunction free_data_func,
226  DBusFreeFunction *old_free_func,
227  void **old_data)
228 {
229 #ifndef DBUS_DISABLE_ASSERT
230  /* We need to take the allocator lock here, because the allocator could
231  * be e.g. realloc()ing allocated_slots. We avoid doing this if asserts
232  * are disabled, since then the asserts are empty.
233  */
234  if (!_dbus_lock (allocator->lock))
235  _dbus_assert_not_reached ("we should have initialized global locks "
236  "before we allocated this slot");
237 
238  _dbus_assert (slot < allocator->n_allocated_slots);
239  _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
240  _dbus_unlock (allocator->lock);
241 #endif
242 
243  if (slot >= list->n_slots)
244  {
245  DBusDataSlot *tmp;
246  int i;
247 
248  tmp = dbus_realloc (list->slots,
249  sizeof (DBusDataSlot) * (slot + 1));
250  if (tmp == NULL)
251  return FALSE;
252 
253  list->slots = tmp;
254  i = list->n_slots;
255  list->n_slots = slot + 1;
256  while (i < list->n_slots)
257  {
258  list->slots[i].data = NULL;
259  list->slots[i].free_data_func = NULL;
260  ++i;
261  }
262  }
263 
264  _dbus_assert (slot < list->n_slots);
265 
266  *old_data = list->slots[slot].data;
267  *old_free_func = list->slots[slot].free_data_func;
268 
269  list->slots[slot].data = data;
270  list->slots[slot].free_data_func = free_data_func;
271 
272  return TRUE;
273 }
274 
284 void*
286  DBusDataSlotList *list,
287  int slot)
288 {
289 #ifndef DBUS_DISABLE_ASSERT
290  /* We need to take the allocator lock here, because the allocator could
291  * be e.g. realloc()ing allocated_slots. We avoid doing this if asserts
292  * are disabled, since then the asserts are empty.
293  */
294  if (!_dbus_lock (allocator->lock))
295  _dbus_assert_not_reached ("we should have initialized global locks "
296  "before we allocated this slot");
297 
298  _dbus_assert (slot >= 0);
299  _dbus_assert (slot < allocator->n_allocated_slots);
300  _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
301  _dbus_unlock (allocator->lock);
302 #endif
303 
304  if (slot >= list->n_slots)
305  return NULL;
306  else
307  return list->slots[slot].data;
308 }
309 
316 void
318 {
319  int i;
320 
321  i = 0;
322  while (i < list->n_slots)
323  {
324  if (list->slots[i].free_data_func)
325  (* list->slots[i].free_data_func) (list->slots[i].data);
326  list->slots[i].data = NULL;
327  list->slots[i].free_data_func = NULL;
328  ++i;
329  }
330 }
331 
339 void
341 {
343 
344  dbus_free (list->slots);
345  list->slots = NULL;
346  list->n_slots = 0;
347 }
348 
351 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
352 #include "dbus-test.h"
353 #include <stdio.h>
354 
355 static int free_counter;
356 
357 static void
358 test_free_slot_data_func (void *data)
359 {
360  int i = _DBUS_POINTER_TO_INT (data);
361 
362  _dbus_assert (free_counter == i);
363  ++free_counter;
364 }
365 
370 _dbus_data_slot_test (void)
371 {
372  DBusDataSlotAllocator allocator;
373  DBusDataSlotList list;
374  int i;
375  DBusFreeFunction old_free_func;
376  void *old_data;
377 
378  if (!_dbus_data_slot_allocator_init (&allocator, _DBUS_LOCK_server_slots))
379  _dbus_assert_not_reached ("no memory for allocator");
380 
382 
383 #define N_SLOTS 100
384 
385  i = 0;
386  while (i < N_SLOTS)
387  {
388  /* we don't really want apps to rely on this ordered
389  * allocation, but it simplifies things to rely on it
390  * here.
391  */
392  dbus_int32_t tmp = -1;
393 
394  _dbus_data_slot_allocator_alloc (&allocator, &tmp);
395 
396  if (tmp != i)
397  _dbus_assert_not_reached ("did not allocate slots in numeric order\n");
398 
399  ++i;
400  }
401 
402  i = 0;
403  while (i < N_SLOTS)
404  {
405  if (!_dbus_data_slot_list_set (&allocator, &list,
406  i,
408  test_free_slot_data_func,
409  &old_free_func, &old_data))
410  _dbus_assert_not_reached ("no memory to set data");
411 
412  _dbus_assert (old_free_func == NULL);
413  _dbus_assert (old_data == NULL);
414 
415  _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
417 
418  ++i;
419  }
420 
421  free_counter = 0;
422  i = 0;
423  while (i < N_SLOTS)
424  {
425  if (!_dbus_data_slot_list_set (&allocator, &list,
426  i,
428  test_free_slot_data_func,
429  &old_free_func, &old_data))
430  _dbus_assert_not_reached ("no memory to set data");
431 
432  _dbus_assert (old_free_func == test_free_slot_data_func);
433  _dbus_assert (_DBUS_POINTER_TO_INT (old_data) == i);
434 
435  (* old_free_func) (old_data);
436  _dbus_assert (i == (free_counter - 1));
437 
438  _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
440 
441  ++i;
442  }
443 
444  free_counter = 0;
446 
447  _dbus_assert (N_SLOTS == free_counter);
448 
449  i = 0;
450  while (i < N_SLOTS)
451  {
452  dbus_int32_t tmp = i;
453 
454  _dbus_data_slot_allocator_free (&allocator, &tmp);
455  _dbus_assert (tmp == -1);
456  ++i;
457  }
458 
459  return TRUE;
460 }
461 
462 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
DBusFreeFunction free_data_func
Free the application data.
Definition: dbus-dataslot.h:39
#define NULL
A null pointer, defined appropriately for C or C++.
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition: dbus-memory.h:64
int n_allocated_slots
number of slots malloc'd
Definition: dbus-dataslot.h:58
void * dbus_realloc(void *memory, size_t bytes)
Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:601
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:701
An allocated slot for storing data.
Definition: dbus-dataslot.h:46
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define _DBUS_INT_TO_POINTER(integer)
Safely stuffs an integer into a pointer, to be extracted later with _DBUS_POINTER_TO_INT.
int n_used_slots
number of slots used
Definition: dbus-dataslot.h:59
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
dbus_bool_t _dbus_data_slot_list_set(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot, void *data, DBusFreeFunction free_data_func, DBusFreeFunction *old_free_func, void **old_data)
Stores a pointer in the data slot list, along with an optional function to be used for freeing the da...
DBusDataSlot * slots
Data slots.
Definition: dbus-dataslot.h:71
#define _DBUS_POINTER_TO_INT(pointer)
Safely casts a void* to an integer; should only be used on void* that actually contain integers...
void _dbus_data_slot_list_clear(DBusDataSlotList *list)
Frees all data slots contained in the list, calling application-provided free functions if they exist...
dbus_int32_t slot_id
ID of this slot.
Definition: dbus-dataslot.h:48
void * _dbus_data_slot_list_get(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot)
Retrieves data previously set with _dbus_data_slot_list_set_data().
#define TRUE
Expands to "1".
void _dbus_data_slot_list_init(DBusDataSlotList *list)
Initializes a slot list.
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
Data structure that stores the actual user data set at a given slot.
Definition: dbus-dataslot.h:69
An allocator that tracks a set of slot IDs.
Definition: dbus-dataslot.h:55
int refcount
Number of uses of the slot.
Definition: dbus-dataslot.h:49
int n_slots
Slots we have storage for in data_slots.
Definition: dbus-dataslot.h:72
#define FALSE
Expands to "0".
void * data
The application data.
Definition: dbus-dataslot.h:38
dbus_bool_t _dbus_data_slot_allocator_init(DBusDataSlotAllocator *allocator, DBusGlobalLock lock)
Initializes a data slot allocator object, used to assign integer IDs for data slots.
Definition: dbus-dataslot.c:46
dbus_bool_t _dbus_data_slot_allocator_alloc(DBusDataSlotAllocator *allocator, dbus_int32_t *slot_id_p)
Allocates an integer ID to be used for storing data in a DBusDataSlotList.
Definition: dbus-dataslot.c:69
int dbus_int32_t
A 32-bit signed integer on all platforms.
void _dbus_data_slot_allocator_free(DBusDataSlotAllocator *allocator, dbus_int32_t *slot_id_p)
Deallocates an ID previously allocated with _dbus_data_slot_allocator_alloc().
DBusAllocatedSlot * allocated_slots
Allocated slots.
Definition: dbus-dataslot.h:57
DBusDataSlot is used to store application data on the connection.
Definition: dbus-dataslot.h:36
DBusGlobalLock lock
index of thread lock
Definition: dbus-dataslot.h:60
void _dbus_data_slot_list_free(DBusDataSlotList *list)
Frees the data slot list and all data slots contained in it, calling application-provided free functi...