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