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