1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: objects.c,v 1.23 2002/10/04 18:07:22 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->i_refcount = 0;
145 p_new->b_die = VLC_FALSE;
146 p_new->b_error = VLC_FALSE;
147 p_new->b_dead = VLC_FALSE;
148 p_new->b_attached = VLC_FALSE;
150 if( i_type == VLC_OBJECT_ROOT )
152 /* If i_type is root, then p_new is actually p_libvlc */
153 p_new->p_libvlc = (libvlc_t*)p_new;
156 p_new->p_libvlc->i_counter = 0;
157 p_new->i_object_id = 0;
159 p_new->p_libvlc->i_objects = 1;
160 p_new->p_libvlc->pp_objects = malloc( sizeof(vlc_object_t *) );
161 p_new->p_libvlc->pp_objects[0] = p_new;
162 p_new->b_attached = VLC_TRUE;
166 p_new->p_libvlc = p_this->p_libvlc;
167 p_new->p_vlc = ( i_type == VLC_OBJECT_VLC ) ? (vlc_t*)p_new
170 vlc_mutex_lock( &structure_lock );
172 p_new->p_libvlc->i_counter++;
173 p_new->i_object_id = p_new->p_libvlc->i_counter;
175 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
176 * useless to try and recover anything if pp_objects gets smashed. */
177 p_new->p_libvlc->i_objects++;
178 p_new->p_libvlc->pp_objects =
179 realloc( p_new->p_libvlc->pp_objects,
180 p_new->p_libvlc->i_objects * sizeof(vlc_object_t *) );
181 p_new->p_libvlc->pp_objects[ p_new->p_libvlc->i_objects - 1 ] = p_new;
183 vlc_mutex_unlock( &structure_lock );
186 p_new->p_parent = NULL;
187 p_new->pp_children = NULL;
188 p_new->i_children = 0;
190 p_new->p_private = NULL;
192 /* Initialize mutexes and condvars */
193 vlc_mutex_init( p_new, &p_new->object_lock );
194 vlc_cond_init( p_new, &p_new->object_wait );
196 if( i_type == VLC_OBJECT_ROOT )
198 vlc_mutex_init( p_new, &structure_lock );
204 /*****************************************************************************
205 * vlc_object_destroy: destroy a vlc object
206 *****************************************************************************
207 * This function destroys an object that has been previously allocated with
208 * vlc_object_create. The object's refcount must be zero and it must not be
209 * attached to other objects in any way.
210 *****************************************************************************/
211 void __vlc_object_destroy( vlc_object_t *p_this )
215 if( p_this->i_children )
217 msg_Err( p_this, "cannot delete object with children" );
218 vlc_dumpstructure( p_this );
222 if( p_this->p_parent )
224 msg_Err( p_this, "cannot delete object with a parent" );
225 vlc_dumpstructure( p_this );
229 while( p_this->i_refcount )
233 /* Don't warn immediately ... 100ms seems OK */
236 msg_Warn( p_this, "refcount is %i, delaying before deletion",
237 p_this->i_refcount );
239 else if( i_delay == 12 )
241 msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
242 p_this->i_refcount );
244 else if( i_delay == 42 )
246 msg_Err( p_this, "we waited too long, cancelling destruction" );
253 if( p_this->i_object_type == VLC_OBJECT_ROOT )
255 /* We are the root object ... no need to lock. */
256 free( p_this->p_libvlc->pp_objects );
257 p_this->p_libvlc->pp_objects = NULL;
259 vlc_mutex_destroy( &structure_lock );
265 vlc_mutex_lock( &structure_lock );
267 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
268 * useless to try and recover anything if pp_objects gets smashed. */
269 i_index = FindIndex( p_this, p_this->p_libvlc->pp_objects,
270 p_this->p_libvlc->i_objects );
271 memmove( p_this->p_libvlc->pp_objects + i_index,
272 p_this->p_libvlc->pp_objects + i_index + 1,
273 (p_this->p_libvlc->i_objects - i_index - 1)
274 * sizeof( vlc_object_t *) );
276 p_this->p_libvlc->pp_objects =
277 realloc( p_this->p_libvlc->pp_objects,
278 (p_this->p_libvlc->i_objects - 1) * sizeof(vlc_object_t *) );
280 vlc_mutex_unlock( &structure_lock );
283 p_this->p_libvlc->i_objects--;
285 vlc_mutex_destroy( &p_this->object_lock );
286 vlc_cond_destroy( &p_this->object_wait );
291 /*****************************************************************************
292 * vlc_object_find: find a typed object and increment its refcount
293 *****************************************************************************
294 * This function recursively looks for a given object type. i_mode can be one
295 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
296 *****************************************************************************/
297 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
299 vlc_object_t *p_found;
301 vlc_mutex_lock( &structure_lock );
303 /* If we are of the requested type ourselves, don't look further */
304 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
306 p_this->i_refcount++;
307 vlc_mutex_unlock( &structure_lock );
311 /* Otherwise, recursively look for the object */
312 if( (i_mode & 0x000f) == FIND_ANYWHERE )
314 p_found = FindObject( VLC_OBJECT(p_this->p_vlc), i_type,
315 (i_mode & ~0x000f) | FIND_CHILD );
319 p_found = FindObject( p_this, i_type, i_mode );
322 vlc_mutex_unlock( &structure_lock );
327 /*****************************************************************************
328 * vlc_object_yield: increment an object refcount
329 *****************************************************************************/
330 void __vlc_object_yield( vlc_object_t *p_this )
332 vlc_mutex_lock( &structure_lock );
333 p_this->i_refcount++;
334 vlc_mutex_unlock( &structure_lock );
337 /*****************************************************************************
338 * vlc_object_release: decrement an object refcount
339 *****************************************************************************/
340 void __vlc_object_release( vlc_object_t *p_this )
342 vlc_mutex_lock( &structure_lock );
343 p_this->i_refcount--;
344 vlc_mutex_unlock( &structure_lock );
347 /*****************************************************************************
348 * vlc_object_attach: attach object to a parent object
349 *****************************************************************************
350 * This function sets p_this as a child of p_parent, and p_parent as a parent
351 * of p_this. This link can be undone using vlc_object_detach.
352 *****************************************************************************/
353 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
355 vlc_mutex_lock( &structure_lock );
357 /* Attach the parent to its child */
358 p_this->p_parent = p_parent;
360 /* Attach the child to its parent */
361 p_parent->i_children++;
362 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
363 p_parent->i_children * sizeof(vlc_object_t *) );
364 p_parent->pp_children[p_parent->i_children - 1] = p_this;
366 /* Climb up the tree to see whether we are connected with the root */
367 if( p_parent->b_attached )
369 SetAttachment( p_this, VLC_TRUE );
372 vlc_mutex_unlock( &structure_lock );
375 /*****************************************************************************
376 * vlc_object_detach: detach object from its parent
377 *****************************************************************************
378 * This function removes all links between an object and its parent.
379 *****************************************************************************/
380 void __vlc_object_detach( vlc_object_t *p_this )
382 vlc_mutex_lock( &structure_lock );
383 if( !p_this->p_parent )
385 msg_Err( p_this, "object is not attached" );
386 vlc_mutex_unlock( &structure_lock );
390 /* Climb up the tree to see whether we are connected with the root */
391 if( p_this->p_parent->b_attached )
393 SetAttachment( p_this, VLC_FALSE );
396 DetachObject( p_this );
397 vlc_mutex_unlock( &structure_lock );
400 /*****************************************************************************
401 * vlc_list_find: find a list typed objects and increment their refcount
402 *****************************************************************************
403 * This function recursively looks for a given object type. i_mode can be one
404 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
405 *****************************************************************************/
406 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
408 vlc_list_t *p_list = NewList();
410 vlc_mutex_lock( &structure_lock );
412 /* Look for the objects */
413 if( (i_mode & 0x000f) == FIND_ANYWHERE )
415 vlc_object_t **pp_current, **pp_end;
417 pp_current = p_this->p_libvlc->pp_objects;
418 pp_end = pp_current + p_this->p_libvlc->i_objects;
420 for( ; pp_current < pp_end ; pp_current++ )
422 if( (*pp_current)->b_attached
423 && (*pp_current)->i_object_type == i_type )
425 p_list = ListAppend( p_list, *pp_current );
431 msg_Err( p_this, "unimplemented!" );
434 vlc_mutex_unlock( &structure_lock );
439 /*****************************************************************************
440 * vlc_liststructure: print the current vlc objects
441 *****************************************************************************
442 * This function prints alist of vlc objects, and additional information such
443 * as their refcount, thread ID, etc.
444 *****************************************************************************/
445 void __vlc_liststructure( vlc_object_t *p_this )
447 vlc_object_t **pp_current, **pp_end;
449 vlc_mutex_lock( &structure_lock );
451 pp_current = p_this->p_libvlc->pp_objects;
452 pp_end = pp_current + p_this->p_libvlc->i_objects;
454 for( ; pp_current < pp_end ; pp_current++ )
456 if( (*pp_current)->b_attached )
458 PrintObject( *pp_current, "" );
462 printf( " o %.6x %s (not attached)\n",
463 (*pp_current)->i_object_id,
464 (*pp_current)->psz_object_type );
468 vlc_mutex_unlock( &structure_lock );
471 /*****************************************************************************
472 * vlc_dumpstructure: print the current vlc structure
473 *****************************************************************************
474 * This function prints an ASCII tree showing the connections between vlc
475 * objects, and additional information such as their refcount, thread ID, etc.
476 *****************************************************************************/
477 void __vlc_dumpstructure( vlc_object_t *p_this )
479 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
481 vlc_mutex_lock( &structure_lock );
483 DumpStructure( p_this, 0, psz_foo );
484 vlc_mutex_unlock( &structure_lock );
487 /*****************************************************************************
488 * vlc_list_release: free a list previously allocated by vlc_list_find
489 *****************************************************************************
490 * This function decreases the refcount of all objects in the list and
492 *****************************************************************************/
493 void vlc_list_release( vlc_list_t *p_list )
495 if( p_list->i_count )
497 vlc_object_t ** pp_current = p_list->pp_objects;
499 vlc_mutex_lock( &structure_lock );
501 while( pp_current[0] )
503 pp_current[0]->i_refcount--;
507 vlc_mutex_unlock( &structure_lock );
513 /* Following functions are local */
515 /*****************************************************************************
516 * FindIndex: find the index of an object in an array of objects
517 *****************************************************************************
518 * This function assumes that p_this can be found in pp_objects. It will not
519 * crash if p_this cannot be found, but will return a wrong value. It is your
520 * duty to check the return value if you are not certain that the object could
522 *****************************************************************************/
523 static int FindIndex( vlc_object_t *p_this,
524 vlc_object_t **pp_objects, int i_count )
526 int i_middle = i_count / 2;
533 if( pp_objects[i_middle] == p_this )
543 /* We take advantage of the sorted array */
544 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
546 return i_middle + FindIndex( p_this, pp_objects + i_middle,
547 i_count - i_middle );
551 return FindIndex( p_this, pp_objects, i_middle );
555 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
560 switch( i_mode & 0x000f )
563 p_tmp = p_this->p_parent;
566 if( p_tmp->i_object_type == i_type )
573 return FindObject( p_tmp, i_type, i_mode );
579 for( i = p_this->i_children; i--; )
581 p_tmp = p_this->pp_children[i];
582 if( p_tmp->i_object_type == i_type )
587 else if( p_tmp->i_children )
589 p_tmp = FindObject( p_tmp, i_type, i_mode );
599 /* Handled in vlc_object_find */
606 static void DetachObject( vlc_object_t *p_this )
608 vlc_object_t *p_parent = p_this->p_parent;
611 /* Remove p_this's parent */
612 p_this->p_parent = NULL;
614 /* Remove all of p_parent's children which are p_this */
615 for( i_index = p_parent->i_children ; i_index-- ; )
617 if( p_parent->pp_children[i_index] == p_this )
619 p_parent->i_children--;
620 for( i = i_index ; i < p_parent->i_children ; i++ )
622 p_parent->pp_children[i] = p_parent->pp_children[i+1];
627 if( p_parent->i_children )
629 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
630 p_parent->i_children * sizeof(vlc_object_t *) );
634 free( p_parent->pp_children );
635 p_parent->pp_children = NULL;
639 /*****************************************************************************
640 * SetAttachment: recursively set the b_attached flag of a subtree.
641 *****************************************************************************
642 * This function is used by the attach and detach functions to propagate
643 * the b_attached flag in a subtree.
644 *****************************************************************************/
645 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
649 for( i_index = p_this->i_children ; i_index-- ; )
651 SetAttachment( p_this->pp_children[i_index], b_attached );
654 p_this->b_attached = b_attached;
657 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
659 char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
662 if( p_this->psz_object_name )
664 snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
669 psz_children[0] = '\0';
670 switch( p_this->i_children )
675 strcpy( psz_children, ", 1 child" );
678 snprintf( psz_children, 20,
679 ", %i children", p_this->i_children );
680 psz_children[19] = '\0';
684 psz_refcount[0] = '\0';
685 if( p_this->i_refcount )
687 snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
688 psz_refcount[19] = '\0';
691 psz_thread[0] = '\0';
692 if( p_this->b_thread )
694 snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
695 psz_thread[19] = '\0';
698 printf( " %so %.6x %s%s%s%s%s\n", psz_prefix,
699 p_this->i_object_id, p_this->psz_object_type,
700 psz_name, psz_thread, psz_refcount, psz_children );
703 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
706 char i_back = psz_foo[i_level];
707 psz_foo[i_level] = '\0';
709 PrintObject( p_this, psz_foo );
711 psz_foo[i_level] = i_back;
713 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
715 msg_Warn( p_this, "structure tree is too deep" );
719 for( i = 0 ; i < p_this->i_children ; i++ )
723 psz_foo[i_level-1] = ' ';
725 if( psz_foo[i_level-2] == '`' )
727 psz_foo[i_level-2] = ' ';
731 if( i == p_this->i_children - 1 )
733 psz_foo[i_level] = '`';
737 psz_foo[i_level] = '|';
740 psz_foo[i_level+1] = '-';
741 psz_foo[i_level+2] = '\0';
743 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
747 static vlc_list_t * NewList( void )
749 vlc_list_t *p_list = malloc( sizeof( vlc_list_t )
750 + 3 * sizeof( vlc_object_t * ) );
758 p_list->pp_objects = &p_list->_p_first;
760 /* We allocated space for NULL and for three extra objects */
761 p_list->_i_extra = 3;
762 p_list->_p_first = NULL;
767 static vlc_list_t * ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
774 if( p_list->_i_extra == 0 )
776 /* If we had X objects it means the array has a size of X+1, we
777 * make it size 2X+2, so we alloc 2X+1 because there is already
778 * one allocated in the real structure */
779 p_list = realloc( p_list, sizeof( vlc_list_t )
780 + (p_list->i_count * 2 + 1)
781 * sizeof( vlc_object_t * ) );
787 /* We have X+1 extra slots */
788 p_list->_i_extra = p_list->i_count + 1;
789 p_list->pp_objects = &p_list->_p_first;
792 p_object->i_refcount++;
794 p_list->pp_objects[p_list->i_count] = p_object;
796 p_list->pp_objects[p_list->i_count] = NULL;