1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: objects.c,v 1.14 2002/08/07 21:36:56 massiot 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 *, vlc_object_t * );
51 static void vlc_dumpstructure_inner( vlc_object_t *, int, char * );
53 /*****************************************************************************
54 * vlc_object_create: initialize a vlc object
55 *****************************************************************************
56 * This function allocates memory for a vlc object and initializes it. If
57 * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
58 * so on, vlc_object_create will use its value for the object size.
59 *****************************************************************************/
60 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
69 i_size = sizeof(vlc_t);
72 case VLC_OBJECT_MODULE:
73 i_size = sizeof(module_t);
77 i_size = sizeof(intf_thread_t);
78 psz_type = "interface";
80 case VLC_OBJECT_PLAYLIST:
81 i_size = sizeof(playlist_t);
82 psz_type = "playlist";
84 case VLC_OBJECT_INPUT:
85 i_size = sizeof(input_thread_t);
88 case VLC_OBJECT_DECODER:
89 i_size = sizeof(decoder_fifo_t);
93 i_size = sizeof(vout_thread_t);
94 psz_type = "video output";
97 i_size = sizeof(aout_instance_t);
98 psz_type = "audio output";
101 i_size = i_type > sizeof(vlc_object_t)
102 ? i_type : sizeof(vlc_object_t);
103 i_type = VLC_OBJECT_GENERIC;
104 psz_type = "generic";
108 p_new = malloc( i_size );
115 memset( p_new, 0, i_size );
117 p_new->i_object_type = i_type;
118 p_new->psz_object_type = psz_type;
120 p_new->psz_object_name = NULL;
122 p_new->i_refcount = 0;
126 /* If i_type is root, then p_new is our own p_vlc */
127 if( i_type == VLC_OBJECT_ROOT )
129 p_new->p_vlc = (vlc_t*)p_new;
130 p_new->p_vlc->i_counter = 0;
131 p_new->i_object_id = 0;
135 p_new->p_vlc = p_this->p_vlc;
137 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
138 p_new->p_vlc->i_counter++;
139 p_new->i_object_id = p_new->p_vlc->i_counter;
140 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
143 p_new->pp_parents = NULL;
144 p_new->i_parents = 0;
145 p_new->pp_children = NULL;
146 p_new->i_children = 0;
148 p_new->p_private = NULL;
150 vlc_mutex_init( p_new, &p_new->object_lock );
151 vlc_cond_init( p_new, &p_new->object_wait );
153 //msg_Dbg( p_new, "created object" );
158 /*****************************************************************************
159 * vlc_object_destroy: destroy a vlc object
160 *****************************************************************************
161 * This function destroys an object that has been previously allocated with
162 * vlc_object_create. The object's refcount must be zero and it must not be
163 * attached to other objects in any way.
164 *****************************************************************************/
165 void __vlc_object_destroy( vlc_object_t *p_this )
169 if( p_this->i_children )
171 msg_Err( p_this, "cannot delete object with children" );
172 vlc_dumpstructure( p_this );
176 if( p_this->i_parents )
178 msg_Err( p_this, "cannot delete object with parents" );
179 vlc_dumpstructure( p_this );
183 while( p_this->i_refcount )
187 /* Don't warn immediately ... 100ms seems OK */
190 msg_Warn( p_this, "refcount is %i, delaying before deletion",
191 p_this->i_refcount );
193 else if( i_delay == 12 )
195 msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
196 p_this->i_refcount );
198 else if( i_delay == 42 )
200 msg_Err( p_this, "we waited too long, cancelling destruction" );
207 //msg_Dbg( p_this, "destroyed object" );
209 vlc_mutex_destroy( &p_this->object_lock );
210 vlc_cond_destroy( &p_this->object_wait );
215 /*****************************************************************************
216 * vlc_object_find: find a typed object and increment its refcount
217 *****************************************************************************
218 * This function recursively looks for a given object type. i_mode can be one
219 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
220 *****************************************************************************/
221 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
223 vlc_object_t *p_found;
225 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
227 /* If we are of the requested type ourselves, don't look further */
228 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
230 p_this->i_refcount++;
231 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
235 /* Otherwise, recursively look for the object */
236 if( (i_mode & 0x000f) == FIND_ANYWHERE )
238 p_found = vlc_object_find_inner( VLC_OBJECT(p_this->p_vlc),
240 (i_mode & ~0x000f) | FIND_CHILD );
244 p_found = vlc_object_find_inner( p_this, i_type, i_mode );
247 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
252 static vlc_object_t * vlc_object_find_inner( vlc_object_t *p_this,
253 int i_type, int i_mode )
258 switch( i_mode & 0x000f )
261 for( i = p_this->i_parents; i--; )
263 p_tmp = p_this->pp_parents[i];
264 if( p_tmp->i_object_type == i_type )
269 else if( p_tmp->i_parents )
271 p_tmp = vlc_object_find_inner( p_tmp, i_type, i_mode );
281 for( i = p_this->i_children; i--; )
283 p_tmp = p_this->pp_children[i];
284 if( p_tmp->i_object_type == i_type )
289 else if( p_tmp->i_children )
291 p_tmp = vlc_object_find_inner( p_tmp, i_type, i_mode );
301 /* Handled in vlc_object_find */
308 /*****************************************************************************
309 * vlc_object_yield: increment an object refcount
310 *****************************************************************************/
311 void __vlc_object_yield( vlc_object_t *p_this )
313 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
314 p_this->i_refcount++;
315 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
318 /*****************************************************************************
319 * vlc_object_release: decrement an object refcount
320 *****************************************************************************/
321 void __vlc_object_release( vlc_object_t *p_this )
323 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
324 p_this->i_refcount--;
325 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
328 /*****************************************************************************
329 * vlc_object_attach: attach object to a parent object
330 *****************************************************************************
331 * This function sets p_this as a child of p_parent, and p_parent as a parent
332 * of p_this. This link can be undone using vlc_object_detach.
333 *****************************************************************************/
334 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
336 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
339 p_this->pp_parents = (vlc_object_t **)realloc( p_this->pp_parents,
340 p_this->i_parents * sizeof(vlc_object_t *) );
341 p_this->pp_parents[p_this->i_parents - 1] = p_parent;
343 p_parent->i_children++;
344 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
345 p_parent->i_children * sizeof(vlc_object_t *) );
346 p_parent->pp_children[p_parent->i_children - 1] = p_this;
348 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
352 /* vlc_object_setchild: attach a child object */
353 void __vlc_object_setchild( vlc_object_t *p_this, vlc_object_t *p_child )
355 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
357 p_this->i_children++;
358 p_this->pp_children = (vlc_object_t **)realloc( p_this->pp_children,
359 p_this->i_children * sizeof(vlc_object_t *) );
360 p_this->pp_children[p_this->i_children - 1] = p_child;
362 p_child->i_parents++;
363 p_child->pp_parents = (vlc_object_t **)realloc( p_child->pp_parents,
364 p_child->i_parents * sizeof(vlc_object_t *) );
365 p_child->pp_parents[p_child->i_parents - 1] = p_this;
367 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
371 /*****************************************************************************
372 * vlc_object_detach_all: detach object from its parents
373 *****************************************************************************
374 * This function unlinks an object from all its parents. It is up to the
375 * object to get rid of its children, so this function doesn't do anything
377 *****************************************************************************/
378 void __vlc_object_detach_all( vlc_object_t *p_this )
380 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
382 /* FIXME: BORK ! BORK ! BORK !!! THIS STUFF IS BORKED !! FIXME */
383 while( p_this->i_parents )
385 /* Not very effective because we know the index, but we'd have to
386 * parse p_parent->pp_children anyway. Plus, we remove duplicates
387 * by not using the object's index */
388 vlc_object_detach_inner( p_this, p_this->pp_parents[0] );
391 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
394 /*****************************************************************************
395 * vlc_object_detach: remove a parent/child link
396 *****************************************************************************
397 * This function removes all links between an object and a given parent.
398 *****************************************************************************/
399 void __vlc_object_detach( vlc_object_t *p_this, vlc_object_t *p_parent )
401 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
402 vlc_object_detach_inner( p_this, p_parent );
403 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
406 static void vlc_object_detach_inner( vlc_object_t *p_this,
407 vlc_object_t *p_parent )
411 /* Remove all of p_this's parents which are p_parent */
412 for( i_index = p_this->i_parents ; i_index-- ; )
414 if( p_this->pp_parents[i_index] == p_parent )
417 for( i = i_index ; i < p_this->i_parents ; i++ )
419 p_this->pp_parents[i] = p_this->pp_parents[i+1];
424 if( p_this->i_parents )
426 p_this->pp_parents = (vlc_object_t **)realloc( p_this->pp_parents,
427 p_this->i_parents * sizeof(vlc_object_t *) );
431 free( p_this->pp_parents );
432 p_this->pp_parents = NULL;
435 /* Remove all of p_parent's children which are p_this */
436 for( i_index = p_parent->i_children ; i_index-- ; )
438 if( p_parent->pp_children[i_index] == p_this )
440 p_parent->i_children--;
441 for( i = i_index ; i < p_parent->i_children ; i++ )
443 p_parent->pp_children[i] = p_parent->pp_children[i+1];
448 if( p_parent->i_children )
450 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
451 p_parent->i_children * sizeof(vlc_object_t *) );
455 free( p_parent->pp_children );
456 p_parent->pp_children = NULL;
460 /*****************************************************************************
461 * vlc_dumpstructure: print the current vlc structure
462 *****************************************************************************
463 * This function prints an ASCII tree showing the connections between vlc
464 * objects, and additional information such as their refcount, thread ID,
466 *****************************************************************************/
467 void __vlc_dumpstructure( vlc_object_t *p_this )
469 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
471 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
473 vlc_dumpstructure_inner( p_this, 0, psz_foo );
474 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
477 static void vlc_dumpstructure_inner( vlc_object_t *p_this,
478 int i_level, char *psz_foo )
481 char i_back = psz_foo[i_level];
482 char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
485 if( p_this->psz_object_name )
487 snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
492 psz_children[0] = '\0';
493 switch( p_this->i_children )
498 strcpy( psz_children, ", 1 child" );
501 snprintf( psz_children, 20,
502 ", %i children", p_this->i_children );
503 psz_children[19] = '\0';
507 psz_refcount[0] = '\0';
508 if( p_this->i_refcount )
510 snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
511 psz_refcount[19] = '\0';
514 psz_thread[0] = '\0';
515 if( p_this->b_thread )
517 snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
518 psz_thread[19] = '\0';
521 psz_foo[i_level] = '\0';
522 msg_Info( p_this, "%so %s %p%s%s%s%s", psz_foo, p_this->psz_object_type,
523 p_this, psz_name, psz_thread, psz_refcount, psz_children );
524 psz_foo[i_level] = i_back;
526 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
528 msg_Warn( p_this, "structure tree is too deep" );
532 for( i = 0 ; i < p_this->i_children ; i++ )
536 psz_foo[i_level-1] = ' ';
538 if( psz_foo[i_level-2] == '`' )
540 psz_foo[i_level-2] = ' ';
544 if( i == p_this->i_children - 1 )
546 psz_foo[i_level] = '`';
550 psz_foo[i_level] = '|';
553 psz_foo[i_level+1] = '-';
554 psz_foo[i_level+2] = '\0';
556 vlc_dumpstructure_inner( p_this->pp_children[i], i_level + 2, psz_foo );