]> git.sesse.net Git - vlc/blob - src/misc/objects.c
043fd9e436047e8099b2aff8b282e28ba8208246
[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     /* Destroy the associated variables, starting from the end so that
325      * no memmove calls have to be done. */
326     while( p_priv->i_vars )
327     {
328         var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
329     }
330
331     free( p_priv->p_vars );
332     vlc_mutex_destroy( &p_priv->var_lock );
333
334     free( p_this->psz_header );
335
336     if( p_this->p_libvlc == NULL )
337     {
338         libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
339
340 #ifndef NDEBUG
341         assert( p_global == vlc_global() );
342         /* Test for leaks */
343         if( p_global->i_objects > 0 )
344         {
345             int i;
346             for( i = 0; i < p_global->i_objects; i++ )
347             {
348                 /* We are leaking this object */
349                 fprintf( stderr,
350                          "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
351                          p_global->pp_objects[i]->i_object_id,
352                          p_global->pp_objects[i]->psz_object_type,
353                          p_global->pp_objects[i]->psz_object_name );
354
355                 /* Dump libvlc object to ease debugging */
356                 vlc_object_dump( p_global->pp_objects[i] );
357
358                 fflush(stderr);
359             }
360
361             /* Dump libvlc object to ease debugging */
362             vlc_object_dump( p_this );
363
364             /* Strongly abort, cause we want these to be fixed */
365             abort();
366         }
367 #endif
368
369         /* We are the global object ... no need to lock. */
370         free( p_global->pp_objects );
371         p_global->pp_objects = NULL;
372
373         vlc_mutex_destroy( &structure_lock );
374     }
375
376     FREENULL( p_this->psz_object_name );
377
378 #if defined(WIN32) || defined(UNDER_CE)
379     /* if object has an associated thread, close it now */
380     if( p_priv->thread_id )
381        CloseHandle(p_priv->thread_id);
382 #endif
383
384     vlc_spin_destroy( &p_priv->ref_spin );
385     vlc_mutex_destroy( &p_this->object_lock );
386     vlc_cond_destroy( &p_this->object_wait );
387     vlc_spin_destroy( &p_priv->spin );
388     if( p_priv->pipes[1] != -1 )
389         close( p_priv->pipes[1] );
390     if( p_priv->pipes[0] != -1 )
391         close( p_priv->pipes[0] );
392
393     free( p_priv );
394 }
395
396
397 /** Inter-object signaling */
398
399 void __vlc_object_lock( vlc_object_t *obj )
400 {
401     vlc_mutex_lock( &obj->object_lock );
402 }
403
404 void __vlc_object_unlock( vlc_object_t *obj )
405 {
406     vlc_assert_locked( &obj->object_lock );
407     vlc_mutex_unlock( &obj->object_lock );
408 }
409
410 #ifdef WIN32
411 # include <winsock2.h>
412 # include <ws2tcpip.h>
413
414 /**
415  * select()-able pipes emulated using Winsock
416  */
417 static int pipe (int fd[2])
418 {
419     SOCKADDR_IN addr;
420     int addrlen = sizeof (addr);
421
422     SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
423            c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
424     if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
425         goto error;
426
427     memset (&addr, 0, sizeof (addr));
428     addr.sin_family = AF_INET;
429     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
430     if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
431      || getsockname (l, (PSOCKADDR)&addr, &addrlen)
432      || listen (l, 1)
433      || connect (c, (PSOCKADDR)&addr, addrlen))
434         goto error;
435
436     a = accept (l, NULL, NULL);
437     if (a == INVALID_SOCKET)
438         goto error;
439
440     closesocket (l);
441     //shutdown (a, 0);
442     //shutdown (c, 1);
443     fd[0] = c;
444     fd[1] = a;
445     return 0;
446
447 error:
448     if (l != INVALID_SOCKET)
449         closesocket (l);
450     if (c != INVALID_SOCKET)
451         closesocket (c);
452     return -1;
453 }
454
455 #undef  read
456 #define read( a, b, c )  recv (a, b, c, 0)
457 #undef  write
458 #define write( a, b, c ) send (a, b, c, 0)
459 #undef  close
460 #define close( a )       closesocket (a)
461 #endif /* WIN32 */
462
463 /**
464  * Returns the readable end of a pipe that becomes readable once termination
465  * of the object is requested (vlc_object_kill()).
466  * This can be used to wake-up out of a select() or poll() event loop, such
467  * typically when doing network I/O.
468  *
469  * Note that the pipe will remain the same for the lifetime of the object.
470  * DO NOT read the pipe nor close it yourself. Ever.
471  *
472  * @param obj object that would be "killed"
473  * @return a readable pipe descriptor, or -1 on error.
474  */
475 int __vlc_object_waitpipe( vlc_object_t *obj )
476 {
477     int pfd[2] = { -1, -1 };
478     vlc_object_internals_t *internals = vlc_internals( obj );
479     bool killed = false;
480
481     vlc_spin_lock (&internals->spin);
482     if (internals->pipes[0] == -1)
483     {
484         /* This can only ever happen if someone killed us without locking: */
485         assert (internals->pipes[1] == -1);
486         vlc_spin_unlock (&internals->spin);
487
488         if (pipe (pfd))
489             return -1;
490
491         vlc_spin_lock (&internals->spin);
492         if (internals->pipes[0] == -1)
493         {
494             internals->pipes[0] = pfd[0];
495             internals->pipes[1] = pfd[1];
496             pfd[0] = pfd[1] = -1;
497         }
498         killed = obj->b_die;
499     }
500     vlc_spin_unlock (&internals->spin);
501
502     if (killed)
503     {
504         /* Race condition: vlc_object_kill() already invoked! */
505         int fd;
506
507         vlc_spin_lock (&internals->spin);
508         fd = internals->pipes[1];
509         internals->pipes[1] = -1;
510         vlc_spin_unlock (&internals->spin);
511
512         msg_Dbg (obj, "waitpipe: object already dying");
513         if (fd != -1)
514             close (fd);
515     }
516
517     /* Race condition: two threads call pipe() - unlikely */
518     if (pfd[0] != -1)
519         close (pfd[0]);
520     if (pfd[1] != -1)
521         close (pfd[1]);
522
523     return internals->pipes[0];
524 }
525
526
527 /**
528  * Waits for the object to be signaled (using vlc_object_signal()).
529  * If the object already has a signal pending, this function will return
530  * immediately. It is asserted that the caller holds the object lock.
531  *
532  * @return true if the object is dying and should terminate.
533  */
534 bool __vlc_object_wait( vlc_object_t *obj )
535 {
536     vlc_assert_locked( &obj->object_lock );
537     vlc_cond_wait( &obj->object_wait, &obj->object_lock );
538     return obj->b_die;
539 }
540
541
542 /**
543  * Waits for the object to be signaled (using vlc_object_signal()), or for
544  * a timer to expire.
545  * If the object already has a signal pending, this function will return
546  * immediately. It is asserted that the caller holds the object lock.
547  *
548  * @return negative if the object is dying and should terminate,
549  * positive if the the object has been signaled but is not dying,
550  * 0 if timeout has been reached.
551  */
552 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
553 {
554     int v;
555
556     vlc_assert_locked( &obj->object_lock );
557     v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
558     if( v == 0 ) /* signaled */
559         return obj->b_die ? -1 : 1;
560     return 0;
561 }
562
563
564 /**
565  * Checks whether an object has been "killed".
566  * The object lock must be held.
567  *
568  * Typical code for an object thread could be:
569  *
570    vlc_object_lock (self);
571    ...initialization...
572    while (vlc_object_alive (self))
573    {
574        ...preprocessing...
575
576        if (vlc_object_wait (self))
577            continue;
578
579        ...postprocessing...
580    }
581    ...deinitialization...
582    vlc_object_unlock (self);
583  *
584  *
585  * @return true iff the object has not been killed yet
586  */
587 bool __vlc_object_alive( vlc_object_t *obj )
588 {
589     vlc_assert_locked( &obj->object_lock );
590     return !obj->b_die;
591 }
592
593
594 /**
595  * Signals an object for which the lock is held.
596  */
597 void __vlc_object_signal_unlocked( vlc_object_t *obj )
598 {
599     vlc_assert_locked (&obj->object_lock);
600     vlc_cond_signal( &obj->object_wait );
601 }
602
603
604 /**
605  * Requests termination of an object.
606  * If the object is LibVLC, also request to terminate all its children.
607  */
608 void __vlc_object_kill( vlc_object_t *p_this )
609 {
610     vlc_object_internals_t *internals = vlc_internals( p_this );
611     int fd;
612
613     vlc_mutex_lock( &p_this->object_lock );
614     p_this->b_die = true;
615
616     vlc_spin_lock (&internals->spin);
617     fd = internals->pipes[1];
618     internals->pipes[1] = -1;
619     vlc_spin_unlock (&internals->spin);
620
621     if( fd != -1 )
622     {
623         msg_Dbg (p_this, "waitpipe: object killed");
624         close (fd);
625     }
626
627     if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
628         for( int i = 0; i < internals->i_children ; i++ )
629             vlc_object_kill( internals->pp_children[i] );
630
631     vlc_object_signal_unlocked( p_this );
632     vlc_mutex_unlock( &p_this->object_lock );
633 }
634
635
636 /**
637  * Find an object given its ID.
638  *
639  * This function looks for the object whose i_object_id field is i_id.
640  * This function is slow, and often used to hide bugs. Do not use it.
641  * If you need to retain reference to an object, yield the object pointer with
642  * vlc_object_yield(), use the pointer as your reference, and call
643  * vlc_object_release() when you're done.
644  */
645 void * vlc_object_get( int i_id )
646 {
647     int i_max, i_middle;
648     vlc_object_t **pp_objects;
649     libvlc_global_data_t *p_libvlc_global = vlc_global();
650     vlc_object_t *obj = NULL;
651
652     vlc_mutex_lock( &structure_lock );
653
654     pp_objects = p_libvlc_global->pp_objects;
655
656     /* Perform our dichotomy */
657     for( i_max = p_libvlc_global->i_objects - 1 ; ; )
658     {
659         i_middle = i_max / 2;
660
661         if( pp_objects[i_middle]->i_object_id > i_id )
662         {
663             i_max = i_middle;
664         }
665         else if( pp_objects[i_middle]->i_object_id < i_id )
666         {
667             if( i_middle )
668             {
669                 pp_objects += i_middle;
670                 i_max -= i_middle;
671             }
672             else
673             {
674                 /* This happens when there are only two remaining objects */
675                 if( pp_objects[i_middle+1]->i_object_id == i_id )
676                 {
677                     vlc_object_yield( pp_objects[i_middle+1] );
678                     obj = pp_objects[i_middle+1];
679                 }
680                 break;
681             }
682         }
683         else
684         {
685             vlc_object_yield( pp_objects[i_middle] );
686             obj = pp_objects[i_middle];
687             break;
688         }
689     }
690
691     vlc_mutex_unlock( &structure_lock );
692     return obj;
693 }
694
695 /**
696  ****************************************************************************
697  * find a typed object and increment its refcount
698  *****************************************************************************
699  * This function recursively looks for a given object type. i_mode can be one
700  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
701  *****************************************************************************/
702 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
703 {
704     vlc_object_t *p_found;
705
706     /* If we are of the requested type ourselves, don't look further */
707     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
708     {
709         vlc_object_yield( p_this );
710         return p_this;
711     }
712
713     /* Otherwise, recursively look for the object */
714     if ((i_mode & 0x000f) == FIND_ANYWHERE)
715     {
716 #ifndef NDEBUG
717         if (i_type == VLC_OBJECT_PLAYLIST)
718             msg_Warn (p_this, "using vlc_object_find(VLC_OBJECT_PLAYLIST) "
719                       "instead of pl_Yield()");
720 #endif
721         return vlc_object_find (p_this->p_libvlc, i_type,
722                                 (i_mode & ~0x000f)|FIND_CHILD);
723     }
724
725     vlc_mutex_lock( &structure_lock );
726     p_found = FindObject( p_this, i_type, i_mode );
727     vlc_mutex_unlock( &structure_lock );
728     return p_found;
729 }
730
731 /**
732  ****************************************************************************
733  * find a named object and increment its refcount
734  *****************************************************************************
735  * This function recursively looks for a given object name. i_mode can be one
736  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
737  *****************************************************************************/
738 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
739                                int i_mode )
740 {
741     vlc_object_t *p_found;
742
743     /* If have the requested name ourselves, don't look further */
744     if( !(i_mode & FIND_STRICT)
745         && p_this->psz_object_name
746         && !strcmp( p_this->psz_object_name, psz_name ) )
747     {
748         vlc_object_yield( p_this );
749         return p_this;
750     }
751
752     vlc_mutex_lock( &structure_lock );
753
754     /* Otherwise, recursively look for the object */
755     if( (i_mode & 0x000f) == FIND_ANYWHERE )
756     {
757         vlc_object_t *p_root = p_this;
758
759         /* Find the root */
760         while( p_root->p_parent != NULL &&
761                p_root != VLC_OBJECT( p_this->p_libvlc ) )
762         {
763             p_root = p_root->p_parent;
764         }
765
766         p_found = FindObjectName( p_root, psz_name,
767                                  (i_mode & ~0x000f)|FIND_CHILD );
768         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
769         {
770             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
771                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
772         }
773     }
774     else
775     {
776         p_found = FindObjectName( p_this, psz_name, i_mode );
777     }
778
779     vlc_mutex_unlock( &structure_lock );
780
781     return p_found;
782 }
783
784 /**
785  * Increment an object reference counter.
786  */
787 void __vlc_object_yield( vlc_object_t *p_this )
788 {
789     vlc_object_internals_t *internals = vlc_internals( p_this );
790
791     vlc_spin_lock( &internals->ref_spin );
792     /* Avoid obvious freed object uses */
793     assert( internals->i_refcount > 0 );
794     /* Increment the counter */
795     internals->i_refcount++;
796     vlc_spin_unlock( &internals->ref_spin );
797 }
798
799 /*****************************************************************************
800  * decrement an object refcount
801  * And destroy the object if its refcount reach zero.
802  *****************************************************************************/
803 void __vlc_object_release( vlc_object_t *p_this )
804 {
805     vlc_object_internals_t *internals = vlc_internals( p_this );
806     bool b_should_destroy;
807
808     vlc_spin_lock( &internals->ref_spin );
809     assert( internals->i_refcount > 0 );
810
811     if( internals->i_refcount > 1 )
812     {
813         /* Fast path */
814         /* There are still other references to the object */
815         internals->i_refcount--;
816         vlc_spin_unlock( &internals->ref_spin );
817         return;
818     }
819     vlc_spin_unlock( &internals->ref_spin );
820
821     /* Slow path */
822     /* Remember that we cannot hold the spin while waiting on the mutex */
823     vlc_mutex_lock( &structure_lock );
824     /* Take the spin again. Note that another thread may have yielded the
825      * object in the (very short) mean time. */
826     vlc_spin_lock( &internals->ref_spin );
827     b_should_destroy = --internals->i_refcount == 0;
828     vlc_spin_unlock( &internals->ref_spin );
829
830     if( b_should_destroy )
831     {
832         /* Remove the object from the table
833          * so that it cannot be encountered by vlc_object_get() */
834         libvlc_global_data_t *p_libvlc_global = vlc_global();
835         int i_index;
836
837         i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
838                              p_libvlc_global->i_objects );
839         REMOVE_ELEM( p_libvlc_global->pp_objects,
840                      p_libvlc_global->i_objects, i_index );
841
842         /* Detach from parent to protect against FIND_CHILDREN */
843         vlc_object_detach_unlocked (p_this);
844         /* Detach from children to protect against FIND_PARENT */
845         for (int i = 0; i < internals->i_children; i++)
846             internals->pp_children[i]->p_parent = NULL;
847     }
848
849     vlc_mutex_unlock( &structure_lock );
850
851     if( b_should_destroy )
852     {
853         free( internals->pp_children );
854         internals->pp_children = NULL;
855         internals->i_children = 0;
856         vlc_object_destroy( p_this );
857     }
858 }
859
860 /**
861  ****************************************************************************
862  * attach object to a parent object
863  *****************************************************************************
864  * This function sets p_this as a child of p_parent, and p_parent as a parent
865  * of p_this. This link can be undone using vlc_object_detach.
866  *****************************************************************************/
867 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
868 {
869     if( !p_this ) return;
870
871     vlc_mutex_lock( &structure_lock );
872
873     /* Attach the parent to its child */
874     assert (!p_this->p_parent);
875     p_this->p_parent = p_parent;
876
877     /* Attach the child to its parent */
878     vlc_object_internals_t *priv = vlc_internals( p_parent );
879     INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
880                  p_this );
881
882     vlc_mutex_unlock( &structure_lock );
883 }
884
885
886 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
887 {
888     vlc_assert_locked (&structure_lock);
889
890     if (p_this->p_parent == NULL)
891         return;
892
893     vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
894
895     int i_index, i;
896
897     /* Remove p_this's parent */
898     p_this->p_parent = NULL;
899
900     /* Remove all of p_parent's children which are p_this */
901     for( i_index = priv->i_children ; i_index-- ; )
902     {
903         if( priv->pp_children[i_index] == p_this )
904         {
905             priv->i_children--;
906             for( i = i_index ; i < priv->i_children ; i++ )
907                 priv->pp_children[i] = priv->pp_children[i+1];
908         }
909     }
910
911     if( priv->i_children )
912     {
913         priv->pp_children = (vlc_object_t **)realloc( priv->pp_children,
914                                priv->i_children * sizeof(vlc_object_t *) );
915     }
916     else
917     {
918         /* Special case - don't realloc() to zero to avoid leaking */
919         free( priv->pp_children );
920         priv->pp_children = NULL;
921     }
922 }
923
924
925 /**
926  ****************************************************************************
927  * detach object from its parent
928  *****************************************************************************
929  * This function removes all links between an object and its parent.
930  *****************************************************************************/
931 void __vlc_object_detach( vlc_object_t *p_this )
932 {
933     if( !p_this ) return;
934
935     vlc_mutex_lock( &structure_lock );
936     if( !p_this->p_parent )
937         msg_Err( p_this, "object is not attached" );
938     else
939         vlc_object_detach_unlocked( p_this );
940     vlc_mutex_unlock( &structure_lock );
941 }
942
943
944 /**
945  ****************************************************************************
946  * find a list typed objects and increment their refcount
947  *****************************************************************************
948  * This function recursively looks for a given object type. i_mode can be one
949  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
950  *****************************************************************************/
951 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
952 {
953     vlc_list_t *p_list;
954     int i_count = 0;
955
956     /* Look for the objects */
957     switch( i_mode & 0x000f )
958     {
959     case FIND_ANYWHERE:
960         /* Modules should probably not be object, and the module should perhaps
961          * not be shared across LibVLC instances. In the mean time, this ugly
962          * hack is brought to you by Courmisch. */
963         if (i_type == VLC_OBJECT_MODULE)
964             return vlc_list_find ((vlc_object_t *)vlc_global ()->p_module_bank,
965                                   i_type, FIND_CHILD);
966         return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
967
968     case FIND_CHILD:
969         vlc_mutex_lock( &structure_lock );
970         i_count = CountChildren( p_this, i_type );
971         p_list = NewList( i_count );
972
973         /* Check allocation was successful */
974         if( p_list->i_count != i_count )
975         {
976             msg_Err( p_this, "list allocation failed!" );
977             p_list->i_count = 0;
978             break;
979         }
980
981         p_list->i_count = 0;
982         ListChildren( p_list, p_this, i_type );
983         vlc_mutex_unlock( &structure_lock );
984         break;
985
986     default:
987         msg_Err( p_this, "unimplemented!" );
988         p_list = NewList( 0 );
989         break;
990     }
991
992     return p_list;
993 }
994
995 /**
996  * Gets the list of children of an objects, and increment their reference
997  * count.
998  * @return a list (possibly empty) or NULL in case of error.
999  */
1000 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
1001 {
1002     vlc_list_t *l;
1003     vlc_object_internals_t *priv = vlc_internals( obj );
1004
1005     vlc_mutex_lock( &structure_lock );
1006     l = NewList( priv->i_children );
1007     for (int i = 0; i < l->i_count; i++)
1008     {
1009         vlc_object_yield( priv->pp_children[i] );
1010         l->p_values[i].p_object = priv->pp_children[i];
1011     }
1012     vlc_mutex_unlock( &structure_lock );
1013     return l;
1014 }
1015
1016 /*****************************************************************************
1017  * DumpCommand: print the current vlc structure
1018  *****************************************************************************
1019  * This function prints either an ASCII tree showing the connections between
1020  * vlc objects, and additional information such as their refcount, thread ID,
1021  * etc. (command "tree"), or the same data as a simple list (command "list").
1022  *****************************************************************************/
1023 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1024                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1025 {
1026     libvlc_global_data_t *p_libvlc_global = vlc_global();
1027
1028     (void)oldval; (void)p_data;
1029     if( *psz_cmd == 'l' )
1030     {
1031         vlc_mutex_lock( &structure_lock );
1032
1033         vlc_object_t **pp_current, **pp_end;
1034
1035         pp_current = p_libvlc_global->pp_objects;
1036         pp_end = pp_current + p_libvlc_global->i_objects;
1037
1038         for( ; pp_current < pp_end ; pp_current++ )
1039             PrintObject( *pp_current, "" );
1040
1041         vlc_mutex_unlock( &structure_lock );
1042     }
1043     else
1044     {
1045         vlc_object_t *p_object = NULL;
1046
1047         if( *newval.psz_string )
1048         {
1049             char *end;
1050             int i_id = strtol( newval.psz_string, &end, 0 );
1051             if( end != newval.psz_string )
1052                 p_object = vlc_object_get( i_id );
1053             else
1054             {
1055                 /* try using the object's name to find it */
1056                 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1057                 if( p_libvlc )
1058                 {
1059                     /* Look in p_libvlc's children tree */
1060                     p_object = vlc_object_find_name( p_libvlc,
1061                                                      newval.psz_string,
1062                                                      FIND_CHILD );
1063                     vlc_object_release( p_libvlc );
1064                 }
1065                 if( !p_object )
1066                 {
1067                     /* If it's not in libvlc, look in libvlc_global (== p_this) */
1068                     p_object = vlc_object_find_name( p_this,
1069                                                      newval.psz_string,
1070                                                      FIND_CHILD );
1071                 }
1072             }
1073
1074             if( !p_object )
1075             {
1076                 return VLC_ENOOBJ;
1077             }
1078         }
1079
1080         vlc_mutex_lock( &structure_lock );
1081
1082         if( *psz_cmd == 't' )
1083         {
1084             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1085
1086             if( !p_object )
1087                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1088
1089             psz_foo[0] = '|';
1090             DumpStructure( p_object, 0, psz_foo );
1091         }
1092         else if( *psz_cmd == 'v' )
1093         {
1094             int i;
1095
1096             if( !p_object )
1097                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1098
1099             PrintObject( p_object, "" );
1100
1101             if( !vlc_internals( p_object )->i_vars )
1102                 printf( " `-o No variables\n" );
1103             for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1104             {
1105                 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1106
1107                 const char *psz_type = "unknown";
1108                 switch( p_var->i_type & VLC_VAR_TYPE )
1109                 {
1110 #define MYCASE( type, nice )                \
1111                     case VLC_VAR_ ## type:  \
1112                         psz_type = nice;    \
1113                         break;
1114                     MYCASE( VOID, "void" );
1115                     MYCASE( BOOL, "bool" );
1116                     MYCASE( INTEGER, "integer" );
1117                     MYCASE( HOTKEY, "hotkey" );
1118                     MYCASE( STRING, "string" );
1119                     MYCASE( MODULE, "module" );
1120                     MYCASE( FILE, "file" );
1121                     MYCASE( DIRECTORY, "directory" );
1122                     MYCASE( VARIABLE, "variable" );
1123                     MYCASE( FLOAT, "float" );
1124                     MYCASE( TIME, "time" );
1125                     MYCASE( ADDRESS, "address" );
1126                     MYCASE( MUTEX, "mutex" );
1127                     MYCASE( LIST, "list" );
1128 #undef MYCASE
1129                 }
1130                 printf( " %c-o \"%s\" (%s",
1131                         i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1132                         p_var->psz_name, psz_type );
1133                 if( p_var->psz_text )
1134                     printf( ", %s", p_var->psz_text );
1135                 printf( ")" );
1136                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1137                     printf( ", command" );
1138                 if( p_var->i_entries )
1139                     printf( ", %d callbacks", p_var->i_entries );
1140                 switch( p_var->i_type & 0x00f0 )
1141                 {
1142                     case VLC_VAR_VOID:
1143                     case VLC_VAR_MUTEX:
1144                         break;
1145                     case VLC_VAR_BOOL:
1146                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1147                         break;
1148                     case VLC_VAR_INTEGER:
1149                         printf( ": %d", p_var->val.i_int );
1150                         break;
1151                     case VLC_VAR_STRING:
1152                         printf( ": \"%s\"", p_var->val.psz_string );
1153                         break;
1154                     case VLC_VAR_FLOAT:
1155                         printf( ": %f", p_var->val.f_float );
1156                         break;
1157                     case VLC_VAR_TIME:
1158                         printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1159                         break;
1160                     case VLC_VAR_ADDRESS:
1161                         printf( ": %p", p_var->val.p_address );
1162                         break;
1163                     case VLC_VAR_LIST:
1164                         printf( ": TODO" );
1165                         break;
1166                 }
1167                 printf( "\n" );
1168             }
1169         }
1170
1171         vlc_mutex_unlock( &structure_lock );
1172
1173         if( *newval.psz_string )
1174         {
1175             vlc_object_release( p_object );
1176         }
1177     }
1178
1179     return VLC_SUCCESS;
1180 }
1181
1182 /*****************************************************************************
1183  * vlc_list_release: free a list previously allocated by vlc_list_find
1184  *****************************************************************************
1185  * This function decreases the refcount of all objects in the list and
1186  * frees the list.
1187  *****************************************************************************/
1188 void vlc_list_release( vlc_list_t *p_list )
1189 {
1190     int i_index;
1191
1192     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1193     {
1194         vlc_object_release( p_list->p_values[i_index].p_object );
1195     }
1196
1197     free( p_list->p_values );
1198     free( p_list );
1199 }
1200
1201 /*****************************************************************************
1202  * dump an object. (Debug function)
1203  *****************************************************************************/
1204 void __vlc_object_dump( vlc_object_t *p_this )
1205 {
1206     vlc_mutex_lock( &structure_lock );
1207     char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1208     psz_foo[0] = '|';
1209     DumpStructure( p_this, 0, psz_foo );
1210     vlc_mutex_unlock( &structure_lock );
1211 }
1212
1213 /* Following functions are local */
1214
1215 /*****************************************************************************
1216  * FindIndex: find the index of an object in an array of objects
1217  *****************************************************************************
1218  * This function assumes that p_this can be found in pp_objects. It will not
1219  * crash if p_this cannot be found, but will return a wrong value. It is your
1220  * duty to check the return value if you are not certain that the object could
1221  * be found for sure.
1222  *****************************************************************************/
1223 static int FindIndex( vlc_object_t *p_this,
1224                       vlc_object_t **pp_objects, int i_count )
1225 {
1226     int i_middle = i_count / 2;
1227
1228     if( i_count == 0 )
1229     {
1230         return 0;
1231     }
1232
1233     if( pp_objects[i_middle] == p_this )
1234     {
1235         return i_middle;
1236     }
1237
1238     if( i_count == 1 )
1239     {
1240         return 0;
1241     }
1242
1243     /* We take advantage of the sorted array */
1244     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1245     {
1246         return i_middle + FindIndex( p_this, pp_objects + i_middle,
1247                                              i_count - i_middle );
1248     }
1249     else
1250     {
1251         return FindIndex( p_this, pp_objects, i_middle );
1252     }
1253 }
1254
1255 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1256 {
1257     int i;
1258     vlc_object_t *p_tmp;
1259
1260     switch( i_mode & 0x000f )
1261     {
1262     case FIND_PARENT:
1263         p_tmp = p_this->p_parent;
1264         if( p_tmp )
1265         {
1266             if( p_tmp->i_object_type == i_type )
1267             {
1268                 vlc_object_yield( p_tmp );
1269                 return p_tmp;
1270             }
1271             else
1272             {
1273                 return FindObject( p_tmp, i_type, i_mode );
1274             }
1275         }
1276         break;
1277
1278     case FIND_CHILD:
1279         for( i = vlc_internals( p_this )->i_children; i--; )
1280         {
1281             p_tmp = vlc_internals( p_this )->pp_children[i];
1282             if( p_tmp->i_object_type == i_type )
1283             {
1284                 vlc_object_yield( p_tmp );
1285                 return p_tmp;
1286             }
1287             else if( vlc_internals( p_tmp )->i_children )
1288             {
1289                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1290                 if( p_tmp )
1291                 {
1292                     return p_tmp;
1293                 }
1294             }
1295         }
1296         break;
1297
1298     case FIND_ANYWHERE:
1299         /* Handled in vlc_object_find */
1300         break;
1301     }
1302
1303     return NULL;
1304 }
1305
1306 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1307                                       const char *psz_name,
1308                                       int i_mode )
1309 {
1310     int i;
1311     vlc_object_t *p_tmp;
1312
1313     switch( i_mode & 0x000f )
1314     {
1315     case FIND_PARENT:
1316         p_tmp = p_this->p_parent;
1317         if( p_tmp )
1318         {
1319             if( p_tmp->psz_object_name
1320                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1321             {
1322                 vlc_object_yield( p_tmp );
1323                 return p_tmp;
1324             }
1325             else
1326             {
1327                 return FindObjectName( p_tmp, psz_name, i_mode );
1328             }
1329         }
1330         break;
1331
1332     case FIND_CHILD:
1333         for( i = vlc_internals( p_this )->i_children; i--; )
1334         {
1335             p_tmp = vlc_internals( p_this )->pp_children[i];
1336             if( p_tmp->psz_object_name
1337                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1338             {
1339                 vlc_object_yield( p_tmp );
1340                 return p_tmp;
1341             }
1342             else if( vlc_internals( p_tmp )->i_children )
1343             {
1344                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1345                 if( p_tmp )
1346                 {
1347                     return p_tmp;
1348                 }
1349             }
1350         }
1351         break;
1352
1353     case FIND_ANYWHERE:
1354         /* Handled in vlc_object_find */
1355         break;
1356     }
1357
1358     return NULL;
1359 }
1360
1361
1362 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1363 {
1364     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1365          psz_parent[20];
1366
1367     memset( &psz_name, 0, sizeof(psz_name) );
1368     if( p_this->psz_object_name )
1369     {
1370         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1371         if( psz_name[48] )
1372             psz_name[48] = '\"';
1373     }
1374
1375     psz_children[0] = '\0';
1376     switch( vlc_internals( p_this )->i_children )
1377     {
1378         case 0:
1379             break;
1380         case 1:
1381             strcpy( psz_children, ", 1 child" );
1382             break;
1383         default:
1384             snprintf( psz_children, 19, ", %i children",
1385                       vlc_internals( p_this )->i_children );
1386             break;
1387     }
1388
1389     psz_refcount[0] = '\0';
1390     if( vlc_internals( p_this )->i_refcount > 0 )
1391         snprintf( psz_refcount, 19, ", refcount %u",
1392                   vlc_internals( p_this )->i_refcount );
1393
1394     psz_thread[0] = '\0';
1395     if( vlc_internals( p_this )->b_thread )
1396         snprintf( psz_thread, 29, " (thread %lu)",
1397                   (unsigned long)vlc_internals( p_this )->thread_id );
1398
1399     psz_parent[0] = '\0';
1400     if( p_this->p_parent )
1401         snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1402
1403     printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1404             p_this->i_object_id, p_this->psz_object_type,
1405             psz_name, psz_thread, psz_refcount, psz_children,
1406             psz_parent );
1407 }
1408
1409 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1410 {
1411     int i;
1412     char i_back = psz_foo[i_level];
1413     psz_foo[i_level] = '\0';
1414
1415     PrintObject( p_this, psz_foo );
1416
1417     psz_foo[i_level] = i_back;
1418
1419     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1420     {
1421         msg_Warn( p_this, "structure tree is too deep" );
1422         return;
1423     }
1424
1425     for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1426     {
1427         if( i_level )
1428         {
1429             psz_foo[i_level-1] = ' ';
1430
1431             if( psz_foo[i_level-2] == '`' )
1432             {
1433                 psz_foo[i_level-2] = ' ';
1434             }
1435         }
1436
1437         if( i == vlc_internals( p_this )->i_children - 1 )
1438         {
1439             psz_foo[i_level] = '`';
1440         }
1441         else
1442         {
1443             psz_foo[i_level] = '|';
1444         }
1445
1446         psz_foo[i_level+1] = '-';
1447         psz_foo[i_level+2] = '\0';
1448
1449         DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1450                        psz_foo );
1451     }
1452 }
1453
1454 static vlc_list_t * NewList( int i_count )
1455 {
1456     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1457     if( p_list == NULL )
1458     {
1459         return NULL;
1460     }
1461
1462     p_list->i_count = i_count;
1463
1464     if( i_count == 0 )
1465     {
1466         p_list->p_values = NULL;
1467         return p_list;
1468     }
1469
1470     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1471     if( p_list->p_values == NULL )
1472     {
1473         p_list->i_count = 0;
1474         return p_list;
1475     }
1476
1477     return p_list;
1478 }
1479
1480 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1481                          int i_index )
1482 {
1483     if( p_list == NULL || i_index >= p_list->i_count )
1484     {
1485         return;
1486     }
1487
1488     vlc_object_yield( p_object );
1489
1490     p_list->p_values[i_index].p_object = p_object;
1491
1492     return;
1493 }
1494
1495 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1496 {
1497     if( p_list == NULL )
1498     {
1499         return;
1500     }
1501
1502     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1503                                 * sizeof( vlc_value_t ) );
1504     if( p_list->p_values == NULL )
1505     {
1506         p_list->i_count = 0;
1507         return;
1508     }
1509
1510     vlc_object_yield( p_object );
1511
1512     p_list->p_values[p_list->i_count].p_object = p_object;
1513     p_list->i_count++;
1514
1515     return;
1516 }*/
1517
1518 static int CountChildren( vlc_object_t *p_this, int i_type )
1519 {
1520     vlc_object_t *p_tmp;
1521     int i, i_count = 0;
1522
1523     for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1524     {
1525         p_tmp = vlc_internals( p_this )->pp_children[i];
1526
1527         if( p_tmp->i_object_type == i_type )
1528         {
1529             i_count++;
1530         }
1531         i_count += CountChildren( p_tmp, i_type );
1532     }
1533
1534     return i_count;
1535 }
1536
1537 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1538 {
1539     vlc_object_t *p_tmp;
1540     int i;
1541
1542     for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1543     {
1544         p_tmp = vlc_internals( p_this )->pp_children[i];
1545
1546         if( p_tmp->i_object_type == i_type )
1547             ListReplace( p_list, p_tmp, p_list->i_count++ );
1548
1549         ListChildren( p_list, p_tmp, i_type );
1550     }
1551 }