summaryrefslogtreecommitdiffstats
path: root/dbus/dbus-object-registry.c
blob: a4d922162f170b5b6a74ed02a26adf34907cecaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-object-registry.c  DBusObjectRegistry (internals of DBusConnection)
 *
 * Copyright (C) 2003  Red Hat Inc.
 *
 * Licensed under the Academic Free License version 1.2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "dbus-object-registry.h"
#include "dbus-connection-internal.h"
#include "dbus-internals.h"
#include "dbus-hash.h"
#include "dbus-protocol.h"
#include <string.h>

/**
 * @defgroup DBusObjectRegistry Map object IDs to implementations
 * @ingroup  DBusInternals
 * @brief DBusObjectRegistry is used by DBusConnection to track object IDs
 *
 * Types and functions related to DBusObjectRegistry. These
 * are all internal.
 *
 * @{
 */

typedef struct DBusObjectEntry DBusObjectEntry;
typedef struct DBusInterfaceEntry DBusInterfaceEntry;

#define DBUS_MAX_OBJECTS_PER_INTERFACE 65535
struct DBusInterfaceEntry
{
  unsigned int n_objects : 16;   /**< Number of objects with this interface */
  unsigned int n_allocated : 16; /**< Allocated size of objects array */
  dbus_uint16_t *objects;        /**< Index of each object with the interface */
  char name[4];                  /**< Name of interface (actually allocated larger) */
};

 /* 14 bits for object index, 32K objects */
#define DBUS_OBJECT_INDEX_BITS          (14)
#define DBUS_OBJECT_INDEX_MASK          (0x3fff)
#define DBUS_MAX_OBJECTS_PER_CONNECTION DBUS_OBJECT_INDEX_MASK
struct DBusObjectEntry
{
  unsigned int id_index      : 14; /**< Index of this entry in the entries array */
  unsigned int id_times_used : 18; /**< Count of times entry has been used; avoids recycling IDs too often */

  void *object_impl;               /**< Pointer to application-supplied implementation */
  const DBusObjectVTable *vtable;  /**< Virtual table for this object */
  DBusInterfaceEntry **interfaces; /**< NULL-terminated list of interfaces */
};

struct DBusObjectRegistry
{
  int refcount;
  DBusConnection *connection;

  DBusObjectEntry *entries;
  int n_entries_allocated;
  int n_entries_used;

  DBusHashTable *interface_table;
};

static void
free_interface_entry (void *entry)
{
  DBusInterfaceEntry *iface = entry;

  if (iface == NULL) /* DBusHashTable stupidity */
    return;
  
  dbus_free (iface->objects);
  dbus_free (iface);
}

DBusObjectRegistry*
_dbus_object_registry_new (DBusConnection *connection)
{
  DBusObjectRegistry *registry;
  DBusHashTable *interface_table;
  
  /* the connection passed in here isn't fully constructed,
   * so don't do anything more than store a pointer to
   * it
   */

  registry = NULL;
  interface_table = NULL;
  
  registry = dbus_new0 (DBusObjectRegistry, 1);
  if (registry == NULL)
    goto oom;

  interface_table = _dbus_hash_table_new (DBUS_HASH_STRING,
                                          NULL, free_interface_entry);
  if (interface_table == NULL)
    goto oom;
  
  registry->refcount = 1;
  registry->connection = connection;
  registry->interface_table = interface_table;
  
  return registry;

 oom:
  if (registry)
    dbus_free (registry);
  if (interface_table)
    _dbus_hash_table_unref (interface_table);

  return NULL;
}

void
_dbus_object_registry_ref (DBusObjectRegistry *registry)
{
  _dbus_assert (registry->refcount > 0);

  registry->refcount += 1;
}

void
_dbus_object_registry_unref (DBusObjectRegistry *registry)
{
  _dbus_assert (registry->refcount > 0);

  registry->refcount -= 1;

  if (registry->refcount == 0)
    {
      int i;
      
      _dbus_assert (registry->n_entries_used == 0);
      _dbus_assert (_dbus_hash_table_get_n_entries (registry->interface_table) == 0);

      i = 0;
      while (i < registry->n_entries_allocated)
        {
          if (registry->entries[i].interfaces)
            dbus_free (registry->entries[i].interfaces);
          ++i;
        }
      
      _dbus_hash_table_unref (registry->interface_table);
      dbus_free (registry->entries);
      dbus_free (registry);
    }
}

#define ENTRY_TO_ID(entry)                                              \
  (((dbus_uint32_t) (entry)->id_index) |                                \
   (((dbus_uint32_t)(entry)->id_times_used) << DBUS_OBJECT_INDEX_BITS))

#define ID_TO_INDEX(id) \
  (((dbus_uint32_t) (id)) & DBUS_OBJECT_INDEX_MASK)

#define ID_TO_TIMES_USED(id) \
  (((dbus_uint32_t) (id)) >> DBUS_OBJECT_INDEX_BITS)

static DBusObjectEntry*
validate_id (DBusObjectRegistry *registry,
             const DBusObjectID *object_id)
{
  int idx;
  int times_used;
  dbus_uint32_t instance_bits;
  
  instance_bits = dbus_object_id_get_instance_bits (object_id);

  /* Verify that connection ID bits are the same */
#ifdef DBUS_BUILD_TESTS
  if (registry->connection)
#endif
    {
      DBusObjectID tmp_id;
      
      _dbus_connection_init_id (registry->connection,
                                &tmp_id);
      dbus_object_id_set_instance_bits (&tmp_id, instance_bits);
      
      if (!dbus_object_id_equal (&tmp_id, object_id))
        return NULL;
    }
  
  idx = ID_TO_INDEX (instance_bits);
  times_used = ID_TO_TIMES_USED (instance_bits);
  
  if (idx >= registry->n_entries_allocated)
    return NULL;
  if (registry->entries[idx].vtable == NULL)
    return NULL;
  if (registry->entries[idx].id_times_used != times_used)
    return NULL;
  _dbus_assert (registry->entries[idx].id_index == idx);
  _dbus_assert (registry->n_entries_used > 0);

  return &registry->entries[idx];
}

static void
info_from_entry (DBusObjectRegistry *registry,
                 DBusObjectInfo     *info,
                 DBusObjectEntry    *entry)
{
  info->connection = registry->connection;
  info->object_impl = entry->object_impl;
#ifdef DBUS_BUILD_TESTS
  if (registry->connection)
#endif
    _dbus_connection_init_id (registry->connection,
                              &info->object_id);
#ifdef DBUS_BUILD_TESTS
  else
    {
      dbus_object_id_set_server_bits (&info->object_id, 1);
      dbus_object_id_set_client_bits (&info->object_id, 2);
    }
#endif

  _dbus_assert (dbus_object_id_get_server_bits (&info->object_id) != 0);
  _dbus_assert (dbus_object_id_get_client_bits (&info->object_id) != 0);
  
  dbus_object_id_set_instance_bits (&info->object_id,
                                    ENTRY_TO_ID (entry));

  _dbus_assert (dbus_object_id_get_instance_bits (&info->object_id) != 0);
}

static DBusInterfaceEntry*
lookup_interface (DBusObjectRegistry *registry,
                  const char         *name,
                  dbus_bool_t         create_if_not_found)
{
  DBusInterfaceEntry *entry;
  int sz;
  int len;
  
  entry = _dbus_hash_table_lookup_string (registry->interface_table,
                                          name);
  if (entry != NULL || !create_if_not_found)
    return entry;
  
  _dbus_assert (create_if_not_found);

  len = strlen (name);
  sz = _DBUS_STRUCT_OFFSET (DBusInterfaceEntry, name) + len + 1;
  entry = dbus_malloc (sz);
  if (entry == NULL)
    return NULL;
  entry->n_objects = 0;
  entry->n_allocated = 0;
  entry->objects = NULL;
  memcpy (entry->name, name, len + 1);

  if (!_dbus_hash_table_insert_string (registry->interface_table,
                                       entry->name, entry))
    {
      dbus_free (entry);
      return NULL;
    }
  
  return entry;
}

static void
delete_interface (DBusObjectRegistry *registry,
                  DBusInterfaceEntry *entry)
{
  _dbus_hash_table_remove_string (registry->interface_table,
                                  entry->name);
}

static dbus_bool_t
interface_entry_add_object (DBusInterfaceEntry *entry,
                            dbus_uint16_t       object_index)
{
  if (entry->n_objects == entry->n_allocated)
    {
      unsigned int new_alloc;
      dbus_uint16_t *new_objects;
      
      if (entry->n_allocated == 0)
        new_alloc = 2;
      else
        new_alloc = entry->n_allocated * 2;

      /* Right now MAX_OBJECTS_PER_INTERFACE can't possibly be reached
       * since the max number of objects _total_ is smaller, but the
       * code is here for future robustness.
       */
      
      if (new_alloc > DBUS_MAX_OBJECTS_PER_INTERFACE)
        new_alloc = DBUS_MAX_OBJECTS_PER_INTERFACE;
      if (new_alloc == entry->n_allocated)
        {
          _dbus_warn ("Attempting to register another instance with interface %s, but max count %d reached\n",
                      entry->name, DBUS_MAX_OBJECTS_PER_INTERFACE);
          return FALSE;
        }

      new_objects = dbus_realloc (entry->objects, new_alloc * sizeof (dbus_uint16_t));
      if (new_objects == NULL)
        return FALSE;
      entry->objects = new_objects;
      entry->n_allocated = new_alloc;
    }

  _dbus_assert (entry->n_objects < entry->n_allocated);

  entry->objects[entry->n_objects] = object_index;
  entry->n_objects += 1;

  return TRUE;
}

static void
interface_entry_remove_object (DBusInterfaceEntry *entry,
                               dbus_uint16_t       object_index)
{
  unsigned int i;

  i = 0;
  while (i < entry->n_objects)
    {
      if (entry->objects[i] == object_index)
        break;
      ++i;
    }

  if (i == entry->n_objects)
    {
      _dbus_assert_not_reached ("Tried to remove object from an interface that didn't list that object\n");
      return;
    }

  memmove (&entry->objects[i],
           &entry->objects[i+1],
           (entry->n_objects - i - 1) * sizeof (entry->objects[0]));
  entry->n_objects -= 1;  
}

static void
object_remove_from_interfaces (DBusObjectRegistry *registry,
                               DBusObjectEntry    *entry)
{
  if (entry->interfaces != NULL)
    {
      int i;
      
      i = 0;
      while (entry->interfaces[i] != NULL)
        {
          DBusInterfaceEntry *iface = entry->interfaces[i];
          
          interface_entry_remove_object (iface, entry->id_index);
          if (iface->n_objects == 0)
            delete_interface (registry, iface);
          ++i;
        }
    }
}

dbus_bool_t
_dbus_object_registry_add_and_unlock (DBusObjectRegistry      *registry,
                                      const char             **interfaces,
                                      const DBusObjectVTable  *vtable,
                                      void                    *object_impl,
                                      DBusObjectID            *object_id)
{
  int idx;
  int i;
  DBusObjectInfo info;
  
  if (registry->n_entries_used == registry->n_entries_allocated)
    {
      DBusObjectEntry *new_entries;
      int new_alloc;

      if (registry->n_entries_allocated == 0)
        new_alloc = 16;
      else
        {
          if (registry->n_entries_allocated == DBUS_MAX_OBJECTS_PER_CONNECTION)
            {
              _dbus_warn ("Attempting to register a new D-BUS object, but maximum object count of %d reached\n",
                          DBUS_MAX_OBJECTS_PER_CONNECTION);
              goto out_0;
            }

          new_alloc = registry->n_entries_allocated * 2;
          if (new_alloc > DBUS_MAX_OBJECTS_PER_CONNECTION)
            new_alloc = DBUS_MAX_OBJECTS_PER_CONNECTION;
        }

      new_entries = dbus_realloc (registry->entries,
                                  new_alloc * sizeof (DBusObjectEntry));

      if (new_entries == NULL)
        goto out_0;

      memset (&new_entries[registry->n_entries_allocated],
              '\0',
              sizeof (DBusObjectEntry) * (new_alloc - registry->n_entries_allocated));

      registry->entries = new_entries;
      registry->n_entries_allocated = new_alloc;
    }
  _dbus_assert (registry->n_entries_used < registry->n_entries_allocated);

  /* We linear search for an available entry. However, short-circuit
   * the hopefully-common situation where we don't have a sparse
   * array.
   */
  if (registry->entries[registry->n_entries_used].vtable == NULL)
    {
      idx = registry->n_entries_used;
    }
  else
    {
      /* If we do have a sparse array, we try to get rid of it rather
       * than using empty slots on the end, so we won't hit this case
       * next time.
       */

      /* If index n_entries_used is occupied, then
       * there is at least one entry outside of
       * the range [0, n_entries_used). Thus, there is
       * at least one blank entry inside that range.
       */
      idx = 0;
      while (idx < registry->n_entries_used)
        {
          if (registry->entries[idx].vtable == NULL)
            break;
          ++idx;
        }

      _dbus_assert (idx < registry->n_entries_used);
    }
  
  registry->entries[idx].id_index = idx;
  /* Overflow is OK here, but zero isn't as it's a null ID */
  registry->entries[idx].id_times_used += 1;
  if (registry->entries[idx].id_times_used == 0)
    registry->entries[idx].id_times_used += 1;
    
  registry->entries[idx].vtable = vtable;
  registry->entries[idx].object_impl = object_impl;

  registry->n_entries_used += 1;

  i = 0;
  if (interfaces != NULL)
    {
      while (interfaces[i] != NULL)
        ++i;
    }

  if (i > 0)
    {
      DBusInterfaceEntry **new_interfaces;
      
      new_interfaces = 
        dbus_realloc (registry->entries[idx].interfaces,
                      (i + 1) * sizeof (DBusInterfaceEntry*));
      
      if (new_interfaces == NULL)
        {
          /* maintain invariant that .interfaces array points to something
           * valid in oom handler (entering this function it pointed to
           * stale data but a valid malloc block)
           */
          dbus_free (registry->entries[idx].interfaces);
          registry->entries[idx].interfaces = NULL;
          goto out_1;
        }

      /* NULL-init so it's NULL-terminated and the OOM
       * case can see how far we got
       */
      while (i >= 0)
        {
          new_interfaces[i] = NULL;
          --i;
        }
      
      registry->entries[idx].interfaces = new_interfaces;
    }
  else
    {
      dbus_free (registry->entries[idx].interfaces);
      registry->entries[idx].interfaces = NULL;
    }

  /* Fill in interfaces */
  if (interfaces != NULL)
    {
      i = 0;
      while (interfaces[i] != NULL)
        {
          DBusInterfaceEntry *iface;
          
          iface = lookup_interface (registry, interfaces[i],
                                    TRUE);
          if (iface == NULL)
            goto out_1;
          
          if (!interface_entry_add_object (iface, idx))
            {
              if (iface->n_objects == 0)
                delete_interface (registry, iface);
              goto out_1;
            }
          
          registry->entries[idx].interfaces[i] = iface;
          
          ++i;
        }
    }
  
  info_from_entry (registry, &info, &registry->entries[idx]);
  if (object_id)
    *object_id = info.object_id;
  
  /* Drop lock and invoke application code */
#ifdef DBUS_BUILD_TESTS
  if (registry->connection)
#endif
    _dbus_connection_unlock (registry->connection);
  
  (* vtable->registered) (&info);

  return TRUE;
  
 out_1:    
  registry->entries[idx].vtable = NULL;
  registry->entries[idx].object_impl = NULL;
  registry->n_entries_used -= 1;

  object_remove_from_interfaces (registry,
                                 &registry->entries[idx]);
  
 out_0:
#ifdef DBUS_BUILD_TESTS
  if (registry->connection)
#endif
    _dbus_connection_unlock (registry->connection);
  return FALSE;
}

void
_dbus_object_registry_remove_and_unlock (DBusObjectRegistry *registry,
                                         const DBusObjectID *object_id)
{
  DBusObjectInfo info;
  DBusObjectEntry *entry;
  const DBusObjectVTable *vtable;

  entry = validate_id (registry, object_id);
  if (entry == NULL)
    {
      _dbus_warn ("Tried to unregister a nonexistent D-BUS object ID\n");
#ifdef DBUS_BUILD_TESTS
      if (registry->connection)
#endif
        _dbus_connection_unlock (registry->connection);
      
      return;
    }

  object_remove_from_interfaces (registry, entry);
  
  info_from_entry (registry, &info, entry);
  vtable = entry->vtable;
  entry->vtable = NULL;
  entry->object_impl = NULL;
  registry->n_entries_used -= 1;
  
  /* Drop lock and invoke application code */
#ifdef DBUS_BUILD_TESTS
  if (registry->connection)
#endif
    _dbus_connection_unlock (registry->connection);

  (* vtable->unregistered) (&info);
}

/**
 * Handle a message, passing it to any objects in the registry that
 * should receive it.
 *
 * @todo handle messages to an object ID, not just those to
 * an interface name.
 * 
 * @param registry the object registry
 * @param message the message to handle
 * @returns what to do with the message next
 */
DBusHandlerResult
_dbus_object_registry_handle_and_unlock (DBusObjectRegistry *registry,
                                         DBusMessage        *message)
{
  DBusInterfaceEntry *iface_entry;
  DBusObjectEntry *object_entry;
  DBusObjectInfo info;
  const DBusObjectVTable *vtable;

  _dbus_assert (registry != NULL);
  _dbus_assert (message != NULL);
  
  if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
    return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
  
  /* If the message isn't to a specific object ID, we send
   * it to the first object that supports the given interface.
   */
  iface_entry = lookup_interface (registry,
                                  dbus_message_get_name (message),
                                  FALSE);
  
  if (iface_entry == NULL)
    return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
  
  _dbus_assert (iface_entry->n_objects > 0);
  _dbus_assert (iface_entry->objects != NULL);

  object_entry = &registry->entries[iface_entry->objects[0]];


  /* Once we have an object entry, pass message to the object */
  
  _dbus_assert (object_entry->vtable != NULL);

  info_from_entry (registry, &info, object_entry);
  vtable = object_entry->vtable;
  
  /* Drop lock and invoke application code */
#ifdef DBUS_BUILD_TESTS
  if (registry->connection)
#endif
    _dbus_connection_unlock (registry->connection);
  
  (* vtable->message) (&info, message);
  
  return DBUS_HANDLER_RESULT_REMOVE_MESSAGE;
}

void
_dbus_object_registry_free_all_unlocked (DBusObjectRegistry *registry)
{
  int i;
  
  i = 0;
  while (registry->n_entries_used > 0)
    {
      _dbus_assert (i < registry->n_entries_allocated);
      if (registry->entries[i].vtable != NULL)
        {
          DBusObjectInfo info;
          const DBusObjectVTable *vtable;

          object_remove_from_interfaces (registry,
                                         &registry->entries[i]);
          
          info_from_entry (registry, &info, &registry->entries[i]);
          vtable = registry->entries[i].vtable;
          registry->entries[i].vtable = NULL;
          registry->entries[i].object_impl = NULL;
          registry->n_entries_used -= 1;
          _dbus_assert (registry->n_entries_used >= 0);

          (* vtable->unregistered) (&info);
        }

      ++i;
    }

  _dbus_assert (registry->n_entries_used == 0);
}

/** @} */

#ifdef DBUS_BUILD_TESTS
#include "dbus-test.h"
#include <stdio.h>

static void
noop_message_function (DBusObjectInfo *info,
                       DBusMessage    *message)
{
  /* nothing */
}

static void
add_and_remove_objects (DBusObjectRegistry *registry)
{
#define N_OBJECTS 73
  DBusObjectID ids[N_OBJECTS];
  const char *zero_interfaces[] = { NULL };
  const char *one_interface[] = { "org.freedesktop.Test.Blah", NULL };
  const char *three_interfaces[] = { "org.freedesktop.Test.Blah",
                                     "org.freedesktop.Test.Baz",
                                     "org.freedesktop.Test.Foo",
                                     NULL };
  int i;
  DBusMessage *message;
  
  i = 0;
  while (i < N_OBJECTS)
    {
      DBusCallbackObject *callback;
      const char **interfaces;
      
      callback = dbus_callback_object_new (noop_message_function, NULL, NULL);
      if (callback == NULL)
        goto out;
      
      switch (i % 3)
        {
        case 0:
          interfaces = zero_interfaces;
          break;
        case 1:
          interfaces = one_interface;
          break;
        case 2:
          interfaces = three_interfaces;
          break;
        }
      
      if (!_dbus_object_registry_add_and_unlock (registry,
                                                 interfaces,
                                                 dbus_callback_object_vtable,
                                                 callback,
                                                 &ids[i]))
        {
          dbus_callback_object_unref (callback);
          goto out;
        }

      dbus_callback_object_unref (callback);
      
      ++i;
    }
                                     
  i = 0;
  while (i < N_OBJECTS)
    {
      if (i > (N_OBJECTS - 20) || (i % 3) == 0)
        {
          _dbus_object_registry_remove_and_unlock (registry,
                                                   &ids[i]);
          dbus_object_id_set_null (&ids[i]);
        }
      
      ++i;
    }
                                     
  i = 0;
  while (i < N_OBJECTS)
    {
      if (dbus_object_id_is_null (&ids[i]))
        {
          DBusCallbackObject *callback;
          const char **interfaces;
      
          callback = dbus_callback_object_new (noop_message_function, NULL, NULL);
          if (callback == NULL)
            goto out;
          
          switch (i % 4)
            {
            case 0:
              interfaces = NULL;
              break;
            case 1:
              interfaces = zero_interfaces;
              break;
            case 2:
              interfaces = one_interface;
              break;
            case 3:
              interfaces = three_interfaces;
              break;
            }
      
          if (!_dbus_object_registry_add_and_unlock (registry,
                                                     interfaces,
                                                     dbus_callback_object_vtable,
                                                     callback,
                                                     &ids[i]))
            {
              dbus_callback_object_unref (callback);
              goto out;
            }
          
          dbus_callback_object_unref (callback);
        }
      
      ++i;
    }

  message = dbus_message_new_method_call ("org.freedesktop.Test.Foo", NULL);
  if (message != NULL)
    {
      if (_dbus_object_registry_handle_and_unlock (registry, message) !=
          DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
        _dbus_assert_not_reached ("message not handled\n");
      dbus_message_unref (message);
    }

  message = dbus_message_new_method_call ("org.freedesktop.Test.Blah", NULL);
  if (message != NULL)
    {
      if (_dbus_object_registry_handle_and_unlock (registry, message) !=
          DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
        _dbus_assert_not_reached ("message not handled\n");
      dbus_message_unref (message);
    }

  message = dbus_message_new_method_call ("org.freedesktop.Test.NotRegisteredIface", NULL);
  if (message != NULL)
    {
      if (_dbus_object_registry_handle_and_unlock (registry, message) !=
          DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS)
        _dbus_assert_not_reached ("message handled but no handler was registered\n");
      dbus_message_unref (message);
    }
  
  i = 0;
  while (i < (N_OBJECTS - 30))
    {
      _dbus_assert (!dbus_object_id_is_null (&ids[i]));
      
      _dbus_object_registry_remove_and_unlock (registry,
                                               &ids[i]);
      ++i;
    }

 out:
  /* unregister the rest this way, to test this function */
  _dbus_object_registry_free_all_unlocked (registry);
}

static dbus_bool_t
object_registry_test_iteration (void *data)
{
  DBusObjectRegistry *registry;
  
  registry = _dbus_object_registry_new (NULL);
  if (registry == NULL)
    return TRUE;

  /* we do this twice since realloc behavior will differ each time,
   * and the IDs will get recycled leading to slightly different
   * codepaths
   */
  add_and_remove_objects (registry);
  add_and_remove_objects (registry);
  
  _dbus_object_registry_unref (registry);

  return TRUE;
}

/**
 * @ingroup DBusObjectRegistry
 * Unit test for DBusObjectRegistry
 * @returns #TRUE on success.
 */
dbus_bool_t
_dbus_object_registry_test (void)
{
  _dbus_test_oom_handling ("object registry",
                           object_registry_test_iteration,
                           NULL);
  
  return TRUE;
}

#endif /* DBUS_BUILD_TESTS */