]> git.sesse.net Git - vlc/blob - src/misc/objects.c
Detach dying objects atomically, so FIND_CHILDREN is safe
[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  ****************************************************************************
964  * find a list typed objects and increment their refcount
965  *****************************************************************************
966  * This function recursively looks for a given object type. i_mode can be one
967  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
968  *****************************************************************************/
969 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
970 {
971     vlc_list_t *p_list;
972     vlc_object_t **pp_current, **pp_end;
973     int i_count = 0, i_index = 0;
974     libvlc_global_data_t *p_libvlc_global = vlc_global();
975
976     vlc_mutex_lock( &structure_lock );
977
978     /* Look for the objects */
979     switch( i_mode & 0x000f )
980     {
981     case FIND_ANYWHERE:
982         pp_current = p_libvlc_global->pp_objects;
983         pp_end = pp_current + p_libvlc_global->i_objects;
984
985         for( ; pp_current < pp_end ; pp_current++ )
986         {
987             if( vlc_internals(*pp_current)->b_attached
988                  && (*pp_current)->i_object_type == i_type )
989             {
990                 i_count++;
991             }
992         }
993
994         p_list = NewList( i_count );
995         pp_current = p_libvlc_global->pp_objects;
996
997         for( ; pp_current < pp_end ; pp_current++ )
998         {
999             if( vlc_internals(*pp_current)->b_attached
1000                  && (*pp_current)->i_object_type == i_type )
1001             {
1002                 ListReplace( p_list, *pp_current, i_index );
1003                 if( i_index < i_count ) i_index++;
1004             }
1005         }
1006     break;
1007
1008     case FIND_CHILD:
1009         i_count = CountChildren( p_this, i_type );
1010         p_list = NewList( i_count );
1011
1012         /* Check allocation was successful */
1013         if( p_list->i_count != i_count )
1014         {
1015             msg_Err( p_this, "list allocation failed!" );
1016             p_list->i_count = 0;
1017             break;
1018         }
1019
1020         p_list->i_count = 0;
1021         ListChildren( p_list, p_this, i_type );
1022         break;
1023
1024     default:
1025         msg_Err( p_this, "unimplemented!" );
1026         p_list = NewList( 0 );
1027         break;
1028     }
1029
1030     vlc_mutex_unlock( &structure_lock );
1031
1032     return p_list;
1033 }
1034
1035 /*****************************************************************************
1036  * DumpCommand: print the current vlc structure
1037  *****************************************************************************
1038  * This function prints either an ASCII tree showing the connections between
1039  * vlc objects, and additional information such as their refcount, thread ID,
1040  * etc. (command "tree"), or the same data as a simple list (command "list").
1041  *****************************************************************************/
1042 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1043                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1044 {
1045     libvlc_global_data_t *p_libvlc_global = vlc_global();
1046
1047     (void)oldval; (void)p_data;
1048     if( *psz_cmd == 'l' )
1049     {
1050         vlc_mutex_lock( &structure_lock );
1051
1052         vlc_object_t **pp_current, **pp_end;
1053
1054         pp_current = p_libvlc_global->pp_objects;
1055         pp_end = pp_current + p_libvlc_global->i_objects;
1056
1057         for( ; pp_current < pp_end ; pp_current++ )
1058         {
1059             if( vlc_internals(*pp_current)->b_attached )
1060             {
1061                 PrintObject( *pp_current, "" );
1062             }
1063             else
1064             {
1065                 printf( " o %.8i %s (not attached)\n",
1066                         (*pp_current)->i_object_id,
1067                         (*pp_current)->psz_object_type );
1068             }
1069         }
1070
1071         vlc_mutex_unlock( &structure_lock );
1072     }
1073     else
1074     {
1075         vlc_object_t *p_object = NULL;
1076
1077         if( *newval.psz_string )
1078         {
1079             char *end;
1080             int i_id = strtol( newval.psz_string, &end, 0 );
1081             if( end != newval.psz_string )
1082                 p_object = vlc_object_get( i_id );
1083             else
1084             {
1085                 /* try using the object's name to find it */
1086                 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1087                 if( p_libvlc )
1088                 {
1089                     /* Look in p_libvlc's children tree */
1090                     p_object = vlc_object_find_name( p_libvlc,
1091                                                      newval.psz_string,
1092                                                      FIND_CHILD );
1093                     vlc_object_release( p_libvlc );
1094                 }
1095                 if( !p_object )
1096                 {
1097                     /* If it's not in libvlc, look in libvlc_global (== p_this) */
1098                     p_object = vlc_object_find_name( p_this,
1099                                                      newval.psz_string,
1100                                                      FIND_CHILD );
1101                 }
1102             }
1103
1104             if( !p_object )
1105             {
1106                 return VLC_ENOOBJ;
1107             }
1108         }
1109
1110         vlc_mutex_lock( &structure_lock );
1111
1112         if( *psz_cmd == 't' )
1113         {
1114             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1115
1116             if( !p_object )
1117                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1118
1119             psz_foo[0] = '|';
1120             DumpStructure( p_object, 0, psz_foo );
1121         }
1122         else if( *psz_cmd == 'v' )
1123         {
1124             int i;
1125
1126             if( !p_object )
1127                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1128
1129             PrintObject( p_object, "" );
1130
1131             if( !vlc_internals( p_object )->i_vars )
1132                 printf( " `-o No variables\n" );
1133             for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1134             {
1135                 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1136
1137                 const char *psz_type = "unknown";
1138                 switch( p_var->i_type & VLC_VAR_TYPE )
1139                 {
1140 #define MYCASE( type, nice )                \
1141                     case VLC_VAR_ ## type:  \
1142                         psz_type = nice;    \
1143                         break;
1144                     MYCASE( VOID, "void" );
1145                     MYCASE( BOOL, "bool" );
1146                     MYCASE( INTEGER, "integer" );
1147                     MYCASE( HOTKEY, "hotkey" );
1148                     MYCASE( STRING, "string" );
1149                     MYCASE( MODULE, "module" );
1150                     MYCASE( FILE, "file" );
1151                     MYCASE( DIRECTORY, "directory" );
1152                     MYCASE( VARIABLE, "variable" );
1153                     MYCASE( FLOAT, "float" );
1154                     MYCASE( TIME, "time" );
1155                     MYCASE( ADDRESS, "address" );
1156                     MYCASE( MUTEX, "mutex" );
1157                     MYCASE( LIST, "list" );
1158 #undef MYCASE
1159                 }
1160                 printf( " %c-o \"%s\" (%s",
1161                         i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1162                         p_var->psz_name, psz_type );
1163                 if( p_var->psz_text )
1164                     printf( ", %s", p_var->psz_text );
1165                 printf( ")" );
1166                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1167                     printf( ", command" );
1168                 if( p_var->i_entries )
1169                     printf( ", %d callbacks", p_var->i_entries );
1170                 switch( p_var->i_type & 0x00f0 )
1171                 {
1172                     case VLC_VAR_VOID:
1173                     case VLC_VAR_MUTEX:
1174                         break;
1175                     case VLC_VAR_BOOL:
1176                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1177                         break;
1178                     case VLC_VAR_INTEGER:
1179                         printf( ": %d", p_var->val.i_int );
1180                         break;
1181                     case VLC_VAR_STRING:
1182                         printf( ": \"%s\"", p_var->val.psz_string );
1183                         break;
1184                     case VLC_VAR_FLOAT:
1185                         printf( ": %f", p_var->val.f_float );
1186                         break;
1187                     case VLC_VAR_TIME:
1188                         printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1189                         break;
1190                     case VLC_VAR_ADDRESS:
1191                         printf( ": %p", p_var->val.p_address );
1192                         break;
1193                     case VLC_VAR_LIST:
1194                         printf( ": TODO" );
1195                         break;
1196                 }
1197                 printf( "\n" );
1198             }
1199         }
1200
1201         vlc_mutex_unlock( &structure_lock );
1202
1203         if( *newval.psz_string )
1204         {
1205             vlc_object_release( p_object );
1206         }
1207     }
1208
1209     return VLC_SUCCESS;
1210 }
1211
1212 /*****************************************************************************
1213  * vlc_list_release: free a list previously allocated by vlc_list_find
1214  *****************************************************************************
1215  * This function decreases the refcount of all objects in the list and
1216  * frees the list.
1217  *****************************************************************************/
1218 void vlc_list_release( vlc_list_t *p_list )
1219 {
1220     int i_index;
1221
1222     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1223     {
1224         vlc_object_release( p_list->p_values[i_index].p_object );
1225     }
1226
1227     free( p_list->p_values );
1228     free( p_list );
1229 }
1230
1231 /*****************************************************************************
1232  * dump an object. (Debug function)
1233  *****************************************************************************/
1234 void __vlc_object_dump( vlc_object_t *p_this )
1235 {
1236     vlc_mutex_lock( &structure_lock );
1237     char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1238     psz_foo[0] = '|';
1239     DumpStructure( p_this, 0, psz_foo );
1240     vlc_mutex_unlock( &structure_lock );
1241 }
1242
1243 /* Following functions are local */
1244
1245 /*****************************************************************************
1246  * FindIndex: find the index of an object in an array of objects
1247  *****************************************************************************
1248  * This function assumes that p_this can be found in pp_objects. It will not
1249  * crash if p_this cannot be found, but will return a wrong value. It is your
1250  * duty to check the return value if you are not certain that the object could
1251  * be found for sure.
1252  *****************************************************************************/
1253 static int FindIndex( vlc_object_t *p_this,
1254                       vlc_object_t **pp_objects, int i_count )
1255 {
1256     int i_middle = i_count / 2;
1257
1258     if( i_count == 0 )
1259     {
1260         return 0;
1261     }
1262
1263     if( pp_objects[i_middle] == p_this )
1264     {
1265         return i_middle;
1266     }
1267
1268     if( i_count == 1 )
1269     {
1270         return 0;
1271     }
1272
1273     /* We take advantage of the sorted array */
1274     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1275     {
1276         return i_middle + FindIndex( p_this, pp_objects + i_middle,
1277                                              i_count - i_middle );
1278     }
1279     else
1280     {
1281         return FindIndex( p_this, pp_objects, i_middle );
1282     }
1283 }
1284
1285 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1286 {
1287     int i;
1288     vlc_object_t *p_tmp;
1289
1290     switch( i_mode & 0x000f )
1291     {
1292     case FIND_PARENT:
1293         p_tmp = p_this->p_parent;
1294         if( p_tmp )
1295         {
1296             if( p_tmp->i_object_type == i_type )
1297             {
1298                 vlc_object_yield( p_tmp );
1299                 return p_tmp;
1300             }
1301             else
1302             {
1303                 return FindObject( p_tmp, i_type, i_mode );
1304             }
1305         }
1306         break;
1307
1308     case FIND_CHILD:
1309         for( i = p_this->i_children; i--; )
1310         {
1311             p_tmp = p_this->pp_children[i];
1312             if( p_tmp->i_object_type == i_type )
1313             {
1314                 vlc_object_yield( p_tmp );
1315                 return p_tmp;
1316             }
1317             else if( p_tmp->i_children )
1318             {
1319                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1320                 if( p_tmp )
1321                 {
1322                     return p_tmp;
1323                 }
1324             }
1325         }
1326         break;
1327
1328     case FIND_ANYWHERE:
1329         /* Handled in vlc_object_find */
1330         break;
1331     }
1332
1333     return NULL;
1334 }
1335
1336 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1337                                       const char *psz_name,
1338                                       int i_mode )
1339 {
1340     int i;
1341     vlc_object_t *p_tmp;
1342
1343     switch( i_mode & 0x000f )
1344     {
1345     case FIND_PARENT:
1346         p_tmp = p_this->p_parent;
1347         if( p_tmp )
1348         {
1349             if( p_tmp->psz_object_name
1350                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1351             {
1352                 vlc_object_yield( p_tmp );
1353                 return p_tmp;
1354             }
1355             else
1356             {
1357                 return FindObjectName( p_tmp, psz_name, i_mode );
1358             }
1359         }
1360         break;
1361
1362     case FIND_CHILD:
1363         for( i = p_this->i_children; i--; )
1364         {
1365             p_tmp = p_this->pp_children[i];
1366             if( p_tmp->psz_object_name
1367                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1368             {
1369                 vlc_object_yield( p_tmp );
1370                 return p_tmp;
1371             }
1372             else if( p_tmp->i_children )
1373             {
1374                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1375                 if( p_tmp )
1376                 {
1377                     return p_tmp;
1378                 }
1379             }
1380         }
1381         break;
1382
1383     case FIND_ANYWHERE:
1384         /* Handled in vlc_object_find */
1385         break;
1386     }
1387
1388     return NULL;
1389 }
1390
1391 static void DetachObject( vlc_object_t *p_this )
1392 {
1393     vlc_object_t *p_parent = p_this->p_parent;
1394     int i_index, i;
1395
1396     /* Remove p_this's parent */
1397     p_this->p_parent = NULL;
1398
1399     /* Remove all of p_parent's children which are p_this */
1400     for( i_index = p_parent->i_children ; i_index-- ; )
1401     {
1402         if( p_parent->pp_children[i_index] == p_this )
1403         {
1404             p_parent->i_children--;
1405             for( i = i_index ; i < p_parent->i_children ; i++ )
1406             {
1407                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1408             }
1409         }
1410     }
1411
1412     if( p_parent->i_children )
1413     {
1414         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1415                                p_parent->i_children * sizeof(vlc_object_t *) );
1416     }
1417     else
1418     {
1419         free( p_parent->pp_children );
1420         p_parent->pp_children = NULL;
1421     }
1422 }
1423
1424 /*****************************************************************************
1425  * SetAttachment: recursively set the b_attached flag of a subtree.
1426  *****************************************************************************
1427  * This function is used by the attach and detach functions to propagate
1428  * the b_attached flag in a subtree.
1429  *****************************************************************************/
1430 static void SetAttachment( vlc_object_t *p_this, bool b_attached )
1431 {
1432     int i_index;
1433
1434     for( i_index = p_this->i_children ; i_index-- ; )
1435     {
1436         SetAttachment( p_this->pp_children[i_index], b_attached );
1437     }
1438
1439     vlc_internals( p_this )->b_attached = b_attached;
1440 }
1441
1442 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1443 {
1444     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1445          psz_parent[20];
1446
1447     memset( &psz_name, 0, sizeof(psz_name) );
1448     if( p_this->psz_object_name )
1449     {
1450         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1451         if( psz_name[48] )
1452             psz_name[48] = '\"';
1453     }
1454
1455     psz_children[0] = '\0';
1456     switch( p_this->i_children )
1457     {
1458         case 0:
1459             break;
1460         case 1:
1461             strcpy( psz_children, ", 1 child" );
1462             break;
1463         default:
1464             snprintf( psz_children, 19, ", %i children", p_this->i_children );
1465             break;
1466     }
1467
1468     psz_refcount[0] = '\0';
1469     if( vlc_internals( p_this )->i_refcount > 0 )
1470         snprintf( psz_refcount, 19, ", refcount %u",
1471                   vlc_internals( p_this )->i_refcount );
1472
1473     psz_thread[0] = '\0';
1474     if( vlc_internals( p_this )->b_thread )
1475         snprintf( psz_thread, 29, " (thread %lu)",
1476                   (unsigned long)vlc_internals( p_this )->thread_id );
1477
1478     psz_parent[0] = '\0';
1479     if( p_this->p_parent )
1480         snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1481
1482     printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1483             p_this->i_object_id, p_this->psz_object_type,
1484             psz_name, psz_thread, psz_refcount, psz_children,
1485             psz_parent );
1486 }
1487
1488 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1489 {
1490     int i;
1491     char i_back = psz_foo[i_level];
1492     psz_foo[i_level] = '\0';
1493
1494     PrintObject( p_this, psz_foo );
1495
1496     psz_foo[i_level] = i_back;
1497
1498     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1499     {
1500         msg_Warn( p_this, "structure tree is too deep" );
1501         return;
1502     }
1503
1504     for( i = 0 ; i < p_this->i_children ; i++ )
1505     {
1506         if( i_level )
1507         {
1508             psz_foo[i_level-1] = ' ';
1509
1510             if( psz_foo[i_level-2] == '`' )
1511             {
1512                 psz_foo[i_level-2] = ' ';
1513             }
1514         }
1515
1516         if( i == p_this->i_children - 1 )
1517         {
1518             psz_foo[i_level] = '`';
1519         }
1520         else
1521         {
1522             psz_foo[i_level] = '|';
1523         }
1524
1525         psz_foo[i_level+1] = '-';
1526         psz_foo[i_level+2] = '\0';
1527
1528         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1529     }
1530 }
1531
1532 static vlc_list_t * NewList( int i_count )
1533 {
1534     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1535     if( p_list == NULL )
1536     {
1537         return NULL;
1538     }
1539
1540     p_list->i_count = i_count;
1541
1542     if( i_count == 0 )
1543     {
1544         p_list->p_values = NULL;
1545         return p_list;
1546     }
1547
1548     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1549     if( p_list->p_values == NULL )
1550     {
1551         p_list->i_count = 0;
1552         return p_list;
1553     }
1554
1555     return p_list;
1556 }
1557
1558 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1559                          int i_index )
1560 {
1561     if( p_list == NULL || i_index >= p_list->i_count )
1562     {
1563         return;
1564     }
1565
1566     vlc_object_yield( p_object );
1567
1568     p_list->p_values[i_index].p_object = p_object;
1569
1570     return;
1571 }
1572
1573 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1574 {
1575     if( p_list == NULL )
1576     {
1577         return;
1578     }
1579
1580     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1581                                 * sizeof( vlc_value_t ) );
1582     if( p_list->p_values == NULL )
1583     {
1584         p_list->i_count = 0;
1585         return;
1586     }
1587
1588     vlc_object_yield( p_object );
1589
1590     p_list->p_values[p_list->i_count].p_object = p_object;
1591     p_list->i_count++;
1592
1593     return;
1594 }*/
1595
1596 static int CountChildren( vlc_object_t *p_this, int i_type )
1597 {
1598     vlc_object_t *p_tmp;
1599     int i, i_count = 0;
1600
1601     for( i = 0; i < p_this->i_children; i++ )
1602     {
1603         p_tmp = p_this->pp_children[i];
1604
1605         if( p_tmp->i_object_type == i_type )
1606         {
1607             i_count++;
1608         }
1609
1610         if( p_tmp->i_children )
1611         {
1612             i_count += CountChildren( p_tmp, i_type );
1613         }
1614     }
1615
1616     return i_count;
1617 }
1618
1619 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1620 {
1621     vlc_object_t *p_tmp;
1622     int i;
1623
1624     for( i = 0; i < p_this->i_children; i++ )
1625     {
1626         p_tmp = p_this->pp_children[i];
1627
1628         if( p_tmp->i_object_type == i_type )
1629         {
1630             ListReplace( p_list, p_tmp, p_list->i_count++ );
1631         }
1632
1633         if( p_tmp->i_children )
1634         {
1635             ListChildren( p_list, p_tmp, i_type );
1636         }
1637     }
1638 }