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