1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: objects.c,v 1.16 2002/08/12 09:34:15 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 * vlc_object_find_inner( vlc_object_t *, int, int );
50 static void vlc_object_detach_inner( vlc_object_t * );
51 static void vlc_dumpstructure_inner( vlc_object_t *, int, char * );
52 static int find_index_inner( vlc_object_t *, vlc_object_t **, int );
53 static void set_attachment_flag( vlc_object_t *, vlc_bool_t );
55 /*****************************************************************************
56 * vlc_object_create: initialize a vlc object
57 *****************************************************************************
58 * This function allocates memory for a vlc object and initializes it. If
59 * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
60 * so on, vlc_object_create will use its value for the object size.
61 *****************************************************************************/
62 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
71 i_size = sizeof(vlc_t);
74 case VLC_OBJECT_MODULE:
75 i_size = sizeof(module_t);
79 i_size = sizeof(intf_thread_t);
80 psz_type = "interface";
82 case VLC_OBJECT_PLAYLIST:
83 i_size = sizeof(playlist_t);
84 psz_type = "playlist";
86 case VLC_OBJECT_INPUT:
87 i_size = sizeof(input_thread_t);
90 case VLC_OBJECT_DECODER:
91 i_size = sizeof(decoder_fifo_t);
95 i_size = sizeof(vout_thread_t);
96 psz_type = "video output";
99 i_size = sizeof(aout_instance_t);
100 psz_type = "audio output";
103 i_size = i_type > sizeof(vlc_object_t)
104 ? i_type : sizeof(vlc_object_t);
105 i_type = VLC_OBJECT_GENERIC;
106 psz_type = "generic";
110 p_new = malloc( i_size );
117 memset( p_new, 0, i_size );
119 p_new->i_object_type = i_type;
120 p_new->psz_object_type = psz_type;
122 p_new->psz_object_name = NULL;
124 p_new->i_refcount = 0;
125 p_new->b_die = VLC_FALSE;
126 p_new->b_error = VLC_FALSE;
127 p_new->b_dead = VLC_FALSE;
128 p_new->b_attached = VLC_FALSE;
130 /* If i_type is root, then p_new is our own p_vlc */
131 if( i_type == VLC_OBJECT_ROOT )
133 /* We are the first object ... no need to lock. */
134 p_new->p_vlc = (vlc_t*)p_new;
136 p_new->p_vlc->i_counter = 0;
137 p_new->i_object_id = 0;
139 p_new->p_vlc->i_objects = 1;
140 p_new->p_vlc->pp_objects = malloc( sizeof(vlc_object_t *) );
141 p_new->p_vlc->pp_objects[0] = p_new;
142 p_new->b_attached = VLC_TRUE;
146 p_new->p_vlc = p_this->p_vlc;
148 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
150 p_new->p_vlc->i_counter++;
151 p_new->i_object_id = p_new->p_vlc->i_counter;
153 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
154 * useless to try and recover anything if pp_objects gets smashed. */
155 p_new->p_vlc->i_objects++;
156 p_new->p_vlc->pp_objects =
157 realloc( p_new->p_vlc->pp_objects,
158 p_new->p_vlc->i_objects * sizeof(vlc_object_t *) );
159 p_new->p_vlc->pp_objects[ p_new->p_vlc->i_objects - 1 ] = p_new;
161 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
164 p_new->p_parent = NULL;
165 p_new->pp_children = NULL;
166 p_new->i_children = 0;
168 p_new->p_private = NULL;
170 vlc_mutex_init( p_new, &p_new->object_lock );
171 vlc_cond_init( p_new, &p_new->object_wait );
176 /*****************************************************************************
177 * vlc_object_destroy: destroy a vlc object
178 *****************************************************************************
179 * This function destroys an object that has been previously allocated with
180 * vlc_object_create. The object's refcount must be zero and it must not be
181 * attached to other objects in any way.
182 *****************************************************************************/
183 void __vlc_object_destroy( vlc_object_t *p_this )
187 if( p_this->i_children )
189 msg_Err( p_this, "cannot delete object with children" );
190 vlc_dumpstructure( p_this );
194 if( p_this->p_parent )
196 msg_Err( p_this, "cannot delete object with a parent" );
197 vlc_dumpstructure( p_this );
201 while( p_this->i_refcount )
205 /* Don't warn immediately ... 100ms seems OK */
208 msg_Warn( p_this, "refcount is %i, delaying before deletion",
209 p_this->i_refcount );
211 else if( i_delay == 12 )
213 msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
214 p_this->i_refcount );
216 else if( i_delay == 42 )
218 msg_Err( p_this, "we waited too long, cancelling destruction" );
225 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
227 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
228 * useless to try and recover anything if pp_objects gets smashed. */
229 if( p_this->p_vlc->i_objects > 1 )
231 int i_index = find_index_inner( p_this, p_this->p_vlc->pp_objects,
232 p_this->p_vlc->i_objects );
233 memmove( p_this->p_vlc->pp_objects + i_index,
234 p_this->p_vlc->pp_objects + i_index + 1,
235 p_this->p_vlc->i_objects - i_index - 1 );
237 p_this->p_vlc->pp_objects =
238 realloc( p_this->p_vlc->pp_objects,
239 (p_this->p_vlc->i_objects - 1) * sizeof(vlc_object_t *) );
243 free( p_this->p_vlc->pp_objects );
244 p_this->p_vlc->pp_objects = NULL;
247 p_this->p_vlc->i_objects--;
249 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
251 vlc_mutex_destroy( &p_this->object_lock );
252 vlc_cond_destroy( &p_this->object_wait );
257 /*****************************************************************************
258 * find_index_inner: find the index of an object in an array of objects
259 *****************************************************************************
260 * This function assumes that p_this can be found in pp_objects. It will not
261 * crash if p_this cannot be found, but will return a random value. It is your
262 * duty to check the return value if you are not certain that the object could
264 *****************************************************************************/
265 static int find_index_inner( vlc_object_t *p_this,
266 vlc_object_t **pp_objects, int i_count )
268 int i_middle = i_count / 2;
275 if( pp_objects[i_middle] == p_this )
285 /* We take advantage of the sorted array */
286 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
288 return i_middle + find_index_inner( p_this, pp_objects + i_middle,
289 i_count - i_middle );
293 return find_index_inner( p_this, pp_objects, i_middle );
297 /*****************************************************************************
298 * vlc_object_find: find a typed object and increment its refcount
299 *****************************************************************************
300 * This function recursively looks for a given object type. i_mode can be one
301 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
302 *****************************************************************************/
303 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
305 vlc_object_t *p_found;
307 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
309 /* If we are of the requested type ourselves, don't look further */
310 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
312 p_this->i_refcount++;
313 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
317 /* Otherwise, recursively look for the object */
318 if( (i_mode & 0x000f) == FIND_ANYWHERE )
320 p_found = vlc_object_find_inner( VLC_OBJECT(p_this->p_vlc),
322 (i_mode & ~0x000f) | FIND_CHILD );
326 p_found = vlc_object_find_inner( p_this, i_type, i_mode );
329 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
334 static vlc_object_t * vlc_object_find_inner( vlc_object_t *p_this,
335 int i_type, int i_mode )
340 switch( i_mode & 0x000f )
343 p_tmp = p_this->p_parent;
346 if( p_tmp->i_object_type == i_type )
353 return vlc_object_find_inner( p_tmp, i_type, i_mode );
359 for( i = p_this->i_children; i--; )
361 p_tmp = p_this->pp_children[i];
362 if( p_tmp->i_object_type == i_type )
367 else if( p_tmp->i_children )
369 p_tmp = vlc_object_find_inner( p_tmp, i_type, i_mode );
379 /* Handled in vlc_object_find */
386 /*****************************************************************************
387 * vlc_object_yield: increment an object refcount
388 *****************************************************************************/
389 void __vlc_object_yield( vlc_object_t *p_this )
391 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
392 p_this->i_refcount++;
393 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
396 /*****************************************************************************
397 * vlc_object_release: decrement an object refcount
398 *****************************************************************************/
399 void __vlc_object_release( vlc_object_t *p_this )
401 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
402 p_this->i_refcount--;
403 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
406 /*****************************************************************************
407 * vlc_object_attach: attach object to a parent object
408 *****************************************************************************
409 * This function sets p_this as a child of p_parent, and p_parent as a parent
410 * of p_this. This link can be undone using vlc_object_detach.
411 *****************************************************************************/
412 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
414 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
416 /* Attach the parent to its child */
417 p_this->p_parent = p_parent;
419 /* Attach the child to its parent */
420 p_parent->i_children++;
421 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
422 p_parent->i_children * sizeof(vlc_object_t *) );
423 p_parent->pp_children[p_parent->i_children - 1] = p_this;
425 /* Climb up the tree to see whether we are connected with the root */
426 if( p_parent->b_attached )
428 set_attachment_flag( p_this, VLC_TRUE );
431 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
434 /*****************************************************************************
435 * vlc_object_detach: detach object from its parent
436 *****************************************************************************
437 * This function removes all links between an object and its parent.
438 *****************************************************************************/
439 void __vlc_object_detach( vlc_object_t *p_this )
441 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
442 if( !p_this->p_parent )
444 msg_Err( p_this, "object is not attached" );
445 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
449 /* Climb up the tree to see whether we are connected with the root */
450 if( p_this->p_parent->b_attached )
452 set_attachment_flag( p_this, VLC_FALSE );
455 vlc_object_detach_inner( p_this );
456 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
459 static void vlc_object_detach_inner( vlc_object_t *p_this )
461 vlc_object_t *p_parent = p_this->p_parent;
464 /* Remove p_this's parent */
465 p_this->p_parent = NULL;
467 /* Remove all of p_parent's children which are p_this */
468 for( i_index = p_parent->i_children ; i_index-- ; )
470 if( p_parent->pp_children[i_index] == p_this )
472 p_parent->i_children--;
473 for( i = i_index ; i < p_parent->i_children ; i++ )
475 p_parent->pp_children[i] = p_parent->pp_children[i+1];
480 if( p_parent->i_children )
482 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
483 p_parent->i_children * sizeof(vlc_object_t *) );
487 free( p_parent->pp_children );
488 p_parent->pp_children = NULL;
492 /*****************************************************************************
493 * set_attachment_flag: recursively set the b_attached flag of a subtree.
494 *****************************************************************************
495 * This function is used by the attach and detach functions to propagate
496 * the b_attached flag in a subtree.
497 *****************************************************************************/
498 static void set_attachment_flag( vlc_object_t *p_this, vlc_bool_t b_attached )
502 for( i_index = p_this->i_children ; i_index-- ; )
504 set_attachment_flag( p_this->pp_children[i_index], b_attached );
507 p_this->b_attached = b_attached;
510 /*****************************************************************************
511 * vlc_dumpstructure: print the current vlc structure
512 *****************************************************************************
513 * This function prints an ASCII tree showing the connections between vlc
514 * objects, and additional information such as their refcount, thread ID,
516 *****************************************************************************/
517 void __vlc_dumpstructure( vlc_object_t *p_this )
519 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
521 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
523 vlc_dumpstructure_inner( p_this, 0, psz_foo );
524 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
527 static void vlc_dumpstructure_inner( vlc_object_t *p_this,
528 int i_level, char *psz_foo )
531 char i_back = psz_foo[i_level];
532 char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
535 if( p_this->psz_object_name )
537 snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
542 psz_children[0] = '\0';
543 switch( p_this->i_children )
548 strcpy( psz_children, ", 1 child" );
551 snprintf( psz_children, 20,
552 ", %i children", p_this->i_children );
553 psz_children[19] = '\0';
557 psz_refcount[0] = '\0';
558 if( p_this->i_refcount )
560 snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
561 psz_refcount[19] = '\0';
564 psz_thread[0] = '\0';
565 if( p_this->b_thread )
567 snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
568 psz_thread[19] = '\0';
571 psz_foo[i_level] = '\0';
572 msg_Info( p_this, "%so %s %p%s%s%s%s", psz_foo, p_this->psz_object_type,
573 p_this, psz_name, psz_thread, psz_refcount, psz_children );
574 psz_foo[i_level] = i_back;
576 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
578 msg_Warn( p_this, "structure tree is too deep" );
582 for( i = 0 ; i < p_this->i_children ; i++ )
586 psz_foo[i_level-1] = ' ';
588 if( psz_foo[i_level-2] == '`' )
590 psz_foo[i_level-2] = ' ';
594 if( i == p_this->i_children - 1 )
596 psz_foo[i_level] = '`';
600 psz_foo[i_level] = '|';
603 psz_foo[i_level+1] = '-';
604 psz_foo[i_level+2] = '\0';
606 vlc_dumpstructure_inner( p_this->pp_children[i], i_level + 2, psz_foo );