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