1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: objects.c,v 1.12 2002/07/31 20:56:53 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_GENERIC;
105 psz_type = "generic";
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 p_new->p_private = NULL;
151 vlc_mutex_init( p_new, &p_new->object_lock );
152 vlc_cond_init( p_new, &p_new->object_wait );
154 //msg_Dbg( p_new, "created object" );
159 /*****************************************************************************
160 * vlc_object_destroy: destroy a vlc object
161 *****************************************************************************
162 * This function destroys an object that has been previously allocated with
163 * vlc_object_create. The object's refcount must be zero and it must not be
164 * attached to other objects in any way.
165 *****************************************************************************/
166 void __vlc_object_destroy( vlc_object_t *p_this )
170 if( p_this->i_children )
172 msg_Err( p_this, "cannot delete object with children" );
173 vlc_dumpstructure( p_this );
177 if( p_this->i_parents )
179 msg_Err( p_this, "cannot delete object with parents" );
180 vlc_dumpstructure( p_this );
184 while( p_this->i_refcount )
188 /* Don't warn immediately ... 100ms seems OK */
191 msg_Warn( p_this, "refcount is %i, delaying before deletion",
192 p_this->i_refcount );
194 else if( i_delay == 12 )
196 msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
197 p_this->i_refcount );
199 else if( i_delay == 42 )
201 msg_Err( p_this, "we waited too long, cancelling destruction" );
208 //msg_Dbg( p_this, "destroyed object" );
210 vlc_mutex_destroy( &p_this->object_lock );
211 vlc_cond_destroy( &p_this->object_wait );
216 /*****************************************************************************
217 * vlc_object_find: find a typed object and increment its refcount
218 *****************************************************************************
219 * This function recursively looks for a given object type. i_mode can be one
220 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
221 *****************************************************************************/
222 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
224 vlc_object_t *p_found;
226 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
228 /* If we are of the requested type ourselves, don't look further */
229 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
231 p_this->i_refcount++;
232 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
236 /* Otherwise, recursively look for the object */
237 if( (i_mode & 0x000f) == FIND_ANYWHERE )
239 p_found = vlc_object_find_inner( VLC_OBJECT(p_this->p_vlc),
241 (i_mode & ~0x000f) | FIND_CHILD );
245 p_found = vlc_object_find_inner( p_this, i_type, i_mode );
248 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
253 static vlc_object_t * vlc_object_find_inner( vlc_object_t *p_this,
254 int i_type, int i_mode )
259 switch( i_mode & 0x000f )
262 for( i = p_this->i_parents; i--; )
264 p_tmp = p_this->pp_parents[i];
265 if( p_tmp->i_object_type == i_type )
270 else if( p_tmp->i_parents )
272 p_tmp = vlc_object_find_inner( p_tmp, i_type, i_mode );
282 for( i = p_this->i_children; i--; )
284 p_tmp = p_this->pp_children[i];
285 if( p_tmp->i_object_type == i_type )
290 else if( p_tmp->i_children )
292 p_tmp = vlc_object_find_inner( p_tmp, i_type, i_mode );
302 /* Handled in vlc_object_find */
309 /*****************************************************************************
310 * vlc_object_yield: increment an object refcount
311 *****************************************************************************/
312 void __vlc_object_yield( vlc_object_t *p_this )
314 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
315 p_this->i_refcount++;
316 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
319 /*****************************************************************************
320 * vlc_object_release: decrement an object refcount
321 *****************************************************************************/
322 void __vlc_object_release( vlc_object_t *p_this )
324 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
325 p_this->i_refcount--;
326 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
329 /*****************************************************************************
330 * vlc_object_attach: attach object to a parent object
331 *****************************************************************************
332 * This function sets p_this as a child of p_parent, and p_parent as a parent
333 * of p_this. This link can be undone using vlc_object_detach.
334 *****************************************************************************/
335 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
337 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
340 p_this->pp_parents = (vlc_object_t **)realloc( p_this->pp_parents,
341 p_this->i_parents * sizeof(vlc_object_t *) );
342 p_this->pp_parents[p_this->i_parents - 1] = p_parent;
344 p_parent->i_children++;
345 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
346 p_parent->i_children * sizeof(vlc_object_t *) );
347 p_parent->pp_children[p_parent->i_children - 1] = p_this;
349 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
353 /* vlc_object_setchild: attach a child object */
354 void __vlc_object_setchild( vlc_object_t *p_this, vlc_object_t *p_child )
356 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
358 p_this->i_children++;
359 p_this->pp_children = (vlc_object_t **)realloc( p_this->pp_children,
360 p_this->i_children * sizeof(vlc_object_t *) );
361 p_this->pp_children[p_this->i_children - 1] = p_child;
363 p_child->i_parents++;
364 p_child->pp_parents = (vlc_object_t **)realloc( p_child->pp_parents,
365 p_child->i_parents * sizeof(vlc_object_t *) );
366 p_child->pp_parents[p_child->i_parents - 1] = p_this;
368 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
372 /*****************************************************************************
373 * vlc_object_detach_all: detach object from its parents
374 *****************************************************************************
375 * This function unlinks an object from all its parents. It is up to the
376 * object to get rid of its children, so this function doesn't do anything
378 *****************************************************************************/
379 void __vlc_object_detach_all( vlc_object_t *p_this )
381 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
383 /* FIXME: BORK ! BORK ! BORK !!! THIS STUFF IS BORKED !! FIXME */
384 while( p_this->i_parents )
386 /* Not very effective because we know the index, but we'd have to
387 * parse p_parent->pp_children anyway. Plus, we remove duplicates
388 * by not using the object's index */
389 vlc_object_detach_inner( p_this, p_this->pp_parents[0] );
392 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
395 /*****************************************************************************
396 * vlc_object_detach: remove a parent/child link
397 *****************************************************************************
398 * This function removes all links between an object and a given parent.
399 *****************************************************************************/
400 void __vlc_object_detach( vlc_object_t *p_this, vlc_object_t *p_parent )
402 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
403 vlc_object_detach_inner( p_this, p_parent );
404 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
407 static void vlc_object_detach_inner( vlc_object_t *p_this,
408 vlc_object_t *p_parent )
412 /* Remove all of p_this's parents which are p_parent */
413 for( i_index = p_this->i_parents ; i_index-- ; )
415 if( p_this->pp_parents[i_index] == p_parent )
418 for( i = i_index ; i < p_this->i_parents ; i++ )
420 p_this->pp_parents[i] = p_this->pp_parents[i+1];
425 if( p_this->i_parents )
427 p_this->pp_parents = (vlc_object_t **)realloc( p_this->pp_parents,
428 p_this->i_parents * sizeof(vlc_object_t *) );
432 free( p_this->pp_parents );
433 p_this->pp_parents = NULL;
436 /* Remove all of p_parent's children which are p_this */
437 for( i_index = p_parent->i_children ; i_index-- ; )
439 if( p_parent->pp_children[i_index] == p_this )
441 p_parent->i_children--;
442 for( i = i_index ; i < p_parent->i_children ; i++ )
444 p_parent->pp_children[i] = p_parent->pp_children[i+1];
449 if( p_parent->i_children )
451 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
452 p_parent->i_children * sizeof(vlc_object_t *) );
456 free( p_parent->pp_children );
457 p_parent->pp_children = NULL;
461 /*****************************************************************************
462 * vlc_dumpstructure: print the current vlc structure
463 *****************************************************************************
464 * This function prints an ASCII tree showing the connections between vlc
465 * objects, and additional information such as their refcount, thread ID,
467 *****************************************************************************/
468 void __vlc_dumpstructure( vlc_object_t *p_this )
470 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
472 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
474 vlc_dumpstructure_inner( p_this, 0, psz_foo );
475 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
478 static void vlc_dumpstructure_inner( vlc_object_t *p_this,
479 int i_level, char *psz_foo )
482 char i_back = psz_foo[i_level];
483 char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
486 if( p_this->psz_object_name )
488 snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
493 psz_children[0] = '\0';
494 switch( p_this->i_children )
499 strcpy( psz_children, ", 1 child" );
502 snprintf( psz_children, 20,
503 ", %i children", p_this->i_children );
504 psz_children[19] = '\0';
508 psz_refcount[0] = '\0';
509 if( p_this->i_refcount )
511 snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
512 psz_refcount[19] = '\0';
515 psz_thread[0] = '\0';
516 if( p_this->b_thread )
518 snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
519 psz_thread[19] = '\0';
522 psz_foo[i_level] = '\0';
523 msg_Info( p_this, "%so %s %p%s%s%s%s", psz_foo, p_this->psz_object_type,
524 p_this, psz_name, psz_thread, psz_refcount, psz_children );
525 psz_foo[i_level] = i_back;
527 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
529 msg_Warn( p_this, "structure tree is too deep" );
533 for( i = 0 ; i < p_this->i_children ; i++ )
537 psz_foo[i_level-1] = ' ';
539 if( psz_foo[i_level-2] == '`' )
541 psz_foo[i_level-2] = ' ';
545 if( i == p_this->i_children - 1 )
547 psz_foo[i_level] = '`';
551 psz_foo[i_level] = '|';
554 psz_foo[i_level+1] = '-';
555 psz_foo[i_level+2] = '\0';
557 vlc_dumpstructure_inner( p_this->pp_children[i], i_level + 2, psz_foo );