]> git.sesse.net Git - vlc/blob - src/misc/objects.c
Use a per-object spin around the reference count.
[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
93 /*****************************************************************************
94  * Local structure lock
95  *****************************************************************************/
96 static vlc_mutex_t    structure_lock;
97
98 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
99                          int i_type, const char *psz_type )
100 {
101     vlc_object_t *p_new;
102     vlc_object_internals_t *p_priv;
103
104     /* NOTE:
105      * VLC objects are laid out as follow:
106      * - first the LibVLC-private per-object data,
107      * - then VLC_COMMON members from vlc_object_t,
108      * - finally, the type-specific data (if any).
109      *
110      * This function initializes the LibVLC and common data,
111      * and zeroes the rest.
112      */
113     p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
114     if( p_priv == NULL )
115         return NULL;
116
117     assert (i_size >= sizeof (vlc_object_t));
118     p_new = (vlc_object_t *)(p_priv + 1);
119
120     p_new->i_object_type = i_type;
121     p_new->psz_object_type = psz_type;
122     p_new->psz_object_name = NULL;
123
124     p_new->b_die = false;
125     p_new->b_error = false;
126     p_new->b_dead = false;
127     p_priv->b_attached = false;
128     p_new->b_force = false;
129
130     p_new->psz_header = NULL;
131
132     if (p_this)
133         p_new->i_flags = p_this->i_flags
134             & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
135
136     p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
137
138     if( !p_priv->p_vars )
139     {
140         free( p_priv );
141         return NULL;
142     }
143
144     libvlc_global_data_t *p_libvlc_global;
145     if( p_this == NULL )
146     {
147         /* Only the global root object is created out of the blue */
148         p_libvlc_global = (libvlc_global_data_t *)p_new;
149         p_new->p_libvlc = NULL;
150
151         p_libvlc_global->i_counter = 0;
152         p_libvlc_global->i_objects = 0;
153         p_libvlc_global->pp_objects = NULL;
154         p_priv->b_attached = true;
155         vlc_mutex_init( &structure_lock );
156     }
157     else
158     {
159         p_libvlc_global = vlc_global();
160         if( i_type == VLC_OBJECT_LIBVLC )
161         {
162             p_new->p_libvlc = (libvlc_int_t*)p_new;
163             p_priv->b_attached = true;
164         }
165         else
166         {
167             p_new->p_libvlc = p_this->p_libvlc;
168         }
169     }
170
171     vlc_spin_init( &p_priv->ref_spin );
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     /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
191      * useless to try and recover anything if pp_objects gets smashed. */
192     TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
193                 p_new );
194     vlc_mutex_unlock( &structure_lock );
195
196     if( i_type == VLC_OBJECT_LIBVLC )
197     {
198         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
199         var_AddCallback( p_new, "list", DumpCommand, NULL );
200         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
201         var_AddCallback( p_new, "tree", DumpCommand, NULL );
202         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
203         var_AddCallback( p_new, "vars", DumpCommand, NULL );
204     }
205
206     return p_new;
207 }
208
209
210 /**
211  * Allocates and initializes a vlc object.
212  *
213  * @param i_type known object type (all of them are negative integer values),
214  *               or object byte size (always positive).
215  *
216  * @return the new object, or NULL on error.
217  */
218 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
219 {
220     const char   * psz_type;
221     size_t         i_size;
222
223     switch( i_type )
224     {
225         case VLC_OBJECT_LIBVLC:
226             i_size = sizeof(libvlc_int_t);
227             psz_type = "libvlc";
228             break;
229         case VLC_OBJECT_INTF:
230             i_size = sizeof(intf_thread_t);
231             psz_type = "interface";
232             break;
233         case VLC_OBJECT_DIALOGS:
234             i_size = sizeof(intf_thread_t);
235             psz_type = "dialogs";
236             break;
237         case VLC_OBJECT_DEMUX:
238             i_size = sizeof(demux_t);
239             psz_type = "demux";
240             break;
241         case VLC_OBJECT_ACCESS:
242             i_size = sizeof(access_t);
243             psz_type = "access";
244             break;
245         case VLC_OBJECT_DECODER:
246             i_size = sizeof(decoder_t);
247             psz_type = "decoder";
248             break;
249         case VLC_OBJECT_PACKETIZER:
250             i_size = sizeof(decoder_t);
251             psz_type = "packetizer";
252             break;
253         case VLC_OBJECT_ENCODER:
254             i_size = sizeof(encoder_t);
255             psz_type = "encoder";
256             break;
257         case VLC_OBJECT_FILTER:
258             i_size = sizeof(filter_t);
259             psz_type = "filter";
260             break;
261         case VLC_OBJECT_VOUT:
262             i_size = sizeof(vout_thread_t);
263             psz_type = "video output";
264             break;
265         case VLC_OBJECT_AOUT:
266             i_size = sizeof(aout_instance_t);
267             psz_type = "audio output";
268             break;
269         case VLC_OBJECT_SOUT:
270             i_size = sizeof(sout_instance_t);
271             psz_type = "stream output";
272             break;
273         case VLC_OBJECT_OPENGL:
274             i_size = sizeof( vout_thread_t );
275             psz_type = "opengl";
276             break;
277         case VLC_OBJECT_ANNOUNCE:
278             i_size = sizeof( announce_handler_t );
279             psz_type = "announce";
280             break;
281         case VLC_OBJECT_INTERACTION:
282             i_size = sizeof( interaction_t );
283             psz_type = "interaction";
284             break;
285         default:
286             i_size = i_type > (int)sizeof(vlc_object_t)
287                          ? i_type : (int)sizeof(vlc_object_t);
288             i_type = VLC_OBJECT_GENERIC;
289             psz_type = "generic";
290             break;
291     }
292
293     return vlc_custom_create( p_this, i_size, i_type, psz_type );
294 }
295
296
297 /**
298  ****************************************************************************
299  * Set the destructor of a vlc object
300  *
301  * This function sets the destructor of the vlc object. It will be called
302  * when the object is destroyed when the its refcount reaches 0.
303  * (It is called by the internal function vlc_object_destroy())
304  *****************************************************************************/
305 void __vlc_object_set_destructor( vlc_object_t *p_this,
306                                   vlc_destructor_t pf_destructor )
307 {
308     vlc_object_internals_t *p_priv = vlc_internals(p_this );
309     p_priv->pf_destructor = pf_destructor;
310 }
311
312 /**
313  ****************************************************************************
314  * Destroy a vlc object (Internal)
315  *
316  * This function destroys an object that has been previously allocated with
317  * vlc_object_create. The object's refcount must be zero and it must not be
318  * attached to other objects in any way.
319  *****************************************************************************/
320 static void vlc_object_destroy( vlc_object_t *p_this )
321 {
322     vlc_object_internals_t *p_priv = vlc_internals( p_this );
323
324     /* Automatically detach the object from its parents */
325     if( p_this->p_parent ) vlc_object_detach( p_this );
326
327
328     /* Send a kill to the object's thread if applicable */
329     vlc_object_kill( p_this );
330
331     /* If we are running on a thread, wait until it ends */
332     if( p_priv->b_thread )
333         vlc_thread_join( p_this );
334
335     /* Call the custom "subclass" destructor */
336     if( p_priv->pf_destructor )
337         p_priv->pf_destructor( p_this );
338
339     /* Sanity checks */
340     if( p_this->i_children )
341     {
342         int i;
343
344         fprintf( stderr,
345                  "ERROR: cannot delete object (%i, %s) with %d children\n",
346                  p_this->i_object_id, p_this->psz_object_name,
347                  p_this->i_children );
348
349         for( i = 0; i < p_this->i_children; i++ )
350         {
351             fprintf( stderr,
352                      "ERROR: Remaining children object "
353                      "(id:%i, type:%s, name:%s)\n",
354                      p_this->pp_children[i]->i_object_id,
355                      p_this->pp_children[i]->psz_object_type,
356                      p_this->pp_children[i]->psz_object_name );
357         }
358         fflush(stderr);
359
360         /* Dump libvlc object to ease debugging */
361         vlc_object_dump( p_this->p_libvlc );
362
363         abort();
364     }
365
366     /* Destroy the associated variables, starting from the end so that
367      * no memmove calls have to be done. */
368     while( p_priv->i_vars )
369     {
370         var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
371     }
372
373     free( p_priv->p_vars );
374     vlc_mutex_destroy( &p_priv->var_lock );
375
376     free( p_this->psz_header );
377
378     if( p_this->p_libvlc == NULL )
379     {
380         libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
381
382 #ifndef NDEBUG
383         assert( p_global == vlc_global() );
384         /* Test for leaks */
385         if( p_global->i_objects > 0 )
386         {
387             int i;
388             for( i = 0; i < p_global->i_objects; i++ )
389             {
390                 /* We are leaking this object */
391                 fprintf( stderr,
392                          "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
393                          p_global->pp_objects[i]->i_object_id,
394                          p_global->pp_objects[i]->psz_object_type,
395                          p_global->pp_objects[i]->psz_object_name );
396
397                 /* Dump libvlc object to ease debugging */
398                 vlc_object_dump( p_global->pp_objects[i] );
399
400                 fflush(stderr);
401             }
402
403             /* Dump libvlc object to ease debugging */
404             vlc_object_dump( p_this );
405
406             /* Strongly abort, cause we want these to be fixed */
407             abort();
408         }
409 #endif
410
411         /* We are the global object ... no need to lock. */
412         free( p_global->pp_objects );
413         p_global->pp_objects = NULL;
414
415         vlc_mutex_destroy( &structure_lock );
416     }
417
418     FREENULL( p_this->psz_object_name );
419
420 #if defined(WIN32) || defined(UNDER_CE)
421     /* if object has an associated thread, close it now */
422     if( p_priv->thread_id.hThread )
423        CloseHandle(p_priv->thread_id.hThread);
424 #endif
425
426     vlc_spin_destroy( &p_priv->ref_spin );
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( pp_objects[i_middle+1] );
717                     obj = pp_objects[i_middle+1];
718                 }
719                 break;
720             }
721         }
722         else
723         {
724             vlc_object_yield( 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( 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     /* If have the requested name ourselves, don't look further */
801     if( !(i_mode & FIND_STRICT)
802         && p_this->psz_object_name
803         && !strcmp( p_this->psz_object_name, psz_name ) )
804     {
805         vlc_object_yield( p_this );
806         vlc_mutex_unlock( &structure_lock );
807         return p_this;
808     }
809
810     /* Otherwise, recursively look for the object */
811     if( (i_mode & 0x000f) == FIND_ANYWHERE )
812     {
813         vlc_object_t *p_root = p_this;
814
815         /* Find the root */
816         while( p_root->p_parent != NULL &&
817                p_root != VLC_OBJECT( p_this->p_libvlc ) )
818         {
819             p_root = p_root->p_parent;
820         }
821
822         p_found = FindObjectName( p_root, psz_name,
823                                  (i_mode & ~0x000f)|FIND_CHILD );
824         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
825         {
826             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
827                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
828         }
829     }
830     else
831     {
832         p_found = FindObjectName( p_this, psz_name, i_mode );
833     }
834
835     vlc_mutex_unlock( &structure_lock );
836
837     return p_found;
838 }
839
840 /**
841  * Increment an object reference counter.
842  */
843 void __vlc_object_yield( vlc_object_t *p_this )
844 {
845     vlc_object_internals_t *internals = vlc_internals( p_this );
846
847     vlc_spin_lock( &internals->ref_spin );
848     /* Avoid obvious freed object uses */
849     assert( internals->i_refcount > 0 );
850     /* Increment the counter */
851     internals->i_refcount++;
852     vlc_spin_unlock( &internals->ref_spin );
853 }
854
855 /*****************************************************************************
856  * decrement an object refcount
857  * And destroy the object if its refcount reach zero.
858  *****************************************************************************/
859 void __vlc_object_release( vlc_object_t *p_this )
860 {
861     vlc_object_internals_t *internals = vlc_internals( p_this );
862     bool b_should_destroy;
863
864     vlc_spin_lock( &internals->ref_spin );
865     assert( internals->i_refcount > 0 );
866
867     if( internals->i_refcount > 1 )
868     {
869         /* Fast path */
870         /* There are still other references to the object */
871         internals->i_refcount--;
872         vlc_spin_unlock( &internals->ref_spin );
873         return;
874     }
875     vlc_spin_unlock( &internals->ref_spin );
876
877     /* Slow path */
878     /* Remember that we cannot hold the spin while waiting on the mutex */
879     vlc_mutex_lock( &structure_lock );
880     /* Take the spin again. Note that another thread may have yielded the
881      * object in the (very short) mean time. */
882     vlc_spin_lock( &internals->ref_spin );
883     b_should_destroy = --internals->i_refcount == 0;
884     vlc_spin_unlock( &internals->ref_spin );
885
886     if( b_should_destroy )
887     {
888         /* Remove the object from the table so that it cannot be returned from
889          * vlc_object_find() and friends. */
890         libvlc_global_data_t *p_libvlc_global = vlc_global();
891         int i_index;
892
893         i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
894                              p_libvlc_global->i_objects );
895         REMOVE_ELEM( p_libvlc_global->pp_objects,
896                      p_libvlc_global->i_objects, i_index );
897     }
898
899     vlc_mutex_unlock( &structure_lock );
900
901     if( b_should_destroy )
902         vlc_object_destroy( p_this );
903 }
904
905 /**
906  ****************************************************************************
907  * attach object to a parent object
908  *****************************************************************************
909  * This function sets p_this as a child of p_parent, and p_parent as a parent
910  * of p_this. This link can be undone using vlc_object_detach.
911  *****************************************************************************/
912 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
913 {
914     if( !p_this ) return;
915
916     vlc_mutex_lock( &structure_lock );
917
918     /* Attach the parent to its child */
919     p_this->p_parent = p_parent;
920
921     /* Attach the child to its parent */
922     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
923                  p_parent->i_children, p_this );
924
925     /* Climb up the tree to see whether we are connected with the root */
926     if( vlc_internals( p_parent )->b_attached )
927     {
928         SetAttachment( p_this, true );
929     }
930
931     vlc_mutex_unlock( &structure_lock );
932 }
933
934 /**
935  ****************************************************************************
936  * detach object from its parent
937  *****************************************************************************
938  * This function removes all links between an object and its parent.
939  *****************************************************************************/
940 void __vlc_object_detach( vlc_object_t *p_this )
941 {
942     if( !p_this ) return;
943
944     vlc_mutex_lock( &structure_lock );
945
946     if( !p_this->p_parent )
947     {
948         msg_Err( p_this, "object is not attached" );
949         vlc_mutex_unlock( &structure_lock );
950         return;
951     }
952
953     /* Climb up the tree to see whether we are connected with the root */
954     if( vlc_internals( p_this->p_parent )->b_attached )
955     {
956         SetAttachment( p_this, false );
957     }
958
959     DetachObject( p_this );
960     vlc_mutex_unlock( &structure_lock );
961     p_this = NULL;
962 }
963
964 /**
965  ****************************************************************************
966  * find a list typed objects and increment their refcount
967  *****************************************************************************
968  * This function recursively looks for a given object type. i_mode can be one
969  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
970  *****************************************************************************/
971 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
972 {
973     vlc_list_t *p_list;
974     vlc_object_t **pp_current, **pp_end;
975     int i_count = 0, i_index = 0;
976     libvlc_global_data_t *p_libvlc_global = vlc_global();
977
978     vlc_mutex_lock( &structure_lock );
979
980     /* Look for the objects */
981     switch( i_mode & 0x000f )
982     {
983     case FIND_ANYWHERE:
984         pp_current = p_libvlc_global->pp_objects;
985         pp_end = pp_current + p_libvlc_global->i_objects;
986
987         for( ; pp_current < pp_end ; pp_current++ )
988         {
989             if( vlc_internals(*pp_current)->b_attached
990                  && (*pp_current)->i_object_type == i_type )
991             {
992                 i_count++;
993             }
994         }
995
996         p_list = NewList( i_count );
997         pp_current = p_libvlc_global->pp_objects;
998
999         for( ; pp_current < pp_end ; pp_current++ )
1000         {
1001             if( vlc_internals(*pp_current)->b_attached
1002                  && (*pp_current)->i_object_type == i_type )
1003             {
1004                 ListReplace( p_list, *pp_current, i_index );
1005                 if( i_index < i_count ) i_index++;
1006             }
1007         }
1008     break;
1009
1010     case FIND_CHILD:
1011         i_count = CountChildren( p_this, i_type );
1012         p_list = NewList( i_count );
1013
1014         /* Check allocation was successful */
1015         if( p_list->i_count != i_count )
1016         {
1017             msg_Err( p_this, "list allocation failed!" );
1018             p_list->i_count = 0;
1019             break;
1020         }
1021
1022         p_list->i_count = 0;
1023         ListChildren( p_list, p_this, i_type );
1024         break;
1025
1026     default:
1027         msg_Err( p_this, "unimplemented!" );
1028         p_list = NewList( 0 );
1029         break;
1030     }
1031
1032     vlc_mutex_unlock( &structure_lock );
1033
1034     return p_list;
1035 }
1036
1037 /*****************************************************************************
1038  * DumpCommand: print the current vlc structure
1039  *****************************************************************************
1040  * This function prints either an ASCII tree showing the connections between
1041  * vlc objects, and additional information such as their refcount, thread ID,
1042  * etc. (command "tree"), or the same data as a simple list (command "list").
1043  *****************************************************************************/
1044 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1045                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1046 {
1047     libvlc_global_data_t *p_libvlc_global = vlc_global();
1048
1049     (void)oldval; (void)p_data;
1050     if( *psz_cmd == 'l' )
1051     {
1052         vlc_mutex_lock( &structure_lock );
1053
1054         vlc_object_t **pp_current, **pp_end;
1055
1056         pp_current = p_libvlc_global->pp_objects;
1057         pp_end = pp_current + p_libvlc_global->i_objects;
1058
1059         for( ; pp_current < pp_end ; pp_current++ )
1060         {
1061             if( vlc_internals(*pp_current)->b_attached )
1062             {
1063                 PrintObject( *pp_current, "" );
1064             }
1065             else
1066             {
1067                 printf( " o %.8i %s (not attached)\n",
1068                         (*pp_current)->i_object_id,
1069                         (*pp_current)->psz_object_type );
1070             }
1071         }
1072
1073         vlc_mutex_unlock( &structure_lock );
1074     }
1075     else
1076     {
1077         vlc_object_t *p_object = NULL;
1078
1079         if( *newval.psz_string )
1080         {
1081             char *end;
1082             int i_id = strtol( newval.psz_string, &end, 0 );
1083             if( end != newval.psz_string )
1084                 p_object = vlc_object_get( i_id );
1085             else
1086             {
1087                 /* try using the object's name to find it */
1088                 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1089                 if( p_libvlc )
1090                 {
1091                     /* Look in p_libvlc's children tree */
1092                     p_object = vlc_object_find_name( p_libvlc,
1093                                                      newval.psz_string,
1094                                                      FIND_CHILD );
1095                     vlc_object_release( p_libvlc );
1096                 }
1097                 if( !p_object )
1098                 {
1099                     /* If it's not in libvlc, look in libvlc_global (== p_this) */
1100                     p_object = vlc_object_find_name( p_this,
1101                                                      newval.psz_string,
1102                                                      FIND_CHILD );
1103                 }
1104             }
1105
1106             if( !p_object )
1107             {
1108                 return VLC_ENOOBJ;
1109             }
1110         }
1111
1112         vlc_mutex_lock( &structure_lock );
1113
1114         if( *psz_cmd == 't' )
1115         {
1116             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1117
1118             if( !p_object )
1119                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1120
1121             psz_foo[0] = '|';
1122             DumpStructure( p_object, 0, psz_foo );
1123         }
1124         else if( *psz_cmd == 'v' )
1125         {
1126             int i;
1127
1128             if( !p_object )
1129                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1130
1131             PrintObject( p_object, "" );
1132
1133             if( !vlc_internals( p_object )->i_vars )
1134                 printf( " `-o No variables\n" );
1135             for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1136             {
1137                 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1138
1139                 const char *psz_type = "unknown";
1140                 switch( p_var->i_type & VLC_VAR_TYPE )
1141                 {
1142 #define MYCASE( type, nice )                \
1143                     case VLC_VAR_ ## type:  \
1144                         psz_type = nice;    \
1145                         break;
1146                     MYCASE( VOID, "void" );
1147                     MYCASE( BOOL, "bool" );
1148                     MYCASE( INTEGER, "integer" );
1149                     MYCASE( HOTKEY, "hotkey" );
1150                     MYCASE( STRING, "string" );
1151                     MYCASE( MODULE, "module" );
1152                     MYCASE( FILE, "file" );
1153                     MYCASE( DIRECTORY, "directory" );
1154                     MYCASE( VARIABLE, "variable" );
1155                     MYCASE( FLOAT, "float" );
1156                     MYCASE( TIME, "time" );
1157                     MYCASE( ADDRESS, "address" );
1158                     MYCASE( MUTEX, "mutex" );
1159                     MYCASE( LIST, "list" );
1160 #undef MYCASE
1161                 }
1162                 printf( " %c-o \"%s\" (%s",
1163                         i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1164                         p_var->psz_name, psz_type );
1165                 if( p_var->psz_text )
1166                     printf( ", %s", p_var->psz_text );
1167                 printf( ")" );
1168                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1169                     printf( ", command" );
1170                 if( p_var->i_entries )
1171                     printf( ", %d callbacks", p_var->i_entries );
1172                 switch( p_var->i_type & 0x00f0 )
1173                 {
1174                     case VLC_VAR_VOID:
1175                     case VLC_VAR_MUTEX:
1176                         break;
1177                     case VLC_VAR_BOOL:
1178                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1179                         break;
1180                     case VLC_VAR_INTEGER:
1181                         printf( ": %d", p_var->val.i_int );
1182                         break;
1183                     case VLC_VAR_STRING:
1184                         printf( ": \"%s\"", p_var->val.psz_string );
1185                         break;
1186                     case VLC_VAR_FLOAT:
1187                         printf( ": %f", p_var->val.f_float );
1188                         break;
1189                     case VLC_VAR_TIME:
1190                         printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1191                         break;
1192                     case VLC_VAR_ADDRESS:
1193                         printf( ": %p", p_var->val.p_address );
1194                         break;
1195                     case VLC_VAR_LIST:
1196                         printf( ": TODO" );
1197                         break;
1198                 }
1199                 printf( "\n" );
1200             }
1201         }
1202
1203         vlc_mutex_unlock( &structure_lock );
1204
1205         if( *newval.psz_string )
1206         {
1207             vlc_object_release( p_object );
1208         }
1209     }
1210
1211     return VLC_SUCCESS;
1212 }
1213
1214 /*****************************************************************************
1215  * vlc_list_release: free a list previously allocated by vlc_list_find
1216  *****************************************************************************
1217  * This function decreases the refcount of all objects in the list and
1218  * frees the list.
1219  *****************************************************************************/
1220 void vlc_list_release( vlc_list_t *p_list )
1221 {
1222     int i_index;
1223
1224     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1225     {
1226         vlc_object_release( p_list->p_values[i_index].p_object );
1227     }
1228
1229     free( p_list->p_values );
1230     free( p_list );
1231 }
1232
1233 /*****************************************************************************
1234  * dump an object. (Debug function)
1235  *****************************************************************************/
1236 void __vlc_object_dump( vlc_object_t *p_this )
1237 {
1238     vlc_mutex_lock( &structure_lock );
1239     char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1240     psz_foo[0] = '|';
1241     DumpStructure( p_this, 0, psz_foo );
1242     vlc_mutex_unlock( &structure_lock );
1243 }
1244
1245 /* Following functions are local */
1246
1247 /*****************************************************************************
1248  * FindIndex: find the index of an object in an array of objects
1249  *****************************************************************************
1250  * This function assumes that p_this can be found in pp_objects. It will not
1251  * crash if p_this cannot be found, but will return a wrong value. It is your
1252  * duty to check the return value if you are not certain that the object could
1253  * be found for sure.
1254  *****************************************************************************/
1255 static int FindIndex( vlc_object_t *p_this,
1256                       vlc_object_t **pp_objects, int i_count )
1257 {
1258     int i_middle = i_count / 2;
1259
1260     if( i_count == 0 )
1261     {
1262         return 0;
1263     }
1264
1265     if( pp_objects[i_middle] == p_this )
1266     {
1267         return i_middle;
1268     }
1269
1270     if( i_count == 1 )
1271     {
1272         return 0;
1273     }
1274
1275     /* We take advantage of the sorted array */
1276     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1277     {
1278         return i_middle + FindIndex( p_this, pp_objects + i_middle,
1279                                              i_count - i_middle );
1280     }
1281     else
1282     {
1283         return FindIndex( p_this, pp_objects, i_middle );
1284     }
1285 }
1286
1287 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1288 {
1289     int i;
1290     vlc_object_t *p_tmp;
1291
1292     switch( i_mode & 0x000f )
1293     {
1294     case FIND_PARENT:
1295         p_tmp = p_this->p_parent;
1296         if( p_tmp )
1297         {
1298             if( p_tmp->i_object_type == i_type )
1299             {
1300                 vlc_object_yield( p_tmp );
1301                 return p_tmp;
1302             }
1303             else
1304             {
1305                 return FindObject( p_tmp, i_type, i_mode );
1306             }
1307         }
1308         break;
1309
1310     case FIND_CHILD:
1311         for( i = p_this->i_children; i--; )
1312         {
1313             p_tmp = p_this->pp_children[i];
1314             if( p_tmp->i_object_type == i_type )
1315             {
1316                 vlc_object_yield( p_tmp );
1317                 return p_tmp;
1318             }
1319             else if( p_tmp->i_children )
1320             {
1321                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1322                 if( p_tmp )
1323                 {
1324                     return p_tmp;
1325                 }
1326             }
1327         }
1328         break;
1329
1330     case FIND_ANYWHERE:
1331         /* Handled in vlc_object_find */
1332         break;
1333     }
1334
1335     return NULL;
1336 }
1337
1338 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1339                                       const char *psz_name,
1340                                       int i_mode )
1341 {
1342     int i;
1343     vlc_object_t *p_tmp;
1344
1345     switch( i_mode & 0x000f )
1346     {
1347     case FIND_PARENT:
1348         p_tmp = p_this->p_parent;
1349         if( p_tmp )
1350         {
1351             if( p_tmp->psz_object_name
1352                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1353             {
1354                 vlc_object_yield( p_tmp );
1355                 return p_tmp;
1356             }
1357             else
1358             {
1359                 return FindObjectName( p_tmp, psz_name, i_mode );
1360             }
1361         }
1362         break;
1363
1364     case FIND_CHILD:
1365         for( i = p_this->i_children; i--; )
1366         {
1367             p_tmp = p_this->pp_children[i];
1368             if( p_tmp->psz_object_name
1369                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1370             {
1371                 vlc_object_yield( p_tmp );
1372                 return p_tmp;
1373             }
1374             else if( p_tmp->i_children )
1375             {
1376                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1377                 if( p_tmp )
1378                 {
1379                     return p_tmp;
1380                 }
1381             }
1382         }
1383         break;
1384
1385     case FIND_ANYWHERE:
1386         /* Handled in vlc_object_find */
1387         break;
1388     }
1389
1390     return NULL;
1391 }
1392
1393 static void DetachObject( vlc_object_t *p_this )
1394 {
1395     vlc_object_t *p_parent = p_this->p_parent;
1396     int i_index, i;
1397
1398     /* Remove p_this's parent */
1399     p_this->p_parent = NULL;
1400
1401     /* Remove all of p_parent's children which are p_this */
1402     for( i_index = p_parent->i_children ; i_index-- ; )
1403     {
1404         if( p_parent->pp_children[i_index] == p_this )
1405         {
1406             p_parent->i_children--;
1407             for( i = i_index ; i < p_parent->i_children ; i++ )
1408             {
1409                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1410             }
1411         }
1412     }
1413
1414     if( p_parent->i_children )
1415     {
1416         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1417                                p_parent->i_children * sizeof(vlc_object_t *) );
1418     }
1419     else
1420     {
1421         free( p_parent->pp_children );
1422         p_parent->pp_children = NULL;
1423     }
1424 }
1425
1426 /*****************************************************************************
1427  * SetAttachment: recursively set the b_attached flag of a subtree.
1428  *****************************************************************************
1429  * This function is used by the attach and detach functions to propagate
1430  * the b_attached flag in a subtree.
1431  *****************************************************************************/
1432 static void SetAttachment( vlc_object_t *p_this, bool b_attached )
1433 {
1434     int i_index;
1435
1436     for( i_index = p_this->i_children ; i_index-- ; )
1437     {
1438         SetAttachment( p_this->pp_children[i_index], b_attached );
1439     }
1440
1441     vlc_internals( p_this )->b_attached = b_attached;
1442 }
1443
1444 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1445 {
1446     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1447          psz_parent[20];
1448
1449     memset( &psz_name, 0, sizeof(psz_name) );
1450     if( p_this->psz_object_name )
1451     {
1452         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1453         if( psz_name[48] )
1454             psz_name[48] = '\"';
1455     }
1456
1457     psz_children[0] = '\0';
1458     switch( p_this->i_children )
1459     {
1460         case 0:
1461             break;
1462         case 1:
1463             strcpy( psz_children, ", 1 child" );
1464             break;
1465         default:
1466             snprintf( psz_children, 19, ", %i children", p_this->i_children );
1467             break;
1468     }
1469
1470     psz_refcount[0] = '\0';
1471     if( vlc_internals( p_this )->i_refcount > 0 )
1472         snprintf( psz_refcount, 19, ", refcount %u",
1473                   vlc_internals( p_this )->i_refcount );
1474
1475     psz_thread[0] = '\0';
1476     if( vlc_internals( p_this )->b_thread )
1477         snprintf( psz_thread, 29, " (thread %u)",
1478 #if defined(WIN32) || defined(UNDER_CE)
1479                   (unsigned)vlc_internals( p_this )->thread_id.id );
1480 #else
1481                   (unsigned)vlc_internals( p_this )->thread_id );
1482 #endif
1483
1484     psz_parent[0] = '\0';
1485     if( p_this->p_parent )
1486         snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1487
1488     printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1489             p_this->i_object_id, p_this->psz_object_type,
1490             psz_name, psz_thread, psz_refcount, psz_children,
1491             psz_parent );
1492 }
1493
1494 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1495 {
1496     int i;
1497     char i_back = psz_foo[i_level];
1498     psz_foo[i_level] = '\0';
1499
1500     PrintObject( p_this, psz_foo );
1501
1502     psz_foo[i_level] = i_back;
1503
1504     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1505     {
1506         msg_Warn( p_this, "structure tree is too deep" );
1507         return;
1508     }
1509
1510     for( i = 0 ; i < p_this->i_children ; i++ )
1511     {
1512         if( i_level )
1513         {
1514             psz_foo[i_level-1] = ' ';
1515
1516             if( psz_foo[i_level-2] == '`' )
1517             {
1518                 psz_foo[i_level-2] = ' ';
1519             }
1520         }
1521
1522         if( i == p_this->i_children - 1 )
1523         {
1524             psz_foo[i_level] = '`';
1525         }
1526         else
1527         {
1528             psz_foo[i_level] = '|';
1529         }
1530
1531         psz_foo[i_level+1] = '-';
1532         psz_foo[i_level+2] = '\0';
1533
1534         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1535     }
1536 }
1537
1538 static vlc_list_t * NewList( int i_count )
1539 {
1540     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1541     if( p_list == NULL )
1542     {
1543         return NULL;
1544     }
1545
1546     p_list->i_count = i_count;
1547
1548     if( i_count == 0 )
1549     {
1550         p_list->p_values = NULL;
1551         return p_list;
1552     }
1553
1554     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1555     if( p_list->p_values == NULL )
1556     {
1557         p_list->i_count = 0;
1558         return p_list;
1559     }
1560
1561     return p_list;
1562 }
1563
1564 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1565                          int i_index )
1566 {
1567     if( p_list == NULL || i_index >= p_list->i_count )
1568     {
1569         return;
1570     }
1571
1572     vlc_object_yield( p_object );
1573
1574     p_list->p_values[i_index].p_object = p_object;
1575
1576     return;
1577 }
1578
1579 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1580 {
1581     if( p_list == NULL )
1582     {
1583         return;
1584     }
1585
1586     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1587                                 * sizeof( vlc_value_t ) );
1588     if( p_list->p_values == NULL )
1589     {
1590         p_list->i_count = 0;
1591         return;
1592     }
1593
1594     vlc_object_yield( p_object );
1595
1596     p_list->p_values[p_list->i_count].p_object = p_object;
1597     p_list->i_count++;
1598
1599     return;
1600 }*/
1601
1602 static int CountChildren( vlc_object_t *p_this, int i_type )
1603 {
1604     vlc_object_t *p_tmp;
1605     int i, i_count = 0;
1606
1607     for( i = 0; i < p_this->i_children; i++ )
1608     {
1609         p_tmp = p_this->pp_children[i];
1610
1611         if( p_tmp->i_object_type == i_type )
1612         {
1613             i_count++;
1614         }
1615
1616         if( p_tmp->i_children )
1617         {
1618             i_count += CountChildren( p_tmp, i_type );
1619         }
1620     }
1621
1622     return i_count;
1623 }
1624
1625 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1626 {
1627     vlc_object_t *p_tmp;
1628     int i;
1629
1630     for( i = 0; i < p_this->i_children; i++ )
1631     {
1632         p_tmp = p_this->pp_children[i];
1633
1634         if( p_tmp->i_object_type == i_type )
1635         {
1636             ListReplace( p_list, p_tmp, p_list->i_count++ );
1637         }
1638
1639         if( p_tmp->i_children )
1640         {
1641             ListChildren( p_list, p_tmp, i_type );
1642         }
1643     }
1644 }