1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2007 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
26 * This file contains the functions to handle the vlc_object_t type
30 /*****************************************************************************
32 *****************************************************************************/
36 # include <stdlib.h> /* realloc() */
39 #include "../libvlc.h"
42 #include "audio_output/aout_internal.h"
44 #include <vlc_access.h>
45 #include <vlc_demux.h>
46 #include <vlc_stream.h>
49 #include "stream_output/stream_output.h"
51 #include "vlc_playlist.h"
52 #include "vlc_interface.h"
53 #include "vlc_codec.h"
54 #include "vlc_filter.h"
56 #include "vlc_httpd.h"
58 #include "input/vlm_internal.h"
65 #include "variables.h"
67 /*****************************************************************************
69 *****************************************************************************/
70 static int DumpCommand( vlc_object_t *, char const *,
71 vlc_value_t, vlc_value_t, void * );
73 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
74 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
75 static void DetachObject ( vlc_object_t * );
76 static void PrintObject ( vlc_object_t *, const char * );
77 static void DumpStructure ( vlc_object_t *, int, char * );
78 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
79 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
81 static vlc_list_t * NewList ( int );
82 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
83 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
84 static int CountChildren ( vlc_object_t *, int );
85 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
87 /*****************************************************************************
88 * Local structure lock
89 *****************************************************************************/
90 static vlc_mutex_t structure_lock;
92 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
93 int i_type, const char *psz_type )
95 vlc_object_t * p_new = NULL;
97 if( i_type == VLC_OBJECT_GLOBAL )
103 p_new = malloc( i_size );
104 if( !p_new ) return NULL;
105 memset( p_new, 0, i_size );
108 p_new->i_object_type = i_type;
109 p_new->psz_object_type = psz_type;
111 p_new->psz_object_name = NULL;
113 p_new->b_die = VLC_FALSE;
114 p_new->b_error = VLC_FALSE;
115 p_new->b_dead = VLC_FALSE;
116 p_new->b_attached = VLC_FALSE;
117 p_new->b_force = VLC_FALSE;
119 p_new->psz_header = NULL;
122 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
123 p_new->i_flags |= OBJECT_FLAGS_NODBG;
124 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
125 p_new->i_flags |= OBJECT_FLAGS_QUIET;
126 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
127 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
130 p_new->p_vars = (variable_t *)malloc( 16 * sizeof( variable_t ) );
134 if( i_type != VLC_OBJECT_GLOBAL )
139 if( i_type == VLC_OBJECT_GLOBAL )
141 /* If i_type is global, then p_new is actually p_libvlc_global */
142 p_new->p_libvlc_global = (libvlc_global_data_t*)p_new;
143 p_new->p_libvlc = NULL;
145 p_new->p_libvlc_global->i_counter = 0;
146 p_new->i_object_id = 0;
148 p_new->p_libvlc_global->i_objects = 1;
149 p_new->p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
150 p_new->p_libvlc_global->pp_objects[0] = p_new;
151 p_new->b_attached = VLC_TRUE;
155 p_new->p_libvlc_global = p_this->p_libvlc_global;
156 p_new->p_libvlc = ( i_type == VLC_OBJECT_LIBVLC ) ? (libvlc_int_t*)p_new
159 vlc_mutex_lock( &structure_lock );
161 p_new->p_libvlc_global->i_counter++;
162 p_new->i_object_id = p_new->p_libvlc_global->i_counter;
164 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
165 * useless to try and recover anything if pp_objects gets smashed. */
166 TAB_APPEND( p_new->p_libvlc_global->i_objects,
167 p_new->p_libvlc_global->pp_objects,
170 vlc_mutex_unlock( &structure_lock );
173 p_new->i_refcount = 0;
174 p_new->p_parent = NULL;
175 p_new->pp_children = NULL;
176 p_new->i_children = 0;
178 p_new->p_private = NULL;
180 /* Initialize mutexes and condvars */
181 vlc_mutex_init( p_new, &p_new->object_lock );
182 vlc_cond_init( p_new, &p_new->object_wait );
183 vlc_mutex_init( p_new, &p_new->var_lock );
185 if( i_type == VLC_OBJECT_GLOBAL )
187 vlc_mutex_init( p_new, &structure_lock );
189 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
190 var_AddCallback( p_new, "list", DumpCommand, NULL );
191 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
192 var_AddCallback( p_new, "tree", DumpCommand, NULL );
193 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
194 var_AddCallback( p_new, "vars", DumpCommand, NULL );
202 * Allocates and initializes a vlc object.
204 * @param i_type known object type (all of them are negative integer values),
205 * or object byte size (always positive).
207 * @return the new object, or NULL on error.
209 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
211 const char * psz_type;
216 case VLC_OBJECT_GLOBAL:
217 i_size = sizeof(libvlc_global_data_t);
220 case VLC_OBJECT_LIBVLC:
221 i_size = sizeof(libvlc_int_t);
224 case VLC_OBJECT_INTF:
225 i_size = sizeof(intf_thread_t);
226 psz_type = "interface";
228 case VLC_OBJECT_DIALOGS:
229 i_size = sizeof(intf_thread_t);
230 psz_type = "dialogs";
232 case VLC_OBJECT_PLAYLIST:
233 i_size = sizeof(playlist_t);
234 psz_type = "playlist";
237 i_size = sizeof(services_discovery_t);
238 psz_type = "services discovery";
240 case VLC_OBJECT_INPUT:
241 i_size = sizeof(input_thread_t);
244 case VLC_OBJECT_DEMUX:
245 i_size = sizeof(demux_t);
248 case VLC_OBJECT_ACCESS:
249 i_size = sizeof(access_t);
252 case VLC_OBJECT_DECODER:
253 i_size = sizeof(decoder_t);
254 psz_type = "decoder";
256 case VLC_OBJECT_PACKETIZER:
257 i_size = sizeof(decoder_t);
258 psz_type = "packetizer";
260 case VLC_OBJECT_ENCODER:
261 i_size = sizeof(encoder_t);
262 psz_type = "encoder";
264 case VLC_OBJECT_FILTER:
265 i_size = sizeof(filter_t);
268 case VLC_OBJECT_VOUT:
269 i_size = sizeof(vout_thread_t);
270 psz_type = "video output";
273 i_size = sizeof(spu_t);
274 psz_type = "subpicture";
276 case VLC_OBJECT_AOUT:
277 i_size = sizeof(aout_instance_t);
278 psz_type = "audio output";
280 case VLC_OBJECT_SOUT:
281 i_size = sizeof(sout_instance_t);
282 psz_type = "stream output";
285 i_size = sizeof( vlm_t );
286 psz_type = "vlm dameon";
289 i_size = sizeof( vod_t );
290 psz_type = "vod server";
293 i_size = sizeof( tls_t );
297 i_size = sizeof( xml_t );
300 case VLC_OBJECT_OPENGL:
301 i_size = sizeof( vout_thread_t );
304 case VLC_OBJECT_ANNOUNCE:
305 i_size = sizeof( announce_handler_t );
306 psz_type = "announce";
308 case VLC_OBJECT_META_ENGINE:
309 i_size = sizeof( meta_engine_t );
310 psz_type = "meta engine";
312 case VLC_OBJECT_OSDMENU:
313 i_size = sizeof( osd_menu_t );
314 psz_type = "osd menu";
317 i_size = i_type > (int)sizeof(vlc_object_t)
318 ? i_type : (int)sizeof(vlc_object_t);
319 i_type = VLC_OBJECT_GENERIC;
320 psz_type = "generic";
324 return vlc_custom_create( p_this, i_size, i_type, psz_type );
329 ****************************************************************************
330 * Destroy a vlc object
332 * This function destroys an object that has been previously allocated with
333 * vlc_object_create. The object's refcount must be zero and it must not be
334 * attached to other objects in any way.
335 *****************************************************************************/
336 void __vlc_object_destroy( vlc_object_t *p_this )
340 if( p_this->i_children )
342 msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
343 p_this->i_object_id, p_this->psz_object_name );
347 if( p_this->p_parent )
349 msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
350 p_this->i_object_id, p_this->psz_object_name );
354 while( p_this->i_refcount )
358 /* Don't warn immediately ... 100ms seems OK */
362 "refcount is %i, delaying before deletion (id=%d,type=%d)",
363 p_this->i_refcount, p_this->i_object_id,
364 p_this->i_object_type );
366 else if( i_delay == 10 )
369 "refcount is %i, delaying again (id=%d,type=%d)",
370 p_this->i_refcount, p_this->i_object_id,
371 p_this->i_object_type );
373 else if( i_delay == 20 )
376 "waited too long, cancelling destruction (id=%d,type=%d)",
377 p_this->i_object_id, p_this->i_object_type );
384 /* Destroy the associated variables, starting from the end so that
385 * no memmove calls have to be done. */
386 while( p_this->i_vars )
388 var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
391 free( p_this->p_vars );
392 vlc_mutex_destroy( &p_this->var_lock );
394 if( p_this->psz_header ) free( p_this->psz_header );
396 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
398 /* We are the global object ... no need to lock. */
399 free( p_this->p_libvlc_global->pp_objects );
400 p_this->p_libvlc_global->pp_objects = NULL;
401 p_this->p_libvlc_global->i_objects--;
403 vlc_mutex_destroy( &structure_lock );
409 vlc_mutex_lock( &structure_lock );
411 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
412 * useless to try and recover anything if pp_objects gets smashed. */
413 i_index = FindIndex( p_this, p_this->p_libvlc_global->pp_objects,
414 p_this->p_libvlc_global->i_objects );
415 REMOVE_ELEM( p_this->p_libvlc_global->pp_objects,
416 p_this->p_libvlc_global->i_objects, i_index );
418 vlc_mutex_unlock( &structure_lock );
421 vlc_mutex_destroy( &p_this->object_lock );
422 vlc_cond_destroy( &p_this->object_wait );
424 /* global is not dynamically allocated by vlc_object_create */
425 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
433 void __vlc_object_kill( vlc_object_t *p_this )
435 vlc_mutex_lock( &p_this->object_lock );
436 p_this->b_die = VLC_TRUE;
437 vlc_mutex_unlock( &p_this->object_lock );
442 * find an object given its ID
444 * This function looks for the object whose i_object_id field is i_id. We
445 * use a dichotomy so that lookups are in log2(n).
446 *****************************************************************************/
447 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
450 vlc_object_t **pp_objects;
452 vlc_mutex_lock( &structure_lock );
454 pp_objects = p_this->p_libvlc_global->pp_objects;
456 /* Perform our dichotomy */
457 for( i_max = p_this->p_libvlc_global->i_objects - 1 ; ; )
459 i_middle = i_max / 2;
461 if( pp_objects[i_middle]->i_object_id > i_id )
465 else if( pp_objects[i_middle]->i_object_id < i_id )
469 pp_objects += i_middle;
474 /* This happens when there are only two remaining objects */
475 if( pp_objects[i_middle+1]->i_object_id == i_id )
477 vlc_mutex_unlock( &structure_lock );
478 pp_objects[i_middle+1]->i_refcount++;
479 return pp_objects[i_middle+1];
486 vlc_mutex_unlock( &structure_lock );
487 pp_objects[i_middle]->i_refcount++;
488 return pp_objects[i_middle];
493 /* this means that i_max == i_middle, and since we have already
494 * tested pp_objects[i_middle]), p_found is properly set. */
499 vlc_mutex_unlock( &structure_lock );
504 ****************************************************************************
505 * find a typed object and increment its refcount
506 *****************************************************************************
507 * This function recursively looks for a given object type. i_mode can be one
508 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
509 *****************************************************************************/
510 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
512 vlc_object_t *p_found;
514 vlc_mutex_lock( &structure_lock );
516 /* If we are of the requested type ourselves, don't look further */
517 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
519 p_this->i_refcount++;
520 vlc_mutex_unlock( &structure_lock );
524 /* Otherwise, recursively look for the object */
525 if( (i_mode & 0x000f) == FIND_ANYWHERE )
527 vlc_object_t *p_root = p_this;
530 while( p_root->p_parent != NULL &&
531 p_root != VLC_OBJECT( p_this->p_libvlc ) )
533 p_root = p_root->p_parent;
536 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
537 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
539 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
540 i_type, (i_mode & ~0x000f)|FIND_CHILD );
545 p_found = FindObject( p_this, i_type, i_mode );
548 vlc_mutex_unlock( &structure_lock );
554 ****************************************************************************
555 * find a named object and increment its refcount
556 *****************************************************************************
557 * This function recursively looks for a given object name. i_mode can be one
558 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
559 *****************************************************************************/
560 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
563 vlc_object_t *p_found;
565 vlc_mutex_lock( &structure_lock );
567 /* If have the requested name ourselves, don't look further */
568 if( !(i_mode & FIND_STRICT)
569 && p_this->psz_object_name
570 && !strcmp( p_this->psz_object_name, psz_name ) )
572 p_this->i_refcount++;
573 vlc_mutex_unlock( &structure_lock );
577 /* Otherwise, recursively look for the object */
578 if( (i_mode & 0x000f) == FIND_ANYWHERE )
580 vlc_object_t *p_root = p_this;
583 while( p_root->p_parent != NULL &&
584 p_root != VLC_OBJECT( p_this->p_libvlc ) )
586 p_root = p_root->p_parent;
589 p_found = FindObjectName( p_root, psz_name,
590 (i_mode & ~0x000f)|FIND_CHILD );
591 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
593 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
594 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
599 p_found = FindObjectName( p_this, psz_name, i_mode );
602 vlc_mutex_unlock( &structure_lock );
608 ****************************************************************************
609 * increment an object refcount
610 *****************************************************************************/
611 void __vlc_object_yield( vlc_object_t *p_this )
613 vlc_mutex_lock( &structure_lock );
614 p_this->i_refcount++;
615 vlc_mutex_unlock( &structure_lock );
619 ****************************************************************************
620 * decrement an object refcount
621 *****************************************************************************/
622 void __vlc_object_release( vlc_object_t *p_this )
624 vlc_mutex_lock( &structure_lock );
625 p_this->i_refcount--;
626 vlc_mutex_unlock( &structure_lock );
630 ****************************************************************************
631 * attach object to a parent object
632 *****************************************************************************
633 * This function sets p_this as a child of p_parent, and p_parent as a parent
634 * of p_this. This link can be undone using vlc_object_detach.
635 *****************************************************************************/
636 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
638 if( !p_this ) return;
640 vlc_mutex_lock( &structure_lock );
642 /* Attach the parent to its child */
643 p_this->p_parent = p_parent;
645 /* Attach the child to its parent */
646 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
647 p_parent->i_children, p_this );
649 /* Climb up the tree to see whether we are connected with the root */
650 if( p_parent->b_attached )
652 SetAttachment( p_this, VLC_TRUE );
655 vlc_mutex_unlock( &structure_lock );
659 ****************************************************************************
660 * detach object from its parent
661 *****************************************************************************
662 * This function removes all links between an object and its parent.
663 *****************************************************************************/
664 void __vlc_object_detach( vlc_object_t *p_this )
666 if( !p_this ) return;
668 vlc_mutex_lock( &structure_lock );
669 if( !p_this->p_parent )
671 msg_Err( p_this, "object is not attached" );
672 vlc_mutex_unlock( &structure_lock );
676 /* Climb up the tree to see whether we are connected with the root */
677 if( p_this->p_parent->b_attached )
679 SetAttachment( p_this, VLC_FALSE );
682 DetachObject( p_this );
683 vlc_mutex_unlock( &structure_lock );
688 ****************************************************************************
689 * find a list typed objects and increment their refcount
690 *****************************************************************************
691 * This function recursively looks for a given object type. i_mode can be one
692 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
693 *****************************************************************************/
694 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
697 vlc_object_t **pp_current, **pp_end;
698 int i_count = 0, i_index = 0;
700 vlc_mutex_lock( &structure_lock );
702 /* Look for the objects */
703 switch( i_mode & 0x000f )
706 pp_current = p_this->p_libvlc_global->pp_objects;
707 pp_end = pp_current + p_this->p_libvlc_global->i_objects;
709 for( ; pp_current < pp_end ; pp_current++ )
711 if( (*pp_current)->b_attached
712 && (*pp_current)->i_object_type == i_type )
718 p_list = NewList( i_count );
719 pp_current = p_this->p_libvlc_global->pp_objects;
721 for( ; pp_current < pp_end ; pp_current++ )
723 if( (*pp_current)->b_attached
724 && (*pp_current)->i_object_type == i_type )
726 ListReplace( p_list, *pp_current, i_index );
727 if( i_index < i_count ) i_index++;
733 i_count = CountChildren( p_this, i_type );
734 p_list = NewList( i_count );
736 /* Check allocation was successful */
737 if( p_list->i_count != i_count )
739 msg_Err( p_this, "list allocation failed!" );
745 ListChildren( p_list, p_this, i_type );
749 msg_Err( p_this, "unimplemented!" );
750 p_list = NewList( 0 );
754 vlc_mutex_unlock( &structure_lock );
759 /*****************************************************************************
760 * DumpCommand: print the current vlc structure
761 *****************************************************************************
762 * This function prints either an ASCII tree showing the connections between
763 * vlc objects, and additional information such as their refcount, thread ID,
764 * etc. (command "tree"), or the same data as a simple list (command "list").
765 *****************************************************************************/
766 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
767 vlc_value_t oldval, vlc_value_t newval, void *p_data )
769 (void)oldval; (void)p_data;
770 if( *psz_cmd == 'l' )
772 vlc_mutex_lock( &structure_lock );
774 vlc_object_t **pp_current, **pp_end;
776 pp_current = p_this->p_libvlc_global->pp_objects;
777 pp_end = pp_current + p_this->p_libvlc_global->i_objects;
779 for( ; pp_current < pp_end ; pp_current++ )
781 if( (*pp_current)->b_attached )
783 PrintObject( *pp_current, "" );
787 printf( " o %.8i %s (not attached)\n",
788 (*pp_current)->i_object_id,
789 (*pp_current)->psz_object_type );
793 vlc_mutex_unlock( &structure_lock );
797 vlc_object_t *p_object = NULL;
799 if( *newval.psz_string )
802 int i_id = strtol( newval.psz_string, &end, 0 );
803 if( end != newval.psz_string )
804 p_object = vlc_object_get( p_this, i_id );
807 /* try using the object's name to find it */
808 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
811 /* Look in p_libvlc's children tree */
812 p_object = vlc_object_find_name( p_libvlc,
815 vlc_object_release( p_libvlc );
819 /* If it's not in libvlc, look in libvlc_global (== p_this) */
820 p_object = vlc_object_find_name( p_this,
832 vlc_mutex_lock( &structure_lock );
834 if( *psz_cmd == 't' )
836 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
839 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
842 DumpStructure( p_object, 0, psz_foo );
844 else if( *psz_cmd == 'v' )
849 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
851 PrintObject( p_object, "" );
853 if( !p_object->i_vars )
854 printf( " `-o No variables\n" );
855 for( i = 0; i < p_object->i_vars; i++ )
857 variable_t *p_var = p_object->p_vars + i;
859 const char *psz_type = "unknown";
860 switch( p_var->i_type & VLC_VAR_TYPE )
862 #define MYCASE( type, nice ) \
863 case VLC_VAR_ ## type: \
866 MYCASE( VOID, "void" );
867 MYCASE( BOOL, "bool" );
868 MYCASE( INTEGER, "integer" );
869 MYCASE( HOTKEY, "hotkey" );
870 MYCASE( STRING, "string" );
871 MYCASE( MODULE, "module" );
872 MYCASE( FILE, "file" );
873 MYCASE( DIRECTORY, "directory" );
874 MYCASE( VARIABLE, "variable" );
875 MYCASE( FLOAT, "float" );
876 MYCASE( TIME, "time" );
877 MYCASE( ADDRESS, "address" );
878 MYCASE( MUTEX, "mutex" );
879 MYCASE( LIST, "list" );
882 printf( " %c-o \"%s\" (%s",
883 i + 1 == p_object->i_vars ? '`' : '|',
884 p_var->psz_name, psz_type );
885 if( p_var->psz_text )
886 printf( ", %s", p_var->psz_text );
888 if( p_var->i_type & VLC_VAR_ISCOMMAND )
889 printf( ", command" );
890 if( p_var->i_entries )
891 printf( ", %d callbacks", p_var->i_entries );
892 switch( p_var->i_type & 0x00f0 )
898 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
900 case VLC_VAR_INTEGER:
901 printf( ": %d", p_var->val.i_int );
904 printf( ": \"%s\"", p_var->val.psz_string );
907 printf( ": %f", p_var->val.f_float );
910 printf( ": " I64Fi, p_var->val.i_time );
912 case VLC_VAR_ADDRESS:
913 printf( ": %p", p_var->val.p_address );
923 vlc_mutex_unlock( &structure_lock );
925 if( *newval.psz_string )
927 vlc_object_release( p_object );
934 /*****************************************************************************
935 * vlc_list_release: free a list previously allocated by vlc_list_find
936 *****************************************************************************
937 * This function decreases the refcount of all objects in the list and
939 *****************************************************************************/
940 void vlc_list_release( vlc_list_t *p_list )
944 vlc_mutex_lock( &structure_lock );
945 for( i_index = 0; i_index < p_list->i_count; i_index++ )
947 p_list->p_values[i_index].p_object->i_refcount--;
949 vlc_mutex_unlock( &structure_lock );
951 free( p_list->p_values );
955 /* Following functions are local */
957 /*****************************************************************************
958 * FindIndex: find the index of an object in an array of objects
959 *****************************************************************************
960 * This function assumes that p_this can be found in pp_objects. It will not
961 * crash if p_this cannot be found, but will return a wrong value. It is your
962 * duty to check the return value if you are not certain that the object could
964 *****************************************************************************/
965 static int FindIndex( vlc_object_t *p_this,
966 vlc_object_t **pp_objects, int i_count )
968 int i_middle = i_count / 2;
975 if( pp_objects[i_middle] == p_this )
985 /* We take advantage of the sorted array */
986 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
988 return i_middle + FindIndex( p_this, pp_objects + i_middle,
989 i_count - i_middle );
993 return FindIndex( p_this, pp_objects, i_middle );
997 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1000 vlc_object_t *p_tmp;
1002 switch( i_mode & 0x000f )
1005 p_tmp = p_this->p_parent;
1008 if( p_tmp->i_object_type == i_type )
1010 p_tmp->i_refcount++;
1015 return FindObject( p_tmp, i_type, i_mode );
1021 for( i = p_this->i_children; i--; )
1023 p_tmp = p_this->pp_children[i];
1024 if( p_tmp->i_object_type == i_type )
1026 p_tmp->i_refcount++;
1029 else if( p_tmp->i_children )
1031 p_tmp = FindObject( p_tmp, i_type, i_mode );
1041 /* Handled in vlc_object_find */
1048 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1049 const char *psz_name,
1053 vlc_object_t *p_tmp;
1055 switch( i_mode & 0x000f )
1058 p_tmp = p_this->p_parent;
1061 if( p_tmp->psz_object_name
1062 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1064 p_tmp->i_refcount++;
1069 return FindObjectName( p_tmp, psz_name, i_mode );
1075 for( i = p_this->i_children; i--; )
1077 p_tmp = p_this->pp_children[i];
1078 if( p_tmp->psz_object_name
1079 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1081 p_tmp->i_refcount++;
1084 else if( p_tmp->i_children )
1086 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1096 /* Handled in vlc_object_find */
1103 static void DetachObject( vlc_object_t *p_this )
1105 vlc_object_t *p_parent = p_this->p_parent;
1108 /* Remove p_this's parent */
1109 p_this->p_parent = NULL;
1111 /* Remove all of p_parent's children which are p_this */
1112 for( i_index = p_parent->i_children ; i_index-- ; )
1114 if( p_parent->pp_children[i_index] == p_this )
1116 p_parent->i_children--;
1117 for( i = i_index ; i < p_parent->i_children ; i++ )
1119 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1124 if( p_parent->i_children )
1126 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1127 p_parent->i_children * sizeof(vlc_object_t *) );
1131 free( p_parent->pp_children );
1132 p_parent->pp_children = NULL;
1136 /*****************************************************************************
1137 * SetAttachment: recursively set the b_attached flag of a subtree.
1138 *****************************************************************************
1139 * This function is used by the attach and detach functions to propagate
1140 * the b_attached flag in a subtree.
1141 *****************************************************************************/
1142 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1146 for( i_index = p_this->i_children ; i_index-- ; )
1148 SetAttachment( p_this->pp_children[i_index], b_attached );
1151 p_this->b_attached = b_attached;
1154 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1156 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1160 if( p_this->psz_object_name )
1162 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1164 psz_name[48] = '\"';
1167 psz_children[0] = '\0';
1168 switch( p_this->i_children )
1173 strcpy( psz_children, ", 1 child" );
1176 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1180 psz_refcount[0] = '\0';
1181 if( p_this->i_refcount )
1182 snprintf( psz_refcount, 19, ", refcount %i", p_this->i_refcount );
1184 psz_thread[0] = '\0';
1185 if( p_this->b_thread )
1186 snprintf( psz_thread, 29, " (thread %d)", (int)p_this->thread_id );
1188 psz_parent[0] = '\0';
1189 if( p_this->p_parent )
1190 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1192 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1193 p_this->i_object_id, p_this->psz_object_type,
1194 psz_name, psz_thread, psz_refcount, psz_children,
1198 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1201 char i_back = psz_foo[i_level];
1202 psz_foo[i_level] = '\0';
1204 PrintObject( p_this, psz_foo );
1206 psz_foo[i_level] = i_back;
1208 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1210 msg_Warn( p_this, "structure tree is too deep" );
1214 for( i = 0 ; i < p_this->i_children ; i++ )
1218 psz_foo[i_level-1] = ' ';
1220 if( psz_foo[i_level-2] == '`' )
1222 psz_foo[i_level-2] = ' ';
1226 if( i == p_this->i_children - 1 )
1228 psz_foo[i_level] = '`';
1232 psz_foo[i_level] = '|';
1235 psz_foo[i_level+1] = '-';
1236 psz_foo[i_level+2] = '\0';
1238 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1242 static vlc_list_t * NewList( int i_count )
1244 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1245 if( p_list == NULL )
1250 p_list->i_count = i_count;
1254 p_list->p_values = NULL;
1258 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1259 if( p_list->p_values == NULL )
1261 p_list->i_count = 0;
1268 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1271 if( p_list == NULL || i_index >= p_list->i_count )
1276 p_object->i_refcount++;
1278 p_list->p_values[i_index].p_object = p_object;
1283 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1285 if( p_list == NULL )
1290 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1291 * sizeof( vlc_value_t ) );
1292 if( p_list->p_values == NULL )
1294 p_list->i_count = 0;
1298 p_object->i_refcount++;
1300 p_list->p_values[p_list->i_count].p_object = p_object;
1306 static int CountChildren( vlc_object_t *p_this, int i_type )
1308 vlc_object_t *p_tmp;
1311 for( i = 0; i < p_this->i_children; i++ )
1313 p_tmp = p_this->pp_children[i];
1315 if( p_tmp->i_object_type == i_type )
1320 if( p_tmp->i_children )
1322 i_count += CountChildren( p_tmp, i_type );
1329 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1331 vlc_object_t *p_tmp;
1334 for( i = 0; i < p_this->i_children; i++ )
1336 p_tmp = p_this->pp_children[i];
1338 if( p_tmp->i_object_type == i_type )
1340 ListReplace( p_list, p_tmp, p_list->i_count++ );
1343 if( p_tmp->i_children )
1345 ListChildren( p_list, p_tmp, i_type );