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