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