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