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