]> git.sesse.net Git - vlc/blob - src/misc/objects.c
Recurse through the object tree instead of using the objects table
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /**
25  * \file
26  * This file contains the functions to handle the vlc_object_t type
27  */
28
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc/vlc.h>
38
39 #include "../libvlc.h"
40 #include <vlc_vout.h>
41 #include <vlc_aout.h>
42 #include "audio_output/aout_internal.h"
43
44 #include <vlc_access.h>
45 #include <vlc_demux.h>
46 #include <vlc_stream.h>
47
48 #include <vlc_sout.h>
49 #include "stream_output/stream_output.h"
50
51 #include "vlc_interface.h"
52 #include "vlc_codec.h"
53 #include "vlc_filter.h"
54
55 #include "variables.h"
56 #ifndef WIN32
57 # include <unistd.h>
58 #else
59 # include <io.h>
60 # include <fcntl.h>
61 # include <errno.h> /* ENOSYS */
62 #endif
63 #include <assert.h>
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int  DumpCommand( vlc_object_t *, char const *,
69                          vlc_value_t, vlc_value_t, void * );
70
71 static vlc_object_t * FindObject    ( vlc_object_t *, int, int );
72 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
73 static void           DetachObject  ( vlc_object_t * );
74 static void           PrintObject   ( vlc_object_t *, const char * );
75 static void           DumpStructure ( vlc_object_t *, int, char * );
76 static int            FindIndex     ( vlc_object_t *, vlc_object_t **, int );
77 static void           SetAttachment ( vlc_object_t *, bool );
78
79 static vlc_list_t   * NewList       ( int );
80 static void           ListReplace   ( vlc_list_t *, vlc_object_t *, int );
81 static void           ListAppend    ( vlc_list_t *, vlc_object_t * );
82 static int            CountChildren ( vlc_object_t *, int );
83 static void           ListChildren  ( vlc_list_t *, vlc_object_t *, int );
84
85 static void vlc_object_destroy( vlc_object_t *p_this );
86 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
87
88 /*****************************************************************************
89  * Local structure lock
90  *****************************************************************************/
91 static vlc_mutex_t    structure_lock;
92
93 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
94                          int i_type, const char *psz_type )
95 {
96     vlc_object_t *p_new;
97     vlc_object_internals_t *p_priv;
98
99     /* NOTE:
100      * VLC objects are laid out as follow:
101      * - first the LibVLC-private per-object data,
102      * - then VLC_COMMON members from vlc_object_t,
103      * - finally, the type-specific data (if any).
104      *
105      * This function initializes the LibVLC and common data,
106      * and zeroes the rest.
107      */
108     p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
109     if( p_priv == NULL )
110         return NULL;
111
112     assert (i_size >= sizeof (vlc_object_t));
113     p_new = (vlc_object_t *)(p_priv + 1);
114
115     p_new->i_object_type = i_type;
116     p_new->psz_object_type = psz_type;
117     p_new->psz_object_name = NULL;
118
119     p_new->b_die = false;
120     p_new->b_error = false;
121     p_new->b_dead = false;
122     p_priv->b_attached = false;
123     p_new->b_force = false;
124
125     p_new->psz_header = NULL;
126
127     if (p_this)
128         p_new->i_flags = p_this->i_flags
129             & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
130
131     p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
132
133     if( !p_priv->p_vars )
134     {
135         free( p_priv );
136         return NULL;
137     }
138
139     libvlc_global_data_t *p_libvlc_global;
140     if( p_this == NULL )
141     {
142         /* Only the global root object is created out of the blue */
143         p_libvlc_global = (libvlc_global_data_t *)p_new;
144         p_new->p_libvlc = NULL;
145
146         p_libvlc_global->i_counter = 0;
147         p_libvlc_global->i_objects = 0;
148         p_libvlc_global->pp_objects = NULL;
149         p_priv->b_attached = true;
150         vlc_mutex_init( &structure_lock );
151     }
152     else
153     {
154         p_libvlc_global = vlc_global();
155         if( i_type == VLC_OBJECT_LIBVLC )
156         {
157             p_new->p_libvlc = (libvlc_int_t*)p_new;
158             p_priv->b_attached = true;
159         }
160         else
161         {
162             p_new->p_libvlc = p_this->p_libvlc;
163         }
164     }
165
166     vlc_spin_init( &p_priv->ref_spin );
167     p_priv->i_refcount = 1;
168     p_priv->pf_destructor = NULL;
169     p_priv->b_thread = false;
170     p_new->p_parent = NULL;
171     p_new->pp_children = NULL;
172     p_new->i_children = 0;
173
174     p_new->p_private = NULL;
175
176     /* Initialize mutexes and condvars */
177     vlc_mutex_init( &p_new->object_lock );
178     vlc_cond_init( p_new, &p_new->object_wait );
179     vlc_mutex_init( &p_priv->var_lock );
180     vlc_spin_init( &p_priv->spin );
181     p_priv->pipes[0] = p_priv->pipes[1] = -1;
182
183     vlc_mutex_lock( &structure_lock );
184     p_new->i_object_id = p_libvlc_global->i_counter++;
185     /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
186      * useless to try and recover anything if pp_objects gets smashed. */
187     TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
188                 p_new );
189     vlc_mutex_unlock( &structure_lock );
190
191     if( i_type == VLC_OBJECT_LIBVLC )
192     {
193         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
194         var_AddCallback( p_new, "list", DumpCommand, NULL );
195         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
196         var_AddCallback( p_new, "tree", DumpCommand, NULL );
197         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
198         var_AddCallback( p_new, "vars", DumpCommand, NULL );
199     }
200
201     return p_new;
202 }
203
204
205 /**
206  * Allocates and initializes a vlc object.
207  *
208  * @param i_type known object type (all of them are negative integer values),
209  *               or object byte size (always positive).
210  *
211  * @return the new object, or NULL on error.
212  */
213 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
214 {
215     const char   * psz_type;
216     size_t         i_size;
217
218     switch( i_type )
219     {
220         case VLC_OBJECT_LIBVLC:
221             i_size = sizeof(libvlc_int_t);
222             psz_type = "libvlc";
223             break;
224         case VLC_OBJECT_INTF:
225             i_size = sizeof(intf_thread_t);
226             psz_type = "interface";
227             break;
228         case VLC_OBJECT_DIALOGS:
229             i_size = sizeof(intf_thread_t);
230             psz_type = "dialogs";
231             break;
232         case VLC_OBJECT_DEMUX:
233             i_size = sizeof(demux_t);
234             psz_type = "demux";
235             break;
236         case VLC_OBJECT_ACCESS:
237             i_size = sizeof(access_t);
238             psz_type = "access";
239             break;
240         case VLC_OBJECT_DECODER:
241             i_size = sizeof(decoder_t);
242             psz_type = "decoder";
243             break;
244         case VLC_OBJECT_PACKETIZER:
245             i_size = sizeof(decoder_t);
246             psz_type = "packetizer";
247             break;
248         case VLC_OBJECT_ENCODER:
249             i_size = sizeof(encoder_t);
250             psz_type = "encoder";
251             break;
252         case VLC_OBJECT_FILTER:
253             i_size = sizeof(filter_t);
254             psz_type = "filter";
255             break;
256         case VLC_OBJECT_VOUT:
257             i_size = sizeof(vout_thread_t);
258             psz_type = "video output";
259             break;
260         case VLC_OBJECT_AOUT:
261             i_size = sizeof(aout_instance_t);
262             psz_type = "audio output";
263             break;
264         case VLC_OBJECT_SOUT:
265             i_size = sizeof(sout_instance_t);
266             psz_type = "stream output";
267             break;
268         case VLC_OBJECT_OPENGL:
269             i_size = sizeof( vout_thread_t );
270             psz_type = "opengl";
271             break;
272         case VLC_OBJECT_ANNOUNCE:
273             i_size = sizeof( announce_handler_t );
274             psz_type = "announce";
275             break;
276         case VLC_OBJECT_INTERACTION:
277             i_size = sizeof( interaction_t );
278             psz_type = "interaction";
279             break;
280         default:
281             i_size = i_type > (int)sizeof(vlc_object_t)
282                          ? i_type : (int)sizeof(vlc_object_t);
283             i_type = VLC_OBJECT_GENERIC;
284             psz_type = "generic";
285             break;
286     }
287
288     return vlc_custom_create( p_this, i_size, i_type, psz_type );
289 }
290
291
292 /**
293  ****************************************************************************
294  * Set the destructor of a vlc object
295  *
296  * This function sets the destructor of the vlc object. It will be called
297  * when the object is destroyed when the its refcount reaches 0.
298  * (It is called by the internal function vlc_object_destroy())
299  *****************************************************************************/
300 void __vlc_object_set_destructor( vlc_object_t *p_this,
301                                   vlc_destructor_t pf_destructor )
302 {
303     vlc_object_internals_t *p_priv = vlc_internals(p_this );
304     p_priv->pf_destructor = pf_destructor;
305 }
306
307 /**
308  ****************************************************************************
309  * Destroy a vlc object (Internal)
310  *
311  * This function destroys an object that has been previously allocated with
312  * vlc_object_create. The object's refcount must be zero and it must not be
313  * attached to other objects in any way.
314  *****************************************************************************/
315 static void vlc_object_destroy( vlc_object_t *p_this )
316 {
317     vlc_object_internals_t *p_priv = vlc_internals( p_this );
318
319     /* Objects are always detached beforehand */
320     assert( !p_this->p_parent );
321
322     /* Send a kill to the object's thread if applicable */
323     vlc_object_kill( p_this );
324
325     /* If we are running on a thread, wait until it ends */
326     if( p_priv->b_thread )
327         vlc_thread_join( p_this );
328
329     /* Call the custom "subclass" destructor */
330     if( p_priv->pf_destructor )
331         p_priv->pf_destructor( p_this );
332
333     /* Sanity checks */
334     if( p_this->i_children )
335     {
336         int i;
337
338         fprintf( stderr,
339                  "ERROR: cannot delete object (%i, %s) with %d children\n",
340                  p_this->i_object_id, p_this->psz_object_name,
341                  p_this->i_children );
342
343         for( i = 0; i < p_this->i_children; i++ )
344         {
345             fprintf( stderr,
346                      "ERROR: Remaining children object "
347                      "(id:%i, type:%s, name:%s)\n",
348                      p_this->pp_children[i]->i_object_id,
349                      p_this->pp_children[i]->psz_object_type,
350                      p_this->pp_children[i]->psz_object_name );
351         }
352         fflush(stderr);
353
354         /* Dump libvlc object to ease debugging */
355         vlc_object_dump( p_this->p_libvlc );
356
357         abort();
358     }
359
360     /* Destroy the associated variables, starting from the end so that
361      * no memmove calls have to be done. */
362     while( p_priv->i_vars )
363     {
364         var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
365     }
366
367     free( p_priv->p_vars );
368     vlc_mutex_destroy( &p_priv->var_lock );
369
370     free( p_this->psz_header );
371
372     if( p_this->p_libvlc == NULL )
373     {
374         libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
375
376 #ifndef NDEBUG
377         assert( p_global == vlc_global() );
378         /* Test for leaks */
379         if( p_global->i_objects > 0 )
380         {
381             int i;
382             for( i = 0; i < p_global->i_objects; i++ )
383             {
384                 /* We are leaking this object */
385                 fprintf( stderr,
386                          "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
387                          p_global->pp_objects[i]->i_object_id,
388                          p_global->pp_objects[i]->psz_object_type,
389                          p_global->pp_objects[i]->psz_object_name );
390
391                 /* Dump libvlc object to ease debugging */
392                 vlc_object_dump( p_global->pp_objects[i] );
393
394                 fflush(stderr);
395             }
396
397             /* Dump libvlc object to ease debugging */
398             vlc_object_dump( p_this );
399
400             /* Strongly abort, cause we want these to be fixed */
401             abort();
402         }
403 #endif
404
405         /* We are the global object ... no need to lock. */
406         free( p_global->pp_objects );
407         p_global->pp_objects = NULL;
408
409         vlc_mutex_destroy( &structure_lock );
410     }
411
412     FREENULL( p_this->psz_object_name );
413
414 #if defined(WIN32) || defined(UNDER_CE)
415     /* if object has an associated thread, close it now */
416     if( p_priv->thread_id )
417        CloseHandle(p_priv->thread_id);
418 #endif
419
420     vlc_spin_destroy( &p_priv->ref_spin );
421     vlc_mutex_destroy( &p_this->object_lock );
422     vlc_cond_destroy( &p_this->object_wait );
423     vlc_spin_destroy( &p_priv->spin );
424     if( p_priv->pipes[1] != -1 )
425         close( p_priv->pipes[1] );
426     if( p_priv->pipes[0] != -1 )
427         close( p_priv->pipes[0] );
428
429     free( p_priv );
430 }
431
432
433 /** Inter-object signaling */
434
435 void __vlc_object_lock( vlc_object_t *obj )
436 {
437     vlc_mutex_lock( &obj->object_lock );
438 }
439
440 void __vlc_object_unlock( vlc_object_t *obj )
441 {
442     vlc_assert_locked( &obj->object_lock );
443     vlc_mutex_unlock( &obj->object_lock );
444 }
445
446 #ifdef WIN32
447 # include <winsock2.h>
448 # include <ws2tcpip.h>
449
450 /**
451  * select()-able pipes emulated using Winsock
452  */
453 static int pipe (int fd[2])
454 {
455     SOCKADDR_IN addr;
456     int addrlen = sizeof (addr);
457
458     SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
459            c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
460     if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
461         goto error;
462
463     memset (&addr, 0, sizeof (addr));
464     addr.sin_family = AF_INET;
465     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
466     if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
467      || getsockname (l, (PSOCKADDR)&addr, &addrlen)
468      || listen (l, 1)
469      || connect (c, (PSOCKADDR)&addr, addrlen))
470         goto error;
471
472     a = accept (l, NULL, NULL);
473     if (a == INVALID_SOCKET)
474         goto error;
475
476     closesocket (l);
477     //shutdown (a, 0);
478     //shutdown (c, 1);
479     fd[0] = c;
480     fd[1] = a;
481     return 0;
482
483 error:
484     if (l != INVALID_SOCKET)
485         closesocket (l);
486     if (c != INVALID_SOCKET)
487         closesocket (c);
488     return -1;
489 }
490
491 #undef  read
492 #define read( a, b, c )  recv (a, b, c, 0)
493 #undef  write
494 #define write( a, b, c ) send (a, b, c, 0)
495 #undef  close
496 #define close( a )       closesocket (a)
497 #endif /* WIN32 */
498
499 /**
500  * Returns the readable end of a pipe that becomes readable once termination
501  * of the object is requested (vlc_object_kill()).
502  * This can be used to wake-up out of a select() or poll() event loop, such
503  * typically when doing network I/O.
504  *
505  * Note that the pipe will remain the same for the lifetime of the object.
506  * DO NOT read the pipe nor close it yourself. Ever.
507  *
508  * @param obj object that would be "killed"
509  * @return a readable pipe descriptor, or -1 on error.
510  */
511 int __vlc_object_waitpipe( vlc_object_t *obj )
512 {
513     int pfd[2] = { -1, -1 };
514     vlc_object_internals_t *internals = vlc_internals( obj );
515     bool killed = false;
516
517     vlc_spin_lock (&internals->spin);
518     if (internals->pipes[0] == -1)
519     {
520         /* This can only ever happen if someone killed us without locking: */
521         assert (internals->pipes[1] == -1);
522         vlc_spin_unlock (&internals->spin);
523
524         if (pipe (pfd))
525             return -1;
526
527         vlc_spin_lock (&internals->spin);
528         if (internals->pipes[0] == -1)
529         {
530             internals->pipes[0] = pfd[0];
531             internals->pipes[1] = pfd[1];
532             pfd[0] = pfd[1] = -1;
533         }
534         killed = obj->b_die;
535     }
536     vlc_spin_unlock (&internals->spin);
537
538     if (killed)
539     {
540         /* Race condition: vlc_object_kill() already invoked! */
541         int fd;
542
543         vlc_spin_lock (&internals->spin);
544         fd = internals->pipes[1];
545         internals->pipes[1] = -1;
546         vlc_spin_unlock (&internals->spin);
547
548         msg_Dbg (obj, "waitpipe: object already dying");
549         if (fd != -1)
550             close (fd);
551     }
552
553     /* Race condition: two threads call pipe() - unlikely */
554     if (pfd[0] != -1)
555         close (pfd[0]);
556     if (pfd[1] != -1)
557         close (pfd[1]);
558
559     return internals->pipes[0];
560 }
561
562
563 /**
564  * Waits for the object to be signaled (using vlc_object_signal()).
565  * If the object already has a signal pending, this function will return
566  * immediately. It is asserted that the caller holds the object lock.
567  *
568  * @return true if the object is dying and should terminate.
569  */
570 bool __vlc_object_wait( vlc_object_t *obj )
571 {
572     vlc_assert_locked( &obj->object_lock );
573     vlc_cond_wait( &obj->object_wait, &obj->object_lock );
574     return obj->b_die;
575 }
576
577
578 /**
579  * Waits for the object to be signaled (using vlc_object_signal()), or for
580  * a timer to expire.
581  * If the object already has a signal pending, this function will return
582  * immediately. It is asserted that the caller holds the object lock.
583  *
584  * @return negative if the object is dying and should terminate,
585  * positive if the the object has been signaled but is not dying,
586  * 0 if timeout has been reached.
587  */
588 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
589 {
590     int v;
591
592     vlc_assert_locked( &obj->object_lock );
593     v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
594     if( v == 0 ) /* signaled */
595         return obj->b_die ? -1 : 1;
596     return 0;
597 }
598
599
600 /**
601  * Checks whether an object has been "killed".
602  * The object lock must be held.
603  *
604  * Typical code for an object thread could be:
605  *
606    vlc_object_lock (self);
607    ...initialization...
608    while (vlc_object_alive (self))
609    {
610        ...preprocessing...
611
612        if (vlc_object_wait (self))
613            continue;
614
615        ...postprocessing...
616    }
617    ...deinitialization...
618    vlc_object_unlock (self);
619  *
620  *
621  * @return true iff the object has not been killed yet
622  */
623 bool __vlc_object_alive( vlc_object_t *obj )
624 {
625     vlc_assert_locked( &obj->object_lock );
626     return !obj->b_die;
627 }
628
629
630 /**
631  * Signals an object for which the lock is held.
632  */
633 void __vlc_object_signal_unlocked( vlc_object_t *obj )
634 {
635     vlc_assert_locked (&obj->object_lock);
636     vlc_cond_signal( &obj->object_wait );
637 }
638
639
640 /**
641  * Requests termination of an object.
642  * If the object is LibVLC, also request to terminate all its children.
643  */
644 void __vlc_object_kill( vlc_object_t *p_this )
645 {
646     vlc_object_internals_t *internals = vlc_internals( p_this );
647     int fd;
648
649     vlc_mutex_lock( &p_this->object_lock );
650     p_this->b_die = true;
651
652     vlc_spin_lock (&internals->spin);
653     fd = internals->pipes[1];
654     internals->pipes[1] = -1;
655     vlc_spin_unlock (&internals->spin);
656
657     if( fd != -1 )
658     {
659         msg_Dbg (p_this, "waitpipe: object killed");
660         close (fd);
661     }
662
663     if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
664         for( int i = 0; i < p_this->i_children ; i++ )
665             vlc_object_kill( p_this->pp_children[i] );
666
667     vlc_object_signal_unlocked( p_this );
668     vlc_mutex_unlock( &p_this->object_lock );
669 }
670
671
672 /**
673  * find an object given its ID
674  *
675  * This function looks for the object whose i_object_id field is i_id. We
676  * use a dichotomy so that lookups are in log2(n).
677  *****************************************************************************/
678 void * vlc_object_get( int i_id )
679 {
680     int i_max, i_middle;
681     vlc_object_t **pp_objects;
682     libvlc_global_data_t *p_libvlc_global = vlc_global();
683     vlc_object_t *obj = NULL;
684
685     vlc_mutex_lock( &structure_lock );
686
687     pp_objects = p_libvlc_global->pp_objects;
688
689     /* Perform our dichotomy */
690     for( i_max = p_libvlc_global->i_objects - 1 ; ; )
691     {
692         i_middle = i_max / 2;
693
694         if( pp_objects[i_middle]->i_object_id > i_id )
695         {
696             i_max = i_middle;
697         }
698         else if( pp_objects[i_middle]->i_object_id < i_id )
699         {
700             if( i_middle )
701             {
702                 pp_objects += i_middle;
703                 i_max -= i_middle;
704             }
705             else
706             {
707                 /* This happens when there are only two remaining objects */
708                 if( pp_objects[i_middle+1]->i_object_id == i_id )
709                 {
710                     vlc_object_yield( pp_objects[i_middle+1] );
711                     obj = pp_objects[i_middle+1];
712                 }
713                 break;
714             }
715         }
716         else
717         {
718             vlc_object_yield( pp_objects[i_middle] );
719             obj = pp_objects[i_middle];
720             break;
721         }
722     }
723
724     vlc_mutex_unlock( &structure_lock );
725     return obj;
726 }
727
728 /**
729  ****************************************************************************
730  * find a typed object and increment its refcount
731  *****************************************************************************
732  * This function recursively looks for a given object type. i_mode can be one
733  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
734  *****************************************************************************/
735 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
736 {
737     vlc_object_t *p_found;
738
739     /* If we are of the requested type ourselves, don't look further */
740     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
741     {
742         vlc_object_yield( p_this );
743         return p_this;
744     }
745
746     vlc_mutex_lock( &structure_lock );
747
748     /* Otherwise, recursively look for the object */
749     if( (i_mode & 0x000f) == FIND_ANYWHERE )
750     {
751         vlc_object_t *p_root = p_this;
752
753         /* Find the root */
754         while( p_root->p_parent != NULL &&
755                p_root != VLC_OBJECT( p_this->p_libvlc ) )
756         {
757             p_root = p_root->p_parent;
758         }
759
760         p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
761         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
762         {
763             p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
764                                   i_type, (i_mode & ~0x000f)|FIND_CHILD );
765         }
766     }
767     else
768     {
769         p_found = FindObject( p_this, i_type, i_mode );
770     }
771
772     vlc_mutex_unlock( &structure_lock );
773
774     return p_found;
775 }
776
777 /**
778  ****************************************************************************
779  * find a named object and increment its refcount
780  *****************************************************************************
781  * This function recursively looks for a given object name. i_mode can be one
782  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
783  *****************************************************************************/
784 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
785                                int i_mode )
786 {
787     vlc_object_t *p_found;
788
789     /* If have the requested name ourselves, don't look further */
790     if( !(i_mode & FIND_STRICT)
791         && p_this->psz_object_name
792         && !strcmp( p_this->psz_object_name, psz_name ) )
793     {
794         vlc_object_yield( p_this );
795         return p_this;
796     }
797
798     vlc_mutex_lock( &structure_lock );
799
800     /* Otherwise, recursively look for the object */
801     if( (i_mode & 0x000f) == FIND_ANYWHERE )
802     {
803         vlc_object_t *p_root = p_this;
804
805         /* Find the root */
806         while( p_root->p_parent != NULL &&
807                p_root != VLC_OBJECT( p_this->p_libvlc ) )
808         {
809             p_root = p_root->p_parent;
810         }
811
812         p_found = FindObjectName( p_root, psz_name,
813                                  (i_mode & ~0x000f)|FIND_CHILD );
814         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
815         {
816             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
817                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
818         }
819     }
820     else
821     {
822         p_found = FindObjectName( p_this, psz_name, i_mode );
823     }
824
825     vlc_mutex_unlock( &structure_lock );
826
827     return p_found;
828 }
829
830 /**
831  * Increment an object reference counter.
832  */
833 void __vlc_object_yield( vlc_object_t *p_this )
834 {
835     vlc_object_internals_t *internals = vlc_internals( p_this );
836
837     vlc_spin_lock( &internals->ref_spin );
838     /* Avoid obvious freed object uses */
839     assert( internals->i_refcount > 0 );
840     /* Increment the counter */
841     internals->i_refcount++;
842     vlc_spin_unlock( &internals->ref_spin );
843 }
844
845 /*****************************************************************************
846  * decrement an object refcount
847  * And destroy the object if its refcount reach zero.
848  *****************************************************************************/
849 void __vlc_object_release( vlc_object_t *p_this )
850 {
851     vlc_object_internals_t *internals = vlc_internals( p_this );
852     bool b_should_destroy;
853
854     vlc_spin_lock( &internals->ref_spin );
855     assert( internals->i_refcount > 0 );
856
857     if( internals->i_refcount > 1 )
858     {
859         /* Fast path */
860         /* There are still other references to the object */
861         internals->i_refcount--;
862         vlc_spin_unlock( &internals->ref_spin );
863         return;
864     }
865     vlc_spin_unlock( &internals->ref_spin );
866
867     /* Slow path */
868     /* Remember that we cannot hold the spin while waiting on the mutex */
869     vlc_mutex_lock( &structure_lock );
870     /* Take the spin again. Note that another thread may have yielded the
871      * object in the (very short) mean time. */
872     vlc_spin_lock( &internals->ref_spin );
873     b_should_destroy = --internals->i_refcount == 0;
874     vlc_spin_unlock( &internals->ref_spin );
875
876     if( b_should_destroy )
877     {
878         /* Remove the object from the table so that it cannot be returned from
879          * vlc_object_find() and friends. */
880         libvlc_global_data_t *p_libvlc_global = vlc_global();
881         int i_index;
882
883         i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
884                              p_libvlc_global->i_objects );
885         REMOVE_ELEM( p_libvlc_global->pp_objects,
886                      p_libvlc_global->i_objects, i_index );
887         if (p_this->p_parent)
888             vlc_object_detach_unlocked (p_this);
889     }
890
891     vlc_mutex_unlock( &structure_lock );
892
893     if( b_should_destroy )
894         vlc_object_destroy( p_this );
895 }
896
897 /**
898  ****************************************************************************
899  * attach object to a parent object
900  *****************************************************************************
901  * This function sets p_this as a child of p_parent, and p_parent as a parent
902  * of p_this. This link can be undone using vlc_object_detach.
903  *****************************************************************************/
904 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
905 {
906     if( !p_this ) return;
907
908     vlc_mutex_lock( &structure_lock );
909
910     /* Attach the parent to its child */
911     p_this->p_parent = p_parent;
912
913     /* Attach the child to its parent */
914     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
915                  p_parent->i_children, p_this );
916
917     /* Climb up the tree to see whether we are connected with the root */
918     if( vlc_internals( p_parent )->b_attached )
919     {
920         SetAttachment( p_this, true );
921     }
922
923     vlc_mutex_unlock( &structure_lock );
924 }
925
926
927 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
928 {
929     assert (p_this->p_parent);
930
931     /* Climb up the tree to see whether we are connected with the root */
932     if( vlc_internals( p_this->p_parent )->b_attached )
933         SetAttachment( p_this, false );
934
935     DetachObject( p_this );
936 }
937
938
939 /**
940  ****************************************************************************
941  * detach object from its parent
942  *****************************************************************************
943  * This function removes all links between an object and its parent.
944  *****************************************************************************/
945 void __vlc_object_detach( vlc_object_t *p_this )
946 {
947     if( !p_this ) return;
948
949     vlc_mutex_lock( &structure_lock );
950
951     if( !p_this->p_parent )
952     {
953         msg_Err( p_this, "object is not attached" );
954         vlc_mutex_unlock( &structure_lock );
955         return;
956     }
957
958     vlc_object_detach_unlocked( p_this );
959     vlc_mutex_unlock( &structure_lock );
960 }
961
962
963 static void vlc_tree_find (vlc_object_t *node, int type, vlc_list_t *list)
964 {
965     assert (node);
966     vlc_assert_locked (&structure_lock);
967
968     if (node->i_object_type == type)
969         ListAppend (list, node);
970
971     for (int i = 0; i < node->i_children; i++)
972         vlc_tree_find (node->pp_children[i], type, list);
973 }
974
975 /**
976  ****************************************************************************
977  * find a list typed objects and increment their refcount
978  *****************************************************************************
979  * This function recursively looks for a given object type. i_mode can be one
980  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
981  *****************************************************************************/
982 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
983 {
984     vlc_list_t *p_list;
985     int i_count = 0, i_index = 0;
986     libvlc_global_data_t *p_libvlc_global = vlc_global();
987
988     vlc_mutex_lock( &structure_lock );
989
990     /* Look for the objects */
991     switch( i_mode & 0x000f )
992     {
993     case FIND_ANYWHERE:
994         p_list = NewList (0);
995         if (p_list != NULL)
996             vlc_tree_find (VLC_OBJECT (p_libvlc_global), i_type, p_list);
997         break;
998
999     case FIND_CHILD:
1000         i_count = CountChildren( p_this, i_type );
1001         p_list = NewList( i_count );
1002
1003         /* Check allocation was successful */
1004         if( p_list->i_count != i_count )
1005         {
1006             msg_Err( p_this, "list allocation failed!" );
1007             p_list->i_count = 0;
1008             break;
1009         }
1010
1011         p_list->i_count = 0;
1012         ListChildren( p_list, p_this, i_type );
1013         break;
1014
1015     default:
1016         msg_Err( p_this, "unimplemented!" );
1017         p_list = NewList( 0 );
1018         break;
1019     }
1020
1021     vlc_mutex_unlock( &structure_lock );
1022
1023     return p_list;
1024 }
1025
1026 /*****************************************************************************
1027  * DumpCommand: print the current vlc structure
1028  *****************************************************************************
1029  * This function prints either an ASCII tree showing the connections between
1030  * vlc objects, and additional information such as their refcount, thread ID,
1031  * etc. (command "tree"), or the same data as a simple list (command "list").
1032  *****************************************************************************/
1033 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1034                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1035 {
1036     libvlc_global_data_t *p_libvlc_global = vlc_global();
1037
1038     (void)oldval; (void)p_data;
1039     if( *psz_cmd == 'l' )
1040     {
1041         vlc_mutex_lock( &structure_lock );
1042
1043         vlc_object_t **pp_current, **pp_end;
1044
1045         pp_current = p_libvlc_global->pp_objects;
1046         pp_end = pp_current + p_libvlc_global->i_objects;
1047
1048         for( ; pp_current < pp_end ; pp_current++ )
1049         {
1050             if( vlc_internals(*pp_current)->b_attached )
1051             {
1052                 PrintObject( *pp_current, "" );
1053             }
1054             else
1055             {
1056                 printf( " o %.8i %s (not attached)\n",
1057                         (*pp_current)->i_object_id,
1058                         (*pp_current)->psz_object_type );
1059             }
1060         }
1061
1062         vlc_mutex_unlock( &structure_lock );
1063     }
1064     else
1065     {
1066         vlc_object_t *p_object = NULL;
1067
1068         if( *newval.psz_string )
1069         {
1070             char *end;
1071             int i_id = strtol( newval.psz_string, &end, 0 );
1072             if( end != newval.psz_string )
1073                 p_object = vlc_object_get( i_id );
1074             else
1075             {
1076                 /* try using the object's name to find it */
1077                 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1078                 if( p_libvlc )
1079                 {
1080                     /* Look in p_libvlc's children tree */
1081                     p_object = vlc_object_find_name( p_libvlc,
1082                                                      newval.psz_string,
1083                                                      FIND_CHILD );
1084                     vlc_object_release( p_libvlc );
1085                 }
1086                 if( !p_object )
1087                 {
1088                     /* If it's not in libvlc, look in libvlc_global (== p_this) */
1089                     p_object = vlc_object_find_name( p_this,
1090                                                      newval.psz_string,
1091                                                      FIND_CHILD );
1092                 }
1093             }
1094
1095             if( !p_object )
1096             {
1097                 return VLC_ENOOBJ;
1098             }
1099         }
1100
1101         vlc_mutex_lock( &structure_lock );
1102
1103         if( *psz_cmd == 't' )
1104         {
1105             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1106
1107             if( !p_object )
1108                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1109
1110             psz_foo[0] = '|';
1111             DumpStructure( p_object, 0, psz_foo );
1112         }
1113         else if( *psz_cmd == 'v' )
1114         {
1115             int i;
1116
1117             if( !p_object )
1118                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1119
1120             PrintObject( p_object, "" );
1121
1122             if( !vlc_internals( p_object )->i_vars )
1123                 printf( " `-o No variables\n" );
1124             for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1125             {
1126                 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1127
1128                 const char *psz_type = "unknown";
1129                 switch( p_var->i_type & VLC_VAR_TYPE )
1130                 {
1131 #define MYCASE( type, nice )                \
1132                     case VLC_VAR_ ## type:  \
1133                         psz_type = nice;    \
1134                         break;
1135                     MYCASE( VOID, "void" );
1136                     MYCASE( BOOL, "bool" );
1137                     MYCASE( INTEGER, "integer" );
1138                     MYCASE( HOTKEY, "hotkey" );
1139                     MYCASE( STRING, "string" );
1140                     MYCASE( MODULE, "module" );
1141                     MYCASE( FILE, "file" );
1142                     MYCASE( DIRECTORY, "directory" );
1143                     MYCASE( VARIABLE, "variable" );
1144                     MYCASE( FLOAT, "float" );
1145                     MYCASE( TIME, "time" );
1146                     MYCASE( ADDRESS, "address" );
1147                     MYCASE( MUTEX, "mutex" );
1148                     MYCASE( LIST, "list" );
1149 #undef MYCASE
1150                 }
1151                 printf( " %c-o \"%s\" (%s",
1152                         i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1153                         p_var->psz_name, psz_type );
1154                 if( p_var->psz_text )
1155                     printf( ", %s", p_var->psz_text );
1156                 printf( ")" );
1157                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1158                     printf( ", command" );
1159                 if( p_var->i_entries )
1160                     printf( ", %d callbacks", p_var->i_entries );
1161                 switch( p_var->i_type & 0x00f0 )
1162                 {
1163                     case VLC_VAR_VOID:
1164                     case VLC_VAR_MUTEX:
1165                         break;
1166                     case VLC_VAR_BOOL:
1167                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1168                         break;
1169                     case VLC_VAR_INTEGER:
1170                         printf( ": %d", p_var->val.i_int );
1171                         break;
1172                     case VLC_VAR_STRING:
1173                         printf( ": \"%s\"", p_var->val.psz_string );
1174                         break;
1175                     case VLC_VAR_FLOAT:
1176                         printf( ": %f", p_var->val.f_float );
1177                         break;
1178                     case VLC_VAR_TIME:
1179                         printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1180                         break;
1181                     case VLC_VAR_ADDRESS:
1182                         printf( ": %p", p_var->val.p_address );
1183                         break;
1184                     case VLC_VAR_LIST:
1185                         printf( ": TODO" );
1186                         break;
1187                 }
1188                 printf( "\n" );
1189             }
1190         }
1191
1192         vlc_mutex_unlock( &structure_lock );
1193
1194         if( *newval.psz_string )
1195         {
1196             vlc_object_release( p_object );
1197         }
1198     }
1199
1200     return VLC_SUCCESS;
1201 }
1202
1203 /*****************************************************************************
1204  * vlc_list_release: free a list previously allocated by vlc_list_find
1205  *****************************************************************************
1206  * This function decreases the refcount of all objects in the list and
1207  * frees the list.
1208  *****************************************************************************/
1209 void vlc_list_release( vlc_list_t *p_list )
1210 {
1211     int i_index;
1212
1213     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1214     {
1215         vlc_object_release( p_list->p_values[i_index].p_object );
1216     }
1217
1218     free( p_list->p_values );
1219     free( p_list );
1220 }
1221
1222 /*****************************************************************************
1223  * dump an object. (Debug function)
1224  *****************************************************************************/
1225 void __vlc_object_dump( vlc_object_t *p_this )
1226 {
1227     vlc_mutex_lock( &structure_lock );
1228     char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1229     psz_foo[0] = '|';
1230     DumpStructure( p_this, 0, psz_foo );
1231     vlc_mutex_unlock( &structure_lock );
1232 }
1233
1234 /* Following functions are local */
1235
1236 /*****************************************************************************
1237  * FindIndex: find the index of an object in an array of objects
1238  *****************************************************************************
1239  * This function assumes that p_this can be found in pp_objects. It will not
1240  * crash if p_this cannot be found, but will return a wrong value. It is your
1241  * duty to check the return value if you are not certain that the object could
1242  * be found for sure.
1243  *****************************************************************************/
1244 static int FindIndex( vlc_object_t *p_this,
1245                       vlc_object_t **pp_objects, int i_count )
1246 {
1247     int i_middle = i_count / 2;
1248
1249     if( i_count == 0 )
1250     {
1251         return 0;
1252     }
1253
1254     if( pp_objects[i_middle] == p_this )
1255     {
1256         return i_middle;
1257     }
1258
1259     if( i_count == 1 )
1260     {
1261         return 0;
1262     }
1263
1264     /* We take advantage of the sorted array */
1265     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1266     {
1267         return i_middle + FindIndex( p_this, pp_objects + i_middle,
1268                                              i_count - i_middle );
1269     }
1270     else
1271     {
1272         return FindIndex( p_this, pp_objects, i_middle );
1273     }
1274 }
1275
1276 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1277 {
1278     int i;
1279     vlc_object_t *p_tmp;
1280
1281     switch( i_mode & 0x000f )
1282     {
1283     case FIND_PARENT:
1284         p_tmp = p_this->p_parent;
1285         if( p_tmp )
1286         {
1287             if( p_tmp->i_object_type == i_type )
1288             {
1289                 vlc_object_yield( p_tmp );
1290                 return p_tmp;
1291             }
1292             else
1293             {
1294                 return FindObject( p_tmp, i_type, i_mode );
1295             }
1296         }
1297         break;
1298
1299     case FIND_CHILD:
1300         for( i = p_this->i_children; i--; )
1301         {
1302             p_tmp = p_this->pp_children[i];
1303             if( p_tmp->i_object_type == i_type )
1304             {
1305                 vlc_object_yield( p_tmp );
1306                 return p_tmp;
1307             }
1308             else if( p_tmp->i_children )
1309             {
1310                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1311                 if( p_tmp )
1312                 {
1313                     return p_tmp;
1314                 }
1315             }
1316         }
1317         break;
1318
1319     case FIND_ANYWHERE:
1320         /* Handled in vlc_object_find */
1321         break;
1322     }
1323
1324     return NULL;
1325 }
1326
1327 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1328                                       const char *psz_name,
1329                                       int i_mode )
1330 {
1331     int i;
1332     vlc_object_t *p_tmp;
1333
1334     switch( i_mode & 0x000f )
1335     {
1336     case FIND_PARENT:
1337         p_tmp = p_this->p_parent;
1338         if( p_tmp )
1339         {
1340             if( p_tmp->psz_object_name
1341                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1342             {
1343                 vlc_object_yield( p_tmp );
1344                 return p_tmp;
1345             }
1346             else
1347             {
1348                 return FindObjectName( p_tmp, psz_name, i_mode );
1349             }
1350         }
1351         break;
1352
1353     case FIND_CHILD:
1354         for( i = p_this->i_children; i--; )
1355         {
1356             p_tmp = p_this->pp_children[i];
1357             if( p_tmp->psz_object_name
1358                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1359             {
1360                 vlc_object_yield( p_tmp );
1361                 return p_tmp;
1362             }
1363             else if( p_tmp->i_children )
1364             {
1365                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1366                 if( p_tmp )
1367                 {
1368                     return p_tmp;
1369                 }
1370             }
1371         }
1372         break;
1373
1374     case FIND_ANYWHERE:
1375         /* Handled in vlc_object_find */
1376         break;
1377     }
1378
1379     return NULL;
1380 }
1381
1382 static void DetachObject( vlc_object_t *p_this )
1383 {
1384     vlc_object_t *p_parent = p_this->p_parent;
1385     int i_index, i;
1386
1387     /* Remove p_this's parent */
1388     p_this->p_parent = NULL;
1389
1390     /* Remove all of p_parent's children which are p_this */
1391     for( i_index = p_parent->i_children ; i_index-- ; )
1392     {
1393         if( p_parent->pp_children[i_index] == p_this )
1394         {
1395             p_parent->i_children--;
1396             for( i = i_index ; i < p_parent->i_children ; i++ )
1397             {
1398                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1399             }
1400         }
1401     }
1402
1403     if( p_parent->i_children )
1404     {
1405         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1406                                p_parent->i_children * sizeof(vlc_object_t *) );
1407     }
1408     else
1409     {
1410         free( p_parent->pp_children );
1411         p_parent->pp_children = NULL;
1412     }
1413 }
1414
1415 /*****************************************************************************
1416  * SetAttachment: recursively set the b_attached flag of a subtree.
1417  *****************************************************************************
1418  * This function is used by the attach and detach functions to propagate
1419  * the b_attached flag in a subtree.
1420  *****************************************************************************/
1421 static void SetAttachment( vlc_object_t *p_this, bool b_attached )
1422 {
1423     int i_index;
1424
1425     for( i_index = p_this->i_children ; i_index-- ; )
1426     {
1427         SetAttachment( p_this->pp_children[i_index], b_attached );
1428     }
1429
1430     vlc_internals( p_this )->b_attached = b_attached;
1431 }
1432
1433 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1434 {
1435     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1436          psz_parent[20];
1437
1438     memset( &psz_name, 0, sizeof(psz_name) );
1439     if( p_this->psz_object_name )
1440     {
1441         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1442         if( psz_name[48] )
1443             psz_name[48] = '\"';
1444     }
1445
1446     psz_children[0] = '\0';
1447     switch( p_this->i_children )
1448     {
1449         case 0:
1450             break;
1451         case 1:
1452             strcpy( psz_children, ", 1 child" );
1453             break;
1454         default:
1455             snprintf( psz_children, 19, ", %i children", p_this->i_children );
1456             break;
1457     }
1458
1459     psz_refcount[0] = '\0';
1460     if( vlc_internals( p_this )->i_refcount > 0 )
1461         snprintf( psz_refcount, 19, ", refcount %u",
1462                   vlc_internals( p_this )->i_refcount );
1463
1464     psz_thread[0] = '\0';
1465     if( vlc_internals( p_this )->b_thread )
1466         snprintf( psz_thread, 29, " (thread %lu)",
1467                   (unsigned long)vlc_internals( p_this )->thread_id );
1468
1469     psz_parent[0] = '\0';
1470     if( p_this->p_parent )
1471         snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1472
1473     printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1474             p_this->i_object_id, p_this->psz_object_type,
1475             psz_name, psz_thread, psz_refcount, psz_children,
1476             psz_parent );
1477 }
1478
1479 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1480 {
1481     int i;
1482     char i_back = psz_foo[i_level];
1483     psz_foo[i_level] = '\0';
1484
1485     PrintObject( p_this, psz_foo );
1486
1487     psz_foo[i_level] = i_back;
1488
1489     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1490     {
1491         msg_Warn( p_this, "structure tree is too deep" );
1492         return;
1493     }
1494
1495     for( i = 0 ; i < p_this->i_children ; i++ )
1496     {
1497         if( i_level )
1498         {
1499             psz_foo[i_level-1] = ' ';
1500
1501             if( psz_foo[i_level-2] == '`' )
1502             {
1503                 psz_foo[i_level-2] = ' ';
1504             }
1505         }
1506
1507         if( i == p_this->i_children - 1 )
1508         {
1509             psz_foo[i_level] = '`';
1510         }
1511         else
1512         {
1513             psz_foo[i_level] = '|';
1514         }
1515
1516         psz_foo[i_level+1] = '-';
1517         psz_foo[i_level+2] = '\0';
1518
1519         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1520     }
1521 }
1522
1523 static vlc_list_t * NewList( int i_count )
1524 {
1525     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1526     if( p_list == NULL )
1527     {
1528         return NULL;
1529     }
1530
1531     p_list->i_count = i_count;
1532
1533     if( i_count == 0 )
1534     {
1535         p_list->p_values = NULL;
1536         return p_list;
1537     }
1538
1539     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1540     if( p_list->p_values == NULL )
1541     {
1542         p_list->i_count = 0;
1543         return p_list;
1544     }
1545
1546     return p_list;
1547 }
1548
1549 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1550                          int i_index )
1551 {
1552     if( p_list == NULL || i_index >= p_list->i_count )
1553     {
1554         return;
1555     }
1556
1557     vlc_object_yield( p_object );
1558
1559     p_list->p_values[i_index].p_object = p_object;
1560
1561     return;
1562 }
1563
1564 static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1565 {
1566     if( p_list == NULL )
1567     {
1568         return;
1569     }
1570
1571     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1572                                 * sizeof( vlc_value_t ) );
1573     if( p_list->p_values == NULL )
1574     {
1575         p_list->i_count = 0;
1576         return;
1577     }
1578
1579     vlc_object_yield( p_object );
1580
1581     p_list->p_values[p_list->i_count].p_object = p_object;
1582     p_list->i_count++;
1583
1584     return;
1585 }
1586
1587 static int CountChildren( vlc_object_t *p_this, int i_type )
1588 {
1589     vlc_object_t *p_tmp;
1590     int i, i_count = 0;
1591
1592     for( i = 0; i < p_this->i_children; i++ )
1593     {
1594         p_tmp = p_this->pp_children[i];
1595
1596         if( p_tmp->i_object_type == i_type )
1597         {
1598             i_count++;
1599         }
1600
1601         if( p_tmp->i_children )
1602         {
1603             i_count += CountChildren( p_tmp, i_type );
1604         }
1605     }
1606
1607     return i_count;
1608 }
1609
1610 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1611 {
1612     vlc_object_t *p_tmp;
1613     int i;
1614
1615     for( i = 0; i < p_this->i_children; i++ )
1616     {
1617         p_tmp = p_this->pp_children[i];
1618
1619         if( p_tmp->i_object_type == i_type )
1620         {
1621             ListReplace( p_list, p_tmp, p_list->i_count++ );
1622         }
1623
1624         if( p_tmp->i_children )
1625         {
1626             ListChildren( p_list, p_tmp, i_type );
1627         }
1628     }
1629 }