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