1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23: #include "dbus-dataslot.h"
24: #include "dbus-threads-internal.h"
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
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_loc = NULL;
50:
51: return TRUE;
52: }
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66: dbus_bool_t
67: _dbus_data_slot_allocator_alloc (DBusDataSlotAllocator *allocator,
68: DBusMutex **mutex_loc,
69: dbus_int32_t *slot_id_p)
70: {
71: dbus_int32_t slot;
72:
73: _dbus_mutex_lock (*mutex_loc);
74:
75: if (allocator->n_allocated_slots == 0)
76: {
77: _dbus_assert (allocator->lock_loc == NULL);
78: allocator->lock_loc = mutex_loc;
79: }
80: else if (allocator->lock_loc != mutex_loc)
81: {
82: _dbus_warn_check_failed ("D-Bus threads were initialized after first using the D-Bus library. If your application does not directly initialize threads or use D-Bus, keep in mind that some library or plugin may have used D-Bus or initialized threads behind your back. You can often fix this problem by calling dbus_init_threads() or dbus_g_threads_init() early in your main() method, before D-Bus is used.\n");
83: _dbus_assert_not_reached ("exiting");
84: }
85:
86: if (*slot_id_p >= 0)
87: {
88: slot = *slot_id_p;
89:
90: _dbus_assert (slot < allocator->n_allocated_slots);
91: _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
92:
93: allocator->allocated_slots[slot].refcount += 1;
94:
95: goto out;
96: }
97:
98: _dbus_assert (*slot_id_p < 0);
99:
100: if (allocator->n_used_slots < allocator->n_allocated_slots)
101: {
102: slot = 0;
103: while (slot < allocator->n_allocated_slots)
104: {
105: if (allocator->allocated_slots[slot].slot_id < 0)
106: {
107: allocator->allocated_slots[slot].slot_id = slot;
108: allocator->allocated_slots[slot].refcount = 1;
109: allocator->n_used_slots += 1;
110: break;
111: }
112: ++slot;
113: }
114:
115: _dbus_assert (slot < allocator->n_allocated_slots);
116: }
117: else
118: {
119: DBusAllocatedSlot *tmp;
120:
121: slot = -1;
122: tmp = dbus_realloc (allocator->allocated_slots,
123: sizeof (DBusAllocatedSlot) * (allocator->n_allocated_slots + 1));
124: if (tmp == NULL)
125: goto out;
126:
127: allocator->allocated_slots = tmp;
128: slot = allocator->n_allocated_slots;
129: allocator->n_allocated_slots += 1;
130: allocator->n_used_slots += 1;
131: allocator->allocated_slots[slot].slot_id = slot;
132: allocator->allocated_slots[slot].refcount = 1;
133: }
134:
135: _dbus_assert (slot >= 0);
136: _dbus_assert (slot < allocator->n_allocated_slots);
137: _dbus_assert (*slot_id_p < 0);
138: _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
139: _dbus_assert (allocator->allocated_slots[slot].refcount == 1);
140:
141: *slot_id_p = slot;
142:
143: _dbus_verbose ("Allocated slot %d on allocator %p total %d slots allocated %d used\n",
144: slot, allocator, allocator->n_allocated_slots, allocator->n_used_slots);
145:
146: out:
147: _dbus_mutex_unlock (*(allocator->lock_loc));
148: return slot >= 0;
149: }
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162: void
163: _dbus_data_slot_allocator_free (DBusDataSlotAllocator *allocator,
164: dbus_int32_t *slot_id_p)
165: {
166: _dbus_mutex_lock (*(allocator->lock_loc));
167:
168: _dbus_assert (*slot_id_p < allocator->n_allocated_slots);
169: _dbus_assert (allocator->allocated_slots[*slot_id_p].slot_id == *slot_id_p);
170: _dbus_assert (allocator->allocated_slots[*slot_id_p].refcount > 0);
171:
172: allocator->allocated_slots[*slot_id_p].refcount -= 1;
173:
174: if (allocator->allocated_slots[*slot_id_p].refcount > 0)
175: {
176: _dbus_mutex_unlock (*(allocator->lock_loc));
177: return;
178: }
179:
180:
181: _dbus_verbose ("Freeing slot %d on allocator %p total %d allocated %d used\n",
182: *slot_id_p, allocator, allocator->n_allocated_slots, allocator->n_used_slots);
183:
184: allocator->allocated_slots[*slot_id_p].slot_id = -1;
185: *slot_id_p = -1;
186:
187: allocator->n_used_slots -= 1;
188:
189: if (allocator->n_used_slots == 0)
190: {
191: DBusMutex **mutex_loc = allocator->lock_loc;
192:
193: dbus_free (allocator->allocated_slots);
194: allocator->allocated_slots = NULL;
195: allocator->n_allocated_slots = 0;
196: allocator->lock_loc = NULL;
197:
198: _dbus_mutex_unlock (*mutex_loc);
199: }
200: else
201: {
202: _dbus_mutex_unlock (*(allocator->lock_loc));
203: }
204: }
205:
206:
207:
208:
209:
210: void
211: _dbus_data_slot_list_init (DBusDataSlotList *list)
212: {
213: list->slots = NULL;
214: list->n_slots = 0;
215: }
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234: dbus_bool_t
235: _dbus_data_slot_list_set (DBusDataSlotAllocator *allocator,
236: DBusDataSlotList *list,
237: int slot,
238: void *data,
239: DBusFreeFunction free_data_func,
240: DBusFreeFunction *old_free_func,
241: void **old_data)
242: {
243: #ifndef DBUS_DISABLE_ASSERT
244:
245:
246:
247:
248: _dbus_mutex_lock (*(allocator->lock_loc));
249: _dbus_assert (slot < allocator->n_allocated_slots);
250: _dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
251: _dbus_mutex_unlock (*(allocator->lock_loc));
252: #endif
253:
254: if (slot >= list->n_slots)
255: {
256: DBusDataSlot *tmp;
257: int i;
258:
259: tmp = dbus_realloc (list->slots,
260: sizeof (DBusDataSlot) * (slot + 1));
261: if (tmp == NULL)
262: return FALSE;
263:
264: list->slots = tmp;
265: i = list->n_slots;
266: list->n_slots = slot + 1;
267: while (i < list->n_slots)
268: {
269: list->slots[i].data = NULL;
270: list->slots[i].free_data_func = NULL;
271: ++i;
272: }
273: }
274:
275: _dbus_assert (slot < list->n_slots);
276:
277: *old_data = list->slots[slot].data;
278: *old_free_func = list->slots[slot].free_data_func;
279:
280: list->slots[slot].data = data;
281: list->slots[slot].free_data_func = free_data_func;
282:
283: return TRUE;
284: }
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295: void*
296: _dbus_data_slot_list_get (DBusDataSlotAllocator *allocator,
297: DBusDataSlotList *list,
298: int slot)
299: {
300: #ifndef DBUS_DISABLE_ASSERT
301:
302:
303:
304:
305: _dbus_mutex_lock (*(allocator->lock_loc));
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_loc));
310: #endif
311:
312: if (slot >= list->n_slots)
313: return NULL;
314: else
315: return list->slots[slot].data;
316: }
317:
318:
319:
320:
321:
322:
323:
324: void
325: _dbus_data_slot_list_clear (DBusDataSlotList *list)
326: {
327: int i;
328:
329: i = 0;
330: while (i < list->n_slots)
331: {
332: if (list->slots[i].free_data_func)
333: (* list->slots[i].free_data_func) (list->slots[i].data);
334: list->slots[i].data = NULL;
335: list->slots[i].free_data_func = NULL;
336: ++i;
337: }
338: }
339:
340:
341:
342:
343:
344:
345:
346:
347: void
348: _dbus_data_slot_list_free (DBusDataSlotList *list)
349: {
350: _dbus_data_slot_list_clear (list);
351:
352: dbus_free (list->slots);
353: list->slots = NULL;
354: list->n_slots = 0;
355: }
356:
357:
358:
359: #ifdef DBUS_BUILD_TESTS
360: #include "dbus-test.h"
361: #include <stdio.h>
362:
363: static int free_counter;
364:
365: static void
366: test_free_slot_data_func (void *data)
367: {
368: int i = _DBUS_POINTER_TO_INT (data);
369:
370: _dbus_assert (free_counter == i);
371: ++free_counter;
372: }
373:
374:
375:
376:
377: dbus_bool_t
378: _dbus_data_slot_test (void)
379: {
380: DBusDataSlotAllocator allocator;
381: DBusDataSlotList list;
382: int i;
383: DBusFreeFunction old_free_func;
384: void *old_data;
385: DBusMutex *mutex;
386:
387: if (!_dbus_data_slot_allocator_init (&allocator))
388: _dbus_assert_not_reached ("no memory for allocator");
389:
390: _dbus_data_slot_list_init (&list);
391:
392: _dbus_mutex_new_at_location (&mutex);
393: if (mutex == NULL)
394: _dbus_assert_not_reached ("failed to alloc mutex");
395:
396: #define N_SLOTS 100
397:
398: i = 0;
399: while (i < N_SLOTS)
400: {
401:
402:
403:
404:
405: dbus_int32_t tmp = -1;
406:
407: _dbus_data_slot_allocator_alloc (&allocator, &mutex, &tmp);
408:
409: if (tmp != i)
410: _dbus_assert_not_reached ("did not allocate slots in numeric order\n");
411:
412: ++i;
413: }
414:
415: i = 0;
416: while (i < N_SLOTS)
417: {
418: if (!_dbus_data_slot_list_set (&allocator, &list,
419: i,
420: _DBUS_INT_TO_POINTER (i),
421: test_free_slot_data_func,
422: &old_free_func, &old_data))
423: _dbus_assert_not_reached ("no memory to set data");
424:
425: _dbus_assert (old_free_func == NULL);
426: _dbus_assert (old_data == NULL);
427:
428: _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
429: _DBUS_INT_TO_POINTER (i));
430:
431: ++i;
432: }
433:
434: free_counter = 0;
435: i = 0;
436: while (i < N_SLOTS)
437: {
438: if (!_dbus_data_slot_list_set (&allocator, &list,
439: i,
440: _DBUS_INT_TO_POINTER (i),
441: test_free_slot_data_func,
442: &old_free_func, &old_data))
443: _dbus_assert_not_reached ("no memory to set data");
444:
445: _dbus_assert (old_free_func == test_free_slot_data_func);
446: _dbus_assert (_DBUS_POINTER_TO_INT (old_data) == i);
447:
448: (* old_free_func) (old_data);
449: _dbus_assert (i == (free_counter - 1));
450:
451: _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
452: _DBUS_INT_TO_POINTER (i));
453:
454: ++i;
455: }
456:
457: free_counter = 0;
458: _dbus_data_slot_list_free (&list);
459:
460: _dbus_assert (N_SLOTS == free_counter);
461:
462: i = 0;
463: while (i < N_SLOTS)
464: {
465: dbus_int32_t tmp = i;
466:
467: _dbus_data_slot_allocator_free (&allocator, &tmp);
468: _dbus_assert (tmp == -1);
469: ++i;
470: }
471:
472: _dbus_mutex_free_at_location (&mutex);
473:
474: return TRUE;
475: }
476:
477: #endif