1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: objects.c,v 1.10 2002/06/07 23:53:44 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 "vdec_ext-plugins.h"
42 #include "audio_output.h"
44 #include "vlc_playlist.h"
45 #include "interface.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 static vlc_object_t * vlc_object_find_inner( vlc_object_t *, int, int );
51 static void vlc_object_detach_inner( vlc_object_t *, vlc_object_t * );
52 static void vlc_dumpstructure_inner( vlc_object_t *, int, char * );
54 /*****************************************************************************
55 * vlc_object_create: initialize a vlc object
56 *****************************************************************************
57 * This function allocates memory for a vlc object and initializes it. If
58 * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
59 * so on, vlc_object_create will use its value for the object size.
60 *****************************************************************************/
61 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
70 i_size = sizeof(vlc_t);
73 case VLC_OBJECT_MODULE:
74 i_size = sizeof(module_t);
78 i_size = sizeof(intf_thread_t);
79 psz_type = "interface";
81 case VLC_OBJECT_PLAYLIST:
82 i_size = sizeof(playlist_t);
83 psz_type = "playlist";
85 case VLC_OBJECT_INPUT:
86 i_size = sizeof(input_thread_t);
89 case VLC_OBJECT_DECODER:
90 i_size = sizeof(decoder_fifo_t);
94 i_size = sizeof(vout_thread_t);
95 psz_type = "video output";
98 i_size = sizeof(aout_thread_t);
99 psz_type = "audio output";
102 i_size = i_type > sizeof(vlc_object_t)
103 ? i_type : sizeof(vlc_object_t);
104 i_type = VLC_OBJECT_PRIVATE;
105 psz_type = "private";
109 p_new = malloc( i_size );
116 memset( p_new, 0, i_size );
118 p_new->i_object_type = i_type;
119 p_new->psz_object_type = psz_type;
121 p_new->psz_object_name = NULL;
123 p_new->i_refcount = 0;
127 /* If i_type is root, then p_new is our own p_vlc */
128 if( i_type == VLC_OBJECT_ROOT )
130 p_new->p_vlc = (vlc_t*)p_new;
131 p_new->p_vlc->i_counter = 0;
132 p_new->i_object_id = 0;
136 p_new->p_vlc = p_this->p_vlc;
138 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
139 p_new->p_vlc->i_counter++;
140 p_new->i_object_id = p_new->p_vlc->i_counter;
141 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
144 p_new->pp_parents = NULL;
145 p_new->i_parents = 0;
146 p_new->pp_children = NULL;
147 p_new->i_children = 0;
149 vlc_mutex_init( p_new, &p_new->object_lock );
150 vlc_cond_init( &p_new->object_wait );
152 //msg_Dbg( p_new, "created object" );
157 /*****************************************************************************
158 * vlc_object_destroy: destroy a vlc object
159 *****************************************************************************
160 * This function destroys an object that has been previously allocated with
161 * vlc_object_create. The object's refcount must be zero and it must not be
162 * attached to other objects in any way.
163 *****************************************************************************/
164 void __vlc_object_destroy( vlc_object_t *p_this )
168 if( p_this->i_children )
170 msg_Err( p_this, "cannot delete object with children" );
171 vlc_dumpstructure( p_this );
175 if( p_this->i_parents )
177 msg_Err( p_this, "cannot delete object with parents" );
178 vlc_dumpstructure( p_this );
182 while( p_this->i_refcount )
186 /* Don't warn immediately ... 100ms seems OK */
189 msg_Warn( p_this, "refcount is %i, delaying before deletion",
190 p_this->i_refcount );
192 else if( i_delay == 12 )
194 msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
195 p_this->i_refcount );
197 else if( i_delay == 42 )
199 msg_Err( p_this, "we waited too long, cancelling destruction" );
206 //msg_Dbg( p_this, "destroyed object" );
208 vlc_mutex_destroy( &p_this->object_lock );
209 vlc_cond_destroy( &p_this->object_wait );
214 /*****************************************************************************
215 * vlc_object_find: find a typed object and increment its refcount
216 *****************************************************************************
217 * This function recursively looks for a given object type. i_mode can be one
218 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
219 *****************************************************************************/
220 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
222 vlc_object_t *p_found;
224 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
226 /* If we are of the requested type ourselves, don't look further */
227 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
229 p_this->i_refcount++;
230 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
234 /* Otherwise, recursively look for the object */
235 if( (i_mode & 0x000f) == FIND_ANYWHERE )
237 p_found = vlc_object_find_inner( CAST_TO_VLC_OBJECT(p_this->p_vlc),
239 (i_mode & ~0x000f) | FIND_CHILD );
243 p_found = vlc_object_find_inner( p_this, i_type, i_mode );
246 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
251 static vlc_object_t * vlc_object_find_inner( vlc_object_t *p_this,
252 int i_type, int i_mode )
257 switch( i_mode & 0x000f )
260 for( i = p_this->i_parents; i--; )
262 p_tmp = p_this->pp_parents[i];
263 if( p_tmp->i_object_type == i_type )
268 else if( p_tmp->i_parents )
270 p_tmp = vlc_object_find_inner( p_tmp, i_type, i_mode );
280 for( i = p_this->i_children; i--; )
282 p_tmp = p_this->pp_children[i];
283 if( p_tmp->i_object_type == i_type )
288 else if( p_tmp->i_children )
290 p_tmp = vlc_object_find_inner( p_tmp, i_type, i_mode );
300 /* Handled in vlc_object_find */
307 /*****************************************************************************
308 * vlc_object_yield: increment an object refcount
309 *****************************************************************************/
310 void __vlc_object_yield( vlc_object_t *p_this )
312 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
313 p_this->i_refcount++;
314 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
317 /*****************************************************************************
318 * vlc_object_release: decrement an object refcount
319 *****************************************************************************/
320 void __vlc_object_release( vlc_object_t *p_this )
322 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
323 p_this->i_refcount--;
324 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
327 /*****************************************************************************
328 * vlc_object_attach: attach object to a parent object
329 *****************************************************************************
330 * This function sets p_this as a child of p_parent, and p_parent as a parent
331 * of p_this. This link can be undone using vlc_object_detach.
332 *****************************************************************************/
333 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
335 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
338 p_this->pp_parents = (vlc_object_t **)realloc( p_this->pp_parents,
339 p_this->i_parents * sizeof(vlc_object_t *) );
340 p_this->pp_parents[p_this->i_parents - 1] = p_parent;
342 p_parent->i_children++;
343 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
344 p_parent->i_children * sizeof(vlc_object_t *) );
345 p_parent->pp_children[p_parent->i_children - 1] = p_this;
347 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
351 /* vlc_object_setchild: attach a child object */
352 void __vlc_object_setchild( vlc_object_t *p_this, vlc_object_t *p_child )
354 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
356 p_this->i_children++;
357 p_this->pp_children = (vlc_object_t **)realloc( p_this->pp_children,
358 p_this->i_children * sizeof(vlc_object_t *) );
359 p_this->pp_children[p_this->i_children - 1] = p_child;
361 p_child->i_parents++;
362 p_child->pp_parents = (vlc_object_t **)realloc( p_child->pp_parents,
363 p_child->i_parents * sizeof(vlc_object_t *) );
364 p_child->pp_parents[p_child->i_parents - 1] = p_this;
366 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
370 /*****************************************************************************
371 * vlc_object_detach_all: detach object from its parents
372 *****************************************************************************
373 * This function unlinks an object from all its parents. It is up to the
374 * object to get rid of its children, so this function doesn't do anything
376 *****************************************************************************/
377 void __vlc_object_detach_all( vlc_object_t *p_this )
379 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
381 /* FIXME: BORK ! BORK ! BORK !!! THIS STUFF IS BORKED !! FIXME */
382 while( p_this->i_parents )
384 /* Not very effective because we know the index, but we'd have to
385 * parse p_parent->pp_children anyway. Plus, we remove duplicates
386 * by not using the object's index */
387 vlc_object_detach_inner( p_this, p_this->pp_parents[0] );
390 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
393 /*****************************************************************************
394 * vlc_object_detach: remove a parent/child link
395 *****************************************************************************
396 * This function removes all links between an object and a given parent.
397 *****************************************************************************/
398 void __vlc_object_detach( vlc_object_t *p_this, vlc_object_t *p_parent )
400 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
401 vlc_object_detach_inner( p_this, p_parent );
402 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
405 static void vlc_object_detach_inner( vlc_object_t *p_this,
406 vlc_object_t *p_parent )
410 /* Remove all of p_this's parents which are p_parent */
411 for( i_index = p_this->i_parents ; i_index-- ; )
413 if( p_this->pp_parents[i_index] == p_parent )
416 for( i = i_index ; i < p_this->i_parents ; i++ )
418 p_this->pp_parents[i] = p_this->pp_parents[i+1];
423 if( p_this->i_parents )
425 p_this->pp_parents = (vlc_object_t **)realloc( p_this->pp_parents,
426 p_this->i_parents * sizeof(vlc_object_t *) );
430 free( p_this->pp_parents );
431 p_this->pp_parents = NULL;
434 /* Remove all of p_parent's children which are p_this */
435 for( i_index = p_parent->i_children ; i_index-- ; )
437 if( p_parent->pp_children[i_index] == p_this )
439 p_parent->i_children--;
440 for( i = i_index ; i < p_parent->i_children ; i++ )
442 p_parent->pp_children[i] = p_parent->pp_children[i+1];
447 if( p_parent->i_children )
449 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
450 p_parent->i_children * sizeof(vlc_object_t *) );
454 free( p_parent->pp_children );
455 p_parent->pp_children = NULL;
459 /*****************************************************************************
460 * vlc_dumpstructure: print the current vlc structure
461 *****************************************************************************
462 * This function prints an ASCII tree showing the connections between vlc
463 * objects, and additional information such as their refcount, thread ID,
465 *****************************************************************************/
466 void __vlc_dumpstructure( vlc_object_t *p_this )
468 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
470 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
472 vlc_dumpstructure_inner( p_this, 0, psz_foo );
473 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
476 static void vlc_dumpstructure_inner( vlc_object_t *p_this,
477 int i_level, char *psz_foo )
480 char i_back = psz_foo[i_level];
481 char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
484 if( p_this->psz_object_name )
486 snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
491 psz_children[0] = '\0';
492 switch( p_this->i_children )
497 strcpy( psz_children, ", 1 child" );
500 snprintf( psz_children, 20, ", %i children", p_this->i_children );
501 psz_children[19] = '\0';
505 psz_refcount[0] = '\0';
506 if( p_this->i_refcount )
508 snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
509 psz_refcount[19] = '\0';
512 psz_thread[0] = '\0';
513 if( p_this->b_thread )
515 snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
516 psz_thread[19] = '\0';
519 psz_foo[i_level] = '\0';
520 msg_Info( p_this, "%so %s %p%s%s%s%s", psz_foo, p_this->psz_object_type,
521 p_this, psz_name, psz_thread, psz_refcount, psz_children );
522 psz_foo[i_level] = i_back;
524 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
526 msg_Warn( p_this, "structure tree is too deep" );
530 for( i = 0 ; i < p_this->i_children ; i++ )
534 psz_foo[i_level-1] = ' ';
536 if( psz_foo[i_level-2] == '`' )
538 psz_foo[i_level-2] = ' ';
542 if( i == p_this->i_children - 1 )
544 psz_foo[i_level] = '`';
548 psz_foo[i_level] = '|';
551 psz_foo[i_level+1] = '-';
552 psz_foo[i_level+2] = '\0';
554 vlc_dumpstructure_inner( p_this->pp_children[i], i_level + 2, psz_foo );