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