1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: objects.c,v 1.31 2002/12/07 15:25:27 gbazin Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 # include <stdlib.h> /* realloc() */
33 #include "stream_control.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-dec.h"
38 #include "video_output.h"
40 #include "audio_output.h"
41 #include "aout_internal.h"
42 #include "stream_output.h"
44 #include "vlc_playlist.h"
45 #include "interface.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 static int DumpCommand( vlc_object_t *, char const *,
51 vlc_value_t, vlc_value_t, void * );
53 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
54 static void DetachObject ( vlc_object_t * );
55 static void PrintObject ( vlc_object_t *, const char * );
56 static void DumpStructure ( vlc_object_t *, int, char * );
57 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
58 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
60 static vlc_list_t * NewList ( void );
61 static vlc_list_t * ListAppend ( vlc_list_t *, vlc_object_t * );
63 /*****************************************************************************
64 * Local structure lock
65 *****************************************************************************/
66 static vlc_mutex_t structure_lock;
68 /*****************************************************************************
69 * vlc_object_create: initialize a vlc object
70 *****************************************************************************
71 * This function allocates memory for a vlc object and initializes it. If
72 * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
73 * so on, vlc_object_create will use its value for the object size.
74 *****************************************************************************/
75 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
84 i_size = sizeof(libvlc_t);
88 i_size = sizeof(vlc_t);
91 case VLC_OBJECT_MODULE:
92 i_size = sizeof(module_t);
96 i_size = sizeof(intf_thread_t);
97 psz_type = "interface";
99 case VLC_OBJECT_PLAYLIST:
100 i_size = sizeof(playlist_t);
101 psz_type = "playlist";
103 case VLC_OBJECT_INPUT:
104 i_size = sizeof(input_thread_t);
107 case VLC_OBJECT_DECODER:
108 i_size = sizeof(decoder_fifo_t);
109 psz_type = "decoder";
111 case VLC_OBJECT_VOUT:
112 i_size = sizeof(vout_thread_t);
113 psz_type = "video output";
115 case VLC_OBJECT_AOUT:
116 i_size = sizeof(aout_instance_t);
117 psz_type = "audio output";
119 case VLC_OBJECT_SOUT:
120 i_size = sizeof(sout_instance_t);
121 psz_type = "stream output";
125 ? i_type > (int)sizeof(vlc_object_t)
126 ? i_type : (int)sizeof(vlc_object_t)
127 : (int)sizeof(vlc_object_t);
128 i_type = VLC_OBJECT_GENERIC;
129 psz_type = "generic";
133 if( i_type == VLC_OBJECT_ROOT )
139 p_new = malloc( i_size );
146 memset( p_new, 0, i_size );
149 p_new->i_object_type = i_type;
150 p_new->psz_object_type = psz_type;
152 p_new->psz_object_name = NULL;
154 p_new->b_die = VLC_FALSE;
155 p_new->b_error = VLC_FALSE;
156 p_new->b_dead = VLC_FALSE;
157 p_new->b_attached = VLC_FALSE;
160 p_new->p_vars = (variable_t *)malloc( 16 * sizeof( variable_t ) );
168 if( i_type == VLC_OBJECT_ROOT )
170 /* If i_type is root, then p_new is actually p_libvlc */
171 p_new->p_libvlc = (libvlc_t*)p_new;
174 p_new->p_libvlc->i_counter = 0;
175 p_new->i_object_id = 0;
177 p_new->p_libvlc->i_objects = 1;
178 p_new->p_libvlc->pp_objects = malloc( sizeof(vlc_object_t *) );
179 p_new->p_libvlc->pp_objects[0] = p_new;
180 p_new->b_attached = VLC_TRUE;
184 p_new->p_libvlc = p_this->p_libvlc;
185 p_new->p_vlc = ( i_type == VLC_OBJECT_VLC ) ? (vlc_t*)p_new
188 vlc_mutex_lock( &structure_lock );
190 p_new->p_libvlc->i_counter++;
191 p_new->i_object_id = p_new->p_libvlc->i_counter;
193 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
194 * useless to try and recover anything if pp_objects gets smashed. */
195 INSERT_ELEM( p_new->p_libvlc->pp_objects,
196 p_new->p_libvlc->i_objects,
197 p_new->p_libvlc->i_objects,
200 vlc_mutex_unlock( &structure_lock );
203 p_new->i_refcount = 0;
204 p_new->p_parent = NULL;
205 p_new->pp_children = NULL;
206 p_new->i_children = 0;
208 p_new->p_private = NULL;
210 /* Initialize mutexes and condvars */
211 vlc_mutex_init( p_new, &p_new->object_lock );
212 vlc_cond_init( p_new, &p_new->object_wait );
213 vlc_mutex_init( p_new, &p_new->var_lock );
215 if( i_type == VLC_OBJECT_ROOT )
217 vlc_mutex_init( p_new, &structure_lock );
219 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
220 var_AddCallback( p_new, "list", DumpCommand, NULL );
221 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
222 var_AddCallback( p_new, "tree", DumpCommand, NULL );
228 /*****************************************************************************
229 * vlc_object_destroy: destroy a vlc object
230 *****************************************************************************
231 * This function destroys an object that has been previously allocated with
232 * vlc_object_create. The object's refcount must be zero and it must not be
233 * attached to other objects in any way.
234 *****************************************************************************/
235 void __vlc_object_destroy( vlc_object_t *p_this )
239 if( p_this->i_children )
241 msg_Err( p_this, "cannot delete object with children" );
245 if( p_this->p_parent )
247 msg_Err( p_this, "cannot delete object with a parent" );
251 while( p_this->i_refcount )
255 /* Don't warn immediately ... 100ms seems OK */
258 msg_Warn( p_this, "refcount is %i, delaying before deletion",
259 p_this->i_refcount );
261 else if( i_delay == 12 )
263 msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
264 p_this->i_refcount );
266 else if( i_delay == 42 )
268 msg_Err( p_this, "we waited too long, cancelling destruction" );
275 /* Destroy the associated variables, starting from the end so that
276 * no memmove calls have to be done. */
277 while( p_this->i_vars )
279 var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
282 free( p_this->p_vars );
283 vlc_mutex_destroy( &p_this->var_lock );
285 if( p_this->i_object_type == VLC_OBJECT_ROOT )
287 /* We are the root object ... no need to lock. */
288 free( p_this->p_libvlc->pp_objects );
289 p_this->p_libvlc->pp_objects = NULL;
290 p_this->p_libvlc->i_objects--;
292 vlc_mutex_destroy( &structure_lock );
298 vlc_mutex_lock( &structure_lock );
300 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
301 * useless to try and recover anything if pp_objects gets smashed. */
302 i_index = FindIndex( p_this, p_this->p_libvlc->pp_objects,
303 p_this->p_libvlc->i_objects );
304 REMOVE_ELEM( p_this->p_libvlc->pp_objects,
305 p_this->p_libvlc->i_objects, i_index );
307 vlc_mutex_unlock( &structure_lock );
310 vlc_mutex_destroy( &p_this->object_lock );
311 vlc_cond_destroy( &p_this->object_wait );
316 /*****************************************************************************
317 * vlc_object_get: find an object given its ID
318 *****************************************************************************
319 * This function looks for the object whose i_object_id field is i_id. We
320 * use a dichotomy so that lookups are in log2(n).
321 *****************************************************************************/
322 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
325 vlc_object_t **pp_objects;
327 vlc_mutex_lock( &structure_lock );
329 pp_objects = p_this->p_libvlc->pp_objects;
331 /* Perform our dichotomy */
332 for( i_max = p_this->p_libvlc->i_objects - 1 ; ; )
334 i_middle = i_max / 2;
336 if( pp_objects[i_middle]->i_object_id > i_id )
340 else if( pp_objects[i_middle]->i_object_id < i_id )
344 pp_objects += i_middle;
349 /* This happens when there are only two remaining objects */
350 if( pp_objects[i_middle+1]->i_object_id == i_id )
352 vlc_mutex_unlock( &structure_lock );
353 return pp_objects[i_middle+1];
360 vlc_mutex_unlock( &structure_lock );
361 return pp_objects[i_middle];
366 /* this means that i_max == i_middle, and since we have already
367 * tested pp_objects[i_middle]), p_found is properly set. */
372 vlc_mutex_unlock( &structure_lock );
376 /*****************************************************************************
377 * vlc_object_find: find a typed object and increment its refcount
378 *****************************************************************************
379 * This function recursively looks for a given object type. i_mode can be one
380 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
381 *****************************************************************************/
382 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
384 vlc_object_t *p_found;
386 vlc_mutex_lock( &structure_lock );
388 /* If we are of the requested type ourselves, don't look further */
389 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
391 p_this->i_refcount++;
392 vlc_mutex_unlock( &structure_lock );
396 /* Otherwise, recursively look for the object */
397 if( (i_mode & 0x000f) == FIND_ANYWHERE )
399 p_found = FindObject( VLC_OBJECT(p_this->p_vlc), i_type,
400 (i_mode & ~0x000f) | FIND_CHILD );
404 p_found = FindObject( p_this, i_type, i_mode );
407 vlc_mutex_unlock( &structure_lock );
412 /*****************************************************************************
413 * vlc_object_yield: increment an object refcount
414 *****************************************************************************/
415 void __vlc_object_yield( vlc_object_t *p_this )
417 vlc_mutex_lock( &structure_lock );
418 p_this->i_refcount++;
419 vlc_mutex_unlock( &structure_lock );
422 /*****************************************************************************
423 * vlc_object_release: decrement an object refcount
424 *****************************************************************************/
425 void __vlc_object_release( vlc_object_t *p_this )
427 vlc_mutex_lock( &structure_lock );
428 p_this->i_refcount--;
429 vlc_mutex_unlock( &structure_lock );
432 /*****************************************************************************
433 * vlc_object_attach: attach object to a parent object
434 *****************************************************************************
435 * This function sets p_this as a child of p_parent, and p_parent as a parent
436 * of p_this. This link can be undone using vlc_object_detach.
437 *****************************************************************************/
438 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
440 vlc_mutex_lock( &structure_lock );
442 /* Attach the parent to its child */
443 p_this->p_parent = p_parent;
445 /* Attach the child to its parent */
446 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
447 p_parent->i_children, p_this );
449 /* Climb up the tree to see whether we are connected with the root */
450 if( p_parent->b_attached )
452 SetAttachment( p_this, VLC_TRUE );
455 vlc_mutex_unlock( &structure_lock );
458 /*****************************************************************************
459 * vlc_object_detach: detach object from its parent
460 *****************************************************************************
461 * This function removes all links between an object and its parent.
462 *****************************************************************************/
463 void __vlc_object_detach( vlc_object_t *p_this )
465 vlc_mutex_lock( &structure_lock );
466 if( !p_this->p_parent )
468 msg_Err( p_this, "object is not attached" );
469 vlc_mutex_unlock( &structure_lock );
473 /* Climb up the tree to see whether we are connected with the root */
474 if( p_this->p_parent->b_attached )
476 SetAttachment( p_this, VLC_FALSE );
479 DetachObject( p_this );
480 vlc_mutex_unlock( &structure_lock );
483 /*****************************************************************************
484 * vlc_list_find: find a list typed objects and increment their 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 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
491 vlc_list_t *p_list = NewList();
493 vlc_mutex_lock( &structure_lock );
495 /* Look for the objects */
496 if( (i_mode & 0x000f) == FIND_ANYWHERE )
498 vlc_object_t **pp_current, **pp_end;
500 pp_current = p_this->p_libvlc->pp_objects;
501 pp_end = pp_current + p_this->p_libvlc->i_objects;
503 for( ; pp_current < pp_end ; pp_current++ )
505 if( (*pp_current)->b_attached
506 && (*pp_current)->i_object_type == i_type )
508 p_list = ListAppend( p_list, *pp_current );
514 msg_Err( p_this, "unimplemented!" );
517 vlc_mutex_unlock( &structure_lock );
522 /*****************************************************************************
523 * DumpCommand: print the current vlc structure
524 *****************************************************************************
525 * This function prints either an ASCII tree showing the connections between
526 * vlc objects, and additional information such as their refcount, thread ID,
527 * etc. (command "tree"), or the same data as a simple list (command "list").
528 *****************************************************************************/
529 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
530 vlc_value_t oldval, vlc_value_t newval, void *p_data )
532 if( *psz_cmd == 't' )
534 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
535 vlc_object_t *p_object;
537 if( *newval.psz_string )
539 p_object = vlc_object_get( p_this, atoi(newval.psz_string) );
548 p_object = p_this->p_vlc ? VLC_OBJECT(p_this->p_vlc) : p_this;
551 vlc_mutex_lock( &structure_lock );
554 DumpStructure( p_object, 0, psz_foo );
556 vlc_mutex_unlock( &structure_lock );
558 else if( *psz_cmd == 'l' )
560 vlc_object_t **pp_current, **pp_end;
562 vlc_mutex_lock( &structure_lock );
564 pp_current = p_this->p_libvlc->pp_objects;
565 pp_end = pp_current + p_this->p_libvlc->i_objects;
567 for( ; pp_current < pp_end ; pp_current++ )
569 if( (*pp_current)->b_attached )
571 PrintObject( *pp_current, "" );
575 printf( " o %.8i %s (not attached)\n",
576 (*pp_current)->i_object_id,
577 (*pp_current)->psz_object_type );
581 vlc_mutex_unlock( &structure_lock );
587 /*****************************************************************************
588 * vlc_list_release: free a list previously allocated by vlc_list_find
589 *****************************************************************************
590 * This function decreases the refcount of all objects in the list and
592 *****************************************************************************/
593 void vlc_list_release( vlc_list_t *p_list )
595 if( p_list->i_count )
597 vlc_object_t ** pp_current = p_list->pp_objects;
599 vlc_mutex_lock( &structure_lock );
601 while( pp_current[0] )
603 pp_current[0]->i_refcount--;
607 vlc_mutex_unlock( &structure_lock );
613 /* Following functions are local */
615 /*****************************************************************************
616 * FindIndex: find the index of an object in an array of objects
617 *****************************************************************************
618 * This function assumes that p_this can be found in pp_objects. It will not
619 * crash if p_this cannot be found, but will return a wrong value. It is your
620 * duty to check the return value if you are not certain that the object could
622 *****************************************************************************/
623 static int FindIndex( vlc_object_t *p_this,
624 vlc_object_t **pp_objects, int i_count )
626 int i_middle = i_count / 2;
633 if( pp_objects[i_middle] == p_this )
643 /* We take advantage of the sorted array */
644 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
646 return i_middle + FindIndex( p_this, pp_objects + i_middle,
647 i_count - i_middle );
651 return FindIndex( p_this, pp_objects, i_middle );
655 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
660 switch( i_mode & 0x000f )
663 p_tmp = p_this->p_parent;
666 if( p_tmp->i_object_type == i_type )
673 return FindObject( p_tmp, i_type, i_mode );
679 for( i = p_this->i_children; i--; )
681 p_tmp = p_this->pp_children[i];
682 if( p_tmp->i_object_type == i_type )
687 else if( p_tmp->i_children )
689 p_tmp = FindObject( p_tmp, i_type, i_mode );
699 /* Handled in vlc_object_find */
706 static void DetachObject( vlc_object_t *p_this )
708 vlc_object_t *p_parent = p_this->p_parent;
711 /* Remove p_this's parent */
712 p_this->p_parent = NULL;
714 /* Remove all of p_parent's children which are p_this */
715 for( i_index = p_parent->i_children ; i_index-- ; )
717 if( p_parent->pp_children[i_index] == p_this )
719 p_parent->i_children--;
720 for( i = i_index ; i < p_parent->i_children ; i++ )
722 p_parent->pp_children[i] = p_parent->pp_children[i+1];
727 if( p_parent->i_children )
729 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
730 p_parent->i_children * sizeof(vlc_object_t *) );
734 free( p_parent->pp_children );
735 p_parent->pp_children = NULL;
739 /*****************************************************************************
740 * SetAttachment: recursively set the b_attached flag of a subtree.
741 *****************************************************************************
742 * This function is used by the attach and detach functions to propagate
743 * the b_attached flag in a subtree.
744 *****************************************************************************/
745 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
749 for( i_index = p_this->i_children ; i_index-- ; )
751 SetAttachment( p_this->pp_children[i_index], b_attached );
754 p_this->b_attached = b_attached;
757 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
759 char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
762 if( p_this->psz_object_name )
764 snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
769 psz_children[0] = '\0';
770 switch( p_this->i_children )
775 strcpy( psz_children, ", 1 child" );
778 snprintf( psz_children, 20,
779 ", %i children", p_this->i_children );
780 psz_children[19] = '\0';
784 psz_refcount[0] = '\0';
785 if( p_this->i_refcount )
787 snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
788 psz_refcount[19] = '\0';
791 psz_thread[0] = '\0';
792 if( p_this->b_thread )
794 snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
795 psz_thread[19] = '\0';
798 printf( " %so %.8i %s%s%s%s%s\n", psz_prefix,
799 p_this->i_object_id, p_this->psz_object_type,
800 psz_name, psz_thread, psz_refcount, psz_children );
803 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
806 char i_back = psz_foo[i_level];
807 psz_foo[i_level] = '\0';
809 PrintObject( p_this, psz_foo );
811 psz_foo[i_level] = i_back;
813 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
815 msg_Warn( p_this, "structure tree is too deep" );
819 for( i = 0 ; i < p_this->i_children ; i++ )
823 psz_foo[i_level-1] = ' ';
825 if( psz_foo[i_level-2] == '`' )
827 psz_foo[i_level-2] = ' ';
831 if( i == p_this->i_children - 1 )
833 psz_foo[i_level] = '`';
837 psz_foo[i_level] = '|';
840 psz_foo[i_level+1] = '-';
841 psz_foo[i_level+2] = '\0';
843 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
847 static vlc_list_t * NewList( void )
849 vlc_list_t *p_list = malloc( sizeof( vlc_list_t )
850 + 3 * sizeof( vlc_object_t * ) );
858 p_list->pp_objects = &p_list->_p_first;
860 /* We allocated space for NULL and for three extra objects */
861 p_list->_i_extra = 3;
862 p_list->_p_first = NULL;
867 static vlc_list_t * ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
874 if( p_list->_i_extra == 0 )
876 /* If we had X objects it means the array has a size of X+1, we
877 * make it size 2X+2, so we alloc 2X+1 because there is already
878 * one allocated in the real structure */
879 p_list = realloc( p_list, sizeof( vlc_list_t )
880 + (p_list->i_count * 2 + 1)
881 * sizeof( vlc_object_t * ) );
887 /* We have X+1 extra slots */
888 p_list->_i_extra = p_list->i_count + 1;
889 p_list->pp_objects = &p_list->_p_first;
892 p_object->i_refcount++;
894 p_list->pp_objects[p_list->i_count] = p_object;
896 p_list->pp_objects[p_list->i_count] = NULL;