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