1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: objects.c,v 1.5 2002/06/02 15:51:30 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 "vdec_ext-plugins.h"
42 #include "audio_output.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 //msg_Dbg( p_new, "created object" );
154 /*****************************************************************************
155 * vlc_object_destroy: destroy a vlc object
156 *****************************************************************************
157 * This function destroys an object that has been previously allocated with
158 * vlc_object_create. The object's refcount must be zero and it must not be
159 * attached to other objects in any way.
160 *****************************************************************************/
161 void __vlc_object_destroy( vlc_object_t *p_this )
163 if( p_this->i_refcount )
165 msg_Err( p_this, "refcount is %i", p_this->i_refcount );
166 vlc_dumpstructure( p_this );
169 if( p_this->i_children )
171 msg_Err( p_this, "object still has children" );
172 vlc_dumpstructure( p_this );
175 if( p_this->i_parents )
177 msg_Err( p_this, "object still has parents" );
178 vlc_dumpstructure( p_this );
181 //msg_Dbg( p_this, "destroyed object" );
186 /*****************************************************************************
187 * vlc_object_find: find a typed object and increment its refcount
188 *****************************************************************************
189 * This function recursively looks for a given object type. i_mode can be one
190 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
191 *****************************************************************************/
192 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
194 vlc_object_t *p_found;
196 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
198 /* If we are of the requested type ourselves, don't look further */
199 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
201 p_this->i_refcount++;
202 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
206 /* Otherwise, recursively look for the object */
207 if( (i_mode & 0x000f) == FIND_ANYWHERE )
209 p_found = vlc_object_find_inner( CAST_TO_VLC_OBJECT(p_this->p_vlc),
211 (i_mode & ~0x000f) | FIND_CHILD );
215 p_found = vlc_object_find_inner( p_this, i_type, i_mode );
218 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
223 static vlc_object_t * vlc_object_find_inner( vlc_object_t *p_this,
224 int i_type, int i_mode )
229 switch( i_mode & 0x000f )
232 for( i = p_this->i_parents; i--; )
234 p_tmp = p_this->pp_parents[i];
235 if( p_tmp->i_object_type == i_type )
240 else if( p_tmp->i_parents )
242 p_tmp = vlc_object_find_inner( p_tmp, i_type, i_mode );
252 for( i = p_this->i_children; i--; )
254 p_tmp = p_this->pp_children[i];
255 if( p_tmp->i_object_type == i_type )
260 else if( p_tmp->i_children )
262 p_tmp = vlc_object_find_inner( p_tmp, i_type, i_mode );
272 /* Handled in vlc_object_find */
279 /*****************************************************************************
280 * vlc_object_yield: increment an object refcount
281 *****************************************************************************/
282 void __vlc_object_yield( vlc_object_t *p_this )
284 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
285 p_this->i_refcount++;
286 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
289 /*****************************************************************************
290 * vlc_object_release: decrement an object refcount
291 *****************************************************************************/
292 void __vlc_object_release( vlc_object_t *p_this )
294 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
295 p_this->i_refcount--;
296 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
299 /*****************************************************************************
300 * vlc_object_attach: attach object to a parent object
301 *****************************************************************************
302 * This function sets p_this as a child of p_parent, and p_parent as a parent
303 * of p_this. This link can be undone using vlc_object_detach.
304 *****************************************************************************/
305 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
307 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
310 p_this->pp_parents = (vlc_object_t **)realloc( p_this->pp_parents,
311 p_this->i_parents * sizeof(vlc_object_t *) );
312 p_this->pp_parents[p_this->i_parents - 1] = p_parent;
314 p_parent->i_children++;
315 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
316 p_parent->i_children * sizeof(vlc_object_t *) );
317 p_parent->pp_children[p_parent->i_children - 1] = p_this;
319 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
323 /* vlc_object_setchild: attach a child object */
324 void __vlc_object_setchild( vlc_object_t *p_this, vlc_object_t *p_child )
326 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
328 p_this->i_children++;
329 p_this->pp_children = (vlc_object_t **)realloc( p_this->pp_children,
330 p_this->i_children * sizeof(vlc_object_t *) );
331 p_this->pp_children[p_this->i_children - 1] = p_child;
333 p_child->i_parents++;
334 p_child->pp_parents = (vlc_object_t **)realloc( p_child->pp_parents,
335 p_child->i_parents * sizeof(vlc_object_t *) );
336 p_child->pp_parents[p_child->i_parents - 1] = p_this;
338 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
342 /*****************************************************************************
343 * vlc_object_detach_all: detach object from its parents
344 *****************************************************************************
345 * This function unlinks an object from all its parents. It is up to the
346 * object to get rid of its children, so this function doesn't do anything
348 *****************************************************************************/
349 void __vlc_object_detach_all( vlc_object_t *p_this )
351 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
353 /* FIXME: BORK ! BORK ! BORK !!! THIS STUFF IS BORKED !! FIXME */
354 while( p_this->i_parents )
356 /* Not very effective because we know the index, but we'd have to
357 * parse p_parent->pp_children anyway. Plus, we remove duplicates
358 * by not using the object's index */
359 vlc_object_detach_inner( p_this, p_this->pp_parents[0] );
362 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
365 /*****************************************************************************
366 * vlc_object_detach: remove a parent/child link
367 *****************************************************************************
368 * This function removes all links between an object and a given parent.
369 *****************************************************************************/
370 void __vlc_object_detach( vlc_object_t *p_this, vlc_object_t *p_parent )
372 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
373 vlc_object_detach_inner( p_this, p_parent );
374 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
377 static void vlc_object_detach_inner( vlc_object_t *p_this,
378 vlc_object_t *p_parent )
382 /* Remove all of p_this's parents which are p_parent */
383 for( i_index = p_this->i_parents ; i_index-- ; )
385 if( p_this->pp_parents[i_index] == p_parent )
388 for( i = i_index ; i < p_this->i_parents ; i++ )
390 p_this->pp_parents[i] = p_this->pp_parents[i+1];
395 if( p_this->i_parents )
397 p_this->pp_parents = (vlc_object_t **)realloc( p_this->pp_parents,
398 p_this->i_parents * sizeof(vlc_object_t *) );
402 free( p_this->pp_parents );
403 p_this->pp_parents = NULL;
406 /* Remove all of p_parent's children which are p_this */
407 for( i_index = p_parent->i_children ; i_index-- ; )
409 if( p_parent->pp_children[i_index] == p_this )
411 p_parent->i_children--;
412 for( i = i_index ; i < p_parent->i_children ; i++ )
414 p_parent->pp_children[i] = p_parent->pp_children[i+1];
419 if( 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 *) );
426 free( p_parent->pp_children );
427 p_parent->pp_children = NULL;
431 /*****************************************************************************
432 * vlc_dumpstructure: print the current vlc structure
433 *****************************************************************************
434 * This function prints an ASCII tree showing the connections between vlc
435 * objects, and additional information such as their refcount, thread ID,
437 *****************************************************************************/
438 void __vlc_dumpstructure( vlc_object_t *p_this )
440 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
442 vlc_mutex_lock( &p_this->p_vlc->structure_lock );
444 vlc_dumpstructure_inner( p_this, 0, psz_foo );
445 vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
448 static void vlc_dumpstructure_inner( vlc_object_t *p_this,
449 int i_level, char *psz_foo )
452 char i_back = psz_foo[i_level];
453 char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
456 if( p_this->psz_object_name )
458 snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
463 psz_children[0] = '\0';
464 switch( p_this->i_children )
469 strcpy( psz_children, ", 1 child" );
472 snprintf( psz_children, 20, ", %i children", p_this->i_children );
473 psz_children[19] = '\0';
477 psz_refcount[0] = '\0';
478 if( p_this->i_refcount )
480 snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
481 psz_refcount[19] = '\0';
484 psz_thread[0] = '\0';
485 if( p_this->b_thread )
487 snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
488 psz_thread[19] = '\0';
491 psz_foo[i_level] = '\0';
492 msg_Info( p_this, "%so %s %p%s%s%s%s", psz_foo, p_this->psz_object_type,
493 p_this, psz_name, psz_thread, psz_refcount, psz_children );
494 psz_foo[i_level] = i_back;
496 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
498 msg_Warn( p_this, "structure tree is too deep" );
502 for( i = 0 ; i < p_this->i_children ; i++ )
506 psz_foo[i_level-1] = ' ';
508 if( psz_foo[i_level-2] == '`' )
510 psz_foo[i_level-2] = ' ';
514 if( i == p_this->i_children - 1 )
516 psz_foo[i_level] = '`';
520 psz_foo[i_level] = '|';
523 psz_foo[i_level+1] = '-';
524 psz_foo[i_level+2] = '\0';
526 vlc_dumpstructure_inner( p_this->pp_children[i], i_level + 2, psz_foo );