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