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