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_interface.h"
52 #include "vlc_codec.h"
53 #include "vlc_filter.h"
55 #include "variables.h"
61 # include <errno.h> /* ENOSYS */
65 /*****************************************************************************
67 *****************************************************************************/
68 static int DumpCommand( vlc_object_t *, char const *,
69 vlc_value_t, vlc_value_t, void * );
71 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
72 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
73 static void DetachObject ( vlc_object_t * );
74 static void PrintObject ( vlc_object_t *, const char * );
75 static void DumpStructure ( vlc_object_t *, int, char * );
76 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
77 static void SetAttachment ( vlc_object_t *, bool );
79 static vlc_list_t * NewList ( int );
80 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
81 static void ListAppend ( vlc_list_t *, vlc_object_t * );
82 static int CountChildren ( vlc_object_t *, int );
83 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
85 static void vlc_object_destroy( vlc_object_t *p_this );
86 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
88 /*****************************************************************************
89 * Local structure lock
90 *****************************************************************************/
91 static vlc_mutex_t structure_lock;
93 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
94 int i_type, const char *psz_type )
97 vlc_object_internals_t *p_priv;
100 * VLC objects are laid out as follow:
101 * - first the LibVLC-private per-object data,
102 * - then VLC_COMMON members from vlc_object_t,
103 * - finally, the type-specific data (if any).
105 * This function initializes the LibVLC and common data,
106 * and zeroes the rest.
108 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
112 assert (i_size >= sizeof (vlc_object_t));
113 p_new = (vlc_object_t *)(p_priv + 1);
115 p_new->i_object_type = i_type;
116 p_new->psz_object_type = psz_type;
117 p_new->psz_object_name = NULL;
119 p_new->b_die = false;
120 p_new->b_error = false;
121 p_new->b_dead = false;
122 p_new->b_force = false;
124 p_new->psz_header = NULL;
127 p_new->i_flags = p_this->i_flags
128 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
130 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
132 if( !p_priv->p_vars )
138 libvlc_global_data_t *p_libvlc_global;
141 /* Only the global root object is created out of the blue */
142 p_libvlc_global = (libvlc_global_data_t *)p_new;
143 p_new->p_libvlc = NULL;
145 p_libvlc_global->i_counter = 0;
146 p_libvlc_global->i_objects = 0;
147 p_libvlc_global->pp_objects = NULL;
148 vlc_mutex_init( &structure_lock );
152 p_libvlc_global = vlc_global();
153 if( i_type == VLC_OBJECT_LIBVLC )
154 p_new->p_libvlc = (libvlc_int_t*)p_new;
156 p_new->p_libvlc = p_this->p_libvlc;
159 vlc_spin_init( &p_priv->ref_spin );
160 p_priv->i_refcount = 1;
161 p_priv->pf_destructor = NULL;
162 p_priv->b_thread = false;
163 p_new->p_parent = NULL;
164 p_new->pp_children = NULL;
165 p_new->i_children = 0;
167 p_new->p_private = NULL;
169 /* Initialize mutexes and condvars */
170 vlc_mutex_init( &p_new->object_lock );
171 vlc_cond_init( p_new, &p_new->object_wait );
172 vlc_mutex_init( &p_priv->var_lock );
173 vlc_spin_init( &p_priv->spin );
174 p_priv->pipes[0] = p_priv->pipes[1] = -1;
176 vlc_mutex_lock( &structure_lock );
177 p_new->i_object_id = p_libvlc_global->i_counter++;
178 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
179 * useless to try and recover anything if pp_objects gets smashed. */
180 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
182 vlc_mutex_unlock( &structure_lock );
184 if( i_type == VLC_OBJECT_LIBVLC )
186 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
187 var_AddCallback( p_new, "list", DumpCommand, NULL );
188 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
189 var_AddCallback( p_new, "tree", DumpCommand, NULL );
190 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
191 var_AddCallback( p_new, "vars", DumpCommand, NULL );
199 * Allocates and initializes a vlc object.
201 * @param i_type known object type (all of them are negative integer values),
202 * or object byte size (always positive).
204 * @return the new object, or NULL on error.
206 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
208 const char * psz_type;
213 case VLC_OBJECT_LIBVLC:
214 i_size = sizeof(libvlc_int_t);
217 case VLC_OBJECT_INTF:
218 i_size = sizeof(intf_thread_t);
219 psz_type = "interface";
221 case VLC_OBJECT_DIALOGS:
222 i_size = sizeof(intf_thread_t);
223 psz_type = "dialogs";
225 case VLC_OBJECT_DEMUX:
226 i_size = sizeof(demux_t);
229 case VLC_OBJECT_ACCESS:
230 i_size = sizeof(access_t);
233 case VLC_OBJECT_DECODER:
234 i_size = sizeof(decoder_t);
235 psz_type = "decoder";
237 case VLC_OBJECT_PACKETIZER:
238 i_size = sizeof(decoder_t);
239 psz_type = "packetizer";
241 case VLC_OBJECT_ENCODER:
242 i_size = sizeof(encoder_t);
243 psz_type = "encoder";
245 case VLC_OBJECT_FILTER:
246 i_size = sizeof(filter_t);
249 case VLC_OBJECT_VOUT:
250 i_size = sizeof(vout_thread_t);
251 psz_type = "video output";
253 case VLC_OBJECT_AOUT:
254 i_size = sizeof(aout_instance_t);
255 psz_type = "audio output";
257 case VLC_OBJECT_SOUT:
258 i_size = sizeof(sout_instance_t);
259 psz_type = "stream output";
261 case VLC_OBJECT_OPENGL:
262 i_size = sizeof( vout_thread_t );
265 case VLC_OBJECT_ANNOUNCE:
266 i_size = sizeof( announce_handler_t );
267 psz_type = "announce";
269 case VLC_OBJECT_INTERACTION:
270 i_size = sizeof( interaction_t );
271 psz_type = "interaction";
274 i_size = i_type > (int)sizeof(vlc_object_t)
275 ? i_type : (int)sizeof(vlc_object_t);
276 i_type = VLC_OBJECT_GENERIC;
277 psz_type = "generic";
281 return vlc_custom_create( p_this, i_size, i_type, psz_type );
286 ****************************************************************************
287 * Set the destructor of a vlc object
289 * This function sets the destructor of the vlc object. It will be called
290 * when the object is destroyed when the its refcount reaches 0.
291 * (It is called by the internal function vlc_object_destroy())
292 *****************************************************************************/
293 void __vlc_object_set_destructor( vlc_object_t *p_this,
294 vlc_destructor_t pf_destructor )
296 vlc_object_internals_t *p_priv = vlc_internals(p_this );
297 p_priv->pf_destructor = pf_destructor;
301 ****************************************************************************
302 * Destroy a vlc object (Internal)
304 * This function destroys an object that has been previously allocated with
305 * vlc_object_create. The object's refcount must be zero and it must not be
306 * attached to other objects in any way.
307 *****************************************************************************/
308 static void vlc_object_destroy( vlc_object_t *p_this )
310 vlc_object_internals_t *p_priv = vlc_internals( p_this );
312 /* Objects are always detached beforehand */
313 assert( !p_this->p_parent );
315 /* Send a kill to the object's thread if applicable */
316 vlc_object_kill( p_this );
318 /* If we are running on a thread, wait until it ends */
319 if( p_priv->b_thread )
320 vlc_thread_join( p_this );
322 /* Call the custom "subclass" destructor */
323 if( p_priv->pf_destructor )
324 p_priv->pf_destructor( p_this );
327 if( p_this->i_children )
332 "ERROR: cannot delete object (%i, %s) with %d children\n",
333 p_this->i_object_id, p_this->psz_object_name,
334 p_this->i_children );
336 for( i = 0; i < p_this->i_children; i++ )
339 "ERROR: Remaining children object "
340 "(id:%i, type:%s, name:%s)\n",
341 p_this->pp_children[i]->i_object_id,
342 p_this->pp_children[i]->psz_object_type,
343 p_this->pp_children[i]->psz_object_name );
347 /* Dump libvlc object to ease debugging */
348 vlc_object_dump( p_this->p_libvlc );
353 /* Destroy the associated variables, starting from the end so that
354 * no memmove calls have to be done. */
355 while( p_priv->i_vars )
357 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
360 free( p_priv->p_vars );
361 vlc_mutex_destroy( &p_priv->var_lock );
363 free( p_this->psz_header );
365 if( p_this->p_libvlc == NULL )
367 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
370 assert( p_global == vlc_global() );
372 if( p_global->i_objects > 0 )
375 for( i = 0; i < p_global->i_objects; i++ )
377 /* We are leaking this object */
379 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
380 p_global->pp_objects[i]->i_object_id,
381 p_global->pp_objects[i]->psz_object_type,
382 p_global->pp_objects[i]->psz_object_name );
384 /* Dump libvlc object to ease debugging */
385 vlc_object_dump( p_global->pp_objects[i] );
390 /* Dump libvlc object to ease debugging */
391 vlc_object_dump( p_this );
393 /* Strongly abort, cause we want these to be fixed */
398 /* We are the global object ... no need to lock. */
399 free( p_global->pp_objects );
400 p_global->pp_objects = NULL;
402 vlc_mutex_destroy( &structure_lock );
405 FREENULL( p_this->psz_object_name );
407 #if defined(WIN32) || defined(UNDER_CE)
408 /* if object has an associated thread, close it now */
409 if( p_priv->thread_id )
410 CloseHandle(p_priv->thread_id);
413 vlc_spin_destroy( &p_priv->ref_spin );
414 vlc_mutex_destroy( &p_this->object_lock );
415 vlc_cond_destroy( &p_this->object_wait );
416 vlc_spin_destroy( &p_priv->spin );
417 if( p_priv->pipes[1] != -1 )
418 close( p_priv->pipes[1] );
419 if( p_priv->pipes[0] != -1 )
420 close( p_priv->pipes[0] );
426 /** Inter-object signaling */
428 void __vlc_object_lock( vlc_object_t *obj )
430 vlc_mutex_lock( &obj->object_lock );
433 void __vlc_object_unlock( vlc_object_t *obj )
435 vlc_assert_locked( &obj->object_lock );
436 vlc_mutex_unlock( &obj->object_lock );
440 # include <winsock2.h>
441 # include <ws2tcpip.h>
444 * select()-able pipes emulated using Winsock
446 static int pipe (int fd[2])
449 int addrlen = sizeof (addr);
451 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
452 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
453 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
456 memset (&addr, 0, sizeof (addr));
457 addr.sin_family = AF_INET;
458 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
459 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
460 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
462 || connect (c, (PSOCKADDR)&addr, addrlen))
465 a = accept (l, NULL, NULL);
466 if (a == INVALID_SOCKET)
477 if (l != INVALID_SOCKET)
479 if (c != INVALID_SOCKET)
485 #define read( a, b, c ) recv (a, b, c, 0)
487 #define write( a, b, c ) send (a, b, c, 0)
489 #define close( a ) closesocket (a)
493 * Returns the readable end of a pipe that becomes readable once termination
494 * of the object is requested (vlc_object_kill()).
495 * This can be used to wake-up out of a select() or poll() event loop, such
496 * typically when doing network I/O.
498 * Note that the pipe will remain the same for the lifetime of the object.
499 * DO NOT read the pipe nor close it yourself. Ever.
501 * @param obj object that would be "killed"
502 * @return a readable pipe descriptor, or -1 on error.
504 int __vlc_object_waitpipe( vlc_object_t *obj )
506 int pfd[2] = { -1, -1 };
507 vlc_object_internals_t *internals = vlc_internals( obj );
510 vlc_spin_lock (&internals->spin);
511 if (internals->pipes[0] == -1)
513 /* This can only ever happen if someone killed us without locking: */
514 assert (internals->pipes[1] == -1);
515 vlc_spin_unlock (&internals->spin);
520 vlc_spin_lock (&internals->spin);
521 if (internals->pipes[0] == -1)
523 internals->pipes[0] = pfd[0];
524 internals->pipes[1] = pfd[1];
525 pfd[0] = pfd[1] = -1;
529 vlc_spin_unlock (&internals->spin);
533 /* Race condition: vlc_object_kill() already invoked! */
536 vlc_spin_lock (&internals->spin);
537 fd = internals->pipes[1];
538 internals->pipes[1] = -1;
539 vlc_spin_unlock (&internals->spin);
541 msg_Dbg (obj, "waitpipe: object already dying");
546 /* Race condition: two threads call pipe() - unlikely */
552 return internals->pipes[0];
557 * Waits for the object to be signaled (using vlc_object_signal()).
558 * If the object already has a signal pending, this function will return
559 * immediately. It is asserted that the caller holds the object lock.
561 * @return true if the object is dying and should terminate.
563 bool __vlc_object_wait( vlc_object_t *obj )
565 vlc_assert_locked( &obj->object_lock );
566 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
572 * Waits for the object to be signaled (using vlc_object_signal()), or for
574 * If the object already has a signal pending, this function will return
575 * immediately. It is asserted that the caller holds the object lock.
577 * @return negative if the object is dying and should terminate,
578 * positive if the the object has been signaled but is not dying,
579 * 0 if timeout has been reached.
581 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
585 vlc_assert_locked( &obj->object_lock );
586 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
587 if( v == 0 ) /* signaled */
588 return obj->b_die ? -1 : 1;
594 * Checks whether an object has been "killed".
595 * The object lock must be held.
597 * Typical code for an object thread could be:
599 vlc_object_lock (self);
601 while (vlc_object_alive (self))
605 if (vlc_object_wait (self))
610 ...deinitialization...
611 vlc_object_unlock (self);
614 * @return true iff the object has not been killed yet
616 bool __vlc_object_alive( vlc_object_t *obj )
618 vlc_assert_locked( &obj->object_lock );
624 * Signals an object for which the lock is held.
626 void __vlc_object_signal_unlocked( vlc_object_t *obj )
628 vlc_assert_locked (&obj->object_lock);
629 vlc_cond_signal( &obj->object_wait );
634 * Requests termination of an object.
635 * If the object is LibVLC, also request to terminate all its children.
637 void __vlc_object_kill( vlc_object_t *p_this )
639 vlc_object_internals_t *internals = vlc_internals( p_this );
642 vlc_mutex_lock( &p_this->object_lock );
643 p_this->b_die = true;
645 vlc_spin_lock (&internals->spin);
646 fd = internals->pipes[1];
647 internals->pipes[1] = -1;
648 vlc_spin_unlock (&internals->spin);
652 msg_Dbg (p_this, "waitpipe: object killed");
656 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
657 for( int i = 0; i < p_this->i_children ; i++ )
658 vlc_object_kill( p_this->pp_children[i] );
660 vlc_object_signal_unlocked( p_this );
661 vlc_mutex_unlock( &p_this->object_lock );
666 * find an object given its ID
668 * This function looks for the object whose i_object_id field is i_id. We
669 * use a dichotomy so that lookups are in log2(n).
670 *****************************************************************************/
671 void * vlc_object_get( int i_id )
674 vlc_object_t **pp_objects;
675 libvlc_global_data_t *p_libvlc_global = vlc_global();
676 vlc_object_t *obj = NULL;
678 vlc_mutex_lock( &structure_lock );
680 pp_objects = p_libvlc_global->pp_objects;
682 /* Perform our dichotomy */
683 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
685 i_middle = i_max / 2;
687 if( pp_objects[i_middle]->i_object_id > i_id )
691 else if( pp_objects[i_middle]->i_object_id < i_id )
695 pp_objects += i_middle;
700 /* This happens when there are only two remaining objects */
701 if( pp_objects[i_middle+1]->i_object_id == i_id )
703 vlc_object_yield( pp_objects[i_middle+1] );
704 obj = pp_objects[i_middle+1];
711 vlc_object_yield( pp_objects[i_middle] );
712 obj = pp_objects[i_middle];
717 vlc_mutex_unlock( &structure_lock );
722 ****************************************************************************
723 * find a typed object and increment its refcount
724 *****************************************************************************
725 * This function recursively looks for a given object type. i_mode can be one
726 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
727 *****************************************************************************/
728 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
730 vlc_object_t *p_found;
732 /* If we are of the requested type ourselves, don't look further */
733 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
735 vlc_object_yield( p_this );
739 vlc_mutex_lock( &structure_lock );
741 /* Otherwise, recursively look for the object */
742 if( (i_mode & 0x000f) == FIND_ANYWHERE )
744 vlc_object_t *p_root = p_this;
747 while( p_root->p_parent != NULL &&
748 p_root != VLC_OBJECT( p_this->p_libvlc ) )
750 p_root = p_root->p_parent;
753 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
754 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
756 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
757 i_type, (i_mode & ~0x000f)|FIND_CHILD );
762 p_found = FindObject( p_this, i_type, i_mode );
765 vlc_mutex_unlock( &structure_lock );
771 ****************************************************************************
772 * find a named object and increment its refcount
773 *****************************************************************************
774 * This function recursively looks for a given object name. i_mode can be one
775 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
776 *****************************************************************************/
777 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
780 vlc_object_t *p_found;
782 /* If have the requested name ourselves, don't look further */
783 if( !(i_mode & FIND_STRICT)
784 && p_this->psz_object_name
785 && !strcmp( p_this->psz_object_name, psz_name ) )
787 vlc_object_yield( p_this );
791 vlc_mutex_lock( &structure_lock );
793 /* Otherwise, recursively look for the object */
794 if( (i_mode & 0x000f) == FIND_ANYWHERE )
796 vlc_object_t *p_root = p_this;
799 while( p_root->p_parent != NULL &&
800 p_root != VLC_OBJECT( p_this->p_libvlc ) )
802 p_root = p_root->p_parent;
805 p_found = FindObjectName( p_root, psz_name,
806 (i_mode & ~0x000f)|FIND_CHILD );
807 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
809 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
810 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
815 p_found = FindObjectName( p_this, psz_name, i_mode );
818 vlc_mutex_unlock( &structure_lock );
824 * Increment an object reference counter.
826 void __vlc_object_yield( vlc_object_t *p_this )
828 vlc_object_internals_t *internals = vlc_internals( p_this );
830 vlc_spin_lock( &internals->ref_spin );
831 /* Avoid obvious freed object uses */
832 assert( internals->i_refcount > 0 );
833 /* Increment the counter */
834 internals->i_refcount++;
835 vlc_spin_unlock( &internals->ref_spin );
838 /*****************************************************************************
839 * decrement an object refcount
840 * And destroy the object if its refcount reach zero.
841 *****************************************************************************/
842 void __vlc_object_release( vlc_object_t *p_this )
844 vlc_object_internals_t *internals = vlc_internals( p_this );
845 bool b_should_destroy;
847 vlc_spin_lock( &internals->ref_spin );
848 assert( internals->i_refcount > 0 );
850 if( internals->i_refcount > 1 )
853 /* There are still other references to the object */
854 internals->i_refcount--;
855 vlc_spin_unlock( &internals->ref_spin );
858 vlc_spin_unlock( &internals->ref_spin );
861 /* Remember that we cannot hold the spin while waiting on the mutex */
862 vlc_mutex_lock( &structure_lock );
863 /* Take the spin again. Note that another thread may have yielded the
864 * object in the (very short) mean time. */
865 vlc_spin_lock( &internals->ref_spin );
866 b_should_destroy = --internals->i_refcount == 0;
867 vlc_spin_unlock( &internals->ref_spin );
869 if( b_should_destroy )
871 /* Remove the object from the table so that it cannot be returned from
872 * vlc_object_find() and friends. */
873 libvlc_global_data_t *p_libvlc_global = vlc_global();
876 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
877 p_libvlc_global->i_objects );
878 REMOVE_ELEM( p_libvlc_global->pp_objects,
879 p_libvlc_global->i_objects, i_index );
880 if (p_this->p_parent)
881 vlc_object_detach_unlocked (p_this);
884 vlc_mutex_unlock( &structure_lock );
886 if( b_should_destroy )
887 vlc_object_destroy( p_this );
891 ****************************************************************************
892 * attach object to a parent object
893 *****************************************************************************
894 * This function sets p_this as a child of p_parent, and p_parent as a parent
895 * of p_this. This link can be undone using vlc_object_detach.
896 *****************************************************************************/
897 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
899 if( !p_this ) return;
901 vlc_mutex_lock( &structure_lock );
903 /* Attach the parent to its child */
904 p_this->p_parent = p_parent;
906 /* Attach the child to its parent */
907 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
908 p_parent->i_children, p_this );
910 vlc_mutex_unlock( &structure_lock );
914 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
916 assert (p_this->p_parent);
918 DetachObject( p_this );
923 ****************************************************************************
924 * detach object from its parent
925 *****************************************************************************
926 * This function removes all links between an object and its parent.
927 *****************************************************************************/
928 void __vlc_object_detach( vlc_object_t *p_this )
930 if( !p_this ) return;
932 vlc_mutex_lock( &structure_lock );
934 if( !p_this->p_parent )
936 msg_Err( p_this, "object is not attached" );
937 vlc_mutex_unlock( &structure_lock );
941 vlc_object_detach_unlocked( p_this );
942 vlc_mutex_unlock( &structure_lock );
946 static void vlc_tree_find (vlc_object_t *node, int type, vlc_list_t *list)
949 vlc_assert_locked (&structure_lock);
951 if (node->i_object_type == type)
952 ListAppend (list, node);
954 for (int i = 0; i < node->i_children; i++)
955 vlc_tree_find (node->pp_children[i], type, list);
959 ****************************************************************************
960 * find a list typed objects and increment their refcount
961 *****************************************************************************
962 * This function recursively looks for a given object type. i_mode can be one
963 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
964 *****************************************************************************/
965 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
968 int i_count = 0, i_index = 0;
969 libvlc_global_data_t *p_libvlc_global = vlc_global();
971 vlc_mutex_lock( &structure_lock );
973 /* Look for the objects */
974 switch( i_mode & 0x000f )
977 p_list = NewList (0);
979 vlc_tree_find (VLC_OBJECT (p_libvlc_global), i_type, p_list);
983 i_count = CountChildren( p_this, i_type );
984 p_list = NewList( i_count );
986 /* Check allocation was successful */
987 if( p_list->i_count != i_count )
989 msg_Err( p_this, "list allocation failed!" );
995 ListChildren( p_list, p_this, i_type );
999 msg_Err( p_this, "unimplemented!" );
1000 p_list = NewList( 0 );
1004 vlc_mutex_unlock( &structure_lock );
1009 /*****************************************************************************
1010 * DumpCommand: print the current vlc structure
1011 *****************************************************************************
1012 * This function prints either an ASCII tree showing the connections between
1013 * vlc objects, and additional information such as their refcount, thread ID,
1014 * etc. (command "tree"), or the same data as a simple list (command "list").
1015 *****************************************************************************/
1016 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1017 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1019 libvlc_global_data_t *p_libvlc_global = vlc_global();
1021 (void)oldval; (void)p_data;
1022 if( *psz_cmd == 'l' )
1024 vlc_mutex_lock( &structure_lock );
1026 vlc_object_t **pp_current, **pp_end;
1028 pp_current = p_libvlc_global->pp_objects;
1029 pp_end = pp_current + p_libvlc_global->i_objects;
1031 for( ; pp_current < pp_end ; pp_current++ )
1032 PrintObject( *pp_current, "" );
1034 vlc_mutex_unlock( &structure_lock );
1038 vlc_object_t *p_object = NULL;
1040 if( *newval.psz_string )
1043 int i_id = strtol( newval.psz_string, &end, 0 );
1044 if( end != newval.psz_string )
1045 p_object = vlc_object_get( i_id );
1048 /* try using the object's name to find it */
1049 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1052 /* Look in p_libvlc's children tree */
1053 p_object = vlc_object_find_name( p_libvlc,
1056 vlc_object_release( p_libvlc );
1060 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1061 p_object = vlc_object_find_name( p_this,
1073 vlc_mutex_lock( &structure_lock );
1075 if( *psz_cmd == 't' )
1077 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1080 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1083 DumpStructure( p_object, 0, psz_foo );
1085 else if( *psz_cmd == 'v' )
1090 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1092 PrintObject( p_object, "" );
1094 if( !vlc_internals( p_object )->i_vars )
1095 printf( " `-o No variables\n" );
1096 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1098 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1100 const char *psz_type = "unknown";
1101 switch( p_var->i_type & VLC_VAR_TYPE )
1103 #define MYCASE( type, nice ) \
1104 case VLC_VAR_ ## type: \
1107 MYCASE( VOID, "void" );
1108 MYCASE( BOOL, "bool" );
1109 MYCASE( INTEGER, "integer" );
1110 MYCASE( HOTKEY, "hotkey" );
1111 MYCASE( STRING, "string" );
1112 MYCASE( MODULE, "module" );
1113 MYCASE( FILE, "file" );
1114 MYCASE( DIRECTORY, "directory" );
1115 MYCASE( VARIABLE, "variable" );
1116 MYCASE( FLOAT, "float" );
1117 MYCASE( TIME, "time" );
1118 MYCASE( ADDRESS, "address" );
1119 MYCASE( MUTEX, "mutex" );
1120 MYCASE( LIST, "list" );
1123 printf( " %c-o \"%s\" (%s",
1124 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1125 p_var->psz_name, psz_type );
1126 if( p_var->psz_text )
1127 printf( ", %s", p_var->psz_text );
1129 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1130 printf( ", command" );
1131 if( p_var->i_entries )
1132 printf( ", %d callbacks", p_var->i_entries );
1133 switch( p_var->i_type & 0x00f0 )
1139 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1141 case VLC_VAR_INTEGER:
1142 printf( ": %d", p_var->val.i_int );
1144 case VLC_VAR_STRING:
1145 printf( ": \"%s\"", p_var->val.psz_string );
1148 printf( ": %f", p_var->val.f_float );
1151 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1153 case VLC_VAR_ADDRESS:
1154 printf( ": %p", p_var->val.p_address );
1164 vlc_mutex_unlock( &structure_lock );
1166 if( *newval.psz_string )
1168 vlc_object_release( p_object );
1175 /*****************************************************************************
1176 * vlc_list_release: free a list previously allocated by vlc_list_find
1177 *****************************************************************************
1178 * This function decreases the refcount of all objects in the list and
1180 *****************************************************************************/
1181 void vlc_list_release( vlc_list_t *p_list )
1185 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1187 vlc_object_release( p_list->p_values[i_index].p_object );
1190 free( p_list->p_values );
1194 /*****************************************************************************
1195 * dump an object. (Debug function)
1196 *****************************************************************************/
1197 void __vlc_object_dump( vlc_object_t *p_this )
1199 vlc_mutex_lock( &structure_lock );
1200 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1202 DumpStructure( p_this, 0, psz_foo );
1203 vlc_mutex_unlock( &structure_lock );
1206 /* Following functions are local */
1208 /*****************************************************************************
1209 * FindIndex: find the index of an object in an array of objects
1210 *****************************************************************************
1211 * This function assumes that p_this can be found in pp_objects. It will not
1212 * crash if p_this cannot be found, but will return a wrong value. It is your
1213 * duty to check the return value if you are not certain that the object could
1214 * be found for sure.
1215 *****************************************************************************/
1216 static int FindIndex( vlc_object_t *p_this,
1217 vlc_object_t **pp_objects, int i_count )
1219 int i_middle = i_count / 2;
1226 if( pp_objects[i_middle] == p_this )
1236 /* We take advantage of the sorted array */
1237 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1239 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1240 i_count - i_middle );
1244 return FindIndex( p_this, pp_objects, i_middle );
1248 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1251 vlc_object_t *p_tmp;
1253 switch( i_mode & 0x000f )
1256 p_tmp = p_this->p_parent;
1259 if( p_tmp->i_object_type == i_type )
1261 vlc_object_yield( p_tmp );
1266 return FindObject( p_tmp, i_type, i_mode );
1272 for( i = p_this->i_children; i--; )
1274 p_tmp = p_this->pp_children[i];
1275 if( p_tmp->i_object_type == i_type )
1277 vlc_object_yield( p_tmp );
1280 else if( p_tmp->i_children )
1282 p_tmp = FindObject( p_tmp, i_type, i_mode );
1292 /* Handled in vlc_object_find */
1299 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1300 const char *psz_name,
1304 vlc_object_t *p_tmp;
1306 switch( i_mode & 0x000f )
1309 p_tmp = p_this->p_parent;
1312 if( p_tmp->psz_object_name
1313 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1315 vlc_object_yield( p_tmp );
1320 return FindObjectName( p_tmp, psz_name, i_mode );
1326 for( i = p_this->i_children; i--; )
1328 p_tmp = p_this->pp_children[i];
1329 if( p_tmp->psz_object_name
1330 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1332 vlc_object_yield( p_tmp );
1335 else if( p_tmp->i_children )
1337 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1347 /* Handled in vlc_object_find */
1354 static void DetachObject( vlc_object_t *p_this )
1356 vlc_object_t *p_parent = p_this->p_parent;
1359 /* Remove p_this's parent */
1360 p_this->p_parent = NULL;
1362 /* Remove all of p_parent's children which are p_this */
1363 for( i_index = p_parent->i_children ; i_index-- ; )
1365 if( p_parent->pp_children[i_index] == p_this )
1367 p_parent->i_children--;
1368 for( i = i_index ; i < p_parent->i_children ; i++ )
1370 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1375 if( p_parent->i_children )
1377 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1378 p_parent->i_children * sizeof(vlc_object_t *) );
1382 free( p_parent->pp_children );
1383 p_parent->pp_children = NULL;
1388 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1390 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1393 memset( &psz_name, 0, sizeof(psz_name) );
1394 if( p_this->psz_object_name )
1396 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1398 psz_name[48] = '\"';
1401 psz_children[0] = '\0';
1402 switch( p_this->i_children )
1407 strcpy( psz_children, ", 1 child" );
1410 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1414 psz_refcount[0] = '\0';
1415 if( vlc_internals( p_this )->i_refcount > 0 )
1416 snprintf( psz_refcount, 19, ", refcount %u",
1417 vlc_internals( p_this )->i_refcount );
1419 psz_thread[0] = '\0';
1420 if( vlc_internals( p_this )->b_thread )
1421 snprintf( psz_thread, 29, " (thread %lu)",
1422 (unsigned long)vlc_internals( p_this )->thread_id );
1424 psz_parent[0] = '\0';
1425 if( p_this->p_parent )
1426 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1428 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1429 p_this->i_object_id, p_this->psz_object_type,
1430 psz_name, psz_thread, psz_refcount, psz_children,
1434 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1437 char i_back = psz_foo[i_level];
1438 psz_foo[i_level] = '\0';
1440 PrintObject( p_this, psz_foo );
1442 psz_foo[i_level] = i_back;
1444 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1446 msg_Warn( p_this, "structure tree is too deep" );
1450 for( i = 0 ; i < p_this->i_children ; i++ )
1454 psz_foo[i_level-1] = ' ';
1456 if( psz_foo[i_level-2] == '`' )
1458 psz_foo[i_level-2] = ' ';
1462 if( i == p_this->i_children - 1 )
1464 psz_foo[i_level] = '`';
1468 psz_foo[i_level] = '|';
1471 psz_foo[i_level+1] = '-';
1472 psz_foo[i_level+2] = '\0';
1474 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1478 static vlc_list_t * NewList( int i_count )
1480 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1481 if( p_list == NULL )
1486 p_list->i_count = i_count;
1490 p_list->p_values = NULL;
1494 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1495 if( p_list->p_values == NULL )
1497 p_list->i_count = 0;
1504 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1507 if( p_list == NULL || i_index >= p_list->i_count )
1512 vlc_object_yield( p_object );
1514 p_list->p_values[i_index].p_object = p_object;
1519 static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1521 if( p_list == NULL )
1526 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1527 * sizeof( vlc_value_t ) );
1528 if( p_list->p_values == NULL )
1530 p_list->i_count = 0;
1534 vlc_object_yield( p_object );
1536 p_list->p_values[p_list->i_count].p_object = p_object;
1542 static int CountChildren( vlc_object_t *p_this, int i_type )
1544 vlc_object_t *p_tmp;
1547 for( i = 0; i < p_this->i_children; i++ )
1549 p_tmp = p_this->pp_children[i];
1551 if( p_tmp->i_object_type == i_type )
1556 if( p_tmp->i_children )
1558 i_count += CountChildren( p_tmp, i_type );
1565 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1567 vlc_object_t *p_tmp;
1570 for( i = 0; i < p_this->i_children; i++ )
1572 p_tmp = p_this->pp_children[i];
1574 if( p_tmp->i_object_type == i_type )
1576 ListReplace( p_list, p_tmp, p_list->i_count++ );
1579 if( p_tmp->i_children )
1581 ListChildren( p_list, p_tmp, i_type );