]> git.sesse.net Git - vlc/blob - src/misc/objects.c
vlc_mutex_init: remove unused paramter
[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( i_type == VLC_OBJECT_GLOBAL )
147     {
148         /* If i_type is global, then p_new is actually p_libvlc_global */
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     vlc_mutex_lock( &structure_lock );
173     p_new->i_object_id = p_libvlc_global->i_counter++;
174
175     /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
176      * useless to try and recover anything if pp_objects gets smashed. */
177     TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
178                 p_new );
179     vlc_mutex_unlock( &structure_lock );
180
181     p_priv->i_refcount = 1;
182     p_priv->pf_destructor = kVLCDestructor;
183     p_priv->b_thread = false;
184     p_new->p_parent = NULL;
185     p_new->pp_children = NULL;
186     p_new->i_children = 0;
187
188     p_new->p_private = NULL;
189
190     /* Initialize mutexes and condvars */
191     vlc_mutex_init( &p_new->object_lock );
192     vlc_cond_init( p_new, &p_new->object_wait );
193     vlc_mutex_init( &p_priv->var_lock );
194     vlc_spin_init( &p_priv->spin );
195     p_priv->pipes[0] = p_priv->pipes[1] = -1;
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
311     vlc_mutex_lock( &structure_lock );
312     p_priv->pf_destructor = pf_destructor;
313     vlc_mutex_unlock( &structure_lock );
314 }
315
316 /**
317  ****************************************************************************
318  * Destroy a vlc object (Internal)
319  *
320  * This function destroys an object that has been previously allocated with
321  * vlc_object_create. The object's refcount must be zero and it must not be
322  * attached to other objects in any way.
323  *****************************************************************************/
324 static void vlc_object_destroy( vlc_object_t *p_this )
325 {
326     vlc_object_internals_t *p_priv = vlc_internals( p_this );
327
328     /* Automatically detach the object from its parents */
329     if( p_this->p_parent ) vlc_object_detach( p_this );
330
331
332     /* Send a kill to the object's thread if applicable */
333     vlc_object_kill( p_this );
334
335     /* If we are running on a thread, wait until it ends */
336     if( p_priv->b_thread )
337         vlc_thread_join( p_this );
338
339     /* Call the custom "subclass" destructor */
340     if( p_priv->pf_destructor )
341         p_priv->pf_destructor( p_this );
342
343     /* Sanity checks */
344     if( p_this->i_children )
345     {
346         int i;
347
348         fprintf( stderr,
349                  "ERROR: cannot delete object (%i, %s) with %d children\n",
350                  p_this->i_object_id, p_this->psz_object_name,
351                  p_this->i_children );
352
353         for( i = 0; i < p_this->i_children; i++ )
354         {
355             fprintf( stderr,
356                      "ERROR: Remaining children object "
357                      "(id:%i, type:%s, name:%s)\n",
358                      p_this->pp_children[i]->i_object_id,
359                      p_this->pp_children[i]->psz_object_type,
360                      p_this->pp_children[i]->psz_object_name );
361         }
362         fflush(stderr);
363
364         /* Dump libvlc object to ease debugging */
365         vlc_object_dump( p_this->p_libvlc );
366
367         abort();
368     }
369
370     /* Destroy the associated variables, starting from the end so that
371      * no memmove calls have to be done. */
372     while( p_priv->i_vars )
373     {
374         var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
375     }
376
377     free( p_priv->p_vars );
378     vlc_mutex_destroy( &p_priv->var_lock );
379
380     free( p_this->psz_header );
381
382     if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
383     {
384         libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
385
386         /* Test for leaks */
387         if( p_global->i_objects > 0 )
388         {
389             int i;
390             for( i = 0; i < p_global->i_objects; i++ )
391             {
392                 /* We are leaking this object */
393                 fprintf( stderr,
394                          "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
395                          p_global->pp_objects[i]->i_object_id,
396                          p_global->pp_objects[i]->psz_object_type,
397                          p_global->pp_objects[i]->psz_object_name );
398
399                 /* Dump libvlc object to ease debugging */
400                 vlc_object_dump( p_global->pp_objects[i] );
401
402                 fflush(stderr);
403             }
404
405             /* Dump libvlc object to ease debugging */
406             vlc_object_dump( p_this );
407
408             /* Strongly abort, cause we want these to be fixed */
409             abort();
410         }
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
690     vlc_mutex_lock( &structure_lock );
691
692     pp_objects = p_libvlc_global->pp_objects;
693
694     /* Perform our dichotomy */
695     for( i_max = p_libvlc_global->i_objects - 1 ; ; )
696     {
697         i_middle = i_max / 2;
698
699         if( pp_objects[i_middle]->i_object_id > i_id )
700         {
701             i_max = i_middle;
702         }
703         else if( pp_objects[i_middle]->i_object_id < i_id )
704         {
705             if( i_middle )
706             {
707                 pp_objects += i_middle;
708                 i_max -= i_middle;
709             }
710             else
711             {
712                 /* This happens when there are only two remaining objects */
713                 if( pp_objects[i_middle+1]->i_object_id == i_id
714                     && vlc_internals( pp_objects[i_middle+1] )->i_refcount > 0 )
715                 {
716                     vlc_object_yield_locked( pp_objects[i_middle+1] );
717                     vlc_mutex_unlock( &structure_lock );
718                     return pp_objects[i_middle+1];
719                 }
720                 break;
721             }
722         }
723         else if( vlc_internals( pp_objects[i_middle] )->i_refcount > 0 )
724         {
725             vlc_object_yield_locked( pp_objects[i_middle] );
726             vlc_mutex_unlock( &structure_lock );
727             return pp_objects[i_middle];
728         }
729
730         if( i_max == 0 )
731         {
732             /* this means that i_max == i_middle, and since we have already
733              * tested pp_objects[i_middle]), p_found is properly set. */
734             break;
735         }
736     }
737
738     vlc_mutex_unlock( &structure_lock );
739     return NULL;
740 }
741
742 /**
743  ****************************************************************************
744  * find a typed object and increment its refcount
745  *****************************************************************************
746  * This function recursively looks for a given object type. i_mode can be one
747  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
748  *****************************************************************************/
749 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
750 {
751     vlc_object_t *p_found;
752
753     vlc_mutex_lock( &structure_lock );
754
755     /* If we are of the requested type ourselves, don't look further */
756     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type
757         && vlc_internals( p_this )->i_refcount > 0 )
758     {
759         vlc_object_yield_locked( p_this );
760         vlc_mutex_unlock( &structure_lock );
761         return p_this;
762     }
763
764     /* Otherwise, recursively look for the object */
765     if( (i_mode & 0x000f) == FIND_ANYWHERE )
766     {
767         vlc_object_t *p_root = p_this;
768
769         /* Find the root */
770         while( p_root->p_parent != NULL &&
771                p_root != VLC_OBJECT( p_this->p_libvlc ) )
772         {
773             p_root = p_root->p_parent;
774         }
775
776         p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
777         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
778         {
779             p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
780                                   i_type, (i_mode & ~0x000f)|FIND_CHILD );
781         }
782     }
783     else
784     {
785         p_found = FindObject( p_this, i_type, i_mode );
786     }
787
788     vlc_mutex_unlock( &structure_lock );
789
790     return p_found;
791 }
792
793 /**
794  ****************************************************************************
795  * find a named object and increment its refcount
796  *****************************************************************************
797  * This function recursively looks for a given object name. i_mode can be one
798  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
799  *****************************************************************************/
800 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
801                                int i_mode )
802 {
803     vlc_object_t *p_found;
804
805     vlc_mutex_lock( &structure_lock );
806
807     /* Avoid obvious freed object uses */
808     assert( vlc_internals( p_this )->i_refcount > 0 );
809
810     /* If have the requested name ourselves, don't look further */
811     if( !(i_mode & FIND_STRICT)
812         && p_this->psz_object_name
813         && !strcmp( p_this->psz_object_name, psz_name )
814         && vlc_internals( p_this )->i_refcount > 0 )
815     {
816         vlc_object_yield_locked( p_this );
817         vlc_mutex_unlock( &structure_lock );
818         return p_this;
819     }
820
821     /* Otherwise, recursively look for the object */
822     if( (i_mode & 0x000f) == FIND_ANYWHERE )
823     {
824         vlc_object_t *p_root = p_this;
825
826         /* Find the root */
827         while( p_root->p_parent != NULL &&
828                p_root != VLC_OBJECT( p_this->p_libvlc ) )
829         {
830             p_root = p_root->p_parent;
831         }
832
833         p_found = FindObjectName( p_root, psz_name,
834                                  (i_mode & ~0x000f)|FIND_CHILD );
835         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
836         {
837             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
838                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
839         }
840     }
841     else
842     {
843         p_found = FindObjectName( p_this, psz_name, i_mode );
844     }
845
846     vlc_mutex_unlock( &structure_lock );
847
848     return p_found;
849 }
850
851 /**
852  ****************************************************************************
853  * increment an object refcount
854  *****************************************************************************/
855
856 /* When the structure_lock is locked */
857 static void vlc_object_yield_locked( vlc_object_t *p_this )
858 {
859     vlc_assert_locked (&structure_lock);
860
861     /* Avoid obvious freed object uses */
862     assert( vlc_internals( p_this )->i_refcount > 0 );
863
864     /* Increment the counter */
865     vlc_internals( p_this )->i_refcount++;
866 }
867
868 /* Public function */
869 void __vlc_object_yield( vlc_object_t *p_this )
870 {
871     vlc_mutex_lock( &structure_lock );
872     vlc_object_yield_locked( p_this );
873     vlc_mutex_unlock( &structure_lock );
874 }
875
876
877 /*****************************************************************************
878  * decrement an object refcount
879  * And destroy the object if its refcount reach zero.
880  *****************************************************************************/
881 void __vlc_object_release( vlc_object_t *p_this )
882 {
883     bool b_should_destroy;
884
885     vlc_mutex_lock( &structure_lock );
886
887     assert( vlc_internals( p_this )->i_refcount > 0 );
888     vlc_internals( p_this )->i_refcount--;
889     b_should_destroy = (vlc_internals( p_this )->i_refcount == 0);
890
891     if( b_should_destroy )
892     {
893         /* Make sure this object can't be obtained via vlc_find_object now that
894          * it is freed */
895         libvlc_global_data_t *p_libvlc_global = vlc_global();
896         int i_index;
897
898         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
899          * useless to try and recover anything if pp_objects gets smashed. */
900         i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
901                              p_libvlc_global->i_objects );
902         REMOVE_ELEM( p_libvlc_global->pp_objects,
903                      p_libvlc_global->i_objects, i_index );
904     }
905
906     vlc_mutex_unlock( &structure_lock );
907
908     if( b_should_destroy )
909         vlc_object_destroy( p_this );
910 }
911
912 /**
913  ****************************************************************************
914  * attach object to a parent object
915  *****************************************************************************
916  * This function sets p_this as a child of p_parent, and p_parent as a parent
917  * of p_this. This link can be undone using vlc_object_detach.
918  *****************************************************************************/
919 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
920 {
921     if( !p_this ) return;
922
923     vlc_mutex_lock( &structure_lock );
924
925     /* Avoid obvious freed object uses */
926     assert( vlc_internals( p_this )->i_refcount > 0 );
927
928     /* Attach the parent to its child */
929     p_this->p_parent = p_parent;
930
931     /* Attach the child to its parent */
932     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
933                  p_parent->i_children, p_this );
934
935     /* Climb up the tree to see whether we are connected with the root */
936     if( vlc_internals( p_parent )->b_attached )
937     {
938         SetAttachment( p_this, true );
939     }
940
941     vlc_mutex_unlock( &structure_lock );
942 }
943
944 /**
945  ****************************************************************************
946  * detach object from its parent
947  *****************************************************************************
948  * This function removes all links between an object and its parent.
949  *****************************************************************************/
950 void __vlc_object_detach( vlc_object_t *p_this )
951 {
952     if( !p_this ) return;
953
954     vlc_mutex_lock( &structure_lock );
955
956     if( !p_this->p_parent )
957     {
958         msg_Err( p_this, "object is not attached" );
959         vlc_mutex_unlock( &structure_lock );
960         return;
961     }
962
963     /* Climb up the tree to see whether we are connected with the root */
964     if( vlc_internals( p_this->p_parent )->b_attached )
965     {
966         SetAttachment( p_this, false );
967     }
968
969     DetachObject( p_this );
970     vlc_mutex_unlock( &structure_lock );
971     p_this = NULL;
972 }
973
974 /**
975  ****************************************************************************
976  * find a list typed objects and increment their refcount
977  *****************************************************************************
978  * This function recursively looks for a given object type. i_mode can be one
979  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
980  *****************************************************************************/
981 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
982 {
983     vlc_list_t *p_list;
984     vlc_object_t **pp_current, **pp_end;
985     int i_count = 0, i_index = 0;
986     libvlc_global_data_t *p_libvlc_global = vlc_global();
987
988     vlc_mutex_lock( &structure_lock );
989
990     /* Look for the objects */
991     switch( i_mode & 0x000f )
992     {
993     case FIND_ANYWHERE:
994         pp_current = p_libvlc_global->pp_objects;
995         pp_end = pp_current + p_libvlc_global->i_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                 i_count++;
1003             }
1004         }
1005
1006         p_list = NewList( i_count );
1007         pp_current = p_libvlc_global->pp_objects;
1008
1009         for( ; pp_current < pp_end ; pp_current++ )
1010         {
1011             if( vlc_internals(*pp_current)->b_attached
1012                  && (*pp_current)->i_object_type == i_type )
1013             {
1014                 ListReplace( p_list, *pp_current, i_index );
1015                 if( i_index < i_count ) i_index++;
1016             }
1017         }
1018     break;
1019
1020     case FIND_CHILD:
1021         i_count = CountChildren( p_this, i_type );
1022         p_list = NewList( i_count );
1023
1024         /* Check allocation was successful */
1025         if( p_list->i_count != i_count )
1026         {
1027             msg_Err( p_this, "list allocation failed!" );
1028             p_list->i_count = 0;
1029             break;
1030         }
1031
1032         p_list->i_count = 0;
1033         ListChildren( p_list, p_this, i_type );
1034         break;
1035
1036     default:
1037         msg_Err( p_this, "unimplemented!" );
1038         p_list = NewList( 0 );
1039         break;
1040     }
1041
1042     vlc_mutex_unlock( &structure_lock );
1043
1044     return p_list;
1045 }
1046
1047 /*****************************************************************************
1048  * DumpCommand: print the current vlc structure
1049  *****************************************************************************
1050  * This function prints either an ASCII tree showing the connections between
1051  * vlc objects, and additional information such as their refcount, thread ID,
1052  * etc. (command "tree"), or the same data as a simple list (command "list").
1053  *****************************************************************************/
1054 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1055                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1056 {
1057     libvlc_global_data_t *p_libvlc_global = vlc_global();
1058
1059     (void)oldval; (void)p_data;
1060     if( *psz_cmd == 'l' )
1061     {
1062         vlc_mutex_lock( &structure_lock );
1063
1064         vlc_object_t **pp_current, **pp_end;
1065
1066         pp_current = p_libvlc_global->pp_objects;
1067         pp_end = pp_current + p_libvlc_global->i_objects;
1068
1069         for( ; pp_current < pp_end ; pp_current++ )
1070         {
1071             if( vlc_internals(*pp_current)->b_attached )
1072             {
1073                 PrintObject( *pp_current, "" );
1074             }
1075             else
1076             {
1077                 printf( " o %.8i %s (not attached)\n",
1078                         (*pp_current)->i_object_id,
1079                         (*pp_current)->psz_object_type );
1080             }
1081         }
1082
1083         vlc_mutex_unlock( &structure_lock );
1084     }
1085     else
1086     {
1087         vlc_object_t *p_object = NULL;
1088
1089         if( *newval.psz_string )
1090         {
1091             char *end;
1092             int i_id = strtol( newval.psz_string, &end, 0 );
1093             if( end != newval.psz_string )
1094                 p_object = vlc_object_get( i_id );
1095             else
1096             {
1097                 /* try using the object's name to find it */
1098                 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1099                 if( p_libvlc )
1100                 {
1101                     /* Look in p_libvlc's children tree */
1102                     p_object = vlc_object_find_name( p_libvlc,
1103                                                      newval.psz_string,
1104                                                      FIND_CHILD );
1105                     vlc_object_release( p_libvlc );
1106                 }
1107                 if( !p_object )
1108                 {
1109                     /* If it's not in libvlc, look in libvlc_global (== p_this) */
1110                     p_object = vlc_object_find_name( p_this,
1111                                                      newval.psz_string,
1112                                                      FIND_CHILD );
1113                 }
1114             }
1115
1116             if( !p_object )
1117             {
1118                 return VLC_ENOOBJ;
1119             }
1120         }
1121
1122         vlc_mutex_lock( &structure_lock );
1123
1124         if( *psz_cmd == 't' )
1125         {
1126             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1127
1128             if( !p_object )
1129                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1130
1131             psz_foo[0] = '|';
1132             DumpStructure( p_object, 0, psz_foo );
1133         }
1134         else if( *psz_cmd == 'v' )
1135         {
1136             int i;
1137
1138             if( !p_object )
1139                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1140
1141             PrintObject( p_object, "" );
1142
1143             if( !vlc_internals( p_object )->i_vars )
1144                 printf( " `-o No variables\n" );
1145             for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1146             {
1147                 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1148
1149                 const char *psz_type = "unknown";
1150                 switch( p_var->i_type & VLC_VAR_TYPE )
1151                 {
1152 #define MYCASE( type, nice )                \
1153                     case VLC_VAR_ ## type:  \
1154                         psz_type = nice;    \
1155                         break;
1156                     MYCASE( VOID, "void" );
1157                     MYCASE( BOOL, "bool" );
1158                     MYCASE( INTEGER, "integer" );
1159                     MYCASE( HOTKEY, "hotkey" );
1160                     MYCASE( STRING, "string" );
1161                     MYCASE( MODULE, "module" );
1162                     MYCASE( FILE, "file" );
1163                     MYCASE( DIRECTORY, "directory" );
1164                     MYCASE( VARIABLE, "variable" );
1165                     MYCASE( FLOAT, "float" );
1166                     MYCASE( TIME, "time" );
1167                     MYCASE( ADDRESS, "address" );
1168                     MYCASE( MUTEX, "mutex" );
1169                     MYCASE( LIST, "list" );
1170 #undef MYCASE
1171                 }
1172                 printf( " %c-o \"%s\" (%s",
1173                         i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1174                         p_var->psz_name, psz_type );
1175                 if( p_var->psz_text )
1176                     printf( ", %s", p_var->psz_text );
1177                 printf( ")" );
1178                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1179                     printf( ", command" );
1180                 if( p_var->i_entries )
1181                     printf( ", %d callbacks", p_var->i_entries );
1182                 switch( p_var->i_type & 0x00f0 )
1183                 {
1184                     case VLC_VAR_VOID:
1185                     case VLC_VAR_MUTEX:
1186                         break;
1187                     case VLC_VAR_BOOL:
1188                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1189                         break;
1190                     case VLC_VAR_INTEGER:
1191                         printf( ": %d", p_var->val.i_int );
1192                         break;
1193                     case VLC_VAR_STRING:
1194                         printf( ": \"%s\"", p_var->val.psz_string );
1195                         break;
1196                     case VLC_VAR_FLOAT:
1197                         printf( ": %f", p_var->val.f_float );
1198                         break;
1199                     case VLC_VAR_TIME:
1200                         printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1201                         break;
1202                     case VLC_VAR_ADDRESS:
1203                         printf( ": %p", p_var->val.p_address );
1204                         break;
1205                     case VLC_VAR_LIST:
1206                         printf( ": TODO" );
1207                         break;
1208                 }
1209                 printf( "\n" );
1210             }
1211         }
1212
1213         vlc_mutex_unlock( &structure_lock );
1214
1215         if( *newval.psz_string )
1216         {
1217             vlc_object_release( p_object );
1218         }
1219     }
1220
1221     return VLC_SUCCESS;
1222 }
1223
1224 /*****************************************************************************
1225  * vlc_list_release: free a list previously allocated by vlc_list_find
1226  *****************************************************************************
1227  * This function decreases the refcount of all objects in the list and
1228  * frees the list.
1229  *****************************************************************************/
1230 void vlc_list_release( vlc_list_t *p_list )
1231 {
1232     int i_index;
1233
1234     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1235     {
1236         vlc_object_release( p_list->p_values[i_index].p_object );
1237     }
1238
1239     free( p_list->p_values );
1240     free( p_list );
1241 }
1242
1243 /*****************************************************************************
1244  * dump an object. (Debug function)
1245  *****************************************************************************/
1246 void __vlc_object_dump( vlc_object_t *p_this )
1247 {
1248     vlc_mutex_lock( &structure_lock );
1249     char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1250     psz_foo[0] = '|';
1251     DumpStructure( p_this, 0, psz_foo );
1252     vlc_mutex_unlock( &structure_lock );
1253 }
1254
1255 /* Following functions are local */
1256
1257 /*****************************************************************************
1258  * FindIndex: find the index of an object in an array of objects
1259  *****************************************************************************
1260  * This function assumes that p_this can be found in pp_objects. It will not
1261  * crash if p_this cannot be found, but will return a wrong value. It is your
1262  * duty to check the return value if you are not certain that the object could
1263  * be found for sure.
1264  *****************************************************************************/
1265 static int FindIndex( vlc_object_t *p_this,
1266                       vlc_object_t **pp_objects, int i_count )
1267 {
1268     int i_middle = i_count / 2;
1269
1270     if( i_count == 0 )
1271     {
1272         return 0;
1273     }
1274
1275     if( pp_objects[i_middle] == p_this )
1276     {
1277         return i_middle;
1278     }
1279
1280     if( i_count == 1 )
1281     {
1282         return 0;
1283     }
1284
1285     /* We take advantage of the sorted array */
1286     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1287     {
1288         return i_middle + FindIndex( p_this, pp_objects + i_middle,
1289                                              i_count - i_middle );
1290     }
1291     else
1292     {
1293         return FindIndex( p_this, pp_objects, i_middle );
1294     }
1295 }
1296
1297 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1298 {
1299     int i;
1300     vlc_object_t *p_tmp;
1301
1302     switch( i_mode & 0x000f )
1303     {
1304     case FIND_PARENT:
1305         p_tmp = p_this->p_parent;
1306         if( p_tmp )
1307         {
1308             if( p_tmp->i_object_type == i_type
1309                 && vlc_internals( p_tmp )->i_refcount > 0 )
1310             {
1311                 vlc_object_yield_locked( p_tmp );
1312                 return p_tmp;
1313             }
1314             else
1315             {
1316                 return FindObject( p_tmp, i_type, i_mode );
1317             }
1318         }
1319         break;
1320
1321     case FIND_CHILD:
1322         for( i = p_this->i_children; i--; )
1323         {
1324             p_tmp = p_this->pp_children[i];
1325             if( p_tmp->i_object_type == i_type
1326                 && vlc_internals( p_tmp )->i_refcount > 0 )
1327             {
1328                 vlc_object_yield_locked( p_tmp );
1329                 return p_tmp;
1330             }
1331             else if( p_tmp->i_children )
1332             {
1333                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1334                 if( p_tmp )
1335                 {
1336                     return p_tmp;
1337                 }
1338             }
1339         }
1340         break;
1341
1342     case FIND_ANYWHERE:
1343         /* Handled in vlc_object_find */
1344         break;
1345     }
1346
1347     return NULL;
1348 }
1349
1350 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1351                                       const char *psz_name,
1352                                       int i_mode )
1353 {
1354     int i;
1355     vlc_object_t *p_tmp;
1356
1357     switch( i_mode & 0x000f )
1358     {
1359     case FIND_PARENT:
1360         p_tmp = p_this->p_parent;
1361         if( p_tmp )
1362         {
1363             if( p_tmp->psz_object_name
1364                 && !strcmp( p_tmp->psz_object_name, psz_name )
1365                 && vlc_internals( p_tmp )->i_refcount > 0 )
1366             {
1367                 vlc_object_yield_locked( p_tmp );
1368                 return p_tmp;
1369             }
1370             else
1371             {
1372                 return FindObjectName( p_tmp, psz_name, i_mode );
1373             }
1374         }
1375         break;
1376
1377     case FIND_CHILD:
1378         for( i = p_this->i_children; i--; )
1379         {
1380             p_tmp = p_this->pp_children[i];
1381             if( p_tmp->psz_object_name
1382                 && !strcmp( p_tmp->psz_object_name, psz_name )
1383                 && vlc_internals( p_tmp )->i_refcount > 0 )
1384             {
1385                 vlc_object_yield_locked( p_tmp );
1386                 return p_tmp;
1387             }
1388             else if( p_tmp->i_children )
1389             {
1390                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1391                 if( p_tmp )
1392                 {
1393                     return p_tmp;
1394                 }
1395             }
1396         }
1397         break;
1398
1399     case FIND_ANYWHERE:
1400         /* Handled in vlc_object_find */
1401         break;
1402     }
1403
1404     return NULL;
1405 }
1406
1407 static void DetachObject( vlc_object_t *p_this )
1408 {
1409     vlc_object_t *p_parent = p_this->p_parent;
1410     int i_index, i;
1411
1412     /* Remove p_this's parent */
1413     p_this->p_parent = NULL;
1414
1415     /* Remove all of p_parent's children which are p_this */
1416     for( i_index = p_parent->i_children ; i_index-- ; )
1417     {
1418         if( p_parent->pp_children[i_index] == p_this )
1419         {
1420             p_parent->i_children--;
1421             for( i = i_index ; i < p_parent->i_children ; i++ )
1422             {
1423                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1424             }
1425         }
1426     }
1427
1428     if( p_parent->i_children )
1429     {
1430         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1431                                p_parent->i_children * sizeof(vlc_object_t *) );
1432     }
1433     else
1434     {
1435         free( p_parent->pp_children );
1436         p_parent->pp_children = NULL;
1437     }
1438 }
1439
1440 /*****************************************************************************
1441  * SetAttachment: recursively set the b_attached flag of a subtree.
1442  *****************************************************************************
1443  * This function is used by the attach and detach functions to propagate
1444  * the b_attached flag in a subtree.
1445  *****************************************************************************/
1446 static void SetAttachment( vlc_object_t *p_this, bool b_attached )
1447 {
1448     int i_index;
1449
1450     for( i_index = p_this->i_children ; i_index-- ; )
1451     {
1452         SetAttachment( p_this->pp_children[i_index], b_attached );
1453     }
1454
1455     vlc_internals( p_this )->b_attached = b_attached;
1456 }
1457
1458 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1459 {
1460     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1461          psz_parent[20];
1462
1463     memset( &psz_name, 0, sizeof(psz_name) );
1464     if( p_this->psz_object_name )
1465     {
1466         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1467         if( psz_name[48] )
1468             psz_name[48] = '\"';
1469     }
1470
1471     psz_children[0] = '\0';
1472     switch( p_this->i_children )
1473     {
1474         case 0:
1475             break;
1476         case 1:
1477             strcpy( psz_children, ", 1 child" );
1478             break;
1479         default:
1480             snprintf( psz_children, 19, ", %i children", p_this->i_children );
1481             break;
1482     }
1483
1484     psz_refcount[0] = '\0';
1485     if( vlc_internals( p_this )->i_refcount > 0 )
1486         snprintf( psz_refcount, 19, ", refcount %u",
1487                   vlc_internals( p_this )->i_refcount );
1488
1489     psz_thread[0] = '\0';
1490     if( vlc_internals( p_this )->b_thread )
1491         snprintf( psz_thread, 29, " (thread %u)",
1492 #if defined(WIN32) || defined(UNDER_CE)
1493                   (unsigned)vlc_internals( p_this )->thread_id.id );
1494 #else
1495                   (unsigned)vlc_internals( p_this )->thread_id );
1496 #endif
1497
1498     psz_parent[0] = '\0';
1499     if( p_this->p_parent )
1500         snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1501
1502     printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1503             p_this->i_object_id, p_this->psz_object_type,
1504             psz_name, psz_thread, psz_refcount, psz_children,
1505             psz_parent );
1506 }
1507
1508 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1509 {
1510     int i;
1511     char i_back = psz_foo[i_level];
1512     psz_foo[i_level] = '\0';
1513
1514     PrintObject( p_this, psz_foo );
1515
1516     psz_foo[i_level] = i_back;
1517
1518     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1519     {
1520         msg_Warn( p_this, "structure tree is too deep" );
1521         return;
1522     }
1523
1524     for( i = 0 ; i < p_this->i_children ; i++ )
1525     {
1526         if( i_level )
1527         {
1528             psz_foo[i_level-1] = ' ';
1529
1530             if( psz_foo[i_level-2] == '`' )
1531             {
1532                 psz_foo[i_level-2] = ' ';
1533             }
1534         }
1535
1536         if( i == p_this->i_children - 1 )
1537         {
1538             psz_foo[i_level] = '`';
1539         }
1540         else
1541         {
1542             psz_foo[i_level] = '|';
1543         }
1544
1545         psz_foo[i_level+1] = '-';
1546         psz_foo[i_level+2] = '\0';
1547
1548         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1549     }
1550 }
1551
1552 static vlc_list_t * NewList( int i_count )
1553 {
1554     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1555     if( p_list == NULL )
1556     {
1557         return NULL;
1558     }
1559
1560     p_list->i_count = i_count;
1561
1562     if( i_count == 0 )
1563     {
1564         p_list->p_values = NULL;
1565         return p_list;
1566     }
1567
1568     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1569     if( p_list->p_values == NULL )
1570     {
1571         p_list->i_count = 0;
1572         return p_list;
1573     }
1574
1575     return p_list;
1576 }
1577
1578 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1579                          int i_index )
1580 {
1581     if( p_list == NULL || i_index >= p_list->i_count )
1582     {
1583         return;
1584     }
1585
1586     vlc_object_yield_locked( p_object );
1587
1588     p_list->p_values[i_index].p_object = p_object;
1589
1590     return;
1591 }
1592
1593 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1594 {
1595     if( p_list == NULL )
1596     {
1597         return;
1598     }
1599
1600     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1601                                 * sizeof( vlc_value_t ) );
1602     if( p_list->p_values == NULL )
1603     {
1604         p_list->i_count = 0;
1605         return;
1606     }
1607
1608     vlc_object_yield_locked( p_object );
1609
1610     p_list->p_values[p_list->i_count].p_object = p_object;
1611     p_list->i_count++;
1612
1613     return;
1614 }*/
1615
1616 static int CountChildren( vlc_object_t *p_this, int i_type )
1617 {
1618     vlc_object_t *p_tmp;
1619     int i, i_count = 0;
1620
1621     for( i = 0; i < p_this->i_children; i++ )
1622     {
1623         p_tmp = p_this->pp_children[i];
1624
1625         if( p_tmp->i_object_type == i_type )
1626         {
1627             i_count++;
1628         }
1629
1630         if( p_tmp->i_children )
1631         {
1632             i_count += CountChildren( p_tmp, i_type );
1633         }
1634     }
1635
1636     return i_count;
1637 }
1638
1639 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1640 {
1641     vlc_object_t *p_tmp;
1642     int i;
1643
1644     for( i = 0; i < p_this->i_children; i++ )
1645     {
1646         p_tmp = p_this->pp_children[i];
1647
1648         if( p_tmp->i_object_type == i_type )
1649         {
1650             ListReplace( p_list, p_tmp, p_list->i_count++ );
1651         }
1652
1653         if( p_tmp->i_children )
1654         {
1655             ListChildren( p_list, p_tmp, i_type );
1656         }
1657     }
1658 }