]> git.sesse.net Git - vlc/blob - src/misc/objects.c
* include/vlc_codec.h: defines decoders/encoders related structures here.
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: objects.c,v 1.41 2003/10/08 21:01:07 gbazin 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_PLAYLIST:
119             i_size = sizeof(playlist_t);
120             psz_type = "playlist";
121             break;
122         case VLC_OBJECT_INPUT:
123             i_size = sizeof(input_thread_t);
124             psz_type = "input";
125             break;
126         case VLC_OBJECT_DECODER:
127             i_size = sizeof(decoder_t);
128             psz_type = "decoder";
129             break;
130         case VLC_OBJECT_DECODER_FIFO: /* tmp for backward compat */
131             i_size = sizeof(decoder_fifo_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 with children" );
278         return;
279     }
280
281     if( p_this->p_parent )
282     {
283         msg_Err( p_this, "cannot delete object with a parent" );
284         return;
285     }
286
287     while( p_this->i_refcount )
288     {
289         i_delay++;
290
291         /* Don't warn immediately ... 100ms seems OK */
292         if( i_delay == 2 )
293         {
294             msg_Warn( p_this, "refcount is %i, delaying before deletion",
295                               p_this->i_refcount );
296         }
297         else if( i_delay == 12 )
298         {
299             msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
300                              p_this->i_refcount );
301         }
302         else if( i_delay == 42 )
303         {
304             msg_Err( p_this, "we waited too long, cancelling destruction" );
305             return;
306         }
307
308         msleep( 100000 );
309     }
310
311     /* Destroy the associated variables, starting from the end so that
312      * no memmove calls have to be done. */
313     while( p_this->i_vars )
314     {
315         var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
316     }
317
318     free( p_this->p_vars );
319     vlc_mutex_destroy( &p_this->var_lock );
320
321     if( p_this->i_object_type == VLC_OBJECT_ROOT )
322     {
323         /* We are the root object ... no need to lock. */
324         free( p_this->p_libvlc->pp_objects );
325         p_this->p_libvlc->pp_objects = NULL;
326         p_this->p_libvlc->i_objects--;
327
328         vlc_mutex_destroy( &structure_lock );
329     }
330     else
331     {
332         int i_index;
333
334         vlc_mutex_lock( &structure_lock );
335
336         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
337          * useless to try and recover anything if pp_objects gets smashed. */
338         i_index = FindIndex( p_this, p_this->p_libvlc->pp_objects,
339                              p_this->p_libvlc->i_objects );
340         REMOVE_ELEM( p_this->p_libvlc->pp_objects,
341                      p_this->p_libvlc->i_objects, i_index );
342
343         vlc_mutex_unlock( &structure_lock );
344     }
345
346     vlc_mutex_destroy( &p_this->object_lock );
347     vlc_cond_destroy( &p_this->object_wait );
348
349     free( p_this );
350 }
351
352 /**
353  * find an object given its ID
354  *
355  * This function looks for the object whose i_object_id field is i_id. We
356  * use a dichotomy so that lookups are in log2(n).
357  *****************************************************************************/
358 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
359 {
360     int i_max, i_middle;
361     vlc_object_t **pp_objects;
362
363     vlc_mutex_lock( &structure_lock );
364
365     pp_objects = p_this->p_libvlc->pp_objects;
366
367     /* Perform our dichotomy */
368     for( i_max = p_this->p_libvlc->i_objects - 1 ; ; )
369     {
370         i_middle = i_max / 2;
371
372         if( pp_objects[i_middle]->i_object_id > i_id )
373         {
374             i_max = i_middle;
375         }
376         else if( pp_objects[i_middle]->i_object_id < i_id )
377         {
378             if( i_middle )
379             {
380                 pp_objects += i_middle;
381                 i_max -= i_middle;
382             }
383             else
384             {
385                 /* This happens when there are only two remaining objects */
386                 if( pp_objects[i_middle+1]->i_object_id == i_id )
387                 {
388                     vlc_mutex_unlock( &structure_lock );
389                     pp_objects[i_middle+1]->i_refcount++;
390                     return pp_objects[i_middle+1];
391                 }
392                 break;
393             }
394         }
395         else
396         {
397             vlc_mutex_unlock( &structure_lock );
398             pp_objects[i_middle]->i_refcount++;
399             return pp_objects[i_middle];
400         }
401
402         if( i_max == 0 )
403         {
404             /* this means that i_max == i_middle, and since we have already
405              * tested pp_objects[i_middle]), p_found is properly set. */
406             break;
407         }
408     }
409
410     vlc_mutex_unlock( &structure_lock );
411     return NULL;
412 }
413
414 /**
415  ****************************************************************************
416  * find a typed object and increment its refcount
417  *****************************************************************************
418  * This function recursively looks for a given object type. i_mode can be one
419  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
420  *****************************************************************************/
421 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
422 {
423     vlc_object_t *p_found;
424
425     vlc_mutex_lock( &structure_lock );
426
427     /* If we are of the requested type ourselves, don't look further */
428     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
429     {
430         p_this->i_refcount++;
431         vlc_mutex_unlock( &structure_lock );
432         return p_this;
433     }
434
435     /* Otherwise, recursively look for the object */
436     if( (i_mode & 0x000f) == FIND_ANYWHERE )
437     {
438         vlc_object_t *p_root = p_this;
439
440         /* Find the root */
441         while( p_root->p_parent != NULL &&
442                p_root != VLC_OBJECT( p_this->p_vlc ) )
443         {
444             p_root = p_root->p_parent;
445         }
446
447         p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
448         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_vlc ) )
449         {
450             p_found = FindObject( VLC_OBJECT( p_this->p_vlc ),
451                                   i_type, (i_mode & ~0x000f)|FIND_CHILD );
452         }
453     }
454     else
455     {
456         p_found = FindObject( p_this, i_type, i_mode );
457     }
458
459     vlc_mutex_unlock( &structure_lock );
460
461     return p_found;
462 }
463
464 /**
465  ****************************************************************************
466  * increment an object refcount
467  *****************************************************************************/
468 void __vlc_object_yield( vlc_object_t *p_this )
469 {
470     vlc_mutex_lock( &structure_lock );
471     p_this->i_refcount++;
472     vlc_mutex_unlock( &structure_lock );
473 }
474
475 /**
476  ****************************************************************************
477  * decrement an object refcount
478  *****************************************************************************/
479 void __vlc_object_release( vlc_object_t *p_this )
480 {
481     vlc_mutex_lock( &structure_lock );
482     p_this->i_refcount--;
483     vlc_mutex_unlock( &structure_lock );
484 }
485
486 /**
487  ****************************************************************************
488  * attach object to a parent object
489  *****************************************************************************
490  * This function sets p_this as a child of p_parent, and p_parent as a parent
491  * of p_this. This link can be undone using vlc_object_detach.
492  *****************************************************************************/
493 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
494 {
495     vlc_mutex_lock( &structure_lock );
496
497     /* Attach the parent to its child */
498     p_this->p_parent = p_parent;
499
500     /* Attach the child to its parent */
501     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
502                  p_parent->i_children, p_this );
503
504     /* Climb up the tree to see whether we are connected with the root */
505     if( p_parent->b_attached )
506     {
507         SetAttachment( p_this, VLC_TRUE );
508     }
509
510     vlc_mutex_unlock( &structure_lock );
511 }
512
513 /**
514  ****************************************************************************
515  * detach object from its parent
516  *****************************************************************************
517  * This function removes all links between an object and its parent.
518  *****************************************************************************/
519 void __vlc_object_detach( vlc_object_t *p_this )
520 {
521     vlc_mutex_lock( &structure_lock );
522     if( !p_this->p_parent )
523     {
524         msg_Err( p_this, "object is not attached" );
525         vlc_mutex_unlock( &structure_lock );
526         return;
527     }
528
529     /* Climb up the tree to see whether we are connected with the root */
530     if( p_this->p_parent->b_attached )
531     {
532         SetAttachment( p_this, VLC_FALSE );
533     }
534
535     DetachObject( p_this );
536     vlc_mutex_unlock( &structure_lock );
537 }
538
539 /**
540  ****************************************************************************
541  * find a list typed objects and increment their refcount
542  *****************************************************************************
543  * This function recursively looks for a given object type. i_mode can be one
544  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
545  *****************************************************************************/
546 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
547 {
548     vlc_list_t *p_list;
549     vlc_object_t **pp_current, **pp_end;
550     int i_count = 0, i_index = 0;
551
552     vlc_mutex_lock( &structure_lock );
553
554     /* Look for the objects */
555     switch( i_mode & 0x000f )
556     {
557     case FIND_ANYWHERE:
558         pp_current = p_this->p_libvlc->pp_objects;
559         pp_end = pp_current + p_this->p_libvlc->i_objects;
560
561         for( ; pp_current < pp_end ; pp_current++ )
562         {
563             if( (*pp_current)->b_attached
564                  && (*pp_current)->i_object_type == i_type )
565             {
566                 i_count++;
567             }
568         }
569
570         p_list = NewList( i_count );
571         pp_current = p_this->p_libvlc->pp_objects;
572
573         for( ; pp_current < pp_end ; pp_current++ )
574         {
575             if( (*pp_current)->b_attached
576                  && (*pp_current)->i_object_type == i_type )
577             {
578                 ListReplace( p_list, *pp_current, i_index );
579                 if( i_index < i_count ) i_index++;
580             }
581         }
582     break;
583
584     case FIND_CHILD:
585         i_count = CountChildren( p_this, i_type );
586         p_list = NewList( i_count );
587
588         /* Check allocation was successful */
589         if( p_list->i_count != i_count )
590         {
591             msg_Err( p_this, "list allocation failed!" );
592             p_list->i_count = 0;
593             break;
594         }
595
596         p_list->i_count = 0;
597         ListChildren( p_list, p_this, i_type );
598         break;
599
600     default:
601         msg_Err( p_this, "unimplemented!" );
602         p_list = NewList( 0 );
603         break;
604     }
605
606     vlc_mutex_unlock( &structure_lock );
607
608     return p_list;
609 }
610
611 /*****************************************************************************
612  * DumpCommand: print the current vlc structure
613  *****************************************************************************
614  * This function prints either an ASCII tree showing the connections between
615  * vlc objects, and additional information such as their refcount, thread ID,
616  * etc. (command "tree"), or the same data as a simple list (command "list").
617  *****************************************************************************/
618 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
619                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
620 {
621     if( *psz_cmd == 't' )
622     {
623         char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
624         vlc_object_t *p_object;
625
626         if( *newval.psz_string )
627         {
628             p_object = vlc_object_get( p_this, atoi(newval.psz_string) );
629
630             if( !p_object )
631             {
632                 return VLC_ENOOBJ;
633             }
634         }
635         else
636         {
637             p_object = p_this->p_vlc ? VLC_OBJECT(p_this->p_vlc) : p_this;
638         }
639
640         vlc_mutex_lock( &structure_lock );
641
642         psz_foo[0] = '|';
643         DumpStructure( p_object, 0, psz_foo );
644
645         vlc_mutex_unlock( &structure_lock );
646
647         if( *newval.psz_string )
648         {
649             vlc_object_release( p_this );
650         }
651     }
652     else if( *psz_cmd == 'l' )
653     {
654         vlc_object_t **pp_current, **pp_end;
655
656         vlc_mutex_lock( &structure_lock );
657
658         pp_current = p_this->p_libvlc->pp_objects;
659         pp_end = pp_current + p_this->p_libvlc->i_objects;
660
661         for( ; pp_current < pp_end ; pp_current++ )
662         {
663             if( (*pp_current)->b_attached )
664             {
665                 PrintObject( *pp_current, "" );
666             }
667             else
668             {
669                 printf( " o %.8i %s (not attached)\n",
670                         (*pp_current)->i_object_id,
671                         (*pp_current)->psz_object_type );
672             }
673         }
674
675         vlc_mutex_unlock( &structure_lock );
676     }
677
678     return VLC_SUCCESS;
679 }
680
681 /*****************************************************************************
682  * vlc_list_release: free a list previously allocated by vlc_list_find
683  *****************************************************************************
684  * This function decreases the refcount of all objects in the list and
685  * frees the list.
686  *****************************************************************************/
687 void vlc_list_release( vlc_list_t *p_list )
688 {
689     int i_index;
690
691     for( i_index = 0; i_index < p_list->i_count; i_index++ )
692     {
693         vlc_mutex_lock( &structure_lock );
694
695         p_list->p_values[i_index].p_object->i_refcount--;
696
697         vlc_mutex_unlock( &structure_lock );
698     }
699
700     free( p_list->p_values );
701     free( p_list );
702 }
703
704 /* Following functions are local */
705
706 /*****************************************************************************
707  * FindIndex: find the index of an object in an array of objects
708  *****************************************************************************
709  * This function assumes that p_this can be found in pp_objects. It will not
710  * crash if p_this cannot be found, but will return a wrong value. It is your
711  * duty to check the return value if you are not certain that the object could
712  * be found for sure.
713  *****************************************************************************/
714 static int FindIndex( vlc_object_t *p_this,
715                       vlc_object_t **pp_objects, int i_count )
716 {
717     int i_middle = i_count / 2;
718
719     if( i_count == 0 )
720     {
721         return 0;
722     }
723
724     if( pp_objects[i_middle] == p_this )
725     {
726         return i_middle;
727     }
728
729     if( i_count == 1 )
730     {
731         return 0;
732     }
733
734     /* We take advantage of the sorted array */
735     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
736     {
737         return i_middle + FindIndex( p_this, pp_objects + i_middle,
738                                              i_count - i_middle );
739     }
740     else
741     {
742         return FindIndex( p_this, pp_objects, i_middle );
743     }
744 }
745
746 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
747 {
748     int i;
749     vlc_object_t *p_tmp;
750
751     switch( i_mode & 0x000f )
752     {
753     case FIND_PARENT:
754         p_tmp = p_this->p_parent;
755         if( p_tmp )
756         {
757             if( p_tmp->i_object_type == i_type )
758             {
759                 p_tmp->i_refcount++;
760                 return p_tmp;
761             }
762             else
763             {
764                 return FindObject( p_tmp, i_type, i_mode );
765             }
766         }
767         break;
768
769     case FIND_CHILD:
770         for( i = p_this->i_children; i--; )
771         {
772             p_tmp = p_this->pp_children[i];
773             if( p_tmp->i_object_type == i_type )
774             {
775                 p_tmp->i_refcount++;
776                 return p_tmp;
777             }
778             else if( p_tmp->i_children )
779             {
780                 p_tmp = FindObject( p_tmp, i_type, i_mode );
781                 if( p_tmp )
782                 {
783                     return p_tmp;
784                 }
785             }
786         }
787         break;
788
789     case FIND_ANYWHERE:
790         /* Handled in vlc_object_find */
791         break;
792     }
793
794     return NULL;
795 }
796
797 static void DetachObject( vlc_object_t *p_this )
798 {
799     vlc_object_t *p_parent = p_this->p_parent;
800     int i_index, i;
801
802     /* Remove p_this's parent */
803     p_this->p_parent = NULL;
804
805     /* Remove all of p_parent's children which are p_this */
806     for( i_index = p_parent->i_children ; i_index-- ; )
807     {
808         if( p_parent->pp_children[i_index] == p_this )
809         {
810             p_parent->i_children--;
811             for( i = i_index ; i < p_parent->i_children ; i++ )
812             {
813                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
814             }
815         }
816     }
817
818     if( p_parent->i_children )
819     {
820         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
821                                p_parent->i_children * sizeof(vlc_object_t *) );
822     }
823     else
824     {
825         free( p_parent->pp_children );
826         p_parent->pp_children = NULL;
827     }
828 }
829
830 /*****************************************************************************
831  * SetAttachment: recursively set the b_attached flag of a subtree.
832  *****************************************************************************
833  * This function is used by the attach and detach functions to propagate
834  * the b_attached flag in a subtree.
835  *****************************************************************************/
836 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
837 {
838     int i_index;
839
840     for( i_index = p_this->i_children ; i_index-- ; )
841     {
842         SetAttachment( p_this->pp_children[i_index], b_attached );
843     }
844
845     p_this->b_attached = b_attached;
846 }
847
848 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
849 {
850     char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
851
852     psz_name[0] = '\0';
853     if( p_this->psz_object_name )
854     {
855         snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
856         psz_name[48] = '\"';
857         psz_name[49] = '\0';
858     }
859
860     psz_children[0] = '\0';
861     switch( p_this->i_children )
862     {
863         case 0:
864             break;
865         case 1:
866             strcpy( psz_children, ", 1 child" );
867             break;
868         default:
869             snprintf( psz_children, 20,
870                       ", %i children", p_this->i_children );
871             psz_children[19] = '\0';
872             break;
873     }
874
875     psz_refcount[0] = '\0';
876     if( p_this->i_refcount )
877     {
878         snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
879         psz_refcount[19] = '\0';
880     }
881
882     psz_thread[0] = '\0';
883     if( p_this->b_thread )
884     {
885         snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
886         psz_thread[19] = '\0';
887     }
888
889     printf( " %so %.8i %s%s%s%s%s\n", psz_prefix,
890             p_this->i_object_id, p_this->psz_object_type,
891             psz_name, psz_thread, psz_refcount, psz_children );
892 }
893
894 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
895 {
896     int i;
897     char i_back = psz_foo[i_level];
898     psz_foo[i_level] = '\0';
899
900     PrintObject( p_this, psz_foo );
901
902     psz_foo[i_level] = i_back;
903
904     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
905     {
906         msg_Warn( p_this, "structure tree is too deep" );
907         return;
908     }
909
910     for( i = 0 ; i < p_this->i_children ; i++ )
911     {
912         if( i_level )
913         {
914             psz_foo[i_level-1] = ' ';
915
916             if( psz_foo[i_level-2] == '`' )
917             {
918                 psz_foo[i_level-2] = ' ';
919             }
920         }
921
922         if( i == p_this->i_children - 1 )
923         {
924             psz_foo[i_level] = '`';
925         }
926         else
927         {
928             psz_foo[i_level] = '|';
929         }
930
931         psz_foo[i_level+1] = '-';
932         psz_foo[i_level+2] = '\0';
933
934         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
935     }
936 }
937
938 static vlc_list_t * NewList( int i_count )
939 {
940     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
941     if( p_list == NULL )
942     {
943         return NULL;
944     }
945
946     p_list->i_count = i_count;
947
948     if( i_count == 0 )
949     {
950         p_list->p_values = NULL;
951         return p_list;
952     }
953
954     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
955     if( p_list->p_values == NULL )
956     {
957         p_list->i_count = 0;
958         return p_list;
959     }
960
961     return p_list;
962 }
963
964 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
965                          int i_index )
966 {
967     if( p_list == NULL || i_index >= p_list->i_count )
968     {
969         return;
970     }
971
972     p_object->i_refcount++;
973
974     p_list->p_values[i_index].p_object = p_object;
975
976     return;
977 }
978
979 static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
980 {
981     if( p_list == NULL )
982     {
983         return;
984     }
985
986     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
987                                 * sizeof( vlc_value_t ) );
988     if( p_list->p_values == NULL )
989     {
990         p_list->i_count = 0;
991         return;
992     }
993
994     p_object->i_refcount++;
995
996     p_list->p_values[p_list->i_count].p_object = p_object;
997     p_list->i_count++;
998
999     return;
1000 }
1001
1002 static int CountChildren( vlc_object_t *p_this, int i_type )
1003 {
1004     vlc_object_t *p_tmp;
1005     int i, i_count = 0;
1006
1007     for( i = 0; i < p_this->i_children; i++ )
1008     {
1009         p_tmp = p_this->pp_children[i];
1010
1011         if( p_tmp->i_object_type == i_type )
1012         {
1013             i_count++;
1014         }
1015
1016         if( p_tmp->i_children )
1017         {
1018             i_count += CountChildren( p_tmp, i_type );
1019         }
1020     }
1021
1022     return i_count;
1023 }
1024
1025 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1026 {
1027     vlc_object_t *p_tmp;
1028     int i;
1029
1030     for( i = 0; i < p_this->i_children; i++ )
1031     {
1032         p_tmp = p_this->pp_children[i];
1033
1034         if( p_tmp->i_object_type == i_type )
1035         {
1036             ListReplace( p_list, p_tmp, p_list->i_count++ );
1037         }
1038
1039         if( p_tmp->i_children )
1040         {
1041             ListChildren( p_list, p_tmp, i_type );
1042         }
1043     }
1044 }