1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
26 * This file contains the functions to handle the vlc_object_t type
30 /*****************************************************************************
32 *****************************************************************************/
39 #include "../libvlc.h"
42 #include "audio_output/aout_internal.h"
44 #include <vlc_access.h>
45 #include <vlc_demux.h>
46 #include <vlc_stream.h>
49 #include "stream_output/stream_output.h"
51 #include "vlc_playlist.h"
52 #include "vlc_interface.h"
53 #include "vlc_codec.h"
54 #include "vlc_filter.h"
58 #include "variables.h"
64 # include <errno.h> /* ENOSYS */
68 /*****************************************************************************
70 *****************************************************************************/
72 const vlc_destructor_t kVLCDestructor = NULL;
74 /*****************************************************************************
76 *****************************************************************************/
77 static int DumpCommand( vlc_object_t *, char const *,
78 vlc_value_t, vlc_value_t, void * );
80 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
81 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
82 static void DetachObject ( vlc_object_t * );
83 static void PrintObject ( vlc_object_t *, const char * );
84 static void DumpStructure ( vlc_object_t *, int, char * );
85 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
86 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
88 static vlc_list_t * NewList ( int );
89 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
90 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
91 static int CountChildren ( vlc_object_t *, int );
92 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
94 static void vlc_object_destroy( vlc_object_t *p_this );
95 static void vlc_object_yield_locked( vlc_object_t *p_this );
97 /*****************************************************************************
98 * Local structure lock
99 *****************************************************************************/
100 static vlc_mutex_t structure_lock;
101 static vlc_object_internals_t global_internals;
103 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
104 int i_type, const char *psz_type )
107 vlc_object_internals_t *p_priv;
109 if( i_type == VLC_OBJECT_GLOBAL )
112 p_priv = &global_internals;
113 memset( p_priv, 0, sizeof( *p_priv ) );
117 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
121 p_new = (vlc_object_t *)(p_priv + 1);
124 p_new->p_internals = p_priv;
125 p_new->i_object_type = i_type;
126 p_new->psz_object_type = psz_type;
128 p_new->psz_object_name = NULL;
130 p_new->b_die = VLC_FALSE;
131 p_new->b_error = VLC_FALSE;
132 p_new->b_dead = VLC_FALSE;
133 p_priv->b_attached = VLC_FALSE;
134 p_new->b_force = VLC_FALSE;
136 p_new->psz_header = NULL;
138 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
139 p_new->i_flags |= OBJECT_FLAGS_NODBG;
140 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
141 p_new->i_flags |= OBJECT_FLAGS_QUIET;
142 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
143 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
145 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
147 if( !p_priv->p_vars )
149 if( i_type != VLC_OBJECT_GLOBAL )
154 if( i_type == VLC_OBJECT_GLOBAL )
156 /* If i_type is global, then p_new is actually p_libvlc_global */
157 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
158 p_new->p_libvlc = NULL;
160 p_libvlc_global->i_counter = 0;
161 p_new->i_object_id = 0;
163 p_libvlc_global->i_objects = 1;
164 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
165 p_libvlc_global->pp_objects[0] = p_new;
166 p_priv->b_attached = VLC_TRUE;
170 libvlc_global_data_t *p_libvlc_global = vlc_global();
171 if( i_type == VLC_OBJECT_LIBVLC )
173 p_new->p_libvlc = (libvlc_int_t*)p_new;
174 p_priv->b_attached = VLC_TRUE;
178 p_new->p_libvlc = p_this->p_libvlc;
181 vlc_mutex_lock( &structure_lock );
183 p_libvlc_global->i_counter++;
184 p_new->i_object_id = p_libvlc_global->i_counter;
186 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
187 * useless to try and recover anything if pp_objects gets smashed. */
188 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
191 vlc_mutex_unlock( &structure_lock );
194 p_priv->i_refcount = 1;
195 p_priv->pf_destructor = kVLCDestructor;
196 p_priv->b_thread = VLC_FALSE;
197 p_new->p_parent = NULL;
198 p_new->pp_children = NULL;
199 p_new->i_children = 0;
201 p_new->p_private = NULL;
203 /* Initialize mutexes and condvars */
204 vlc_mutex_init( p_new, &p_new->object_lock );
205 vlc_cond_init( p_new, &p_new->object_wait );
206 vlc_mutex_init( p_new, &p_priv->var_lock );
207 vlc_spin_init( &p_priv->spin );
208 p_priv->pipes[0] = p_priv->pipes[1] = -1;
210 if( i_type == VLC_OBJECT_GLOBAL )
212 vlc_mutex_init( p_new, &structure_lock );
215 if( i_type == VLC_OBJECT_LIBVLC )
217 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
218 var_AddCallback( p_new, "list", DumpCommand, NULL );
219 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
220 var_AddCallback( p_new, "tree", DumpCommand, NULL );
221 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
222 var_AddCallback( p_new, "vars", DumpCommand, NULL );
230 * Allocates and initializes a vlc object.
232 * @param i_type known object type (all of them are negative integer values),
233 * or object byte size (always positive).
235 * @return the new object, or NULL on error.
237 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
239 const char * psz_type;
244 case VLC_OBJECT_GLOBAL:
245 i_size = sizeof(libvlc_global_data_t);
248 case VLC_OBJECT_LIBVLC:
249 i_size = sizeof(libvlc_int_t);
252 case VLC_OBJECT_INTF:
253 i_size = sizeof(intf_thread_t);
254 psz_type = "interface";
256 case VLC_OBJECT_DIALOGS:
257 i_size = sizeof(intf_thread_t);
258 psz_type = "dialogs";
260 case VLC_OBJECT_PLAYLIST:
261 i_size = sizeof(playlist_t);
262 psz_type = "playlist";
264 case VLC_OBJECT_INPUT:
265 i_size = sizeof(input_thread_t);
268 case VLC_OBJECT_DEMUX:
269 i_size = sizeof(demux_t);
272 case VLC_OBJECT_ACCESS:
273 i_size = sizeof(access_t);
276 case VLC_OBJECT_DECODER:
277 i_size = sizeof(decoder_t);
278 psz_type = "decoder";
280 case VLC_OBJECT_PACKETIZER:
281 i_size = sizeof(decoder_t);
282 psz_type = "packetizer";
284 case VLC_OBJECT_ENCODER:
285 i_size = sizeof(encoder_t);
286 psz_type = "encoder";
288 case VLC_OBJECT_FILTER:
289 i_size = sizeof(filter_t);
292 case VLC_OBJECT_VOUT:
293 i_size = sizeof(vout_thread_t);
294 psz_type = "video output";
296 case VLC_OBJECT_AOUT:
297 i_size = sizeof(aout_instance_t);
298 psz_type = "audio output";
300 case VLC_OBJECT_SOUT:
301 i_size = sizeof(sout_instance_t);
302 psz_type = "stream output";
304 case VLC_OBJECT_OPENGL:
305 i_size = sizeof( vout_thread_t );
308 case VLC_OBJECT_ANNOUNCE:
309 i_size = sizeof( announce_handler_t );
310 psz_type = "announce";
312 case VLC_OBJECT_INTERACTION:
313 i_size = sizeof( interaction_t );
314 psz_type = "interaction";
317 i_size = i_type > (int)sizeof(vlc_object_t)
318 ? i_type : (int)sizeof(vlc_object_t);
319 i_type = VLC_OBJECT_GENERIC;
320 psz_type = "generic";
324 return vlc_custom_create( p_this, i_size, i_type, psz_type );
329 ****************************************************************************
330 * Set the destructor of a vlc object
332 * This function sets the destructor of the vlc object. It will be called
333 * when the object is destroyed when the its refcount reaches 0.
334 * (It is called by the internal function vlc_object_destroy())
335 *****************************************************************************/
336 void __vlc_object_set_destructor( vlc_object_t *p_this,
337 vlc_destructor_t pf_destructor )
339 vlc_object_internals_t *p_priv = vlc_internals(p_this );
341 vlc_mutex_lock( &structure_lock );
342 p_priv->pf_destructor = pf_destructor;
343 vlc_mutex_unlock( &structure_lock );
347 ****************************************************************************
348 * Destroy a vlc object (Internal)
350 * This function destroys an object that has been previously allocated with
351 * vlc_object_create. The object's refcount must be zero and it must not be
352 * attached to other objects in any way.
353 *****************************************************************************/
354 static void vlc_object_destroy( vlc_object_t *p_this )
356 vlc_object_internals_t *p_priv = vlc_internals( p_this );
358 /* Automatically detach the object from its parents */
359 if( p_this->p_parent ) vlc_object_detach( p_this );
362 /* Send a kill to the object's thread if applicable */
363 vlc_object_kill( p_this );
365 /* If we are running on a thread, wait until it ends */
366 if( p_priv->b_thread )
367 vlc_thread_join( p_this );
369 /* Call the custom "subclass" destructor */
370 if( p_priv->pf_destructor )
371 p_priv->pf_destructor( p_this );
374 if( p_this->i_children )
379 "ERROR: cannot delete object (%i, %s) with %d children\n",
380 p_this->i_object_id, p_this->psz_object_name,
381 p_this->i_children );
383 for( i = 0; i < p_this->i_children; i++ )
386 "ERROR: Remaining children object "
387 "(id:%i, type:%s, name:%s)\n",
388 p_this->pp_children[i]->i_object_id,
389 p_this->pp_children[i]->psz_object_type,
390 p_this->pp_children[i]->psz_object_name );
396 /* Destroy the associated variables, starting from the end so that
397 * no memmove calls have to be done. */
398 while( p_priv->i_vars )
400 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
403 free( p_priv->p_vars );
404 vlc_mutex_destroy( &p_priv->var_lock );
406 free( p_this->psz_header );
408 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
410 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
413 if( p_global->i_objects > 0 )
416 for( i = 0; i < p_global->i_objects; i++ )
418 /* We are leaking this object */
420 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
421 p_global->pp_objects[i]->i_object_id,
422 p_global->pp_objects[i]->psz_object_type,
423 p_global->pp_objects[i]->psz_object_name );
426 /* Strongly abort, cause we want these to be fixed */
430 /* We are the global object ... no need to lock. */
431 free( p_global->pp_objects );
432 p_global->pp_objects = NULL;
434 vlc_mutex_destroy( &structure_lock );
437 #if defined(WIN32) || defined(UNDER_CE)
438 /* if object has an associated thread, close it now */
439 if( p_priv->thread_id.hThread )
440 CloseHandle(p_priv->thread_id.hThread);
443 vlc_mutex_destroy( &p_this->object_lock );
444 vlc_cond_destroy( &p_this->object_wait );
445 vlc_spin_destroy( &p_priv->spin );
446 if( p_priv->pipes[1] != -1 )
447 close( p_priv->pipes[1] );
448 if( p_priv->pipes[0] != -1 )
449 close( p_priv->pipes[0] );
451 /* global is not dynamically allocated by vlc_object_create */
452 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
457 /** Inter-object signaling */
459 void __vlc_object_lock( vlc_object_t *obj )
461 vlc_mutex_lock( &obj->object_lock );
464 void __vlc_object_unlock( vlc_object_t *obj )
466 vlc_assert_locked( &obj->object_lock );
467 vlc_mutex_unlock( &obj->object_lock );
471 # include <winsock2.h>
472 # include <ws2tcpip.h>
475 * select()-able pipes emulated using Winsock
477 static int pipe (int fd[2])
480 int addrlen = sizeof (addr);
482 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
483 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
484 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
487 memset (&addr, 0, sizeof (addr));
488 addr.sin_family = AF_INET;
489 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
490 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
491 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
493 || connect (c, (PSOCKADDR)&addr, addrlen))
496 a = accept (l, NULL, NULL);
497 if (a == INVALID_SOCKET)
508 if (l != INVALID_SOCKET)
510 if (c != INVALID_SOCKET)
516 #define read( a, b, c ) recv (a, b, c, 0)
518 #define write( a, b, c ) send (a, b, c, 0)
520 #define close( a ) closesocket (a)
524 * Returns the readable end of a pipe that becomes readable once termination
525 * of the object is requested (vlc_object_kill()).
526 * This can be used to wake-up out of a select() or poll() event loop, such
527 * typically when doing network I/O.
529 * Note that the pipe will remain the same for the lifetime of the object.
530 * DO NOT read the pipe nor close it yourself. Ever.
532 * @param obj object that would be "killed"
533 * @return a readable pipe descriptor, or -1 on error.
535 int __vlc_object_waitpipe( vlc_object_t *obj )
537 int pfd[2] = { -1, -1 };
538 struct vlc_object_internals_t *internals = obj->p_internals;
539 vlc_bool_t killed = VLC_FALSE;
541 vlc_spin_lock (&internals->spin);
542 if (internals->pipes[0] == -1)
544 /* This can only ever happen if someone killed us without locking: */
545 assert (internals->pipes[1] == -1);
546 vlc_spin_unlock (&internals->spin);
551 vlc_spin_lock (&internals->spin);
552 if (internals->pipes[0] == -1)
554 internals->pipes[0] = pfd[0];
555 internals->pipes[1] = pfd[1];
556 pfd[0] = pfd[1] = -1;
560 vlc_spin_unlock (&internals->spin);
564 /* Race condition: vlc_object_kill() already invoked! */
567 vlc_spin_lock (&internals->spin);
568 fd = internals->pipes[1];
569 internals->pipes[1] = -1;
570 vlc_spin_unlock (&internals->spin);
572 msg_Dbg (obj, "waitpipe: object already dying");
577 /* Race condition: two threads call pipe() - unlikely */
583 return internals->pipes[0];
588 * Waits for the object to be signaled (using vlc_object_signal()).
589 * If the object already has a signal pending, this function will return
590 * immediately. It is asserted that the caller holds the object lock.
592 * @return true if the object is dying and should terminate.
594 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
596 vlc_assert_locked( &obj->object_lock );
597 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
603 * Waits for the object to be signaled (using vlc_object_signal()), or for
605 * If the object already has a signal pending, this function will return
606 * immediately. It is asserted that the caller holds the object lock.
608 * @return negative if the object is dying and should terminate,
609 * positive if the the object has been signaled but is not dying,
610 * 0 if timeout has been reached.
612 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
616 vlc_assert_locked( &obj->object_lock );
617 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
618 if( v == 0 ) /* signaled */
619 return obj->b_die ? -1 : 1;
625 * Checks whether an object has been "killed".
626 * The object lock must be held.
628 * Typical code for an object thread could be:
630 vlc_object_lock (self);
632 while (vlc_object_alive (self))
636 if (vlc_object_wait (self))
641 ...deinitialization...
642 vlc_object_unlock (self);
645 * @return true iff the object has not been killed yet
647 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
649 vlc_assert_locked( &obj->object_lock );
655 * Signals an object for which the lock is held.
657 void __vlc_object_signal_unlocked( vlc_object_t *obj )
659 vlc_assert_locked (&obj->object_lock);
660 vlc_cond_signal( &obj->object_wait );
665 * Requests termination of an object.
666 * If the object is LibVLC, also request to terminate all its children.
668 void __vlc_object_kill( vlc_object_t *p_this )
670 struct vlc_object_internals_t *internals = p_this->p_internals;
673 vlc_mutex_lock( &p_this->object_lock );
674 p_this->b_die = VLC_TRUE;
676 vlc_spin_lock (&internals->spin);
677 fd = internals->pipes[1];
678 internals->pipes[1] = -1;
679 vlc_spin_unlock (&internals->spin);
683 msg_Dbg (p_this, "waitpipe: object killed");
687 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
688 for( int i = 0; i < p_this->i_children ; i++ )
689 vlc_object_kill( p_this->pp_children[i] );
691 vlc_object_signal_unlocked( p_this );
692 vlc_mutex_unlock( &p_this->object_lock );
697 * find an object given its ID
699 * This function looks for the object whose i_object_id field is i_id. We
700 * use a dichotomy so that lookups are in log2(n).
701 *****************************************************************************/
702 void * vlc_object_get( int i_id )
705 vlc_object_t **pp_objects;
706 libvlc_global_data_t *p_libvlc_global = vlc_global();
708 vlc_mutex_lock( &structure_lock );
710 pp_objects = p_libvlc_global->pp_objects;
712 /* Perform our dichotomy */
713 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
715 i_middle = i_max / 2;
717 if( pp_objects[i_middle]->i_object_id > i_id )
721 else if( pp_objects[i_middle]->i_object_id < i_id )
725 pp_objects += i_middle;
730 /* This happens when there are only two remaining objects */
731 if( pp_objects[i_middle+1]->i_object_id == i_id
732 && pp_objects[i_middle+1]->p_internals->i_refcount > 0 )
734 vlc_object_yield_locked( pp_objects[i_middle+1] );
735 vlc_mutex_unlock( &structure_lock );
736 return pp_objects[i_middle+1];
741 else if( pp_objects[i_middle]->p_internals->i_refcount > 0 )
743 vlc_object_yield_locked( pp_objects[i_middle] );
744 vlc_mutex_unlock( &structure_lock );
745 return pp_objects[i_middle];
750 /* this means that i_max == i_middle, and since we have already
751 * tested pp_objects[i_middle]), p_found is properly set. */
756 vlc_mutex_unlock( &structure_lock );
761 ****************************************************************************
762 * find a typed object and increment its refcount
763 *****************************************************************************
764 * This function recursively looks for a given object type. i_mode can be one
765 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
766 *****************************************************************************/
767 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
769 vlc_object_t *p_found;
771 vlc_mutex_lock( &structure_lock );
773 /* If we are of the requested type ourselves, don't look further */
774 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type
775 && p_this->p_internals->i_refcount > 0 )
777 vlc_object_yield_locked( p_this );
778 vlc_mutex_unlock( &structure_lock );
782 /* Otherwise, recursively look for the object */
783 if( (i_mode & 0x000f) == FIND_ANYWHERE )
785 vlc_object_t *p_root = p_this;
788 while( p_root->p_parent != NULL &&
789 p_root != VLC_OBJECT( p_this->p_libvlc ) )
791 p_root = p_root->p_parent;
794 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
795 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
797 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
798 i_type, (i_mode & ~0x000f)|FIND_CHILD );
803 p_found = FindObject( p_this, i_type, i_mode );
806 vlc_mutex_unlock( &structure_lock );
812 ****************************************************************************
813 * find a named object and increment its refcount
814 *****************************************************************************
815 * This function recursively looks for a given object name. i_mode can be one
816 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
817 *****************************************************************************/
818 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
821 vlc_object_t *p_found;
823 vlc_mutex_lock( &structure_lock );
825 /* Avoid obvious freed object uses */
826 assert( p_this->p_internals->i_refcount > 0 );
828 /* If have the requested name ourselves, don't look further */
829 if( !(i_mode & FIND_STRICT)
830 && p_this->psz_object_name
831 && !strcmp( p_this->psz_object_name, psz_name )
832 && p_this->p_internals->i_refcount > 0 )
834 vlc_object_yield_locked( p_this );
835 vlc_mutex_unlock( &structure_lock );
839 /* Otherwise, recursively look for the object */
840 if( (i_mode & 0x000f) == FIND_ANYWHERE )
842 vlc_object_t *p_root = p_this;
845 while( p_root->p_parent != NULL &&
846 p_root != VLC_OBJECT( p_this->p_libvlc ) )
848 p_root = p_root->p_parent;
851 p_found = FindObjectName( p_root, psz_name,
852 (i_mode & ~0x000f)|FIND_CHILD );
853 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
855 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
856 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
861 p_found = FindObjectName( p_this, psz_name, i_mode );
864 vlc_mutex_unlock( &structure_lock );
870 ****************************************************************************
871 * increment an object refcount
872 *****************************************************************************/
874 /* When the structure_lock is locked */
875 static void vlc_object_yield_locked( vlc_object_t *p_this )
877 vlc_assert_locked (&structure_lock);
879 /* Avoid obvious freed object uses */
880 assert( p_this->p_internals->i_refcount > 0 );
882 /* Increment the counter */
883 p_this->p_internals->i_refcount++;
886 /* Public function */
887 void __vlc_object_yield( vlc_object_t *p_this )
889 vlc_mutex_lock( &structure_lock );
890 vlc_object_yield_locked( p_this );
891 vlc_mutex_unlock( &structure_lock );
895 /*****************************************************************************
896 * decrement an object refcount
897 * And destroy the object if its refcount reach zero.
898 *****************************************************************************/
899 void __vlc_object_release( vlc_object_t *p_this )
901 vlc_bool_t b_should_destroy;
903 vlc_mutex_lock( &structure_lock );
905 assert( p_this->p_internals->i_refcount > 0 );
906 p_this->p_internals->i_refcount--;
907 b_should_destroy = (p_this->p_internals->i_refcount == 0);
909 if( b_should_destroy )
911 /* Make sure this object can't be obtained via vlc_find_object now that
913 libvlc_global_data_t *p_libvlc_global = vlc_global();
916 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
917 * useless to try and recover anything if pp_objects gets smashed. */
918 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
919 p_libvlc_global->i_objects );
920 REMOVE_ELEM( p_libvlc_global->pp_objects,
921 p_libvlc_global->i_objects, i_index );
924 vlc_mutex_unlock( &structure_lock );
926 if( b_should_destroy )
927 vlc_object_destroy( p_this );
931 ****************************************************************************
932 * attach object to a parent object
933 *****************************************************************************
934 * This function sets p_this as a child of p_parent, and p_parent as a parent
935 * of p_this. This link can be undone using vlc_object_detach.
936 *****************************************************************************/
937 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
939 if( !p_this ) return;
941 vlc_mutex_lock( &structure_lock );
943 /* Avoid obvious freed object uses */
944 assert( p_this->p_internals->i_refcount > 0 );
946 /* Attach the parent to its child */
947 p_this->p_parent = p_parent;
949 /* Attach the child to its parent */
950 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
951 p_parent->i_children, p_this );
953 /* Climb up the tree to see whether we are connected with the root */
954 if( p_parent->p_internals->b_attached )
956 SetAttachment( p_this, VLC_TRUE );
959 vlc_mutex_unlock( &structure_lock );
963 ****************************************************************************
964 * detach object from its parent
965 *****************************************************************************
966 * This function removes all links between an object and its parent.
967 *****************************************************************************/
968 void __vlc_object_detach( vlc_object_t *p_this )
970 if( !p_this ) return;
972 vlc_mutex_lock( &structure_lock );
974 if( !p_this->p_parent )
976 msg_Err( p_this, "object is not attached" );
977 vlc_mutex_unlock( &structure_lock );
981 /* Climb up the tree to see whether we are connected with the root */
982 if( p_this->p_parent->p_internals->b_attached )
984 SetAttachment( p_this, VLC_FALSE );
987 DetachObject( p_this );
988 vlc_mutex_unlock( &structure_lock );
993 ****************************************************************************
994 * find a list typed objects and increment their refcount
995 *****************************************************************************
996 * This function recursively looks for a given object type. i_mode can be one
997 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
998 *****************************************************************************/
999 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
1002 vlc_object_t **pp_current, **pp_end;
1003 int i_count = 0, i_index = 0;
1004 libvlc_global_data_t *p_libvlc_global = vlc_global();
1006 vlc_mutex_lock( &structure_lock );
1008 /* Look for the objects */
1009 switch( i_mode & 0x000f )
1012 pp_current = p_libvlc_global->pp_objects;
1013 pp_end = pp_current + p_libvlc_global->i_objects;
1015 for( ; pp_current < pp_end ; pp_current++ )
1017 if( (*pp_current)->p_internals->b_attached
1018 && (*pp_current)->i_object_type == i_type )
1024 p_list = NewList( i_count );
1025 pp_current = p_libvlc_global->pp_objects;
1027 for( ; pp_current < pp_end ; pp_current++ )
1029 if( (*pp_current)->p_internals->b_attached
1030 && (*pp_current)->i_object_type == i_type )
1032 ListReplace( p_list, *pp_current, i_index );
1033 if( i_index < i_count ) i_index++;
1039 i_count = CountChildren( p_this, i_type );
1040 p_list = NewList( i_count );
1042 /* Check allocation was successful */
1043 if( p_list->i_count != i_count )
1045 msg_Err( p_this, "list allocation failed!" );
1046 p_list->i_count = 0;
1050 p_list->i_count = 0;
1051 ListChildren( p_list, p_this, i_type );
1055 msg_Err( p_this, "unimplemented!" );
1056 p_list = NewList( 0 );
1060 vlc_mutex_unlock( &structure_lock );
1065 /*****************************************************************************
1066 * DumpCommand: print the current vlc structure
1067 *****************************************************************************
1068 * This function prints either an ASCII tree showing the connections between
1069 * vlc objects, and additional information such as their refcount, thread ID,
1070 * etc. (command "tree"), or the same data as a simple list (command "list").
1071 *****************************************************************************/
1072 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1073 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1075 libvlc_global_data_t *p_libvlc_global = vlc_global();
1077 (void)oldval; (void)p_data;
1078 if( *psz_cmd == 'l' )
1080 vlc_mutex_lock( &structure_lock );
1082 vlc_object_t **pp_current, **pp_end;
1084 pp_current = p_libvlc_global->pp_objects;
1085 pp_end = pp_current + p_libvlc_global->i_objects;
1087 for( ; pp_current < pp_end ; pp_current++ )
1089 if( (*pp_current)->p_internals->b_attached )
1091 PrintObject( *pp_current, "" );
1095 printf( " o %.8i %s (not attached)\n",
1096 (*pp_current)->i_object_id,
1097 (*pp_current)->psz_object_type );
1101 vlc_mutex_unlock( &structure_lock );
1105 vlc_object_t *p_object = NULL;
1107 if( *newval.psz_string )
1110 int i_id = strtol( newval.psz_string, &end, 0 );
1111 if( end != newval.psz_string )
1112 p_object = vlc_object_get( i_id );
1115 /* try using the object's name to find it */
1116 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1119 /* Look in p_libvlc's children tree */
1120 p_object = vlc_object_find_name( p_libvlc,
1123 vlc_object_release( p_libvlc );
1127 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1128 p_object = vlc_object_find_name( p_this,
1140 vlc_mutex_lock( &structure_lock );
1142 if( *psz_cmd == 't' )
1144 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1147 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1150 DumpStructure( p_object, 0, psz_foo );
1152 else if( *psz_cmd == 'v' )
1157 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1159 PrintObject( p_object, "" );
1161 if( !p_object->p_internals->i_vars )
1162 printf( " `-o No variables\n" );
1163 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1165 variable_t *p_var = p_object->p_internals->p_vars + i;
1167 const char *psz_type = "unknown";
1168 switch( p_var->i_type & VLC_VAR_TYPE )
1170 #define MYCASE( type, nice ) \
1171 case VLC_VAR_ ## type: \
1174 MYCASE( VOID, "void" );
1175 MYCASE( BOOL, "bool" );
1176 MYCASE( INTEGER, "integer" );
1177 MYCASE( HOTKEY, "hotkey" );
1178 MYCASE( STRING, "string" );
1179 MYCASE( MODULE, "module" );
1180 MYCASE( FILE, "file" );
1181 MYCASE( DIRECTORY, "directory" );
1182 MYCASE( VARIABLE, "variable" );
1183 MYCASE( FLOAT, "float" );
1184 MYCASE( TIME, "time" );
1185 MYCASE( ADDRESS, "address" );
1186 MYCASE( MUTEX, "mutex" );
1187 MYCASE( LIST, "list" );
1190 printf( " %c-o \"%s\" (%s",
1191 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1192 p_var->psz_name, psz_type );
1193 if( p_var->psz_text )
1194 printf( ", %s", p_var->psz_text );
1196 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1197 printf( ", command" );
1198 if( p_var->i_entries )
1199 printf( ", %d callbacks", p_var->i_entries );
1200 switch( p_var->i_type & 0x00f0 )
1206 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1208 case VLC_VAR_INTEGER:
1209 printf( ": %d", p_var->val.i_int );
1211 case VLC_VAR_STRING:
1212 printf( ": \"%s\"", p_var->val.psz_string );
1215 printf( ": %f", p_var->val.f_float );
1218 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1220 case VLC_VAR_ADDRESS:
1221 printf( ": %p", p_var->val.p_address );
1231 vlc_mutex_unlock( &structure_lock );
1233 if( *newval.psz_string )
1235 vlc_object_release( p_object );
1242 /*****************************************************************************
1243 * vlc_list_release: free a list previously allocated by vlc_list_find
1244 *****************************************************************************
1245 * This function decreases the refcount of all objects in the list and
1247 *****************************************************************************/
1248 void vlc_list_release( vlc_list_t *p_list )
1252 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1254 vlc_object_release( p_list->p_values[i_index].p_object );
1257 free( p_list->p_values );
1261 /* Following functions are local */
1263 /*****************************************************************************
1264 * FindIndex: find the index of an object in an array of objects
1265 *****************************************************************************
1266 * This function assumes that p_this can be found in pp_objects. It will not
1267 * crash if p_this cannot be found, but will return a wrong value. It is your
1268 * duty to check the return value if you are not certain that the object could
1269 * be found for sure.
1270 *****************************************************************************/
1271 static int FindIndex( vlc_object_t *p_this,
1272 vlc_object_t **pp_objects, int i_count )
1274 int i_middle = i_count / 2;
1281 if( pp_objects[i_middle] == p_this )
1291 /* We take advantage of the sorted array */
1292 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1294 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1295 i_count - i_middle );
1299 return FindIndex( p_this, pp_objects, i_middle );
1303 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1306 vlc_object_t *p_tmp;
1308 switch( i_mode & 0x000f )
1311 p_tmp = p_this->p_parent;
1314 if( p_tmp->i_object_type == i_type
1315 && p_tmp->p_internals->i_refcount > 0 )
1317 vlc_object_yield_locked( p_tmp );
1322 return FindObject( p_tmp, i_type, i_mode );
1328 for( i = p_this->i_children; i--; )
1330 p_tmp = p_this->pp_children[i];
1331 if( p_tmp->i_object_type == i_type
1332 && p_tmp->p_internals->i_refcount > 0 )
1334 vlc_object_yield_locked( p_tmp );
1337 else if( p_tmp->i_children )
1339 p_tmp = FindObject( p_tmp, i_type, i_mode );
1349 /* Handled in vlc_object_find */
1356 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1357 const char *psz_name,
1361 vlc_object_t *p_tmp;
1363 switch( i_mode & 0x000f )
1366 p_tmp = p_this->p_parent;
1369 if( p_tmp->psz_object_name
1370 && !strcmp( p_tmp->psz_object_name, psz_name )
1371 && p_tmp->p_internals->i_refcount > 0 )
1373 vlc_object_yield_locked( p_tmp );
1378 return FindObjectName( p_tmp, psz_name, i_mode );
1384 for( i = p_this->i_children; i--; )
1386 p_tmp = p_this->pp_children[i];
1387 if( p_tmp->psz_object_name
1388 && !strcmp( p_tmp->psz_object_name, psz_name )
1389 && p_tmp->p_internals->i_refcount > 0 )
1391 vlc_object_yield_locked( p_tmp );
1394 else if( p_tmp->i_children )
1396 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1406 /* Handled in vlc_object_find */
1413 static void DetachObject( vlc_object_t *p_this )
1415 vlc_object_t *p_parent = p_this->p_parent;
1418 /* Remove p_this's parent */
1419 p_this->p_parent = NULL;
1421 /* Remove all of p_parent's children which are p_this */
1422 for( i_index = p_parent->i_children ; i_index-- ; )
1424 if( p_parent->pp_children[i_index] == p_this )
1426 p_parent->i_children--;
1427 for( i = i_index ; i < p_parent->i_children ; i++ )
1429 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1434 if( p_parent->i_children )
1436 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1437 p_parent->i_children * sizeof(vlc_object_t *) );
1441 free( p_parent->pp_children );
1442 p_parent->pp_children = NULL;
1446 /*****************************************************************************
1447 * SetAttachment: recursively set the b_attached flag of a subtree.
1448 *****************************************************************************
1449 * This function is used by the attach and detach functions to propagate
1450 * the b_attached flag in a subtree.
1451 *****************************************************************************/
1452 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1456 for( i_index = p_this->i_children ; i_index-- ; )
1458 SetAttachment( p_this->pp_children[i_index], b_attached );
1461 p_this->p_internals->b_attached = b_attached;
1464 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1466 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1470 if( p_this->psz_object_name )
1472 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1474 psz_name[48] = '\"';
1477 psz_children[0] = '\0';
1478 switch( p_this->i_children )
1483 strcpy( psz_children, ", 1 child" );
1486 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1490 psz_refcount[0] = '\0';
1491 if( p_this->p_internals->i_refcount > 0 )
1492 snprintf( psz_refcount, 19, ", refcount %u",
1493 p_this->p_internals->i_refcount );
1495 psz_thread[0] = '\0';
1496 if( p_this->p_internals->b_thread )
1497 snprintf( psz_thread, 29, " (thread %u)",
1498 #if defined(WIN32) || defined(UNDER_CE)
1499 (unsigned)p_this->p_internals->thread_id.id );
1501 (unsigned)p_this->p_internals->thread_id );
1504 psz_parent[0] = '\0';
1505 if( p_this->p_parent )
1506 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1508 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1509 p_this->i_object_id, p_this->psz_object_type,
1510 psz_name, psz_thread, psz_refcount, psz_children,
1514 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1517 char i_back = psz_foo[i_level];
1518 psz_foo[i_level] = '\0';
1520 PrintObject( p_this, psz_foo );
1522 psz_foo[i_level] = i_back;
1524 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1526 msg_Warn( p_this, "structure tree is too deep" );
1530 for( i = 0 ; i < p_this->i_children ; i++ )
1534 psz_foo[i_level-1] = ' ';
1536 if( psz_foo[i_level-2] == '`' )
1538 psz_foo[i_level-2] = ' ';
1542 if( i == p_this->i_children - 1 )
1544 psz_foo[i_level] = '`';
1548 psz_foo[i_level] = '|';
1551 psz_foo[i_level+1] = '-';
1552 psz_foo[i_level+2] = '\0';
1554 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1558 static vlc_list_t * NewList( int i_count )
1560 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1561 if( p_list == NULL )
1566 p_list->i_count = i_count;
1570 p_list->p_values = NULL;
1574 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1575 if( p_list->p_values == NULL )
1577 p_list->i_count = 0;
1584 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1587 if( p_list == NULL || i_index >= p_list->i_count )
1592 vlc_object_yield_locked( p_object );
1594 p_list->p_values[i_index].p_object = p_object;
1599 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1601 if( p_list == NULL )
1606 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1607 * sizeof( vlc_value_t ) );
1608 if( p_list->p_values == NULL )
1610 p_list->i_count = 0;
1614 vlc_object_yield_locked( p_object );
1616 p_list->p_values[p_list->i_count].p_object = p_object;
1622 static int CountChildren( vlc_object_t *p_this, int i_type )
1624 vlc_object_t *p_tmp;
1627 for( i = 0; i < p_this->i_children; i++ )
1629 p_tmp = p_this->pp_children[i];
1631 if( p_tmp->i_object_type == i_type )
1636 if( p_tmp->i_children )
1638 i_count += CountChildren( p_tmp, i_type );
1645 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1647 vlc_object_t *p_tmp;
1650 for( i = 0; i < p_this->i_children; i++ )
1652 p_tmp = p_this->pp_children[i];
1654 if( p_tmp->i_object_type == i_type )
1656 ListReplace( p_list, p_tmp, p_list->i_count++ );
1659 if( p_tmp->i_children )
1661 ListChildren( p_list, p_tmp, i_type );