1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: objects.c,v 1.20 2002/08/24 17:04:36 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"
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 * vlc_object_create: initialize a vlc object
61 *****************************************************************************
62 * This function allocates memory for a vlc object and initializes it. If
63 * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
64 * so on, vlc_object_create will use its value for the object size.
65 *****************************************************************************/
66 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
75 i_size = sizeof(vlc_t);
78 case VLC_OBJECT_MODULE:
79 i_size = sizeof(module_t);
83 i_size = sizeof(intf_thread_t);
84 psz_type = "interface";
86 case VLC_OBJECT_PLAYLIST:
87 i_size = sizeof(playlist_t);
88 psz_type = "playlist";
90 case VLC_OBJECT_INPUT:
91 i_size = sizeof(input_thread_t);
94 case VLC_OBJECT_DECODER:
95 i_size = sizeof(decoder_fifo_t);
99 i_size = sizeof(vout_thread_t);
100 psz_type = "video output";
102 case VLC_OBJECT_AOUT:
103 i_size = sizeof(aout_instance_t);
104 psz_type = "audio output";
107 i_size = i_type > sizeof(vlc_object_t)
108 ? i_type : sizeof(vlc_object_t);
109 i_type = VLC_OBJECT_GENERIC;
110 psz_type = "generic";
114 p_new = malloc( i_size );
121 memset( p_new, 0, i_size );
123 p_new->i_object_type = i_type;
124 p_new->psz_object_type = psz_type;
126 p_new->psz_object_name = NULL;
128 p_new->i_refcount = 0;
129 p_new->b_die = VLC_FALSE;
130 p_new->b_error = VLC_FALSE;
131 p_new->b_dead = VLC_FALSE;
132 p_new->b_attached = VLC_FALSE;
134 /* If i_type is root, then p_new is our own p_vlc */
135 if( i_type == VLC_OBJECT_ROOT )
137 /* We are the first object ... no need to lock. */
138 p_new->p_vlc = (vlc_t*)p_new;
140 p_new->p_vlc->i_counter = 0;
141 p_new->i_object_id = 0;
143 p_new->p_vlc->i_objects = 1;
144 p_new->p_vlc->pp_objects = malloc( sizeof(vlc_object_t *) );
145 p_new->p_vlc->pp_objects[0] = p_new;
146 p_new->b_attached = VLC_TRUE;
150 p_new->p_vlc = p_this->p_vlc;
152 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
154 p_new->p_vlc->i_counter++;
155 p_new->i_object_id = p_new->p_vlc->i_counter;
157 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
158 * useless to try and recover anything if pp_objects gets smashed. */
159 p_new->p_vlc->i_objects++;
160 p_new->p_vlc->pp_objects =
161 realloc( p_new->p_vlc->pp_objects,
162 p_new->p_vlc->i_objects * sizeof(vlc_object_t *) );
163 p_new->p_vlc->pp_objects[ p_new->p_vlc->i_objects - 1 ] = p_new;
165 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
168 p_new->p_parent = NULL;
169 p_new->pp_children = NULL;
170 p_new->i_children = 0;
172 p_new->p_private = NULL;
174 vlc_mutex_init( p_new, &p_new->object_lock );
175 vlc_cond_init( p_new, &p_new->object_wait );
180 /*****************************************************************************
181 * vlc_object_destroy: destroy a vlc object
182 *****************************************************************************
183 * This function destroys an object that has been previously allocated with
184 * vlc_object_create. The object's refcount must be zero and it must not be
185 * attached to other objects in any way.
186 *****************************************************************************/
187 void __vlc_object_destroy( vlc_object_t *p_this )
191 if( p_this->i_children )
193 msg_Err( p_this, "cannot delete object with children" );
194 vlc_dumpstructure( p_this );
198 if( p_this->p_parent )
200 msg_Err( p_this, "cannot delete object with a parent" );
201 vlc_dumpstructure( p_this );
205 while( p_this->i_refcount )
209 /* Don't warn immediately ... 100ms seems OK */
212 msg_Warn( p_this, "refcount is %i, delaying before deletion",
213 p_this->i_refcount );
215 else if( i_delay == 12 )
217 msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
218 p_this->i_refcount );
220 else if( i_delay == 42 )
222 msg_Err( p_this, "we waited too long, cancelling destruction" );
229 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
230 * useless to try and recover anything if pp_objects gets smashed. */
231 if( p_this->i_object_type != VLC_OBJECT_ROOT )
235 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
237 i_index = FindIndex( p_this, p_this->p_vlc->pp_objects,
238 p_this->p_vlc->i_objects );
239 memmove( p_this->p_vlc->pp_objects + i_index,
240 p_this->p_vlc->pp_objects + i_index + 1,
241 (p_this->p_vlc->i_objects - i_index - 1)
242 * sizeof( vlc_object_t *) );
244 p_this->p_vlc->pp_objects =
245 realloc( p_this->p_vlc->pp_objects,
246 (p_this->p_vlc->i_objects - 1) * sizeof(vlc_object_t *) );
248 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
252 /* We are the root object ... no need to lock. */
253 free( p_this->p_vlc->pp_objects );
254 p_this->p_vlc->pp_objects = NULL;
257 p_this->p_vlc->i_objects--;
259 vlc_mutex_destroy( &p_this->object_lock );
260 vlc_cond_destroy( &p_this->object_wait );
265 /*****************************************************************************
266 * vlc_object_find: find a typed object and increment its refcount
267 *****************************************************************************
268 * This function recursively looks for a given object type. i_mode can be one
269 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
270 *****************************************************************************/
271 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
273 vlc_object_t *p_found;
275 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
277 /* If we are of the requested type ourselves, don't look further */
278 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
280 p_this->i_refcount++;
281 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
285 /* Otherwise, recursively look for the object */
286 if( (i_mode & 0x000f) == FIND_ANYWHERE )
288 p_found = FindObject( VLC_OBJECT(p_this->p_vlc), i_type,
289 (i_mode & ~0x000f) | FIND_CHILD );
293 p_found = FindObject( p_this, i_type, i_mode );
296 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
301 /*****************************************************************************
302 * vlc_object_yield: increment an object refcount
303 *****************************************************************************/
304 void __vlc_object_yield( vlc_object_t *p_this )
306 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
307 p_this->i_refcount++;
308 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
311 /*****************************************************************************
312 * vlc_object_release: decrement an object refcount
313 *****************************************************************************/
314 void __vlc_object_release( vlc_object_t *p_this )
316 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
317 p_this->i_refcount--;
318 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
321 /*****************************************************************************
322 * vlc_object_attach: attach object to a parent object
323 *****************************************************************************
324 * This function sets p_this as a child of p_parent, and p_parent as a parent
325 * of p_this. This link can be undone using vlc_object_detach.
326 *****************************************************************************/
327 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
329 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
331 /* Attach the parent to its child */
332 p_this->p_parent = p_parent;
334 /* Attach the child to its parent */
335 p_parent->i_children++;
336 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
337 p_parent->i_children * sizeof(vlc_object_t *) );
338 p_parent->pp_children[p_parent->i_children - 1] = p_this;
340 /* Climb up the tree to see whether we are connected with the root */
341 if( p_parent->b_attached )
343 SetAttachment( p_this, VLC_TRUE );
346 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
349 /*****************************************************************************
350 * vlc_object_detach: detach object from its parent
351 *****************************************************************************
352 * This function removes all links between an object and its parent.
353 *****************************************************************************/
354 void __vlc_object_detach( vlc_object_t *p_this )
356 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
357 if( !p_this->p_parent )
359 msg_Err( p_this, "object is not attached" );
360 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
364 /* Climb up the tree to see whether we are connected with the root */
365 if( p_this->p_parent->b_attached )
367 SetAttachment( p_this, VLC_FALSE );
370 DetachObject( p_this );
371 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
374 /*****************************************************************************
375 * vlc_list_find: find a list typed objects and increment their refcount
376 *****************************************************************************
377 * This function recursively looks for a given object type. i_mode can be one
378 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
379 *****************************************************************************/
380 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
382 vlc_list_t *p_list = NewList();
384 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
386 /* Look for the objects */
387 if( (i_mode & 0x000f) == FIND_ANYWHERE )
389 vlc_object_t **pp_current, **pp_end;
391 pp_current = p_this->p_vlc->pp_objects;
392 pp_end = pp_current + p_this->p_vlc->i_objects;
394 for( ; pp_current < pp_end ; pp_current++ )
396 if( (*pp_current)->b_attached
397 && (*pp_current)->i_object_type == i_type )
399 p_list = ListAppend( p_list, *pp_current );
405 msg_Err( p_this, "unimplemented!" );
408 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
413 /*****************************************************************************
414 * vlc_liststructure: print the current vlc objects
415 *****************************************************************************
416 * This function prints alist of vlc objects, and additional information such
417 * as their refcount, thread ID, etc.
418 *****************************************************************************/
419 void __vlc_liststructure( vlc_object_t *p_this )
421 vlc_object_t **pp_current, **pp_end;
423 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
425 pp_current = p_this->p_vlc->pp_objects;
426 pp_end = pp_current + p_this->p_vlc->i_objects;
428 for( ; pp_current < pp_end ; pp_current++ )
430 if( (*pp_current)->b_attached )
432 PrintObject( *pp_current, "" );
436 msg_Info( p_this->p_vlc, "o %.6x %s (not attached)",
437 (*pp_current)->i_object_id,
438 (*pp_current)->psz_object_type );
442 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
445 /*****************************************************************************
446 * vlc_dumpstructure: print the current vlc structure
447 *****************************************************************************
448 * This function prints an ASCII tree showing the connections between vlc
449 * objects, and additional information such as their refcount, thread ID, etc.
450 *****************************************************************************/
451 void __vlc_dumpstructure( vlc_object_t *p_this )
453 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
455 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
457 DumpStructure( p_this, 0, psz_foo );
458 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
461 /*****************************************************************************
462 * vlc_list_release: free a list previously allocated by vlc_list_find
463 *****************************************************************************
464 * This function decreases the refcount of all objects in the list and
466 *****************************************************************************/
467 void vlc_list_release( vlc_list_t *p_list )
469 if( p_list->i_count )
471 vlc_t * p_vlc = p_list->pp_objects[0]->p_vlc;
472 vlc_object_t ** pp_current = p_list->pp_objects;
474 vlc_mutex_lock( &p_vlc->structure_lock );
476 while( pp_current[0] )
478 pp_current[0]->i_refcount--;
482 vlc_mutex_unlock( &p_vlc->structure_lock );
488 /* Following functions are local */
490 /*****************************************************************************
491 * FindIndex: find the index of an object in an array of objects
492 *****************************************************************************
493 * This function assumes that p_this can be found in pp_objects. It will not
494 * crash if p_this cannot be found, but will return a wrong value. It is your
495 * duty to check the return value if you are not certain that the object could
497 *****************************************************************************/
498 static int FindIndex( vlc_object_t *p_this,
499 vlc_object_t **pp_objects, int i_count )
501 int i_middle = i_count / 2;
508 if( pp_objects[i_middle] == p_this )
518 /* We take advantage of the sorted array */
519 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
521 return i_middle + FindIndex( p_this, pp_objects + i_middle,
522 i_count - i_middle );
526 return FindIndex( p_this, pp_objects, i_middle );
530 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
535 switch( i_mode & 0x000f )
538 p_tmp = p_this->p_parent;
541 if( p_tmp->i_object_type == i_type )
548 return FindObject( p_tmp, i_type, i_mode );
554 for( i = p_this->i_children; i--; )
556 p_tmp = p_this->pp_children[i];
557 if( p_tmp->i_object_type == i_type )
562 else if( p_tmp->i_children )
564 p_tmp = FindObject( p_tmp, i_type, i_mode );
574 /* Handled in vlc_object_find */
581 static void DetachObject( vlc_object_t *p_this )
583 vlc_object_t *p_parent = p_this->p_parent;
586 /* Remove p_this's parent */
587 p_this->p_parent = NULL;
589 /* Remove all of p_parent's children which are p_this */
590 for( i_index = p_parent->i_children ; i_index-- ; )
592 if( p_parent->pp_children[i_index] == p_this )
594 p_parent->i_children--;
595 for( i = i_index ; i < p_parent->i_children ; i++ )
597 p_parent->pp_children[i] = p_parent->pp_children[i+1];
602 if( p_parent->i_children )
604 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
605 p_parent->i_children * sizeof(vlc_object_t *) );
609 free( p_parent->pp_children );
610 p_parent->pp_children = NULL;
614 /*****************************************************************************
615 * SetAttachment: recursively set the b_attached flag of a subtree.
616 *****************************************************************************
617 * This function is used by the attach and detach functions to propagate
618 * the b_attached flag in a subtree.
619 *****************************************************************************/
620 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
624 for( i_index = p_this->i_children ; i_index-- ; )
626 SetAttachment( p_this->pp_children[i_index], b_attached );
629 p_this->b_attached = b_attached;
632 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
634 char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
637 if( p_this->psz_object_name )
639 snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
644 psz_children[0] = '\0';
645 switch( p_this->i_children )
650 strcpy( psz_children, ", 1 child" );
653 snprintf( psz_children, 20,
654 ", %i children", p_this->i_children );
655 psz_children[19] = '\0';
659 psz_refcount[0] = '\0';
660 if( p_this->i_refcount )
662 snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
663 psz_refcount[19] = '\0';
666 psz_thread[0] = '\0';
667 if( p_this->b_thread )
669 snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
670 psz_thread[19] = '\0';
673 msg_Info( p_this->p_vlc, "%so %.6x %s%s%s%s%s", psz_prefix,
674 p_this->i_object_id, p_this->psz_object_type,
675 psz_name, psz_thread, psz_refcount, psz_children );
678 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
681 char i_back = psz_foo[i_level];
682 psz_foo[i_level] = '\0';
684 PrintObject( p_this, psz_foo );
686 psz_foo[i_level] = i_back;
688 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
690 msg_Warn( p_this, "structure tree is too deep" );
694 for( i = 0 ; i < p_this->i_children ; i++ )
698 psz_foo[i_level-1] = ' ';
700 if( psz_foo[i_level-2] == '`' )
702 psz_foo[i_level-2] = ' ';
706 if( i == p_this->i_children - 1 )
708 psz_foo[i_level] = '`';
712 psz_foo[i_level] = '|';
715 psz_foo[i_level+1] = '-';
716 psz_foo[i_level+2] = '\0';
718 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
722 static vlc_list_t * NewList( void )
724 vlc_list_t *p_list = malloc( sizeof( vlc_list_t )
725 + 3 * sizeof( vlc_object_t * ) );
733 p_list->pp_objects = &p_list->_p_first;
735 /* We allocated space for NULL and for three extra objects */
736 p_list->_i_extra = 3;
737 p_list->_p_first = NULL;
742 static vlc_list_t * ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
749 if( p_list->_i_extra == 0 )
751 /* If we had X objects it means the array has a size of X+1, we
752 * make it size 2X+2, so we alloc 2X+1 because there is already
753 * one allocated in the real structure */
754 p_list = realloc( p_list, sizeof( vlc_list_t )
755 + (p_list->i_count * 2 + 1)
756 * sizeof( vlc_object_t * ) );
762 /* We have X+1 extra slots */
763 p_list->_i_extra = p_list->i_count + 1;
764 p_list->pp_objects = &p_list->_p_first;
767 p_object->i_refcount++;
769 p_list->pp_objects[p_list->i_count] = p_object;
771 p_list->pp_objects[p_list->i_count] = NULL;