1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: objects.c,v 1.24 2002/10/11 22:32:56 sam 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"
43 #include "vlc_playlist.h"
44 #include "interface.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
50 static void DetachObject ( vlc_object_t * );
51 static void PrintObject ( vlc_object_t *, const char * );
52 static void DumpStructure ( vlc_object_t *, int, char * );
53 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
54 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
56 static vlc_list_t * NewList ( void );
57 static vlc_list_t * ListAppend ( vlc_list_t *, vlc_object_t * );
59 /*****************************************************************************
60 * Local structure lock
61 *****************************************************************************/
62 static vlc_mutex_t structure_lock;
64 /*****************************************************************************
65 * vlc_object_create: initialize a vlc object
66 *****************************************************************************
67 * This function allocates memory for a vlc object and initializes it. If
68 * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
69 * so on, vlc_object_create will use its value for the object size.
70 *****************************************************************************/
71 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
80 i_size = sizeof(libvlc_t);
84 i_size = sizeof(vlc_t);
87 case VLC_OBJECT_MODULE:
88 i_size = sizeof(module_t);
92 i_size = sizeof(intf_thread_t);
93 psz_type = "interface";
95 case VLC_OBJECT_PLAYLIST:
96 i_size = sizeof(playlist_t);
97 psz_type = "playlist";
99 case VLC_OBJECT_INPUT:
100 i_size = sizeof(input_thread_t);
103 case VLC_OBJECT_DECODER:
104 i_size = sizeof(decoder_fifo_t);
105 psz_type = "decoder";
107 case VLC_OBJECT_VOUT:
108 i_size = sizeof(vout_thread_t);
109 psz_type = "video output";
111 case VLC_OBJECT_AOUT:
112 i_size = sizeof(aout_instance_t);
113 psz_type = "audio output";
116 i_size = i_type > sizeof(vlc_object_t)
117 ? i_type : sizeof(vlc_object_t);
118 i_type = VLC_OBJECT_GENERIC;
119 psz_type = "generic";
123 if( i_type == VLC_OBJECT_ROOT )
129 p_new = malloc( i_size );
136 memset( p_new, 0, i_size );
139 p_new->i_object_type = i_type;
140 p_new->psz_object_type = psz_type;
142 p_new->psz_object_name = NULL;
144 p_new->b_die = VLC_FALSE;
145 p_new->b_error = VLC_FALSE;
146 p_new->b_dead = VLC_FALSE;
147 p_new->b_attached = VLC_FALSE;
150 p_new->p_vars = (variable_t *)malloc( 16 * sizeof( variable_t ) );
158 if( i_type == VLC_OBJECT_ROOT )
160 /* If i_type is root, then p_new is actually p_libvlc */
161 p_new->p_libvlc = (libvlc_t*)p_new;
164 p_new->p_libvlc->i_counter = 0;
165 p_new->i_object_id = 0;
167 p_new->p_libvlc->i_objects = 1;
168 p_new->p_libvlc->pp_objects = malloc( sizeof(vlc_object_t *) );
169 p_new->p_libvlc->pp_objects[0] = p_new;
170 p_new->b_attached = VLC_TRUE;
174 p_new->p_libvlc = p_this->p_libvlc;
175 p_new->p_vlc = ( i_type == VLC_OBJECT_VLC ) ? (vlc_t*)p_new
178 vlc_mutex_lock( &structure_lock );
180 p_new->p_libvlc->i_counter++;
181 p_new->i_object_id = p_new->p_libvlc->i_counter;
183 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
184 * useless to try and recover anything if pp_objects gets smashed. */
185 p_new->p_libvlc->i_objects++;
186 p_new->p_libvlc->pp_objects =
187 realloc( p_new->p_libvlc->pp_objects,
188 p_new->p_libvlc->i_objects * sizeof(vlc_object_t *) );
189 p_new->p_libvlc->pp_objects[ p_new->p_libvlc->i_objects - 1 ] = p_new;
191 vlc_mutex_unlock( &structure_lock );
194 p_new->i_refcount = 0;
195 p_new->p_parent = NULL;
196 p_new->pp_children = NULL;
197 p_new->i_children = 0;
199 p_new->p_private = NULL;
201 /* Initialize mutexes and condvars */
202 vlc_mutex_init( p_new, &p_new->object_lock );
203 vlc_cond_init( p_new, &p_new->object_wait );
204 vlc_mutex_init( p_new, &p_new->var_lock );
206 if( i_type == VLC_OBJECT_ROOT )
208 vlc_mutex_init( p_new, &structure_lock );
214 /*****************************************************************************
215 * vlc_object_destroy: destroy a vlc object
216 *****************************************************************************
217 * This function destroys an object that has been previously allocated with
218 * vlc_object_create. The object's refcount must be zero and it must not be
219 * attached to other objects in any way.
220 *****************************************************************************/
221 void __vlc_object_destroy( vlc_object_t *p_this )
225 if( p_this->i_children )
227 msg_Err( p_this, "cannot delete object with children" );
228 vlc_dumpstructure( p_this );
232 if( p_this->p_parent )
234 msg_Err( p_this, "cannot delete object with a parent" );
235 vlc_dumpstructure( p_this );
239 while( p_this->i_refcount )
243 /* Don't warn immediately ... 100ms seems OK */
246 msg_Warn( p_this, "refcount is %i, delaying before deletion",
247 p_this->i_refcount );
249 else if( i_delay == 12 )
251 msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
252 p_this->i_refcount );
254 else if( i_delay == 42 )
256 msg_Err( p_this, "we waited too long, cancelling destruction" );
263 /* Destroy the associated variables, starting from the end so that
264 * no memmove calls have to be done. */
265 while( p_this->i_vars )
267 var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
270 free( p_this->p_vars );
271 vlc_mutex_destroy( &p_this->var_lock );
273 if( p_this->i_object_type == VLC_OBJECT_ROOT )
275 /* We are the root object ... no need to lock. */
276 free( p_this->p_libvlc->pp_objects );
277 p_this->p_libvlc->pp_objects = NULL;
279 vlc_mutex_destroy( &structure_lock );
285 vlc_mutex_lock( &structure_lock );
287 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
288 * useless to try and recover anything if pp_objects gets smashed. */
289 i_index = FindIndex( p_this, p_this->p_libvlc->pp_objects,
290 p_this->p_libvlc->i_objects );
291 memmove( p_this->p_libvlc->pp_objects + i_index,
292 p_this->p_libvlc->pp_objects + i_index + 1,
293 (p_this->p_libvlc->i_objects - i_index - 1)
294 * sizeof( vlc_object_t *) );
296 p_this->p_libvlc->pp_objects =
297 realloc( p_this->p_libvlc->pp_objects,
298 (p_this->p_libvlc->i_objects - 1) * sizeof(vlc_object_t *) );
300 vlc_mutex_unlock( &structure_lock );
303 p_this->p_libvlc->i_objects--;
305 vlc_mutex_destroy( &p_this->object_lock );
306 vlc_cond_destroy( &p_this->object_wait );
311 /*****************************************************************************
312 * vlc_object_get: find an object given its ID
313 *****************************************************************************
314 * This function looks for the object whose i_object_id field is i_id. We
315 * use a dichotomy so that lookups are in log2(n).
316 *****************************************************************************/
317 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
320 vlc_object_t **pp_objects;
322 vlc_mutex_lock( &structure_lock );
324 pp_objects = p_this->p_libvlc->pp_objects;
326 /* Perform our dichotomy */
327 for( i_max = p_this->p_libvlc->i_objects - 1 ; ; )
329 i_middle = i_max / 2;
331 if( pp_objects[i_middle]->i_object_id > i_id )
335 else if( pp_objects[i_middle]->i_object_id < i_id )
339 pp_objects += i_middle;
344 /* This happens when there are only two remaining objects */
345 if( pp_objects[i_middle+1]->i_object_id == i_id )
347 vlc_mutex_unlock( &structure_lock );
348 return pp_objects[i_middle+1];
355 vlc_mutex_unlock( &structure_lock );
356 return pp_objects[i_middle];
361 /* this means that i_max == i_middle, and since we have already
362 * tested pp_objects[i_middle]), p_found is properly set. */
367 vlc_mutex_unlock( &structure_lock );
371 /*****************************************************************************
372 * vlc_object_find: find a typed object and increment its refcount
373 *****************************************************************************
374 * This function recursively looks for a given object type. i_mode can be one
375 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
376 *****************************************************************************/
377 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
379 vlc_object_t *p_found;
381 vlc_mutex_lock( &structure_lock );
383 /* If we are of the requested type ourselves, don't look further */
384 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
386 p_this->i_refcount++;
387 vlc_mutex_unlock( &structure_lock );
391 /* Otherwise, recursively look for the object */
392 if( (i_mode & 0x000f) == FIND_ANYWHERE )
394 p_found = FindObject( VLC_OBJECT(p_this->p_vlc), i_type,
395 (i_mode & ~0x000f) | FIND_CHILD );
399 p_found = FindObject( p_this, i_type, i_mode );
402 vlc_mutex_unlock( &structure_lock );
407 /*****************************************************************************
408 * vlc_object_yield: increment an object refcount
409 *****************************************************************************/
410 void __vlc_object_yield( vlc_object_t *p_this )
412 vlc_mutex_lock( &structure_lock );
413 p_this->i_refcount++;
414 vlc_mutex_unlock( &structure_lock );
417 /*****************************************************************************
418 * vlc_object_release: decrement an object refcount
419 *****************************************************************************/
420 void __vlc_object_release( vlc_object_t *p_this )
422 vlc_mutex_lock( &structure_lock );
423 p_this->i_refcount--;
424 vlc_mutex_unlock( &structure_lock );
427 /*****************************************************************************
428 * vlc_object_attach: attach object to a parent object
429 *****************************************************************************
430 * This function sets p_this as a child of p_parent, and p_parent as a parent
431 * of p_this. This link can be undone using vlc_object_detach.
432 *****************************************************************************/
433 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
435 vlc_mutex_lock( &structure_lock );
437 /* Attach the parent to its child */
438 p_this->p_parent = p_parent;
440 /* Attach the child to its parent */
441 p_parent->i_children++;
442 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
443 p_parent->i_children * sizeof(vlc_object_t *) );
444 p_parent->pp_children[p_parent->i_children - 1] = p_this;
446 /* Climb up the tree to see whether we are connected with the root */
447 if( p_parent->b_attached )
449 SetAttachment( p_this, VLC_TRUE );
452 vlc_mutex_unlock( &structure_lock );
455 /*****************************************************************************
456 * vlc_object_detach: detach object from its parent
457 *****************************************************************************
458 * This function removes all links between an object and its parent.
459 *****************************************************************************/
460 void __vlc_object_detach( vlc_object_t *p_this )
462 vlc_mutex_lock( &structure_lock );
463 if( !p_this->p_parent )
465 msg_Err( p_this, "object is not attached" );
466 vlc_mutex_unlock( &structure_lock );
470 /* Climb up the tree to see whether we are connected with the root */
471 if( p_this->p_parent->b_attached )
473 SetAttachment( p_this, VLC_FALSE );
476 DetachObject( p_this );
477 vlc_mutex_unlock( &structure_lock );
480 /*****************************************************************************
481 * vlc_list_find: find a list typed objects and increment their refcount
482 *****************************************************************************
483 * This function recursively looks for a given object type. i_mode can be one
484 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
485 *****************************************************************************/
486 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
488 vlc_list_t *p_list = NewList();
490 vlc_mutex_lock( &structure_lock );
492 /* Look for the objects */
493 if( (i_mode & 0x000f) == FIND_ANYWHERE )
495 vlc_object_t **pp_current, **pp_end;
497 pp_current = p_this->p_libvlc->pp_objects;
498 pp_end = pp_current + p_this->p_libvlc->i_objects;
500 for( ; pp_current < pp_end ; pp_current++ )
502 if( (*pp_current)->b_attached
503 && (*pp_current)->i_object_type == i_type )
505 p_list = ListAppend( p_list, *pp_current );
511 msg_Err( p_this, "unimplemented!" );
514 vlc_mutex_unlock( &structure_lock );
519 /*****************************************************************************
520 * vlc_liststructure: print the current vlc objects
521 *****************************************************************************
522 * This function prints alist of vlc objects, and additional information such
523 * as their refcount, thread ID, etc.
524 *****************************************************************************/
525 void __vlc_liststructure( vlc_object_t *p_this )
527 vlc_object_t **pp_current, **pp_end;
529 vlc_mutex_lock( &structure_lock );
531 pp_current = p_this->p_libvlc->pp_objects;
532 pp_end = pp_current + p_this->p_libvlc->i_objects;
534 for( ; pp_current < pp_end ; pp_current++ )
536 if( (*pp_current)->b_attached )
538 PrintObject( *pp_current, "" );
542 printf( " o %.6x %s (not attached)\n",
543 (*pp_current)->i_object_id,
544 (*pp_current)->psz_object_type );
548 vlc_mutex_unlock( &structure_lock );
551 /*****************************************************************************
552 * vlc_dumpstructure: print the current vlc structure
553 *****************************************************************************
554 * This function prints an ASCII tree showing the connections between vlc
555 * objects, and additional information such as their refcount, thread ID, etc.
556 *****************************************************************************/
557 void __vlc_dumpstructure( vlc_object_t *p_this )
559 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
561 vlc_mutex_lock( &structure_lock );
563 DumpStructure( p_this, 0, psz_foo );
564 vlc_mutex_unlock( &structure_lock );
567 /*****************************************************************************
568 * vlc_list_release: free a list previously allocated by vlc_list_find
569 *****************************************************************************
570 * This function decreases the refcount of all objects in the list and
572 *****************************************************************************/
573 void vlc_list_release( vlc_list_t *p_list )
575 if( p_list->i_count )
577 vlc_object_t ** pp_current = p_list->pp_objects;
579 vlc_mutex_lock( &structure_lock );
581 while( pp_current[0] )
583 pp_current[0]->i_refcount--;
587 vlc_mutex_unlock( &structure_lock );
593 /* Following functions are local */
595 /*****************************************************************************
596 * FindIndex: find the index of an object in an array of objects
597 *****************************************************************************
598 * This function assumes that p_this can be found in pp_objects. It will not
599 * crash if p_this cannot be found, but will return a wrong value. It is your
600 * duty to check the return value if you are not certain that the object could
602 *****************************************************************************/
603 static int FindIndex( vlc_object_t *p_this,
604 vlc_object_t **pp_objects, int i_count )
606 int i_middle = i_count / 2;
613 if( pp_objects[i_middle] == p_this )
623 /* We take advantage of the sorted array */
624 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
626 return i_middle + FindIndex( p_this, pp_objects + i_middle,
627 i_count - i_middle );
631 return FindIndex( p_this, pp_objects, i_middle );
635 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
640 switch( i_mode & 0x000f )
643 p_tmp = p_this->p_parent;
646 if( p_tmp->i_object_type == i_type )
653 return FindObject( p_tmp, i_type, i_mode );
659 for( i = p_this->i_children; i--; )
661 p_tmp = p_this->pp_children[i];
662 if( p_tmp->i_object_type == i_type )
667 else if( p_tmp->i_children )
669 p_tmp = FindObject( p_tmp, i_type, i_mode );
679 /* Handled in vlc_object_find */
686 static void DetachObject( vlc_object_t *p_this )
688 vlc_object_t *p_parent = p_this->p_parent;
691 /* Remove p_this's parent */
692 p_this->p_parent = NULL;
694 /* Remove all of p_parent's children which are p_this */
695 for( i_index = p_parent->i_children ; i_index-- ; )
697 if( p_parent->pp_children[i_index] == p_this )
699 p_parent->i_children--;
700 for( i = i_index ; i < p_parent->i_children ; i++ )
702 p_parent->pp_children[i] = p_parent->pp_children[i+1];
707 if( p_parent->i_children )
709 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
710 p_parent->i_children * sizeof(vlc_object_t *) );
714 free( p_parent->pp_children );
715 p_parent->pp_children = NULL;
719 /*****************************************************************************
720 * SetAttachment: recursively set the b_attached flag of a subtree.
721 *****************************************************************************
722 * This function is used by the attach and detach functions to propagate
723 * the b_attached flag in a subtree.
724 *****************************************************************************/
725 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
729 for( i_index = p_this->i_children ; i_index-- ; )
731 SetAttachment( p_this->pp_children[i_index], b_attached );
734 p_this->b_attached = b_attached;
737 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
739 char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
742 if( p_this->psz_object_name )
744 snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
749 psz_children[0] = '\0';
750 switch( p_this->i_children )
755 strcpy( psz_children, ", 1 child" );
758 snprintf( psz_children, 20,
759 ", %i children", p_this->i_children );
760 psz_children[19] = '\0';
764 psz_refcount[0] = '\0';
765 if( p_this->i_refcount )
767 snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
768 psz_refcount[19] = '\0';
771 psz_thread[0] = '\0';
772 if( p_this->b_thread )
774 snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
775 psz_thread[19] = '\0';
778 printf( " %so %.6x %s%s%s%s%s\n", psz_prefix,
779 p_this->i_object_id, p_this->psz_object_type,
780 psz_name, psz_thread, psz_refcount, psz_children );
783 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
786 char i_back = psz_foo[i_level];
787 psz_foo[i_level] = '\0';
789 PrintObject( p_this, psz_foo );
791 psz_foo[i_level] = i_back;
793 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
795 msg_Warn( p_this, "structure tree is too deep" );
799 for( i = 0 ; i < p_this->i_children ; i++ )
803 psz_foo[i_level-1] = ' ';
805 if( psz_foo[i_level-2] == '`' )
807 psz_foo[i_level-2] = ' ';
811 if( i == p_this->i_children - 1 )
813 psz_foo[i_level] = '`';
817 psz_foo[i_level] = '|';
820 psz_foo[i_level+1] = '-';
821 psz_foo[i_level+2] = '\0';
823 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
827 static vlc_list_t * NewList( void )
829 vlc_list_t *p_list = malloc( sizeof( vlc_list_t )
830 + 3 * sizeof( vlc_object_t * ) );
838 p_list->pp_objects = &p_list->_p_first;
840 /* We allocated space for NULL and for three extra objects */
841 p_list->_i_extra = 3;
842 p_list->_p_first = NULL;
847 static vlc_list_t * ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
854 if( p_list->_i_extra == 0 )
856 /* If we had X objects it means the array has a size of X+1, we
857 * make it size 2X+2, so we alloc 2X+1 because there is already
858 * one allocated in the real structure */
859 p_list = realloc( p_list, sizeof( vlc_list_t )
860 + (p_list->i_count * 2 + 1)
861 * sizeof( vlc_object_t * ) );
867 /* We have X+1 extra slots */
868 p_list->_i_extra = p_list->i_count + 1;
869 p_list->pp_objects = &p_list->_p_first;
872 p_object->i_refcount++;
874 p_list->pp_objects[p_list->i_count] = p_object;
876 p_list->pp_objects[p_list->i_count] = NULL;