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_priv->b_attached = false;
123 p_new->b_force = false;
125 p_new->psz_header = NULL;
128 p_new->i_flags = p_this->i_flags
129 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
131 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
133 if( !p_priv->p_vars )
139 libvlc_global_data_t *p_libvlc_global;
142 /* Only the global root object is created out of the blue */
143 p_libvlc_global = (libvlc_global_data_t *)p_new;
144 p_new->p_libvlc = NULL;
146 p_libvlc_global->i_counter = 0;
147 p_libvlc_global->i_objects = 0;
148 p_libvlc_global->pp_objects = NULL;
149 p_priv->b_attached = true;
150 vlc_mutex_init( &structure_lock );
154 p_libvlc_global = vlc_global();
155 if( i_type == VLC_OBJECT_LIBVLC )
157 p_new->p_libvlc = (libvlc_int_t*)p_new;
158 p_priv->b_attached = true;
162 p_new->p_libvlc = p_this->p_libvlc;
166 vlc_spin_init( &p_priv->ref_spin );
167 p_priv->i_refcount = 1;
168 p_priv->pf_destructor = NULL;
169 p_priv->b_thread = false;
170 p_new->p_parent = NULL;
171 p_new->pp_children = NULL;
172 p_new->i_children = 0;
174 p_new->p_private = NULL;
176 /* Initialize mutexes and condvars */
177 vlc_mutex_init( &p_new->object_lock );
178 vlc_cond_init( p_new, &p_new->object_wait );
179 vlc_mutex_init( &p_priv->var_lock );
180 vlc_spin_init( &p_priv->spin );
181 p_priv->pipes[0] = p_priv->pipes[1] = -1;
183 vlc_mutex_lock( &structure_lock );
184 p_new->i_object_id = p_libvlc_global->i_counter++;
185 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
186 * useless to try and recover anything if pp_objects gets smashed. */
187 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
189 vlc_mutex_unlock( &structure_lock );
191 if( i_type == VLC_OBJECT_LIBVLC )
193 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
194 var_AddCallback( p_new, "list", DumpCommand, NULL );
195 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
196 var_AddCallback( p_new, "tree", DumpCommand, NULL );
197 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
198 var_AddCallback( p_new, "vars", DumpCommand, NULL );
206 * Allocates and initializes a vlc object.
208 * @param i_type known object type (all of them are negative integer values),
209 * or object byte size (always positive).
211 * @return the new object, or NULL on error.
213 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
215 const char * psz_type;
220 case VLC_OBJECT_LIBVLC:
221 i_size = sizeof(libvlc_int_t);
224 case VLC_OBJECT_INTF:
225 i_size = sizeof(intf_thread_t);
226 psz_type = "interface";
228 case VLC_OBJECT_DIALOGS:
229 i_size = sizeof(intf_thread_t);
230 psz_type = "dialogs";
232 case VLC_OBJECT_DEMUX:
233 i_size = sizeof(demux_t);
236 case VLC_OBJECT_ACCESS:
237 i_size = sizeof(access_t);
240 case VLC_OBJECT_DECODER:
241 i_size = sizeof(decoder_t);
242 psz_type = "decoder";
244 case VLC_OBJECT_PACKETIZER:
245 i_size = sizeof(decoder_t);
246 psz_type = "packetizer";
248 case VLC_OBJECT_ENCODER:
249 i_size = sizeof(encoder_t);
250 psz_type = "encoder";
252 case VLC_OBJECT_FILTER:
253 i_size = sizeof(filter_t);
256 case VLC_OBJECT_VOUT:
257 i_size = sizeof(vout_thread_t);
258 psz_type = "video output";
260 case VLC_OBJECT_AOUT:
261 i_size = sizeof(aout_instance_t);
262 psz_type = "audio output";
264 case VLC_OBJECT_SOUT:
265 i_size = sizeof(sout_instance_t);
266 psz_type = "stream output";
268 case VLC_OBJECT_OPENGL:
269 i_size = sizeof( vout_thread_t );
272 case VLC_OBJECT_ANNOUNCE:
273 i_size = sizeof( announce_handler_t );
274 psz_type = "announce";
276 case VLC_OBJECT_INTERACTION:
277 i_size = sizeof( interaction_t );
278 psz_type = "interaction";
281 i_size = i_type > (int)sizeof(vlc_object_t)
282 ? i_type : (int)sizeof(vlc_object_t);
283 i_type = VLC_OBJECT_GENERIC;
284 psz_type = "generic";
288 return vlc_custom_create( p_this, i_size, i_type, psz_type );
293 ****************************************************************************
294 * Set the destructor of a vlc object
296 * This function sets the destructor of the vlc object. It will be called
297 * when the object is destroyed when the its refcount reaches 0.
298 * (It is called by the internal function vlc_object_destroy())
299 *****************************************************************************/
300 void __vlc_object_set_destructor( vlc_object_t *p_this,
301 vlc_destructor_t pf_destructor )
303 vlc_object_internals_t *p_priv = vlc_internals(p_this );
304 p_priv->pf_destructor = pf_destructor;
308 ****************************************************************************
309 * Destroy a vlc object (Internal)
311 * This function destroys an object that has been previously allocated with
312 * vlc_object_create. The object's refcount must be zero and it must not be
313 * attached to other objects in any way.
314 *****************************************************************************/
315 static void vlc_object_destroy( vlc_object_t *p_this )
317 vlc_object_internals_t *p_priv = vlc_internals( p_this );
319 /* Objects are always detached beforehand */
320 assert( !p_this->p_parent );
322 /* Send a kill to the object's thread if applicable */
323 vlc_object_kill( p_this );
325 /* If we are running on a thread, wait until it ends */
326 if( p_priv->b_thread )
327 vlc_thread_join( p_this );
329 /* Call the custom "subclass" destructor */
330 if( p_priv->pf_destructor )
331 p_priv->pf_destructor( p_this );
334 if( p_this->i_children )
339 "ERROR: cannot delete object (%i, %s) with %d children\n",
340 p_this->i_object_id, p_this->psz_object_name,
341 p_this->i_children );
343 for( i = 0; i < p_this->i_children; i++ )
346 "ERROR: Remaining children object "
347 "(id:%i, type:%s, name:%s)\n",
348 p_this->pp_children[i]->i_object_id,
349 p_this->pp_children[i]->psz_object_type,
350 p_this->pp_children[i]->psz_object_name );
354 /* Dump libvlc object to ease debugging */
355 vlc_object_dump( p_this->p_libvlc );
360 /* Destroy the associated variables, starting from the end so that
361 * no memmove calls have to be done. */
362 while( p_priv->i_vars )
364 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
367 free( p_priv->p_vars );
368 vlc_mutex_destroy( &p_priv->var_lock );
370 free( p_this->psz_header );
372 if( p_this->p_libvlc == NULL )
374 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
377 assert( p_global == vlc_global() );
379 if( p_global->i_objects > 0 )
382 for( i = 0; i < p_global->i_objects; i++ )
384 /* We are leaking this object */
386 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
387 p_global->pp_objects[i]->i_object_id,
388 p_global->pp_objects[i]->psz_object_type,
389 p_global->pp_objects[i]->psz_object_name );
391 /* Dump libvlc object to ease debugging */
392 vlc_object_dump( p_global->pp_objects[i] );
397 /* Dump libvlc object to ease debugging */
398 vlc_object_dump( p_this );
400 /* Strongly abort, cause we want these to be fixed */
405 /* We are the global object ... no need to lock. */
406 free( p_global->pp_objects );
407 p_global->pp_objects = NULL;
409 vlc_mutex_destroy( &structure_lock );
412 FREENULL( p_this->psz_object_name );
414 #if defined(WIN32) || defined(UNDER_CE)
415 /* if object has an associated thread, close it now */
416 if( p_priv->thread_id )
417 CloseHandle(p_priv->thread_id);
420 vlc_spin_destroy( &p_priv->ref_spin );
421 vlc_mutex_destroy( &p_this->object_lock );
422 vlc_cond_destroy( &p_this->object_wait );
423 vlc_spin_destroy( &p_priv->spin );
424 if( p_priv->pipes[1] != -1 )
425 close( p_priv->pipes[1] );
426 if( p_priv->pipes[0] != -1 )
427 close( p_priv->pipes[0] );
433 /** Inter-object signaling */
435 void __vlc_object_lock( vlc_object_t *obj )
437 vlc_mutex_lock( &obj->object_lock );
440 void __vlc_object_unlock( vlc_object_t *obj )
442 vlc_assert_locked( &obj->object_lock );
443 vlc_mutex_unlock( &obj->object_lock );
447 # include <winsock2.h>
448 # include <ws2tcpip.h>
451 * select()-able pipes emulated using Winsock
453 static int pipe (int fd[2])
456 int addrlen = sizeof (addr);
458 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
459 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
460 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
463 memset (&addr, 0, sizeof (addr));
464 addr.sin_family = AF_INET;
465 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
466 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
467 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
469 || connect (c, (PSOCKADDR)&addr, addrlen))
472 a = accept (l, NULL, NULL);
473 if (a == INVALID_SOCKET)
484 if (l != INVALID_SOCKET)
486 if (c != INVALID_SOCKET)
492 #define read( a, b, c ) recv (a, b, c, 0)
494 #define write( a, b, c ) send (a, b, c, 0)
496 #define close( a ) closesocket (a)
500 * Returns the readable end of a pipe that becomes readable once termination
501 * of the object is requested (vlc_object_kill()).
502 * This can be used to wake-up out of a select() or poll() event loop, such
503 * typically when doing network I/O.
505 * Note that the pipe will remain the same for the lifetime of the object.
506 * DO NOT read the pipe nor close it yourself. Ever.
508 * @param obj object that would be "killed"
509 * @return a readable pipe descriptor, or -1 on error.
511 int __vlc_object_waitpipe( vlc_object_t *obj )
513 int pfd[2] = { -1, -1 };
514 vlc_object_internals_t *internals = vlc_internals( obj );
517 vlc_spin_lock (&internals->spin);
518 if (internals->pipes[0] == -1)
520 /* This can only ever happen if someone killed us without locking: */
521 assert (internals->pipes[1] == -1);
522 vlc_spin_unlock (&internals->spin);
527 vlc_spin_lock (&internals->spin);
528 if (internals->pipes[0] == -1)
530 internals->pipes[0] = pfd[0];
531 internals->pipes[1] = pfd[1];
532 pfd[0] = pfd[1] = -1;
536 vlc_spin_unlock (&internals->spin);
540 /* Race condition: vlc_object_kill() already invoked! */
543 vlc_spin_lock (&internals->spin);
544 fd = internals->pipes[1];
545 internals->pipes[1] = -1;
546 vlc_spin_unlock (&internals->spin);
548 msg_Dbg (obj, "waitpipe: object already dying");
553 /* Race condition: two threads call pipe() - unlikely */
559 return internals->pipes[0];
564 * Waits for the object to be signaled (using vlc_object_signal()).
565 * If the object already has a signal pending, this function will return
566 * immediately. It is asserted that the caller holds the object lock.
568 * @return true if the object is dying and should terminate.
570 bool __vlc_object_wait( vlc_object_t *obj )
572 vlc_assert_locked( &obj->object_lock );
573 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
579 * Waits for the object to be signaled (using vlc_object_signal()), or for
581 * If the object already has a signal pending, this function will return
582 * immediately. It is asserted that the caller holds the object lock.
584 * @return negative if the object is dying and should terminate,
585 * positive if the the object has been signaled but is not dying,
586 * 0 if timeout has been reached.
588 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
592 vlc_assert_locked( &obj->object_lock );
593 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
594 if( v == 0 ) /* signaled */
595 return obj->b_die ? -1 : 1;
601 * Checks whether an object has been "killed".
602 * The object lock must be held.
604 * Typical code for an object thread could be:
606 vlc_object_lock (self);
608 while (vlc_object_alive (self))
612 if (vlc_object_wait (self))
617 ...deinitialization...
618 vlc_object_unlock (self);
621 * @return true iff the object has not been killed yet
623 bool __vlc_object_alive( vlc_object_t *obj )
625 vlc_assert_locked( &obj->object_lock );
631 * Signals an object for which the lock is held.
633 void __vlc_object_signal_unlocked( vlc_object_t *obj )
635 vlc_assert_locked (&obj->object_lock);
636 vlc_cond_signal( &obj->object_wait );
641 * Requests termination of an object.
642 * If the object is LibVLC, also request to terminate all its children.
644 void __vlc_object_kill( vlc_object_t *p_this )
646 vlc_object_internals_t *internals = vlc_internals( p_this );
649 vlc_mutex_lock( &p_this->object_lock );
650 p_this->b_die = true;
652 vlc_spin_lock (&internals->spin);
653 fd = internals->pipes[1];
654 internals->pipes[1] = -1;
655 vlc_spin_unlock (&internals->spin);
659 msg_Dbg (p_this, "waitpipe: object killed");
663 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
664 for( int i = 0; i < p_this->i_children ; i++ )
665 vlc_object_kill( p_this->pp_children[i] );
667 vlc_object_signal_unlocked( p_this );
668 vlc_mutex_unlock( &p_this->object_lock );
673 * find an object given its ID
675 * This function looks for the object whose i_object_id field is i_id. We
676 * use a dichotomy so that lookups are in log2(n).
677 *****************************************************************************/
678 void * vlc_object_get( int i_id )
681 vlc_object_t **pp_objects;
682 libvlc_global_data_t *p_libvlc_global = vlc_global();
683 vlc_object_t *obj = NULL;
685 vlc_mutex_lock( &structure_lock );
687 pp_objects = p_libvlc_global->pp_objects;
689 /* Perform our dichotomy */
690 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
692 i_middle = i_max / 2;
694 if( pp_objects[i_middle]->i_object_id > i_id )
698 else if( pp_objects[i_middle]->i_object_id < i_id )
702 pp_objects += i_middle;
707 /* This happens when there are only two remaining objects */
708 if( pp_objects[i_middle+1]->i_object_id == i_id )
710 vlc_object_yield( pp_objects[i_middle+1] );
711 obj = pp_objects[i_middle+1];
718 vlc_object_yield( pp_objects[i_middle] );
719 obj = pp_objects[i_middle];
724 vlc_mutex_unlock( &structure_lock );
729 ****************************************************************************
730 * find a typed object and increment its refcount
731 *****************************************************************************
732 * This function recursively looks for a given object type. i_mode can be one
733 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
734 *****************************************************************************/
735 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
737 vlc_object_t *p_found;
739 /* If we are of the requested type ourselves, don't look further */
740 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
742 vlc_object_yield( p_this );
746 vlc_mutex_lock( &structure_lock );
748 /* Otherwise, recursively look for the object */
749 if( (i_mode & 0x000f) == FIND_ANYWHERE )
751 vlc_object_t *p_root = p_this;
754 while( p_root->p_parent != NULL &&
755 p_root != VLC_OBJECT( p_this->p_libvlc ) )
757 p_root = p_root->p_parent;
760 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
761 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
763 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
764 i_type, (i_mode & ~0x000f)|FIND_CHILD );
769 p_found = FindObject( p_this, i_type, i_mode );
772 vlc_mutex_unlock( &structure_lock );
778 ****************************************************************************
779 * find a named object and increment its refcount
780 *****************************************************************************
781 * This function recursively looks for a given object name. i_mode can be one
782 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
783 *****************************************************************************/
784 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
787 vlc_object_t *p_found;
789 /* If have the requested name ourselves, don't look further */
790 if( !(i_mode & FIND_STRICT)
791 && p_this->psz_object_name
792 && !strcmp( p_this->psz_object_name, psz_name ) )
794 vlc_object_yield( p_this );
798 vlc_mutex_lock( &structure_lock );
800 /* Otherwise, recursively look for the object */
801 if( (i_mode & 0x000f) == FIND_ANYWHERE )
803 vlc_object_t *p_root = p_this;
806 while( p_root->p_parent != NULL &&
807 p_root != VLC_OBJECT( p_this->p_libvlc ) )
809 p_root = p_root->p_parent;
812 p_found = FindObjectName( p_root, psz_name,
813 (i_mode & ~0x000f)|FIND_CHILD );
814 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
816 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
817 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
822 p_found = FindObjectName( p_this, psz_name, i_mode );
825 vlc_mutex_unlock( &structure_lock );
831 * Increment an object reference counter.
833 void __vlc_object_yield( vlc_object_t *p_this )
835 vlc_object_internals_t *internals = vlc_internals( p_this );
837 vlc_spin_lock( &internals->ref_spin );
838 /* Avoid obvious freed object uses */
839 assert( internals->i_refcount > 0 );
840 /* Increment the counter */
841 internals->i_refcount++;
842 vlc_spin_unlock( &internals->ref_spin );
845 /*****************************************************************************
846 * decrement an object refcount
847 * And destroy the object if its refcount reach zero.
848 *****************************************************************************/
849 void __vlc_object_release( vlc_object_t *p_this )
851 vlc_object_internals_t *internals = vlc_internals( p_this );
852 bool b_should_destroy;
854 vlc_spin_lock( &internals->ref_spin );
855 assert( internals->i_refcount > 0 );
857 if( internals->i_refcount > 1 )
860 /* There are still other references to the object */
861 internals->i_refcount--;
862 vlc_spin_unlock( &internals->ref_spin );
865 vlc_spin_unlock( &internals->ref_spin );
868 /* Remember that we cannot hold the spin while waiting on the mutex */
869 vlc_mutex_lock( &structure_lock );
870 /* Take the spin again. Note that another thread may have yielded the
871 * object in the (very short) mean time. */
872 vlc_spin_lock( &internals->ref_spin );
873 b_should_destroy = --internals->i_refcount == 0;
874 vlc_spin_unlock( &internals->ref_spin );
876 if( b_should_destroy )
878 /* Remove the object from the table so that it cannot be returned from
879 * vlc_object_find() and friends. */
880 libvlc_global_data_t *p_libvlc_global = vlc_global();
883 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
884 p_libvlc_global->i_objects );
885 REMOVE_ELEM( p_libvlc_global->pp_objects,
886 p_libvlc_global->i_objects, i_index );
887 if (p_this->p_parent)
888 vlc_object_detach_unlocked (p_this);
891 vlc_mutex_unlock( &structure_lock );
893 if( b_should_destroy )
894 vlc_object_destroy( p_this );
898 ****************************************************************************
899 * attach object to a parent object
900 *****************************************************************************
901 * This function sets p_this as a child of p_parent, and p_parent as a parent
902 * of p_this. This link can be undone using vlc_object_detach.
903 *****************************************************************************/
904 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
906 if( !p_this ) return;
908 vlc_mutex_lock( &structure_lock );
910 /* Attach the parent to its child */
911 p_this->p_parent = p_parent;
913 /* Attach the child to its parent */
914 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
915 p_parent->i_children, p_this );
917 /* Climb up the tree to see whether we are connected with the root */
918 if( vlc_internals( p_parent )->b_attached )
920 SetAttachment( p_this, true );
923 vlc_mutex_unlock( &structure_lock );
927 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
929 assert (p_this->p_parent);
931 /* Climb up the tree to see whether we are connected with the root */
932 if( vlc_internals( p_this->p_parent )->b_attached )
933 SetAttachment( p_this, false );
935 DetachObject( p_this );
940 ****************************************************************************
941 * detach object from its parent
942 *****************************************************************************
943 * This function removes all links between an object and its parent.
944 *****************************************************************************/
945 void __vlc_object_detach( vlc_object_t *p_this )
947 if( !p_this ) return;
949 vlc_mutex_lock( &structure_lock );
951 if( !p_this->p_parent )
953 msg_Err( p_this, "object is not attached" );
954 vlc_mutex_unlock( &structure_lock );
958 vlc_object_detach_unlocked( p_this );
959 vlc_mutex_unlock( &structure_lock );
963 static void vlc_tree_find (vlc_object_t *node, int type, vlc_list_t *list)
966 vlc_assert_locked (&structure_lock);
968 if (node->i_object_type == type)
969 ListAppend (list, node);
971 for (int i = 0; i < node->i_children; i++)
972 vlc_tree_find (node->pp_children[i], type, list);
976 ****************************************************************************
977 * find a list typed objects and increment their refcount
978 *****************************************************************************
979 * This function recursively looks for a given object type. i_mode can be one
980 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
981 *****************************************************************************/
982 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
985 int i_count = 0, i_index = 0;
986 libvlc_global_data_t *p_libvlc_global = vlc_global();
988 vlc_mutex_lock( &structure_lock );
990 /* Look for the objects */
991 switch( i_mode & 0x000f )
994 p_list = NewList (0);
996 vlc_tree_find (VLC_OBJECT (p_libvlc_global), i_type, p_list);
1000 i_count = CountChildren( p_this, i_type );
1001 p_list = NewList( i_count );
1003 /* Check allocation was successful */
1004 if( p_list->i_count != i_count )
1006 msg_Err( p_this, "list allocation failed!" );
1007 p_list->i_count = 0;
1011 p_list->i_count = 0;
1012 ListChildren( p_list, p_this, i_type );
1016 msg_Err( p_this, "unimplemented!" );
1017 p_list = NewList( 0 );
1021 vlc_mutex_unlock( &structure_lock );
1026 /*****************************************************************************
1027 * DumpCommand: print the current vlc structure
1028 *****************************************************************************
1029 * This function prints either an ASCII tree showing the connections between
1030 * vlc objects, and additional information such as their refcount, thread ID,
1031 * etc. (command "tree"), or the same data as a simple list (command "list").
1032 *****************************************************************************/
1033 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1034 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1036 libvlc_global_data_t *p_libvlc_global = vlc_global();
1038 (void)oldval; (void)p_data;
1039 if( *psz_cmd == 'l' )
1041 vlc_mutex_lock( &structure_lock );
1043 vlc_object_t **pp_current, **pp_end;
1045 pp_current = p_libvlc_global->pp_objects;
1046 pp_end = pp_current + p_libvlc_global->i_objects;
1048 for( ; pp_current < pp_end ; pp_current++ )
1050 if( vlc_internals(*pp_current)->b_attached )
1052 PrintObject( *pp_current, "" );
1056 printf( " o %.8i %s (not attached)\n",
1057 (*pp_current)->i_object_id,
1058 (*pp_current)->psz_object_type );
1062 vlc_mutex_unlock( &structure_lock );
1066 vlc_object_t *p_object = NULL;
1068 if( *newval.psz_string )
1071 int i_id = strtol( newval.psz_string, &end, 0 );
1072 if( end != newval.psz_string )
1073 p_object = vlc_object_get( i_id );
1076 /* try using the object's name to find it */
1077 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1080 /* Look in p_libvlc's children tree */
1081 p_object = vlc_object_find_name( p_libvlc,
1084 vlc_object_release( p_libvlc );
1088 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1089 p_object = vlc_object_find_name( p_this,
1101 vlc_mutex_lock( &structure_lock );
1103 if( *psz_cmd == 't' )
1105 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1108 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1111 DumpStructure( p_object, 0, psz_foo );
1113 else if( *psz_cmd == 'v' )
1118 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1120 PrintObject( p_object, "" );
1122 if( !vlc_internals( p_object )->i_vars )
1123 printf( " `-o No variables\n" );
1124 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1126 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1128 const char *psz_type = "unknown";
1129 switch( p_var->i_type & VLC_VAR_TYPE )
1131 #define MYCASE( type, nice ) \
1132 case VLC_VAR_ ## type: \
1135 MYCASE( VOID, "void" );
1136 MYCASE( BOOL, "bool" );
1137 MYCASE( INTEGER, "integer" );
1138 MYCASE( HOTKEY, "hotkey" );
1139 MYCASE( STRING, "string" );
1140 MYCASE( MODULE, "module" );
1141 MYCASE( FILE, "file" );
1142 MYCASE( DIRECTORY, "directory" );
1143 MYCASE( VARIABLE, "variable" );
1144 MYCASE( FLOAT, "float" );
1145 MYCASE( TIME, "time" );
1146 MYCASE( ADDRESS, "address" );
1147 MYCASE( MUTEX, "mutex" );
1148 MYCASE( LIST, "list" );
1151 printf( " %c-o \"%s\" (%s",
1152 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1153 p_var->psz_name, psz_type );
1154 if( p_var->psz_text )
1155 printf( ", %s", p_var->psz_text );
1157 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1158 printf( ", command" );
1159 if( p_var->i_entries )
1160 printf( ", %d callbacks", p_var->i_entries );
1161 switch( p_var->i_type & 0x00f0 )
1167 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1169 case VLC_VAR_INTEGER:
1170 printf( ": %d", p_var->val.i_int );
1172 case VLC_VAR_STRING:
1173 printf( ": \"%s\"", p_var->val.psz_string );
1176 printf( ": %f", p_var->val.f_float );
1179 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1181 case VLC_VAR_ADDRESS:
1182 printf( ": %p", p_var->val.p_address );
1192 vlc_mutex_unlock( &structure_lock );
1194 if( *newval.psz_string )
1196 vlc_object_release( p_object );
1203 /*****************************************************************************
1204 * vlc_list_release: free a list previously allocated by vlc_list_find
1205 *****************************************************************************
1206 * This function decreases the refcount of all objects in the list and
1208 *****************************************************************************/
1209 void vlc_list_release( vlc_list_t *p_list )
1213 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1215 vlc_object_release( p_list->p_values[i_index].p_object );
1218 free( p_list->p_values );
1222 /*****************************************************************************
1223 * dump an object. (Debug function)
1224 *****************************************************************************/
1225 void __vlc_object_dump( vlc_object_t *p_this )
1227 vlc_mutex_lock( &structure_lock );
1228 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1230 DumpStructure( p_this, 0, psz_foo );
1231 vlc_mutex_unlock( &structure_lock );
1234 /* Following functions are local */
1236 /*****************************************************************************
1237 * FindIndex: find the index of an object in an array of objects
1238 *****************************************************************************
1239 * This function assumes that p_this can be found in pp_objects. It will not
1240 * crash if p_this cannot be found, but will return a wrong value. It is your
1241 * duty to check the return value if you are not certain that the object could
1242 * be found for sure.
1243 *****************************************************************************/
1244 static int FindIndex( vlc_object_t *p_this,
1245 vlc_object_t **pp_objects, int i_count )
1247 int i_middle = i_count / 2;
1254 if( pp_objects[i_middle] == p_this )
1264 /* We take advantage of the sorted array */
1265 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1267 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1268 i_count - i_middle );
1272 return FindIndex( p_this, pp_objects, i_middle );
1276 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1279 vlc_object_t *p_tmp;
1281 switch( i_mode & 0x000f )
1284 p_tmp = p_this->p_parent;
1287 if( p_tmp->i_object_type == i_type )
1289 vlc_object_yield( p_tmp );
1294 return FindObject( p_tmp, i_type, i_mode );
1300 for( i = p_this->i_children; i--; )
1302 p_tmp = p_this->pp_children[i];
1303 if( p_tmp->i_object_type == i_type )
1305 vlc_object_yield( p_tmp );
1308 else if( p_tmp->i_children )
1310 p_tmp = FindObject( p_tmp, i_type, i_mode );
1320 /* Handled in vlc_object_find */
1327 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1328 const char *psz_name,
1332 vlc_object_t *p_tmp;
1334 switch( i_mode & 0x000f )
1337 p_tmp = p_this->p_parent;
1340 if( p_tmp->psz_object_name
1341 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1343 vlc_object_yield( p_tmp );
1348 return FindObjectName( p_tmp, psz_name, i_mode );
1354 for( i = p_this->i_children; i--; )
1356 p_tmp = p_this->pp_children[i];
1357 if( p_tmp->psz_object_name
1358 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1360 vlc_object_yield( p_tmp );
1363 else if( p_tmp->i_children )
1365 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1375 /* Handled in vlc_object_find */
1382 static void DetachObject( vlc_object_t *p_this )
1384 vlc_object_t *p_parent = p_this->p_parent;
1387 /* Remove p_this's parent */
1388 p_this->p_parent = NULL;
1390 /* Remove all of p_parent's children which are p_this */
1391 for( i_index = p_parent->i_children ; i_index-- ; )
1393 if( p_parent->pp_children[i_index] == p_this )
1395 p_parent->i_children--;
1396 for( i = i_index ; i < p_parent->i_children ; i++ )
1398 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1403 if( p_parent->i_children )
1405 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1406 p_parent->i_children * sizeof(vlc_object_t *) );
1410 free( p_parent->pp_children );
1411 p_parent->pp_children = NULL;
1415 /*****************************************************************************
1416 * SetAttachment: recursively set the b_attached flag of a subtree.
1417 *****************************************************************************
1418 * This function is used by the attach and detach functions to propagate
1419 * the b_attached flag in a subtree.
1420 *****************************************************************************/
1421 static void SetAttachment( vlc_object_t *p_this, bool b_attached )
1425 for( i_index = p_this->i_children ; i_index-- ; )
1427 SetAttachment( p_this->pp_children[i_index], b_attached );
1430 vlc_internals( p_this )->b_attached = b_attached;
1433 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1435 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1438 memset( &psz_name, 0, sizeof(psz_name) );
1439 if( p_this->psz_object_name )
1441 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1443 psz_name[48] = '\"';
1446 psz_children[0] = '\0';
1447 switch( p_this->i_children )
1452 strcpy( psz_children, ", 1 child" );
1455 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1459 psz_refcount[0] = '\0';
1460 if( vlc_internals( p_this )->i_refcount > 0 )
1461 snprintf( psz_refcount, 19, ", refcount %u",
1462 vlc_internals( p_this )->i_refcount );
1464 psz_thread[0] = '\0';
1465 if( vlc_internals( p_this )->b_thread )
1466 snprintf( psz_thread, 29, " (thread %lu)",
1467 (unsigned long)vlc_internals( p_this )->thread_id );
1469 psz_parent[0] = '\0';
1470 if( p_this->p_parent )
1471 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1473 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1474 p_this->i_object_id, p_this->psz_object_type,
1475 psz_name, psz_thread, psz_refcount, psz_children,
1479 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1482 char i_back = psz_foo[i_level];
1483 psz_foo[i_level] = '\0';
1485 PrintObject( p_this, psz_foo );
1487 psz_foo[i_level] = i_back;
1489 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1491 msg_Warn( p_this, "structure tree is too deep" );
1495 for( i = 0 ; i < p_this->i_children ; i++ )
1499 psz_foo[i_level-1] = ' ';
1501 if( psz_foo[i_level-2] == '`' )
1503 psz_foo[i_level-2] = ' ';
1507 if( i == p_this->i_children - 1 )
1509 psz_foo[i_level] = '`';
1513 psz_foo[i_level] = '|';
1516 psz_foo[i_level+1] = '-';
1517 psz_foo[i_level+2] = '\0';
1519 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1523 static vlc_list_t * NewList( int i_count )
1525 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1526 if( p_list == NULL )
1531 p_list->i_count = i_count;
1535 p_list->p_values = NULL;
1539 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1540 if( p_list->p_values == NULL )
1542 p_list->i_count = 0;
1549 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1552 if( p_list == NULL || i_index >= p_list->i_count )
1557 vlc_object_yield( p_object );
1559 p_list->p_values[i_index].p_object = p_object;
1564 static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1566 if( p_list == NULL )
1571 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1572 * sizeof( vlc_value_t ) );
1573 if( p_list->p_values == NULL )
1575 p_list->i_count = 0;
1579 vlc_object_yield( p_object );
1581 p_list->p_values[p_list->i_count].p_object = p_object;
1587 static int CountChildren( vlc_object_t *p_this, int i_type )
1589 vlc_object_t *p_tmp;
1592 for( i = 0; i < p_this->i_children; i++ )
1594 p_tmp = p_this->pp_children[i];
1596 if( p_tmp->i_object_type == i_type )
1601 if( p_tmp->i_children )
1603 i_count += CountChildren( p_tmp, i_type );
1610 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1612 vlc_object_t *p_tmp;
1615 for( i = 0; i < p_this->i_children; i++ )
1617 p_tmp = p_this->pp_children[i];
1619 if( p_tmp->i_object_type == i_type )
1621 ListReplace( p_list, p_tmp, p_list->i_count++ );
1624 if( p_tmp->i_children )
1626 ListChildren( p_list, p_tmp, i_type );