2003-09-29 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / dbus / dbus-object-tree.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-object-tree.c  DBusObjectTree (internals of DBusConnection)
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-object-tree.h"
24 #include "dbus-connection-internal.h"
25 #include "dbus-internals.h"
26 #include "dbus-hash.h"
27 #include "dbus-protocol.h"
28 #include "dbus-string.h"
29 #include <string.h>
30 #include <stdlib.h>
31
32 /**
33  * @defgroup DBusObjectTree A hierarchy of objects with container-contained relationship
34  * @ingroup  DBusInternals
35  * @brief DBusObjectTree is used by DBusConnection to track the object tree
36  *
37  * Types and functions related to DBusObjectTree. These
38  * are all library-internal.
39  *
40  * @{
41  */
42
43 /** Subnode of the object hierarchy */
44 typedef struct DBusObjectSubtree DBusObjectSubtree;
45
46 static DBusObjectSubtree* _dbus_object_subtree_new   (const char                  *name,
47                                                       const DBusObjectPathVTable  *vtable,
48                                                       void                        *user_data);
49 static void               _dbus_object_subtree_ref   (DBusObjectSubtree           *subtree);
50 static void               _dbus_object_subtree_unref (DBusObjectSubtree           *subtree);
51
52 /**
53  * Internals of DBusObjectTree
54  */
55 struct DBusObjectTree
56 {
57   int                 refcount;   /**< Reference count */
58   DBusConnection     *connection; /**< Connection this tree belongs to */
59
60   DBusObjectSubtree  *root;       /**< Root of the tree ("/" node) */
61 };
62
63 /**
64  * Struct representing a single registered subtree handler, or node
65  * that's a parent of a registered subtree handler. If
66  * message_function != NULL there's actually a handler at this node.
67  */
68 struct DBusObjectSubtree
69 {
70   DBusAtomic                         refcount;            /**< Reference count */
71   DBusObjectSubtree                 *parent;              /**< Parent node */
72   DBusObjectPathUnregisterFunction   unregister_function; /**< Function to call on unregister */
73   DBusObjectPathMessageFunction      message_function;    /**< Function to handle messages */
74   void                              *user_data;           /**< Data for functions */
75   DBusObjectSubtree                **subtrees;            /**< Child nodes */
76   int                                n_subtrees;          /**< Number of child nodes */
77   unsigned int                       subtrees_sorted : 1; /**< Whether children are sorted */
78   unsigned int                       invoke_as_fallback : 1; /**< Whether to invoke message_function when child nodes don't handle the message */
79   char                               name[1]; /**< Allocated as large as necessary */
80 };
81
82 /**
83  * Creates a new object tree, representing a mapping from paths
84  * to handler vtables.
85  *
86  * @param connection the connection this tree belongs to
87  * @returns the new tree or #NULL if no memory
88  */
89 DBusObjectTree*
90 _dbus_object_tree_new (DBusConnection *connection)
91 {
92   DBusObjectTree *tree;
93
94   /* the connection passed in here isn't fully constructed,
95    * so don't do anything more than store a pointer to
96    * it
97    */
98
99   tree = dbus_new0 (DBusObjectTree, 1);
100   if (tree == NULL)
101     goto oom;
102
103   tree->refcount = 1;
104   tree->connection = connection;
105   tree->root = _dbus_object_subtree_new ("/", NULL, NULL);
106   if (tree->root == NULL)
107     goto oom;
108   tree->root->invoke_as_fallback = TRUE;
109   
110   return tree;
111
112  oom:
113   if (tree)
114     {
115       dbus_free (tree);
116     }
117
118   return NULL;
119 }
120
121 /**
122  * Increment the reference count
123  * @param tree the object tree
124  */
125 void
126 _dbus_object_tree_ref (DBusObjectTree *tree)
127 {
128   _dbus_assert (tree->refcount > 0);
129
130   tree->refcount += 1;
131 }
132
133 /**
134  * Decrement the reference count
135  * @param tree the object tree
136  */
137 void
138 _dbus_object_tree_unref (DBusObjectTree *tree)
139 {
140   _dbus_assert (tree->refcount > 0);
141
142   tree->refcount -= 1;
143
144   if (tree->refcount == 0)
145     {
146       _dbus_object_tree_free_all_unlocked (tree);
147
148       dbus_free (tree);
149     }
150 }
151
152 static int
153 subtree_cmp (DBusObjectSubtree *subtree_a,
154              DBusObjectSubtree *subtree_b)
155 {
156   return strcmp (subtree_a->name, subtree_b->name);
157 }
158
159 static int
160 subtree_qsort_cmp (const void *a,
161                    const void *b)
162 {
163   DBusObjectSubtree **subtree_a_p = (void*) a;
164   DBusObjectSubtree **subtree_b_p = (void*) b;
165
166   return subtree_cmp (*subtree_a_p, *subtree_b_p);
167 }
168
169 static void
170 ensure_sorted (DBusObjectSubtree *subtree)
171 {
172   if (subtree->subtrees && !subtree->subtrees_sorted)
173     {
174       qsort (subtree->subtrees,
175              subtree->n_subtrees,
176              sizeof (DBusObjectSubtree*),
177              subtree_qsort_cmp);
178       subtree->subtrees_sorted = TRUE;
179     }
180 }
181
182 /** Set to 1 to get a bunch of debug spew about finding the
183  * subtree nodes
184  */
185 #define VERBOSE_FIND 0
186
187 static DBusObjectSubtree*
188 find_subtree_recurse (DBusObjectSubtree  *subtree,
189                       const char        **path,
190                       dbus_bool_t         return_deepest_match,
191                       dbus_bool_t         create_if_not_found,
192                       int                *index_in_parent)
193 {
194   int i;
195
196   _dbus_assert (!(return_deepest_match && create_if_not_found));
197
198   if (path[0] == NULL)
199     {
200 #if VERBOSE_FIND
201       _dbus_verbose ("  path exhausted, returning %s\n",
202                      subtree->name);
203 #endif
204       return subtree;
205     }
206
207 #if VERBOSE_FIND
208   _dbus_verbose ("  searching children of %s for %s\n",
209                  subtree->name, path[0]);
210 #endif
211   
212   ensure_sorted (subtree);
213
214   /* FIXME we should do a binary search here instead
215    * of O(n)
216    */
217
218   i = 0;
219   while (i < subtree->n_subtrees)
220     {
221       int v;
222
223       v = strcmp (path[0], subtree->subtrees[i]->name);
224
225 #if VERBOSE_FIND
226       _dbus_verbose ("  %s cmp %s = %d\n",
227                      path[0], subtree->subtrees[i]->name,
228                      v);
229 #endif
230       
231       if (v == 0)
232         {
233           if (index_in_parent)
234             {
235 #if VERBOSE_FIND
236               _dbus_verbose ("  storing parent index %d\n", i);
237 #endif
238               *index_in_parent = i;
239             }
240
241           if (return_deepest_match)
242             {
243               DBusObjectSubtree *next;
244
245               next = find_subtree_recurse (subtree->subtrees[i],
246                                            &path[1], return_deepest_match,
247                                            create_if_not_found, index_in_parent);
248               if (next == NULL &&
249                   subtree->invoke_as_fallback)
250                 {
251 #if VERBOSE_FIND
252                   _dbus_verbose ("  no deeper match found, returning %s\n",
253                                  subtree->name);
254 #endif
255                   return subtree;
256                 }
257               else
258                 return next;
259             }
260           else
261             return find_subtree_recurse (subtree->subtrees[i],
262                                          &path[1], return_deepest_match,
263                                          create_if_not_found, index_in_parent);
264         }
265       else if (v < 0)
266         {
267           goto not_found;
268         }
269
270       ++i;
271     }
272
273  not_found:
274 #if VERBOSE_FIND
275   _dbus_verbose ("  no match found, current tree %s, create_if_not_found = %d\n",
276                  subtree->name, create_if_not_found);
277 #endif
278   
279   if (create_if_not_found)
280     {
281       DBusObjectSubtree* child;
282       DBusObjectSubtree **new_subtrees;
283       int new_n_subtrees;
284
285 #if VERBOSE_FIND
286       _dbus_verbose ("  creating subtree %s\n",
287                      path[0]);
288 #endif
289       
290       child = _dbus_object_subtree_new (path[0],
291                                         NULL, NULL);
292       if (child == NULL)
293         return NULL;
294
295       /* FIXME we should do the "double alloc each time" standard thing */
296       new_n_subtrees = subtree->n_subtrees + 1;
297       new_subtrees = dbus_realloc (subtree->subtrees,
298                                    new_n_subtrees * sizeof (DBusObjectSubtree*));
299       if (new_subtrees == NULL)
300         {
301           child->unregister_function = NULL;
302           child->message_function = NULL;
303           _dbus_object_subtree_unref (child);
304           return FALSE;
305         }
306
307       new_subtrees[subtree->n_subtrees] = child;
308       if (index_in_parent)
309         *index_in_parent = subtree->n_subtrees;
310       subtree->subtrees_sorted = FALSE;
311       subtree->n_subtrees = new_n_subtrees;
312       subtree->subtrees = new_subtrees;
313
314       child->parent = subtree;
315
316       return find_subtree_recurse (child,
317                                    &path[1], return_deepest_match,
318                                    create_if_not_found, index_in_parent);
319     }
320   else
321     return (return_deepest_match && subtree->invoke_as_fallback) ? subtree : NULL;
322 }
323
324 static DBusObjectSubtree*
325 find_subtree (DBusObjectTree *tree,
326               const char    **path,
327               int            *index_in_parent)
328 {
329   DBusObjectSubtree *subtree;
330
331 #if VERBOSE_FIND
332   _dbus_verbose ("Looking for exact registered subtree\n");
333 #endif
334   
335   subtree = find_subtree_recurse (tree->root, path, FALSE, FALSE, index_in_parent);
336
337   if (subtree && subtree->message_function == NULL)
338     return NULL;
339   else
340     return subtree;
341 }
342
343 static DBusObjectSubtree*
344 find_handler (DBusObjectTree *tree,
345               const char    **path)
346 {
347 #if VERBOSE_FIND
348   _dbus_verbose ("Looking for deepest handler\n");
349 #endif
350   return find_subtree_recurse (tree->root, path, TRUE, FALSE, NULL);
351 }
352
353 static DBusObjectSubtree*
354 ensure_subtree (DBusObjectTree *tree,
355                 const char    **path)
356 {
357 #if VERBOSE_FIND
358   _dbus_verbose ("Ensuring subtree\n");
359 #endif
360   return find_subtree_recurse (tree->root, path, FALSE, TRUE, NULL);
361 }
362
363 /**
364  * Registers a new subtree in the global object tree.
365  *
366  * @param tree the global object tree
367  * @param fallback #TRUE to handle messages to children of this path
368  * @param path NULL-terminated array of path elements giving path to subtree
369  * @param vtable the vtable used to traverse this subtree
370  * @param user_data user data to pass to methods in the vtable
371  * @returns #FALSE if not enough memory
372  */
373 dbus_bool_t
374 _dbus_object_tree_register (DBusObjectTree              *tree,
375                             dbus_bool_t                  fallback,
376                             const char                 **path,
377                             const DBusObjectPathVTable  *vtable,
378                             void                        *user_data)
379 {
380   DBusObjectSubtree  *subtree;
381
382   _dbus_assert (tree != NULL);
383   _dbus_assert (vtable->message_function != NULL);
384   _dbus_assert (path != NULL);
385
386   subtree = ensure_subtree (tree, path);
387   if (subtree == NULL)
388     return FALSE;
389
390 #ifndef DBUS_DISABLE_CHECKS
391   if (subtree->message_function != NULL)
392     {
393       _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
394                   path[0] ? path[0] : "null");
395       return FALSE;
396     }
397 #else
398   _dbus_assert (subtree->message_function == NULL);
399 #endif
400
401   subtree->message_function = vtable->message_function;
402   subtree->unregister_function = vtable->unregister_function;
403   subtree->user_data = user_data;
404   subtree->invoke_as_fallback = fallback != FALSE;
405   
406   return TRUE;
407 }
408
409 /**
410  * Unregisters an object subtree that was registered with the
411  * same path.
412  *
413  * @param tree the global object tree
414  * @param path path to the subtree (same as the one passed to _dbus_object_tree_register())
415  */
416 void
417 _dbus_object_tree_unregister_and_unlock (DBusObjectTree          *tree,
418                                          const char             **path)
419 {
420   int i;
421   DBusObjectSubtree *subtree;
422   DBusObjectPathUnregisterFunction unregister_function;
423   void *user_data;
424   DBusConnection *connection;
425
426   _dbus_assert (path != NULL);
427
428   subtree = find_subtree (tree, path, &i);
429
430 #ifndef DBUS_DISABLE_CHECKS
431   if (subtree == NULL)
432     {
433       _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
434                   path[0] ? path[0] : "null",
435                   path[1] ? path[1] : "null");
436       return;
437     }
438 #else
439   _dbus_assert (subtree != NULL);
440 #endif
441
442   _dbus_assert (subtree->parent == NULL ||
443                 (i >= 0 && subtree->parent->subtrees[i] == subtree));
444
445   subtree->message_function = NULL;
446
447   unregister_function = subtree->unregister_function;
448   user_data = subtree->user_data;
449
450   subtree->unregister_function = NULL;
451   subtree->user_data = NULL;
452
453   /* If we have no subtrees of our own, remove from
454    * our parent (FIXME could also be more aggressive
455    * and remove our parent if it becomes empty)
456    */
457   if (subtree->parent && subtree->n_subtrees == 0)
458     {
459       /* assumes a 0-byte memmove is OK */
460       memmove (&subtree->parent->subtrees[i],
461                &subtree->parent->subtrees[i+1],
462                (subtree->parent->n_subtrees - i - 1) *
463                sizeof (subtree->parent->subtrees[0]));
464       subtree->parent->n_subtrees -= 1;
465
466       subtree->parent = NULL;
467
468       _dbus_object_subtree_unref (subtree);
469     }
470   subtree = NULL;
471
472   connection = tree->connection;
473
474   /* Unlock and call application code */
475 #ifdef DBUS_BUILD_TESTS
476   if (connection)
477 #endif
478     {
479       _dbus_connection_ref_unlocked (connection);
480       _dbus_connection_unlock (connection);
481     }
482
483   if (unregister_function)
484     (* unregister_function) (connection, user_data);
485
486 #ifdef DBUS_BUILD_TESTS
487   if (connection)
488 #endif
489     dbus_connection_unref (connection);
490 }
491
492 static void
493 free_subtree_recurse (DBusConnection    *connection,
494                       DBusObjectSubtree *subtree)
495 {
496   /* Delete them from the end, for slightly
497    * more robustness against odd reentrancy.
498    */
499   while (subtree->n_subtrees > 0)
500     {
501       DBusObjectSubtree *child;
502
503       child = subtree->subtrees[subtree->n_subtrees - 1];
504       subtree->subtrees[subtree->n_subtrees - 1] = NULL;
505       subtree->n_subtrees -= 1;
506       child->parent = NULL;
507
508       free_subtree_recurse (connection, child);
509     }
510
511   /* Call application code */
512   if (subtree->unregister_function)
513     {
514       (* subtree->unregister_function) (connection,
515                                         subtree->user_data);
516       subtree->message_function = NULL;
517       subtree->unregister_function = NULL;
518       subtree->user_data = NULL;
519     }
520
521   /* Now free ourselves */
522   _dbus_object_subtree_unref (subtree);
523 }
524
525 /**
526  * Free all the handlers in the tree. Lock on tree's connection
527  * must not be held.
528  *
529  * @param tree the object tree
530  */
531 void
532 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
533 {
534   if (tree->root)
535     free_subtree_recurse (tree->connection,
536                           tree->root);
537   tree->root = NULL;
538 }
539
540 static dbus_bool_t
541 _dbus_object_tree_list_registered_unlocked (DBusObjectTree *tree,
542                                             const char    **parent_path,
543                                             char         ***child_entries)
544 {
545   DBusObjectSubtree *subtree;
546   char **retval;
547   
548   _dbus_assert (parent_path != NULL);
549   _dbus_assert (child_entries != NULL);
550
551   *child_entries = NULL;
552   
553   subtree = find_subtree (tree, parent_path, NULL);
554   if (subtree == NULL)
555     {
556       retval = dbus_new0 (char *, 1);
557       if (retval == NULL)
558         goto out;
559     }
560   else
561     {
562       int i;
563       retval = dbus_new0 (char*, subtree->n_subtrees + 1);
564       if (retval == NULL)
565         goto out;
566       i = 0;
567       while (i < subtree->n_subtrees)
568         {
569           retval[i] = _dbus_strdup (subtree->subtrees[i]->name);
570           if (retval[i] == NULL)
571             {
572               dbus_free_string_array (retval);
573               retval = NULL;
574               goto out;
575             }
576           ++i;
577         }
578     }
579
580  out:
581     
582   *child_entries = retval;
583   return retval != NULL;
584 }
585
586 static DBusHandlerResult
587 handle_default_introspect_unlocked (DBusObjectTree          *tree,
588                                     DBusMessage             *message,
589                                     const char             **path)
590 {
591   DBusString xml;
592   DBusHandlerResult result;
593   char **children;
594   int i;
595
596   if (!dbus_message_is_method_call (message,
597                                     DBUS_INTERFACE_ORG_FREEDESKTOP_INTROSPECTABLE,
598                                     "Introspect"))
599     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
600   
601   if (!_dbus_string_init (&xml))
602     return DBUS_HANDLER_RESULT_NEED_MEMORY;
603
604   result = DBUS_HANDLER_RESULT_NEED_MEMORY;
605
606   children = NULL;
607   if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
608     goto out;
609
610   if (!_dbus_string_append (&xml, "<node>\n"))
611     goto out;
612
613   i = 0;
614   while (children[i] != NULL)
615     {
616       if (!_dbus_string_append_printf (&xml, "  <node name=\"%s\"/>\n",
617                                        children[i]))
618         goto out;
619
620       ++i;
621     }
622
623   if (!_dbus_string_append (&xml, "</node>\n"))
624     goto out;
625   
626   result = DBUS_HANDLER_RESULT_HANDLED;
627   
628  out:
629   _dbus_string_free (&xml);
630   dbus_free_string_array (children);
631   
632   return result;
633 }
634
635 /**
636  * Tries to dispatch a message by directing it to handler for the
637  * object path listed in the message header, if any. Messages are
638  * dispatched first to the registered handler that matches the largest
639  * number of path elements; that is, message to /foo/bar/baz would go
640  * to the handler for /foo/bar before the one for /foo.
641  *
642  * @todo thread problems
643  *
644  * @param tree the global object tree
645  * @param message the message to dispatch
646  * @returns whether message was handled successfully
647  */
648 DBusHandlerResult
649 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
650                                        DBusMessage             *message)
651 {
652   char **path;
653   DBusList *list;
654   DBusList *link;
655   DBusHandlerResult result;
656   DBusObjectSubtree *subtree;
657   
658 #if 0
659   _dbus_verbose ("Dispatch of message by object path\n");
660 #endif
661   
662   path = NULL;
663   if (!dbus_message_get_path_decomposed (message, &path))
664     {
665 #ifdef DBUS_BUILD_TESTS
666       if (tree->connection)
667 #endif
668         _dbus_connection_unlock (tree->connection);
669       
670       _dbus_verbose ("No memory to get decomposed path\n");
671
672       return DBUS_HANDLER_RESULT_NEED_MEMORY;
673     }
674
675   if (path == NULL)
676     {
677 #ifdef DBUS_BUILD_TESTS
678       if (tree->connection)
679 #endif
680         _dbus_connection_unlock (tree->connection);
681       
682       _dbus_verbose ("No path field in message\n");
683       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
684     }
685   
686   /* Find the deepest path that covers the path in the message */
687   subtree = find_handler (tree, (const char**) path);
688   
689   /* Build a list of all paths that cover the path in the message */
690
691   list = NULL;
692
693   while (subtree != NULL)
694     {
695       if (subtree->message_function != NULL)
696         {
697           _dbus_object_subtree_ref (subtree);
698
699           /* run deepest paths first */
700           if (!_dbus_list_append (&list, subtree))
701             {
702               result = DBUS_HANDLER_RESULT_NEED_MEMORY;
703               _dbus_object_subtree_unref (subtree);
704               goto free_and_return;
705             }
706         }
707
708       subtree = subtree->parent;
709     }
710
711   _dbus_verbose ("%d handlers in the path tree for this message\n",
712                  _dbus_list_get_length (&list));
713
714   /* Invoke each handler in the list */
715
716   result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
717
718   link = _dbus_list_get_first_link (&list);
719   while (link != NULL)
720     {
721       DBusList *next = _dbus_list_get_next_link (&list, link);
722       subtree = link->data;
723
724       /* message_function is NULL if we're unregistered
725        * due to reentrancy
726        */
727       if (subtree->message_function)
728         {
729           DBusObjectPathMessageFunction message_function;
730           void *user_data;
731
732           message_function = subtree->message_function;
733           user_data = subtree->user_data;
734
735 #if 0
736           _dbus_verbose ("  (invoking a handler)\n");
737 #endif
738           
739 #ifdef DBUS_BUILD_TESTS
740           if (tree->connection)
741 #endif
742             _dbus_connection_unlock (tree->connection);
743
744           /* FIXME you could unregister the subtree in another thread
745            * before we invoke the callback, and I can't figure out a
746            * good way to solve this.
747            */
748
749           result = (* message_function) (tree->connection,
750                                          message,
751                                          user_data);
752
753 #ifdef DBUS_BUILD_TESTS
754           if (tree->connection)
755 #endif
756             _dbus_connection_lock (tree->connection);
757
758           if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
759             goto free_and_return;
760         }
761
762       link = next;
763     }
764
765  free_and_return:
766
767   if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
768     {
769       /* This hardcoded default handler does a minimal Introspect()
770        */
771       result = handle_default_introspect_unlocked (tree, message,
772                                                    (const char**) path);
773     }
774
775 #ifdef DBUS_BUILD_TESTS
776   if (tree->connection)
777 #endif
778     _dbus_connection_unlock (tree->connection);
779   
780   while (list != NULL)
781     {
782       link = _dbus_list_get_first_link (&list);
783       _dbus_object_subtree_unref (link->data);
784       _dbus_list_remove_link (&list, link);
785     }
786   
787   dbus_free_string_array (path);
788
789   return result;
790 }
791
792 /**
793  * Allocates a subtree object.
794  *
795  * @param name name to duplicate.
796  * @returns newly-allocated subtree
797  */
798 static DBusObjectSubtree*
799 allocate_subtree_object (const char *name)
800 {
801   int len;
802   DBusObjectSubtree *subtree;
803   const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
804
805   _dbus_assert (name != NULL);
806
807   len = strlen (name);
808
809   subtree = dbus_malloc (front_padding + (len + 1));
810
811   if (subtree == NULL)
812     return NULL;
813
814   memcpy (subtree->name, name, len + 1);
815
816   return subtree;
817 }
818
819 static DBusObjectSubtree*
820 _dbus_object_subtree_new (const char                  *name,
821                           const DBusObjectPathVTable  *vtable,
822                           void                        *user_data)
823 {
824   DBusObjectSubtree *subtree;
825
826   subtree = allocate_subtree_object (name);
827   if (subtree == NULL)
828     goto oom;
829
830   _dbus_assert (name != NULL);
831
832   subtree->parent = NULL;
833
834   if (vtable)
835     {
836       subtree->message_function = vtable->message_function;
837       subtree->unregister_function = vtable->unregister_function;
838     }
839   else
840     {
841       subtree->message_function = NULL;
842       subtree->unregister_function = NULL;
843     }
844
845   subtree->user_data = user_data;
846   subtree->refcount.value = 1;
847   subtree->subtrees = NULL;
848   subtree->n_subtrees = 0;
849   subtree->subtrees_sorted = TRUE;
850   
851   return subtree;
852
853  oom:
854   if (subtree)
855     {
856       dbus_free (subtree);
857     }
858
859   return NULL;
860 }
861
862 static void
863 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
864 {
865   _dbus_assert (subtree->refcount.value > 0);
866   _dbus_atomic_inc (&subtree->refcount);
867 }
868
869 static void
870 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
871 {
872   _dbus_assert (subtree->refcount.value > 0);
873
874   if (_dbus_atomic_dec (&subtree->refcount) == 1)
875     {
876       _dbus_assert (subtree->unregister_function == NULL);
877       _dbus_assert (subtree->message_function == NULL);
878
879       dbus_free (subtree->subtrees);
880       dbus_free (subtree);
881     }
882 }
883
884 /**
885  * Lists the registered fallback handlers and object path handlers at
886  * the given parent_path. The returned array should be freed with
887  * dbus_free_string_array().
888  *
889  * @param connection the connection
890  * @param parent_path the path to list the child handlers of
891  * @param child_entries returns #NULL-terminated array of children
892  * @returns #FALSE if no memory to allocate the child entries
893  */
894 dbus_bool_t
895 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
896                                               const char    **parent_path,
897                                               char         ***child_entries)
898 {
899   dbus_bool_t result;
900
901   result = _dbus_object_tree_list_registered_unlocked (tree,
902                                                        parent_path,
903                                                        child_entries);
904   
905 #ifdef DBUS_BUILD_TESTS
906   if (tree->connection)
907 #endif
908     _dbus_connection_unlock (tree->connection);
909
910   return result;
911 }
912      
913 /** @} */
914
915 #ifdef DBUS_BUILD_TESTS
916 #include "dbus-test.h"
917 #include <stdio.h>
918
919 static char*
920 flatten_path (const char **path)
921 {
922   DBusString str;
923   int i;
924   char *s;
925
926   if (!_dbus_string_init (&str))
927     return NULL;
928
929   i = 0;
930   while (path[i])
931     {
932       if (!_dbus_string_append_byte (&str, '/'))
933         goto nomem;
934
935       if (!_dbus_string_append (&str, path[i]))
936         goto nomem;
937
938       ++i;
939     }
940
941   if (!_dbus_string_steal_data (&str, &s))
942     goto nomem;
943
944   _dbus_string_free (&str);
945
946   return s;
947
948  nomem:
949   _dbus_string_free (&str);
950   return NULL;
951 }
952
953 /* Returns TRUE if container is a parent of child
954  */
955 static dbus_bool_t
956 path_contains (const char **container,
957                const char **child)
958 {
959   int i;
960
961   i = 0;
962   while (child[i] != NULL)
963     {
964       int v;
965
966       if (container[i] == NULL)
967         return TRUE; /* container ran out, child continues;
968                       * thus the container is a parent of the
969                       * child.
970                       */
971
972       _dbus_assert (container[i] != NULL);
973       _dbus_assert (child[i] != NULL);
974
975       v = strcmp (container[i], child[i]);
976
977       if (v != 0)
978         return FALSE; /* they overlap until here and then are different,
979                        * not overlapping
980                        */
981
982       ++i;
983     }
984
985   /* Child ran out; if container also did, they are equal;
986    * otherwise, the child is a parent of the container.
987    */
988   if (container[i] == NULL)
989     return TRUE; /* equal is counted as containing */
990   else
991     return FALSE;
992 }
993
994 static void
995 spew_subtree_recurse (DBusObjectSubtree *subtree,
996                       int                indent)
997 {
998   int i;
999
1000   i = 0;
1001   while (i < indent)
1002     {
1003       _dbus_verbose (" ");
1004       ++i;
1005     }
1006
1007   _dbus_verbose ("%s (%d children)\n",
1008                  subtree->name, subtree->n_subtrees);
1009
1010   i = 0;
1011   while (i < subtree->n_subtrees)
1012     {
1013       spew_subtree_recurse (subtree->subtrees[i], indent + 2);
1014
1015       ++i;
1016     }
1017 }
1018
1019 static void
1020 spew_tree (DBusObjectTree *tree)
1021 {
1022   spew_subtree_recurse (tree->root, 0);
1023 }
1024
1025 /**
1026  * Callback data used in tests
1027  */
1028 typedef struct
1029 {
1030   const char **path; /**< Path */
1031   dbus_bool_t message_handled; /**< Gets set to true if message handler called */
1032   dbus_bool_t handler_unregistered; /**< gets set to true if handler is unregistered */
1033
1034 } TreeTestData;
1035
1036
1037 static void
1038 test_unregister_function (DBusConnection  *connection,
1039                           void            *user_data)
1040 {
1041   TreeTestData *ttd = user_data;
1042
1043   ttd->handler_unregistered = TRUE;
1044 }
1045
1046 static DBusHandlerResult
1047 test_message_function (DBusConnection  *connection,
1048                        DBusMessage     *message,
1049                        void            *user_data)
1050 {
1051   TreeTestData *ttd = user_data;
1052
1053   ttd->message_handled = TRUE;
1054
1055   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1056 }
1057
1058 static dbus_bool_t
1059 do_register (DBusObjectTree *tree,
1060              const char    **path,
1061              int             i,
1062              TreeTestData   *tree_test_data)
1063 {
1064   DBusObjectPathVTable vtable = { test_unregister_function,
1065                                   test_message_function, NULL };
1066
1067   tree_test_data[i].message_handled = FALSE;
1068   tree_test_data[i].handler_unregistered = FALSE;
1069   tree_test_data[i].path = path;
1070
1071   if (!_dbus_object_tree_register (tree, TRUE, path,
1072                                    &vtable,
1073                                    &tree_test_data[i]))
1074     return FALSE;
1075
1076   return TRUE;
1077 }
1078
1079 static dbus_bool_t
1080 do_test_dispatch (DBusObjectTree *tree,
1081                   const char    **path,
1082                   int             i,
1083                   TreeTestData   *tree_test_data,
1084                   int             n_test_data)
1085 {
1086   DBusMessage *message;
1087   int j;
1088   DBusHandlerResult result;
1089   char *flat;
1090
1091   message = NULL;
1092   
1093   flat = flatten_path (path);
1094   if (flat == NULL)
1095     goto oom;
1096
1097   message = dbus_message_new_method_call (NULL,
1098                                           flat,
1099                                           "org.freedesktop.TestInterface",
1100                                           "Foo");
1101   dbus_free (flat);
1102   if (message == NULL)
1103     goto oom;
1104
1105   j = 0;
1106   while (j < n_test_data)
1107     {
1108       tree_test_data[j].message_handled = FALSE;
1109       ++j;
1110     }
1111
1112   result = _dbus_object_tree_dispatch_and_unlock (tree, message);
1113   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
1114     goto oom;
1115
1116   _dbus_assert (tree_test_data[i].message_handled);
1117
1118   j = 0;
1119   while (j < n_test_data)
1120     {
1121       if (tree_test_data[j].message_handled)
1122         _dbus_assert (path_contains (tree_test_data[j].path,
1123                                      path));
1124       else
1125         _dbus_assert (!path_contains (tree_test_data[j].path,
1126                                       path));
1127
1128       ++j;
1129     }
1130
1131   dbus_message_unref (message);
1132
1133   return TRUE;
1134
1135  oom:
1136   if (message)
1137     dbus_message_unref (message);
1138   return FALSE;
1139 }
1140
1141 static dbus_bool_t
1142 object_tree_test_iteration (void *data)
1143 {
1144   const char *path1[] = { "foo", NULL };
1145   const char *path2[] = { "foo", "bar", NULL };
1146   const char *path3[] = { "foo", "bar", "baz", NULL };
1147   const char *path4[] = { "foo", "bar", "boo", NULL };
1148   const char *path5[] = { "blah", NULL };
1149   const char *path6[] = { "blah", "boof", NULL };
1150   const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
1151   const char *path8[] = { "childless", NULL };
1152   DBusObjectTree *tree;
1153   TreeTestData tree_test_data[8];
1154   int i;
1155
1156   tree = NULL;
1157
1158   tree = _dbus_object_tree_new (NULL);
1159   if (tree == NULL)
1160     goto out;
1161
1162   if (!do_register (tree, path1, 0, tree_test_data))
1163     goto out;
1164
1165   _dbus_assert (find_subtree (tree, path1, NULL));
1166   _dbus_assert (!find_subtree (tree, path2, NULL));
1167   _dbus_assert (!find_subtree (tree, path3, NULL));
1168   _dbus_assert (!find_subtree (tree, path4, NULL));
1169   _dbus_assert (!find_subtree (tree, path5, NULL));
1170   _dbus_assert (!find_subtree (tree, path6, NULL));
1171   _dbus_assert (!find_subtree (tree, path7, NULL));
1172   _dbus_assert (!find_subtree (tree, path8, NULL));
1173
1174   _dbus_assert (find_handler (tree, path1));
1175   _dbus_assert (find_handler (tree, path2));
1176   _dbus_assert (find_handler (tree, path3));
1177   _dbus_assert (find_handler (tree, path4));
1178   _dbus_assert (find_handler (tree, path5) == tree->root);
1179   _dbus_assert (find_handler (tree, path6) == tree->root);
1180   _dbus_assert (find_handler (tree, path7) == tree->root);
1181   _dbus_assert (find_handler (tree, path8) == tree->root);
1182
1183   if (!do_register (tree, path2, 1, tree_test_data))
1184     goto out;
1185
1186   _dbus_assert (find_subtree (tree, path1, NULL));
1187   _dbus_assert (find_subtree (tree, path2, NULL));
1188   _dbus_assert (!find_subtree (tree, path3, NULL));
1189   _dbus_assert (!find_subtree (tree, path4, NULL));
1190   _dbus_assert (!find_subtree (tree, path5, NULL));
1191   _dbus_assert (!find_subtree (tree, path6, NULL));
1192   _dbus_assert (!find_subtree (tree, path7, NULL));
1193   _dbus_assert (!find_subtree (tree, path8, NULL));
1194
1195   if (!do_register (tree, path3, 2, tree_test_data))
1196     goto out;
1197
1198   _dbus_assert (find_subtree (tree, path1, NULL));
1199   _dbus_assert (find_subtree (tree, path2, NULL));
1200   _dbus_assert (find_subtree (tree, path3, NULL));
1201   _dbus_assert (!find_subtree (tree, path4, NULL));
1202   _dbus_assert (!find_subtree (tree, path5, NULL));
1203   _dbus_assert (!find_subtree (tree, path6, NULL));
1204   _dbus_assert (!find_subtree (tree, path7, NULL));
1205   _dbus_assert (!find_subtree (tree, path8, NULL));
1206   
1207   if (!do_register (tree, path4, 3, tree_test_data))
1208     goto out;
1209
1210   _dbus_assert (find_subtree (tree, path1, NULL));
1211   _dbus_assert (find_subtree (tree, path2, NULL));
1212   _dbus_assert (find_subtree (tree, path3, NULL));  
1213   _dbus_assert (find_subtree (tree, path4, NULL));
1214   _dbus_assert (!find_subtree (tree, path5, NULL));
1215   _dbus_assert (!find_subtree (tree, path6, NULL));
1216   _dbus_assert (!find_subtree (tree, path7, NULL));
1217   _dbus_assert (!find_subtree (tree, path8, NULL));
1218   
1219   if (!do_register (tree, path5, 4, tree_test_data))
1220     goto out;
1221
1222   _dbus_assert (find_subtree (tree, path1, NULL));
1223   _dbus_assert (find_subtree (tree, path2, NULL));
1224   _dbus_assert (find_subtree (tree, path3, NULL));
1225   _dbus_assert (find_subtree (tree, path4, NULL));
1226   _dbus_assert (find_subtree (tree, path5, NULL));
1227   _dbus_assert (!find_subtree (tree, path6, NULL));
1228   _dbus_assert (!find_subtree (tree, path7, NULL));
1229   _dbus_assert (!find_subtree (tree, path8, NULL));
1230   
1231   _dbus_assert (find_handler (tree, path1) != tree->root);
1232   _dbus_assert (find_handler (tree, path2) != tree->root);
1233   _dbus_assert (find_handler (tree, path3) != tree->root);
1234   _dbus_assert (find_handler (tree, path4) != tree->root);
1235   _dbus_assert (find_handler (tree, path5) != tree->root);
1236   _dbus_assert (find_handler (tree, path6) != tree->root);
1237   _dbus_assert (find_handler (tree, path7) != tree->root);
1238   _dbus_assert (find_handler (tree, path8) == tree->root);
1239
1240   if (!do_register (tree, path6, 5, tree_test_data))
1241     goto out;
1242
1243   _dbus_assert (find_subtree (tree, path1, NULL));
1244   _dbus_assert (find_subtree (tree, path2, NULL));
1245   _dbus_assert (find_subtree (tree, path3, NULL));
1246   _dbus_assert (find_subtree (tree, path4, NULL));
1247   _dbus_assert (find_subtree (tree, path5, NULL));
1248   _dbus_assert (find_subtree (tree, path6, NULL));
1249   _dbus_assert (!find_subtree (tree, path7, NULL));
1250   _dbus_assert (!find_subtree (tree, path8, NULL));
1251
1252   if (!do_register (tree, path7, 6, tree_test_data))
1253     goto out;
1254
1255   _dbus_assert (find_subtree (tree, path1, NULL));
1256   _dbus_assert (find_subtree (tree, path2, NULL));
1257   _dbus_assert (find_subtree (tree, path3, NULL));
1258   _dbus_assert (find_subtree (tree, path4, NULL));
1259   _dbus_assert (find_subtree (tree, path5, NULL));
1260   _dbus_assert (find_subtree (tree, path6, NULL));
1261   _dbus_assert (find_subtree (tree, path7, NULL));
1262   _dbus_assert (!find_subtree (tree, path8, NULL));
1263
1264   if (!do_register (tree, path8, 7, tree_test_data))
1265     goto out;
1266
1267   _dbus_assert (find_subtree (tree, path1, NULL));
1268   _dbus_assert (find_subtree (tree, path2, NULL));
1269   _dbus_assert (find_subtree (tree, path3, NULL));
1270   _dbus_assert (find_subtree (tree, path4, NULL));
1271   _dbus_assert (find_subtree (tree, path5, NULL));
1272   _dbus_assert (find_subtree (tree, path6, NULL));
1273   _dbus_assert (find_subtree (tree, path7, NULL));
1274   _dbus_assert (find_subtree (tree, path8, NULL));
1275   
1276   _dbus_assert (find_handler (tree, path1) != tree->root);
1277   _dbus_assert (find_handler (tree, path2) != tree->root);
1278   _dbus_assert (find_handler (tree, path3) != tree->root);
1279   _dbus_assert (find_handler (tree, path4) != tree->root);
1280   _dbus_assert (find_handler (tree, path5) != tree->root);
1281   _dbus_assert (find_handler (tree, path6) != tree->root);
1282   _dbus_assert (find_handler (tree, path7) != tree->root);
1283   _dbus_assert (find_handler (tree, path8) != tree->root);
1284   
1285   /* Check that destroying tree calls unregister funcs */
1286   _dbus_object_tree_unref (tree);
1287
1288   i = 0;
1289   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1290     {
1291       _dbus_assert (tree_test_data[i].handler_unregistered);
1292       _dbus_assert (!tree_test_data[i].message_handled);
1293       ++i;
1294     }
1295
1296   /* Now start again and try the individual unregister function */
1297   tree = _dbus_object_tree_new (NULL);
1298   if (tree == NULL)
1299     goto out;
1300
1301   if (!do_register (tree, path1, 0, tree_test_data))
1302     goto out;
1303   if (!do_register (tree, path2, 1, tree_test_data))
1304     goto out;
1305   if (!do_register (tree, path3, 2, tree_test_data))
1306     goto out;
1307   if (!do_register (tree, path4, 3, tree_test_data))
1308     goto out;
1309   if (!do_register (tree, path5, 4, tree_test_data))
1310     goto out;
1311   if (!do_register (tree, path6, 5, tree_test_data))
1312     goto out;
1313   if (!do_register (tree, path7, 6, tree_test_data))
1314     goto out;
1315   if (!do_register (tree, path8, 7, tree_test_data))
1316     goto out;
1317   
1318   _dbus_object_tree_unregister_and_unlock (tree, path1);
1319
1320   _dbus_assert (!find_subtree (tree, path1, NULL));
1321   _dbus_assert (find_subtree (tree, path2, NULL));
1322   _dbus_assert (find_subtree (tree, path3, NULL));
1323   _dbus_assert (find_subtree (tree, path4, NULL));
1324   _dbus_assert (find_subtree (tree, path5, NULL));
1325   _dbus_assert (find_subtree (tree, path6, NULL));
1326   _dbus_assert (find_subtree (tree, path7, NULL));
1327   _dbus_assert (find_subtree (tree, path8, NULL));
1328
1329   _dbus_object_tree_unregister_and_unlock (tree, path2);
1330
1331   _dbus_assert (!find_subtree (tree, path1, NULL));
1332   _dbus_assert (!find_subtree (tree, path2, NULL));
1333   _dbus_assert (find_subtree (tree, path3, NULL));
1334   _dbus_assert (find_subtree (tree, path4, NULL));
1335   _dbus_assert (find_subtree (tree, path5, NULL));
1336   _dbus_assert (find_subtree (tree, path6, NULL));
1337   _dbus_assert (find_subtree (tree, path7, NULL));
1338   _dbus_assert (find_subtree (tree, path8, NULL));
1339   
1340   _dbus_object_tree_unregister_and_unlock (tree, path3);
1341
1342   _dbus_assert (!find_subtree (tree, path1, NULL));
1343   _dbus_assert (!find_subtree (tree, path2, NULL));
1344   _dbus_assert (!find_subtree (tree, path3, NULL));
1345   _dbus_assert (find_subtree (tree, path4, NULL));
1346   _dbus_assert (find_subtree (tree, path5, NULL));
1347   _dbus_assert (find_subtree (tree, path6, NULL));
1348   _dbus_assert (find_subtree (tree, path7, NULL));
1349   _dbus_assert (find_subtree (tree, path8, NULL));
1350   
1351   _dbus_object_tree_unregister_and_unlock (tree, path4);
1352
1353   _dbus_assert (!find_subtree (tree, path1, NULL));
1354   _dbus_assert (!find_subtree (tree, path2, NULL));
1355   _dbus_assert (!find_subtree (tree, path3, NULL));
1356   _dbus_assert (!find_subtree (tree, path4, NULL));
1357   _dbus_assert (find_subtree (tree, path5, NULL));
1358   _dbus_assert (find_subtree (tree, path6, NULL));
1359   _dbus_assert (find_subtree (tree, path7, NULL));
1360   _dbus_assert (find_subtree (tree, path8, NULL));
1361   
1362   _dbus_object_tree_unregister_and_unlock (tree, path5);
1363
1364   _dbus_assert (!find_subtree (tree, path1, NULL));
1365   _dbus_assert (!find_subtree (tree, path2, NULL));
1366   _dbus_assert (!find_subtree (tree, path3, NULL));
1367   _dbus_assert (!find_subtree (tree, path4, NULL));
1368   _dbus_assert (!find_subtree (tree, path5, NULL));
1369   _dbus_assert (find_subtree (tree, path6, NULL));
1370   _dbus_assert (find_subtree (tree, path7, NULL));
1371   _dbus_assert (find_subtree (tree, path8, NULL));
1372   
1373   _dbus_object_tree_unregister_and_unlock (tree, path6);
1374
1375   _dbus_assert (!find_subtree (tree, path1, NULL));
1376   _dbus_assert (!find_subtree (tree, path2, NULL));
1377   _dbus_assert (!find_subtree (tree, path3, NULL));
1378   _dbus_assert (!find_subtree (tree, path4, NULL));
1379   _dbus_assert (!find_subtree (tree, path5, NULL));
1380   _dbus_assert (!find_subtree (tree, path6, NULL));
1381   _dbus_assert (find_subtree (tree, path7, NULL));
1382   _dbus_assert (find_subtree (tree, path8, NULL));
1383
1384   _dbus_object_tree_unregister_and_unlock (tree, path7);
1385
1386   _dbus_assert (!find_subtree (tree, path1, NULL));
1387   _dbus_assert (!find_subtree (tree, path2, NULL));
1388   _dbus_assert (!find_subtree (tree, path3, NULL));
1389   _dbus_assert (!find_subtree (tree, path4, NULL));
1390   _dbus_assert (!find_subtree (tree, path5, NULL));
1391   _dbus_assert (!find_subtree (tree, path6, NULL));
1392   _dbus_assert (!find_subtree (tree, path7, NULL));
1393   _dbus_assert (find_subtree (tree, path8, NULL));
1394
1395   _dbus_object_tree_unregister_and_unlock (tree, path8);
1396
1397   _dbus_assert (!find_subtree (tree, path1, NULL));
1398   _dbus_assert (!find_subtree (tree, path2, NULL));
1399   _dbus_assert (!find_subtree (tree, path3, NULL));
1400   _dbus_assert (!find_subtree (tree, path4, NULL));
1401   _dbus_assert (!find_subtree (tree, path5, NULL));
1402   _dbus_assert (!find_subtree (tree, path6, NULL));
1403   _dbus_assert (!find_subtree (tree, path7, NULL));
1404   _dbus_assert (!find_subtree (tree, path8, NULL));
1405   
1406   i = 0;
1407   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1408     {
1409       _dbus_assert (tree_test_data[i].handler_unregistered);
1410       _dbus_assert (!tree_test_data[i].message_handled);
1411       ++i;
1412     }
1413
1414   /* Register it all again, and test dispatch */
1415
1416   if (!do_register (tree, path1, 0, tree_test_data))
1417     goto out;
1418   if (!do_register (tree, path2, 1, tree_test_data))
1419     goto out;
1420   if (!do_register (tree, path3, 2, tree_test_data))
1421     goto out;
1422   if (!do_register (tree, path4, 3, tree_test_data))
1423     goto out;
1424   if (!do_register (tree, path5, 4, tree_test_data))
1425     goto out;
1426   if (!do_register (tree, path6, 5, tree_test_data))
1427     goto out;
1428   if (!do_register (tree, path7, 6, tree_test_data))
1429     goto out;
1430   if (!do_register (tree, path8, 7, tree_test_data))
1431     goto out;
1432
1433 #if 0
1434   spew_tree (tree);
1435 #endif
1436   
1437   if (!do_test_dispatch (tree, path1, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1438     goto out;
1439   if (!do_test_dispatch (tree, path2, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1440     goto out;
1441   if (!do_test_dispatch (tree, path3, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1442     goto out;
1443   if (!do_test_dispatch (tree, path4, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1444     goto out;
1445   if (!do_test_dispatch (tree, path5, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1446     goto out;
1447   if (!do_test_dispatch (tree, path6, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1448     goto out;
1449   if (!do_test_dispatch (tree, path7, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1450     goto out;
1451   if (!do_test_dispatch (tree, path8, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1452     goto out;
1453   
1454  out:
1455   if (tree)
1456     {
1457       /* test ref */
1458       _dbus_object_tree_ref (tree);
1459       _dbus_object_tree_unref (tree);
1460       _dbus_object_tree_unref (tree);
1461     }
1462
1463   return TRUE;
1464 }
1465
1466 /**
1467  * @ingroup DBusObjectTree
1468  * Unit test for DBusObjectTree
1469  * @returns #TRUE on success.
1470  */
1471 dbus_bool_t
1472 _dbus_object_tree_test (void)
1473 {
1474   _dbus_test_oom_handling ("object tree",
1475                            object_tree_test_iteration,
1476                            NULL);
1477
1478   return TRUE;
1479 }
1480
1481 #endif /* DBUS_BUILD_TESTS */