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