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