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