]> git.sesse.net Git - vlc/blob - src/misc/objects.c
95aa7b14b4d9751400d397b01fc440f929e8262e
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2004 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 #include <vlc/vlc.h>
34 #include <vlc/input.h>
35
36 #ifdef HAVE_STDLIB_H
37 #   include <stdlib.h>                                          /* realloc() */
38 #endif
39
40 #include "vlc_video.h"
41 #include "video_output.h"
42 #include "vlc_spu.h"
43
44 #include "audio_output.h"
45 #include "aout_internal.h"
46 #include "stream_output.h"
47
48 #include "vlc_playlist.h"
49 #include "vlc_interface.h"
50 #include "vlc_codec.h"
51 #include "vlc_filter.h"
52
53 #include "vlc_httpd.h"
54 #include "vlc_vlm.h"
55 #include "vlc_vod.h"
56 #include "vlc_tls.h"
57 #include "vlc_xml.h"
58 #include "vlc_osd.h"
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63 static int  DumpCommand( vlc_object_t *, char const *,
64                          vlc_value_t, vlc_value_t, void * );
65
66 static vlc_object_t * FindObject    ( vlc_object_t *, int, int );
67 static void           DetachObject  ( vlc_object_t * );
68 static void           PrintObject   ( vlc_object_t *, const char * );
69 static void           DumpStructure ( vlc_object_t *, int, char * );
70 static int            FindIndex     ( vlc_object_t *, vlc_object_t **, int );
71 static void           SetAttachment ( vlc_object_t *, vlc_bool_t );
72
73 static vlc_list_t   * NewList       ( int );
74 static void           ListReplace   ( vlc_list_t *, vlc_object_t *, int );
75 /*static void           ListAppend    ( vlc_list_t *, vlc_object_t * );*/
76 static int            CountChildren ( vlc_object_t *, int );
77 static void           ListChildren  ( vlc_list_t *, vlc_object_t *, int );
78
79 /*****************************************************************************
80  * Local structure lock
81  *****************************************************************************/
82 static vlc_mutex_t    structure_lock;
83
84 /*****************************************************************************
85  * vlc_object_create: initialize a vlc object
86  *****************************************************************************
87  * This function allocates memory for a vlc object and initializes it. If
88  * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
89  * so on, vlc_object_create will use its value for the object size.
90  *****************************************************************************/
91
92 /**
93  * Initialize a vlc object
94  *
95  * This function allocates memory for a vlc object and initializes it. If
96  * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
97  * so on, vlc_object_create will use its value for the object size.
98  */
99 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
100 {
101     vlc_object_t * p_new;
102     const char   * psz_type;
103     size_t         i_size;
104
105     switch( i_type )
106     {
107         case VLC_OBJECT_ROOT:
108             i_size = sizeof(libvlc_t);
109             psz_type = "root";
110             break;
111         case VLC_OBJECT_VLC:
112             i_size = sizeof(vlc_t);
113             psz_type = "vlc";
114             break;
115         case VLC_OBJECT_MODULE:
116             i_size = sizeof(module_t);
117             psz_type = "module";
118             break;
119         case VLC_OBJECT_INTF:
120             i_size = sizeof(intf_thread_t);
121             psz_type = "interface";
122             break;
123         case VLC_OBJECT_DIALOGS:
124             i_size = sizeof(intf_thread_t);
125             psz_type = "dialogs provider";
126             break;
127         case VLC_OBJECT_PLAYLIST:
128             i_size = sizeof(playlist_t);
129             psz_type = "playlist";
130             break;
131         case VLC_OBJECT_SD:
132             i_size = sizeof(services_discovery_t);
133             psz_type = "services discovery";
134             break;
135         case VLC_OBJECT_INPUT:
136             i_size = sizeof(input_thread_t);
137             psz_type = "input";
138             break;
139         case VLC_OBJECT_DEMUX:
140             i_size = sizeof(demux_t);
141             psz_type = "demux";
142             break;
143         case VLC_OBJECT_STREAM:
144             i_size = sizeof(stream_t);
145             psz_type = "stream";
146             break;
147         case VLC_OBJECT_ACCESS:
148             i_size = sizeof(access_t);
149             psz_type = "access";
150             break;
151         case VLC_OBJECT_DECODER:
152             i_size = sizeof(decoder_t);
153             psz_type = "decoder";
154             break;
155         case VLC_OBJECT_PACKETIZER:
156             i_size = sizeof(decoder_t);
157             psz_type = "packetizer";
158             break;
159         case VLC_OBJECT_ENCODER:
160             i_size = sizeof(encoder_t);
161             psz_type = "encoder";
162             break;
163         case VLC_OBJECT_FILTER:
164             i_size = sizeof(filter_t);
165             psz_type = "filter";
166             break;
167         case VLC_OBJECT_VOUT:
168             i_size = sizeof(vout_thread_t);
169             psz_type = "video output";
170             break;
171         case VLC_OBJECT_SPU:
172             i_size = sizeof(spu_t);
173             psz_type = "subpicture unit";
174             break;
175         case VLC_OBJECT_AOUT:
176             i_size = sizeof(aout_instance_t);
177             psz_type = "audio output";
178             break;
179         case VLC_OBJECT_SOUT:
180             i_size = sizeof(sout_instance_t);
181             psz_type = "stream output";
182             break;
183         case VLC_OBJECT_HTTPD:
184             i_size = sizeof( httpd_t );
185             psz_type = "http daemon";
186             break;
187         case VLC_OBJECT_VLM:
188             i_size = sizeof( vlm_t );
189             psz_type = "vlm dameon";
190             break;
191         case VLC_OBJECT_VOD:
192             i_size = sizeof( vod_t );
193             psz_type = "vod server";
194             break;
195         case VLC_OBJECT_TLS:
196             i_size = sizeof( tls_t );
197             psz_type = "tls";
198             break;
199         case VLC_OBJECT_XML:
200             i_size = sizeof( xml_t );
201             psz_type = "xml";
202             break;
203         case VLC_OBJECT_OPENGL:
204             i_size = sizeof( vout_thread_t );
205             psz_type = "opengl provider";
206             break;
207         case VLC_OBJECT_ANNOUNCE:
208             i_size = sizeof( announce_handler_t );
209             psz_type = "announce handler";
210             break;
211         case VLC_OBJECT_OSDMENU:
212             i_size = sizeof( osd_menu_t );
213             psz_type = "osd menu";
214             break;
215         case VLC_OBJECT_STATS:
216             i_size = sizeof( stats_handler_t );
217             psz_type = "statistics";
218             break;
219         default:
220             i_size = i_type > (int)sizeof(vlc_object_t)
221                          ? i_type : (int)sizeof(vlc_object_t);
222             i_type = VLC_OBJECT_GENERIC;
223             psz_type = "generic";
224             break;
225     }
226
227     if( i_type == VLC_OBJECT_ROOT )
228     {
229         p_new = p_this;
230     }
231     else
232     {
233         p_new = malloc( i_size );
234         if( !p_new ) return NULL;
235         memset( p_new, 0, i_size );
236     }
237
238     p_new->i_object_type = i_type;
239     p_new->psz_object_type = psz_type;
240
241     p_new->psz_object_name = NULL;
242
243     p_new->b_die = VLC_FALSE;
244     p_new->b_error = VLC_FALSE;
245     p_new->b_dead = VLC_FALSE;
246     p_new->b_attached = VLC_FALSE;
247     p_new->b_force = VLC_FALSE;
248
249     p_new->psz_header = NULL;
250
251     p_new->i_flags = 0;
252     if( p_this->i_flags & OBJECT_FLAGS_NODBG )
253         p_new->i_flags |= OBJECT_FLAGS_NODBG;
254     if( p_this->i_flags & OBJECT_FLAGS_QUIET )
255         p_new->i_flags |= OBJECT_FLAGS_QUIET;
256     if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
257         p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
258
259     p_new->i_vars = 0;
260     p_new->p_vars = (variable_t *)malloc( 16 * sizeof( variable_t ) );
261
262     if( !p_new->p_vars )
263     {
264         if( i_type != VLC_OBJECT_ROOT )
265             free( p_new );
266         return NULL;
267     }
268
269     if( i_type == VLC_OBJECT_ROOT )
270     {
271         /* If i_type is root, then p_new is actually p_libvlc */
272         p_new->p_libvlc = (libvlc_t*)p_new;
273         p_new->p_vlc = NULL;
274
275         p_new->p_libvlc->i_counter = 0;
276         p_new->i_object_id = 0;
277
278         p_new->p_libvlc->i_objects = 1;
279         p_new->p_libvlc->pp_objects = malloc( sizeof(vlc_object_t *) );
280         p_new->p_libvlc->pp_objects[0] = p_new;
281         p_new->b_attached = VLC_TRUE;
282     }
283     else
284     {
285         p_new->p_libvlc = p_this->p_libvlc;
286         p_new->p_vlc = ( i_type == VLC_OBJECT_VLC ) ? (vlc_t*)p_new
287                                                     : p_this->p_vlc;
288
289         vlc_mutex_lock( &structure_lock );
290
291         p_new->p_libvlc->i_counter++;
292         p_new->i_object_id = p_new->p_libvlc->i_counter;
293
294         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
295          * useless to try and recover anything if pp_objects gets smashed. */
296         INSERT_ELEM( p_new->p_libvlc->pp_objects,
297                      p_new->p_libvlc->i_objects,
298                      p_new->p_libvlc->i_objects,
299                      p_new );
300
301         vlc_mutex_unlock( &structure_lock );
302     }
303
304     p_new->i_refcount = 0;
305     p_new->p_parent = NULL;
306     p_new->pp_children = NULL;
307     p_new->i_children = 0;
308
309     p_new->p_private = NULL;
310
311     /* Initialize mutexes and condvars */
312     vlc_mutex_init( p_new, &p_new->object_lock );
313     vlc_cond_init( p_new, &p_new->object_wait );
314     vlc_mutex_init( p_new, &p_new->var_lock );
315
316     if( i_type == VLC_OBJECT_ROOT )
317     {
318         vlc_mutex_init( p_new, &structure_lock );
319
320         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
321         var_AddCallback( p_new, "list", DumpCommand, NULL );
322         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
323         var_AddCallback( p_new, "tree", DumpCommand, NULL );
324     }
325
326     return p_new;
327 }
328
329 /**
330  ****************************************************************************
331  * Destroy a vlc object
332  *
333  * This function destroys an object that has been previously allocated with
334  * vlc_object_create. The object's refcount must be zero and it must not be
335  * attached to other objects in any way.
336  *****************************************************************************/
337 void __vlc_object_destroy( vlc_object_t *p_this )
338 {
339     int i_delay = 0;
340
341     if( p_this->i_children )
342     {
343         msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
344                  p_this->i_object_id, p_this->psz_object_name );
345         return;
346     }
347
348     if( p_this->p_parent )
349     {
350         msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
351                  p_this->i_object_id, p_this->psz_object_name );
352         return;
353     }
354
355     while( p_this->i_refcount )
356     {
357         i_delay++;
358
359         /* Don't warn immediately ... 100ms seems OK */
360         if( i_delay == 2 )
361         {
362             msg_Warn( p_this,
363                   "refcount is %i, delaying before deletion (id=%d,type=%d)",
364                   p_this->i_refcount, p_this->i_object_id,
365                   p_this->i_object_type );
366         }
367         else if( i_delay == 10 )
368         {
369             msg_Err( p_this,
370                   "refcount is %i, delaying again (id=%d,type=%d)",
371                   p_this->i_refcount, p_this->i_object_id,
372                   p_this->i_object_type );
373         }
374         else if( i_delay == 20 )
375         {
376             msg_Err( p_this,
377                   "we waited too long, cancelling destruction (id=%d,type=%d)",
378                   p_this->i_object_id, p_this->i_object_type );
379             return;
380         }
381
382         msleep( 100000 );
383     }
384
385     /* Destroy the associated variables, starting from the end so that
386      * no memmove calls have to be done. */
387     while( p_this->i_vars )
388     {
389         var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
390     }
391
392     free( p_this->p_vars );
393     vlc_mutex_destroy( &p_this->var_lock );
394
395     if( p_this->psz_header ) free( p_this->psz_header );
396
397     if( p_this->i_object_type == VLC_OBJECT_ROOT )
398     {
399         /* We are the root object ... no need to lock. */
400         free( p_this->p_libvlc->pp_objects );
401         p_this->p_libvlc->pp_objects = NULL;
402         p_this->p_libvlc->i_objects--;
403
404         vlc_mutex_destroy( &structure_lock );
405     }
406     else
407     {
408         int i_index;
409
410         vlc_mutex_lock( &structure_lock );
411
412         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
413          * useless to try and recover anything if pp_objects gets smashed. */
414         i_index = FindIndex( p_this, p_this->p_libvlc->pp_objects,
415                              p_this->p_libvlc->i_objects );
416         REMOVE_ELEM( p_this->p_libvlc->pp_objects,
417                      p_this->p_libvlc->i_objects, i_index );
418
419         vlc_mutex_unlock( &structure_lock );
420     }
421
422     vlc_mutex_destroy( &p_this->object_lock );
423     vlc_cond_destroy( &p_this->object_wait );
424
425     /* root is not dynamically allocated by vlc_object_create */
426     if( p_this->i_object_type != VLC_OBJECT_ROOT )
427         free( p_this );
428 }
429
430 /**
431  * find an object given its ID
432  *
433  * This function looks for the object whose i_object_id field is i_id. We
434  * use a dichotomy so that lookups are in log2(n).
435  *****************************************************************************/
436 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
437 {
438     int i_max, i_middle;
439     vlc_object_t **pp_objects;
440
441     vlc_mutex_lock( &structure_lock );
442
443     pp_objects = p_this->p_libvlc->pp_objects;
444
445     /* Perform our dichotomy */
446     for( i_max = p_this->p_libvlc->i_objects - 1 ; ; )
447     {
448         i_middle = i_max / 2;
449
450         if( pp_objects[i_middle]->i_object_id > i_id )
451         {
452             i_max = i_middle;
453         }
454         else if( pp_objects[i_middle]->i_object_id < i_id )
455         {
456             if( i_middle )
457             {
458                 pp_objects += i_middle;
459                 i_max -= i_middle;
460             }
461             else
462             {
463                 /* This happens when there are only two remaining objects */
464                 if( pp_objects[i_middle+1]->i_object_id == i_id )
465                 {
466                     vlc_mutex_unlock( &structure_lock );
467                     pp_objects[i_middle+1]->i_refcount++;
468                     return pp_objects[i_middle+1];
469                 }
470                 break;
471             }
472         }
473         else
474         {
475             vlc_mutex_unlock( &structure_lock );
476             pp_objects[i_middle]->i_refcount++;
477             return pp_objects[i_middle];
478         }
479
480         if( i_max == 0 )
481         {
482             /* this means that i_max == i_middle, and since we have already
483              * tested pp_objects[i_middle]), p_found is properly set. */
484             break;
485         }
486     }
487
488     vlc_mutex_unlock( &structure_lock );
489     return NULL;
490 }
491
492 /**
493  ****************************************************************************
494  * find a typed object and increment its refcount
495  *****************************************************************************
496  * This function recursively looks for a given object type. i_mode can be one
497  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
498  *****************************************************************************/
499 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
500 {
501     vlc_object_t *p_found;
502
503     vlc_mutex_lock( &structure_lock );
504
505     /* If we are of the requested type ourselves, don't look further */
506     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
507     {
508         p_this->i_refcount++;
509         vlc_mutex_unlock( &structure_lock );
510         return p_this;
511     }
512
513     /* Otherwise, recursively look for the object */
514     if( (i_mode & 0x000f) == FIND_ANYWHERE )
515     {
516         vlc_object_t *p_root = p_this;
517
518         /* Find the root */
519         while( p_root->p_parent != NULL &&
520                p_root != VLC_OBJECT( p_this->p_vlc ) )
521         {
522             p_root = p_root->p_parent;
523         }
524
525         p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
526         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_vlc ) )
527         {
528             p_found = FindObject( VLC_OBJECT( p_this->p_vlc ),
529                                   i_type, (i_mode & ~0x000f)|FIND_CHILD );
530         }
531     }
532     else
533     {
534         p_found = FindObject( p_this, i_type, i_mode );
535     }
536
537     vlc_mutex_unlock( &structure_lock );
538
539     return p_found;
540 }
541
542 /**
543  ****************************************************************************
544  * increment an object refcount
545  *****************************************************************************/
546 void __vlc_object_yield( vlc_object_t *p_this )
547 {
548     vlc_mutex_lock( &structure_lock );
549     p_this->i_refcount++;
550     vlc_mutex_unlock( &structure_lock );
551 }
552
553 /**
554  ****************************************************************************
555  * decrement an object refcount
556  *****************************************************************************/
557 void __vlc_object_release( vlc_object_t *p_this )
558 {
559     vlc_mutex_lock( &structure_lock );
560     p_this->i_refcount--;
561     vlc_mutex_unlock( &structure_lock );
562 }
563
564 /**
565  ****************************************************************************
566  * attach object to a parent object
567  *****************************************************************************
568  * This function sets p_this as a child of p_parent, and p_parent as a parent
569  * of p_this. This link can be undone using vlc_object_detach.
570  *****************************************************************************/
571 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
572 {
573     vlc_mutex_lock( &structure_lock );
574
575     /* Attach the parent to its child */
576     p_this->p_parent = p_parent;
577
578     /* Attach the child to its parent */
579     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
580                  p_parent->i_children, p_this );
581
582     /* Climb up the tree to see whether we are connected with the root */
583     if( p_parent->b_attached )
584     {
585         SetAttachment( p_this, VLC_TRUE );
586     }
587
588     vlc_mutex_unlock( &structure_lock );
589 }
590
591 /**
592  ****************************************************************************
593  * detach object from its parent
594  *****************************************************************************
595  * This function removes all links between an object and its parent.
596  *****************************************************************************/
597 void __vlc_object_detach( vlc_object_t *p_this )
598 {
599     vlc_mutex_lock( &structure_lock );
600     if( !p_this->p_parent )
601     {
602         msg_Err( p_this, "object is not attached" );
603         vlc_mutex_unlock( &structure_lock );
604         return;
605     }
606
607     /* Climb up the tree to see whether we are connected with the root */
608     if( p_this->p_parent->b_attached )
609     {
610         SetAttachment( p_this, VLC_FALSE );
611     }
612
613     DetachObject( p_this );
614     vlc_mutex_unlock( &structure_lock );
615 }
616
617 /**
618  ****************************************************************************
619  * find a list typed objects and increment their refcount
620  *****************************************************************************
621  * This function recursively looks for a given object type. i_mode can be one
622  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
623  *****************************************************************************/
624 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
625 {
626     vlc_list_t *p_list;
627     vlc_object_t **pp_current, **pp_end;
628     int i_count = 0, i_index = 0;
629
630     vlc_mutex_lock( &structure_lock );
631
632     /* Look for the objects */
633     switch( i_mode & 0x000f )
634     {
635     case FIND_ANYWHERE:
636         pp_current = p_this->p_libvlc->pp_objects;
637         pp_end = pp_current + p_this->p_libvlc->i_objects;
638
639         for( ; pp_current < pp_end ; pp_current++ )
640         {
641             if( (*pp_current)->b_attached
642                  && (*pp_current)->i_object_type == i_type )
643             {
644                 i_count++;
645             }
646         }
647
648         p_list = NewList( i_count );
649         pp_current = p_this->p_libvlc->pp_objects;
650
651         for( ; pp_current < pp_end ; pp_current++ )
652         {
653             if( (*pp_current)->b_attached
654                  && (*pp_current)->i_object_type == i_type )
655             {
656                 ListReplace( p_list, *pp_current, i_index );
657                 if( i_index < i_count ) i_index++;
658             }
659         }
660     break;
661
662     case FIND_CHILD:
663         i_count = CountChildren( p_this, i_type );
664         p_list = NewList( i_count );
665
666         /* Check allocation was successful */
667         if( p_list->i_count != i_count )
668         {
669             msg_Err( p_this, "list allocation failed!" );
670             p_list->i_count = 0;
671             break;
672         }
673
674         p_list->i_count = 0;
675         ListChildren( p_list, p_this, i_type );
676         break;
677
678     default:
679         msg_Err( p_this, "unimplemented!" );
680         p_list = NewList( 0 );
681         break;
682     }
683
684     vlc_mutex_unlock( &structure_lock );
685
686     return p_list;
687 }
688
689 /*****************************************************************************
690  * DumpCommand: print the current vlc structure
691  *****************************************************************************
692  * This function prints either an ASCII tree showing the connections between
693  * vlc objects, and additional information such as their refcount, thread ID,
694  * etc. (command "tree"), or the same data as a simple list (command "list").
695  *****************************************************************************/
696 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
697                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
698 {
699     if( *psz_cmd == 't' )
700     {
701         char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
702         vlc_object_t *p_object;
703
704         if( *newval.psz_string )
705         {
706             p_object = vlc_object_get( p_this, atoi(newval.psz_string) );
707
708             if( !p_object )
709             {
710                 return VLC_ENOOBJ;
711             }
712         }
713         else
714         {
715             p_object = p_this->p_vlc ? VLC_OBJECT(p_this->p_vlc) : p_this;
716         }
717
718         vlc_mutex_lock( &structure_lock );
719
720         psz_foo[0] = '|';
721         DumpStructure( p_object, 0, psz_foo );
722
723         vlc_mutex_unlock( &structure_lock );
724
725         if( *newval.psz_string )
726         {
727             vlc_object_release( p_this );
728         }
729     }
730     else if( *psz_cmd == 'l' )
731     {
732         vlc_object_t **pp_current, **pp_end;
733
734         vlc_mutex_lock( &structure_lock );
735
736         pp_current = p_this->p_libvlc->pp_objects;
737         pp_end = pp_current + p_this->p_libvlc->i_objects;
738
739         for( ; pp_current < pp_end ; pp_current++ )
740         {
741             if( (*pp_current)->b_attached )
742             {
743                 PrintObject( *pp_current, "" );
744             }
745             else
746             {
747                 printf( " o %.8i %s (not attached)\n",
748                         (*pp_current)->i_object_id,
749                         (*pp_current)->psz_object_type );
750             }
751         }
752
753         vlc_mutex_unlock( &structure_lock );
754     }
755
756     return VLC_SUCCESS;
757 }
758
759 /*****************************************************************************
760  * vlc_list_release: free a list previously allocated by vlc_list_find
761  *****************************************************************************
762  * This function decreases the refcount of all objects in the list and
763  * frees the list.
764  *****************************************************************************/
765 void vlc_list_release( vlc_list_t *p_list )
766 {
767     int i_index;
768
769     for( i_index = 0; i_index < p_list->i_count; i_index++ )
770     {
771         vlc_mutex_lock( &structure_lock );
772
773         p_list->p_values[i_index].p_object->i_refcount--;
774
775         vlc_mutex_unlock( &structure_lock );
776     }
777
778     free( p_list->p_values );
779     free( p_list );
780 }
781
782 /* Following functions are local */
783
784 /*****************************************************************************
785  * FindIndex: find the index of an object in an array of objects
786  *****************************************************************************
787  * This function assumes that p_this can be found in pp_objects. It will not
788  * crash if p_this cannot be found, but will return a wrong value. It is your
789  * duty to check the return value if you are not certain that the object could
790  * be found for sure.
791  *****************************************************************************/
792 static int FindIndex( vlc_object_t *p_this,
793                       vlc_object_t **pp_objects, int i_count )
794 {
795     int i_middle = i_count / 2;
796
797     if( i_count == 0 )
798     {
799         return 0;
800     }
801
802     if( pp_objects[i_middle] == p_this )
803     {
804         return i_middle;
805     }
806
807     if( i_count == 1 )
808     {
809         return 0;
810     }
811
812     /* We take advantage of the sorted array */
813     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
814     {
815         return i_middle + FindIndex( p_this, pp_objects + i_middle,
816                                              i_count - i_middle );
817     }
818     else
819     {
820         return FindIndex( p_this, pp_objects, i_middle );
821     }
822 }
823
824 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
825 {
826     int i;
827     vlc_object_t *p_tmp;
828
829     switch( i_mode & 0x000f )
830     {
831     case FIND_PARENT:
832         p_tmp = p_this->p_parent;
833         if( p_tmp )
834         {
835             if( p_tmp->i_object_type == i_type )
836             {
837                 p_tmp->i_refcount++;
838                 return p_tmp;
839             }
840             else
841             {
842                 return FindObject( p_tmp, i_type, i_mode );
843             }
844         }
845         break;
846
847     case FIND_CHILD:
848         for( i = p_this->i_children; i--; )
849         {
850             p_tmp = p_this->pp_children[i];
851             if( p_tmp->i_object_type == i_type )
852             {
853                 p_tmp->i_refcount++;
854                 return p_tmp;
855             }
856             else if( p_tmp->i_children )
857             {
858                 p_tmp = FindObject( p_tmp, i_type, i_mode );
859                 if( p_tmp )
860                 {
861                     return p_tmp;
862                 }
863             }
864         }
865         break;
866
867     case FIND_ANYWHERE:
868         /* Handled in vlc_object_find */
869         break;
870     }
871
872     return NULL;
873 }
874
875 static void DetachObject( vlc_object_t *p_this )
876 {
877     vlc_object_t *p_parent = p_this->p_parent;
878     int i_index, i;
879
880     /* Remove p_this's parent */
881     p_this->p_parent = NULL;
882
883     /* Remove all of p_parent's children which are p_this */
884     for( i_index = p_parent->i_children ; i_index-- ; )
885     {
886         if( p_parent->pp_children[i_index] == p_this )
887         {
888             p_parent->i_children--;
889             for( i = i_index ; i < p_parent->i_children ; i++ )
890             {
891                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
892             }
893         }
894     }
895
896     if( p_parent->i_children )
897     {
898         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
899                                p_parent->i_children * sizeof(vlc_object_t *) );
900     }
901     else
902     {
903         free( p_parent->pp_children );
904         p_parent->pp_children = NULL;
905     }
906 }
907
908 /*****************************************************************************
909  * SetAttachment: recursively set the b_attached flag of a subtree.
910  *****************************************************************************
911  * This function is used by the attach and detach functions to propagate
912  * the b_attached flag in a subtree.
913  *****************************************************************************/
914 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
915 {
916     int i_index;
917
918     for( i_index = p_this->i_children ; i_index-- ; )
919     {
920         SetAttachment( p_this->pp_children[i_index], b_attached );
921     }
922
923     p_this->b_attached = b_attached;
924 }
925
926 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
927 {
928     char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
929
930     psz_name[0] = '\0';
931     if( p_this->psz_object_name )
932     {
933         snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
934         psz_name[48] = '\"';
935         psz_name[49] = '\0';
936     }
937
938     psz_children[0] = '\0';
939     switch( p_this->i_children )
940     {
941         case 0:
942             break;
943         case 1:
944             strcpy( psz_children, ", 1 child" );
945             break;
946         default:
947             snprintf( psz_children, 20,
948                       ", %i children", p_this->i_children );
949             psz_children[19] = '\0';
950             break;
951     }
952
953     psz_refcount[0] = '\0';
954     if( p_this->i_refcount )
955     {
956         snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
957         psz_refcount[19] = '\0';
958     }
959
960     psz_thread[0] = '\0';
961     if( p_this->b_thread )
962     {
963         snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
964         psz_thread[19] = '\0';
965     }
966
967     printf( " %so %.8i %s%s%s%s%s\n", psz_prefix,
968             p_this->i_object_id, p_this->psz_object_type,
969             psz_name, psz_thread, psz_refcount, psz_children );
970 }
971
972 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
973 {
974     int i;
975     char i_back = psz_foo[i_level];
976     psz_foo[i_level] = '\0';
977
978     PrintObject( p_this, psz_foo );
979
980     psz_foo[i_level] = i_back;
981
982     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
983     {
984         msg_Warn( p_this, "structure tree is too deep" );
985         return;
986     }
987
988     for( i = 0 ; i < p_this->i_children ; i++ )
989     {
990         if( i_level )
991         {
992             psz_foo[i_level-1] = ' ';
993
994             if( psz_foo[i_level-2] == '`' )
995             {
996                 psz_foo[i_level-2] = ' ';
997             }
998         }
999
1000         if( i == p_this->i_children - 1 )
1001         {
1002             psz_foo[i_level] = '`';
1003         }
1004         else
1005         {
1006             psz_foo[i_level] = '|';
1007         }
1008
1009         psz_foo[i_level+1] = '-';
1010         psz_foo[i_level+2] = '\0';
1011
1012         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1013     }
1014 }
1015
1016 static vlc_list_t * NewList( int i_count )
1017 {
1018     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1019     if( p_list == NULL )
1020     {
1021         return NULL;
1022     }
1023
1024     p_list->i_count = i_count;
1025
1026     if( i_count == 0 )
1027     {
1028         p_list->p_values = NULL;
1029         return p_list;
1030     }
1031
1032     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1033     if( p_list->p_values == NULL )
1034     {
1035         p_list->i_count = 0;
1036         return p_list;
1037     }
1038
1039     return p_list;
1040 }
1041
1042 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1043                          int i_index )
1044 {
1045     if( p_list == NULL || i_index >= p_list->i_count )
1046     {
1047         return;
1048     }
1049
1050     p_object->i_refcount++;
1051
1052     p_list->p_values[i_index].p_object = p_object;
1053
1054     return;
1055 }
1056
1057 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1058 {
1059     if( p_list == NULL )
1060     {
1061         return;
1062     }
1063
1064     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1065                                 * sizeof( vlc_value_t ) );
1066     if( p_list->p_values == NULL )
1067     {
1068         p_list->i_count = 0;
1069         return;
1070     }
1071
1072     p_object->i_refcount++;
1073
1074     p_list->p_values[p_list->i_count].p_object = p_object;
1075     p_list->i_count++;
1076
1077     return;
1078 }*/
1079
1080 static int CountChildren( vlc_object_t *p_this, int i_type )
1081 {
1082     vlc_object_t *p_tmp;
1083     int i, i_count = 0;
1084
1085     for( i = 0; i < p_this->i_children; i++ )
1086     {
1087         p_tmp = p_this->pp_children[i];
1088
1089         if( p_tmp->i_object_type == i_type )
1090         {
1091             i_count++;
1092         }
1093
1094         if( p_tmp->i_children )
1095         {
1096             i_count += CountChildren( p_tmp, i_type );
1097         }
1098     }
1099
1100     return i_count;
1101 }
1102
1103 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1104 {
1105     vlc_object_t *p_tmp;
1106     int i;
1107
1108     for( i = 0; i < p_this->i_children; i++ )
1109     {
1110         p_tmp = p_this->pp_children[i];
1111
1112         if( p_tmp->i_object_type == i_type )
1113         {
1114             ListReplace( p_list, p_tmp, p_list->i_count++ );
1115         }
1116
1117         if( p_tmp->i_children )
1118         {
1119             ListChildren( p_list, p_tmp, i_type );
1120         }
1121     }
1122 }