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"
56 #include "vlc_httpd.h"
58 #include "input/vlm_internal.h"
63 #include "variables.h"
69 # include <errno.h> /* ENOSYS */
73 /*****************************************************************************
75 *****************************************************************************/
77 const vlc_destructor_t kVLCDestructor = NULL;
79 /*****************************************************************************
81 *****************************************************************************/
82 static int DumpCommand( vlc_object_t *, char const *,
83 vlc_value_t, vlc_value_t, void * );
85 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
86 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
87 static void DetachObject ( vlc_object_t * );
88 static void PrintObject ( vlc_object_t *, const char * );
89 static void DumpStructure ( vlc_object_t *, int, char * );
90 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
91 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
93 static vlc_list_t * NewList ( int );
94 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
95 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
96 static int CountChildren ( vlc_object_t *, int );
97 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
99 static void vlc_object_destroy( vlc_object_t *p_this );
100 static void vlc_object_yield_locked( vlc_object_t *p_this );
102 /*****************************************************************************
103 * Local structure lock
104 *****************************************************************************/
105 static vlc_mutex_t structure_lock;
106 static vlc_object_internals_t global_internals;
108 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
109 int i_type, const char *psz_type )
112 vlc_object_internals_t *p_priv;
114 if( i_type == VLC_OBJECT_GLOBAL )
117 p_priv = &global_internals;
118 memset( p_priv, 0, sizeof( *p_priv ) );
122 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
126 p_new = (vlc_object_t *)(p_priv + 1);
129 p_new->p_internals = p_priv;
130 p_new->i_object_type = i_type;
131 p_new->psz_object_type = psz_type;
133 p_new->psz_object_name = NULL;
135 p_new->b_die = VLC_FALSE;
136 p_new->b_error = VLC_FALSE;
137 p_new->b_dead = VLC_FALSE;
138 p_priv->b_attached = VLC_FALSE;
139 p_new->b_force = VLC_FALSE;
141 p_new->psz_header = NULL;
143 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
144 p_new->i_flags |= OBJECT_FLAGS_NODBG;
145 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
146 p_new->i_flags |= OBJECT_FLAGS_QUIET;
147 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
148 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
150 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
152 if( !p_priv->p_vars )
154 if( i_type != VLC_OBJECT_GLOBAL )
159 if( i_type == VLC_OBJECT_GLOBAL )
161 /* If i_type is global, then p_new is actually p_libvlc_global */
162 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
163 p_new->p_libvlc = NULL;
165 p_libvlc_global->i_counter = 0;
166 p_new->i_object_id = 0;
168 p_libvlc_global->i_objects = 1;
169 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
170 p_libvlc_global->pp_objects[0] = p_new;
171 p_priv->b_attached = VLC_TRUE;
175 libvlc_global_data_t *p_libvlc_global = vlc_global();
176 if( i_type == VLC_OBJECT_LIBVLC )
178 p_new->p_libvlc = (libvlc_int_t*)p_new;
179 p_priv->b_attached = VLC_TRUE;
183 p_new->p_libvlc = p_this->p_libvlc;
186 vlc_mutex_lock( &structure_lock );
188 p_libvlc_global->i_counter++;
189 p_new->i_object_id = p_libvlc_global->i_counter;
191 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
192 * useless to try and recover anything if pp_objects gets smashed. */
193 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
196 vlc_mutex_unlock( &structure_lock );
199 p_priv->i_refcount = 1;
200 p_priv->pf_destructor = kVLCDestructor;
201 p_priv->b_thread = VLC_FALSE;
202 p_new->p_parent = NULL;
203 p_new->pp_children = NULL;
204 p_new->i_children = 0;
206 p_new->p_private = NULL;
208 /* Initialize mutexes and condvars */
209 vlc_mutex_init( p_new, &p_new->object_lock );
210 vlc_cond_init( p_new, &p_new->object_wait );
211 vlc_mutex_init( p_new, &p_priv->var_lock );
212 vlc_spin_init( &p_priv->spin );
213 p_priv->pipes[0] = p_priv->pipes[1] = -1;
215 if( i_type == VLC_OBJECT_GLOBAL )
217 vlc_mutex_init( p_new, &structure_lock );
220 if( i_type == VLC_OBJECT_LIBVLC )
222 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
223 var_AddCallback( p_new, "list", DumpCommand, NULL );
224 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
225 var_AddCallback( p_new, "tree", DumpCommand, NULL );
226 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
227 var_AddCallback( p_new, "vars", DumpCommand, NULL );
235 * Allocates and initializes a vlc object.
237 * @param i_type known object type (all of them are negative integer values),
238 * or object byte size (always positive).
240 * @return the new object, or NULL on error.
242 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
244 const char * psz_type;
249 case VLC_OBJECT_GLOBAL:
250 i_size = sizeof(libvlc_global_data_t);
253 case VLC_OBJECT_LIBVLC:
254 i_size = sizeof(libvlc_int_t);
257 case VLC_OBJECT_INTF:
258 i_size = sizeof(intf_thread_t);
259 psz_type = "interface";
261 case VLC_OBJECT_DIALOGS:
262 i_size = sizeof(intf_thread_t);
263 psz_type = "dialogs";
265 case VLC_OBJECT_PLAYLIST:
266 i_size = sizeof(playlist_t);
267 psz_type = "playlist";
269 case VLC_OBJECT_INPUT:
270 i_size = sizeof(input_thread_t);
273 case VLC_OBJECT_DEMUX:
274 i_size = sizeof(demux_t);
277 case VLC_OBJECT_ACCESS:
278 i_size = sizeof(access_t);
281 case VLC_OBJECT_DECODER:
282 i_size = sizeof(decoder_t);
283 psz_type = "decoder";
285 case VLC_OBJECT_PACKETIZER:
286 i_size = sizeof(decoder_t);
287 psz_type = "packetizer";
289 case VLC_OBJECT_ENCODER:
290 i_size = sizeof(encoder_t);
291 psz_type = "encoder";
293 case VLC_OBJECT_FILTER:
294 i_size = sizeof(filter_t);
297 case VLC_OBJECT_VOUT:
298 i_size = sizeof(vout_thread_t);
299 psz_type = "video output";
301 case VLC_OBJECT_AOUT:
302 i_size = sizeof(aout_instance_t);
303 psz_type = "audio output";
305 case VLC_OBJECT_SOUT:
306 i_size = sizeof(sout_instance_t);
307 psz_type = "stream output";
310 i_size = sizeof( vlm_t );
311 psz_type = "vlm dameon";
314 i_size = sizeof( xml_t );
317 case VLC_OBJECT_OPENGL:
318 i_size = sizeof( vout_thread_t );
321 case VLC_OBJECT_ANNOUNCE:
322 i_size = sizeof( announce_handler_t );
323 psz_type = "announce";
325 case VLC_OBJECT_META_ENGINE:
326 i_size = sizeof( meta_engine_t );
327 psz_type = "meta engine";
329 case VLC_OBJECT_OSDMENU:
330 i_size = sizeof( osd_menu_t );
331 psz_type = "osd menu";
333 case VLC_OBJECT_INTERACTION:
334 i_size = sizeof( interaction_t );
335 psz_type = "interaction";
338 i_size = i_type > (int)sizeof(vlc_object_t)
339 ? i_type : (int)sizeof(vlc_object_t);
340 i_type = VLC_OBJECT_GENERIC;
341 psz_type = "generic";
345 return vlc_custom_create( p_this, i_size, i_type, psz_type );
350 ****************************************************************************
351 * Set the destructor of a vlc object
353 * This function sets the destructor of the vlc object. It will be called
354 * when the object is destroyed when the its refcount reaches 0.
355 * (It is called by the internal function vlc_object_destroy())
356 *****************************************************************************/
357 void __vlc_object_set_destructor( vlc_object_t *p_this,
358 vlc_destructor_t pf_destructor )
360 vlc_object_internals_t *p_priv = vlc_internals(p_this );
362 vlc_mutex_lock( &structure_lock );
363 p_priv->pf_destructor = pf_destructor;
364 vlc_mutex_unlock( &structure_lock );
368 ****************************************************************************
369 * Destroy a vlc object (Internal)
371 * This function destroys an object that has been previously allocated with
372 * vlc_object_create. The object's refcount must be zero and it must not be
373 * attached to other objects in any way.
374 *****************************************************************************/
375 static void vlc_object_destroy( vlc_object_t *p_this )
377 vlc_object_internals_t *p_priv = vlc_internals( p_this );
379 /* Automatically detach the object from its parents */
380 if( p_this->p_parent ) vlc_object_detach( p_this );
383 /* Send a kill to the object's thread if applicable */
384 vlc_object_kill( p_this );
386 /* If we are running on a thread, wait until it ends */
387 if( p_priv->b_thread )
388 vlc_thread_join( p_this );
390 /* Call the custom "subclass" destructor */
391 if( p_priv->pf_destructor )
392 p_priv->pf_destructor( p_this );
395 if( p_this->i_children )
400 "ERROR: cannot delete object (%i, %s) with %d children\n",
401 p_this->i_object_id, p_this->psz_object_name,
402 p_this->i_children );
404 for( i = 0; i < p_this->i_children; i++ )
407 "ERROR: Remaining children object "
408 "(id:%i, type:%s, name:%s)\n",
409 p_this->pp_children[i]->i_object_id,
410 p_this->pp_children[i]->psz_object_type,
411 p_this->pp_children[i]->psz_object_name );
417 /* Destroy the associated variables, starting from the end so that
418 * no memmove calls have to be done. */
419 while( p_priv->i_vars )
421 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
424 free( p_priv->p_vars );
425 vlc_mutex_destroy( &p_priv->var_lock );
427 free( p_this->psz_header );
429 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
431 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
434 if( p_global->i_objects > 0 )
437 for( i = 0; i < p_global->i_objects; i++ )
439 /* We are leaking this object */
441 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
442 p_global->pp_objects[i]->i_object_id,
443 p_global->pp_objects[i]->psz_object_type,
444 p_global->pp_objects[i]->psz_object_name );
447 /* Strongly abort, cause we want these to be fixed */
451 /* We are the global object ... no need to lock. */
452 free( p_global->pp_objects );
453 p_global->pp_objects = NULL;
455 vlc_mutex_destroy( &structure_lock );
458 #if defined(WIN32) || defined(UNDER_CE)
459 /* if object has an associated thread, close it now */
460 if( p_priv->thread_id.hThread )
461 CloseHandle(p_priv->thread_id.hThread);
464 vlc_mutex_destroy( &p_this->object_lock );
465 vlc_cond_destroy( &p_this->object_wait );
466 vlc_spin_destroy( &p_priv->spin );
467 if( p_priv->pipes[1] != -1 )
468 close( p_priv->pipes[1] );
469 if( p_priv->pipes[0] != -1 )
470 close( p_priv->pipes[0] );
472 /* global is not dynamically allocated by vlc_object_create */
473 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
478 /** Inter-object signaling */
480 void __vlc_object_lock( vlc_object_t *obj )
482 vlc_mutex_lock( &obj->object_lock );
485 void __vlc_object_unlock( vlc_object_t *obj )
487 vlc_assert_locked( &obj->object_lock );
488 vlc_mutex_unlock( &obj->object_lock );
492 # include <winsock2.h>
493 # include <ws2tcpip.h>
496 * select()-able pipes emulated using Winsock
498 static int pipe (int fd[2])
501 int addrlen = sizeof (addr);
503 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
504 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
505 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
508 memset (&addr, 0, sizeof (addr));
509 addr.sin_family = AF_INET;
510 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
511 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
512 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
514 || connect (c, (PSOCKADDR)&addr, addrlen))
517 a = accept (l, NULL, NULL);
518 if (a == INVALID_SOCKET)
529 if (l != INVALID_SOCKET)
531 if (c != INVALID_SOCKET)
537 #define read( a, b, c ) recv (a, b, c, 0)
539 #define write( a, b, c ) send (a, b, c, 0)
541 #define close( a ) closesocket (a)
545 * Returns the readable end of a pipe that becomes readable once termination
546 * of the object is requested (vlc_object_kill()).
547 * This can be used to wake-up out of a select() or poll() event loop, such
548 * typically when doing network I/O.
550 * Note that the pipe will remain the same for the lifetime of the object.
551 * DO NOT read the pipe nor close it yourself. Ever.
553 * @param obj object that would be "killed"
554 * @return a readable pipe descriptor, or -1 on error.
556 int __vlc_object_waitpipe( vlc_object_t *obj )
558 int pfd[2] = { -1, -1 };
559 struct vlc_object_internals_t *internals = obj->p_internals;
560 vlc_bool_t killed = VLC_FALSE;
562 vlc_spin_lock (&internals->spin);
563 if (internals->pipes[0] == -1)
565 /* This can only ever happen if someone killed us without locking: */
566 assert (internals->pipes[1] == -1);
567 vlc_spin_unlock (&internals->spin);
572 vlc_spin_lock (&internals->spin);
573 if (internals->pipes[0] == -1)
575 internals->pipes[0] = pfd[0];
576 internals->pipes[1] = pfd[1];
577 pfd[0] = pfd[1] = -1;
581 vlc_spin_unlock (&internals->spin);
585 /* Race condition: vlc_object_kill() already invoked! */
588 vlc_spin_lock (&internals->spin);
589 fd = internals->pipes[1];
590 internals->pipes[1] = -1;
591 vlc_spin_unlock (&internals->spin);
593 msg_Dbg (obj, "waitpipe: object already dying");
598 /* Race condition: two threads call pipe() - unlikely */
604 return internals->pipes[0];
609 * Waits for the object to be signaled (using vlc_object_signal()).
610 * If the object already has a signal pending, this function will return
611 * immediately. It is asserted that the caller holds the object lock.
613 * @return true if the object is dying and should terminate.
615 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
617 vlc_assert_locked( &obj->object_lock );
618 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
624 * Waits for the object to be signaled (using vlc_object_signal()), or for
626 * If the object already has a signal pending, this function will return
627 * immediately. It is asserted that the caller holds the object lock.
629 * @return negative if the object is dying and should terminate,
630 * positive if the the object has been signaled but is not dying,
631 * 0 if timeout has been reached.
633 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
637 vlc_assert_locked( &obj->object_lock );
638 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
639 if( v == 0 ) /* signaled */
640 return obj->b_die ? -1 : 1;
646 * Checks whether an object has been "killed".
647 * The object lock must be held.
649 * Typical code for an object thread could be:
651 vlc_object_lock (self);
653 while (vlc_object_alive (self))
657 if (vlc_object_wait (self))
662 ...deinitialization...
663 vlc_object_unlock (self);
666 * @return true iff the object has not been killed yet
668 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
670 vlc_assert_locked( &obj->object_lock );
676 * Signals an object for which the lock is held.
678 void __vlc_object_signal_unlocked( vlc_object_t *obj )
680 vlc_assert_locked (&obj->object_lock);
681 vlc_cond_signal( &obj->object_wait );
686 * Requests termination of an object.
687 * If the object is LibVLC, also request to terminate all its children.
689 void __vlc_object_kill( vlc_object_t *p_this )
691 struct vlc_object_internals_t *internals = p_this->p_internals;
694 vlc_mutex_lock( &p_this->object_lock );
695 p_this->b_die = VLC_TRUE;
697 vlc_spin_lock (&internals->spin);
698 fd = internals->pipes[1];
699 internals->pipes[1] = -1;
700 vlc_spin_unlock (&internals->spin);
704 msg_Dbg (p_this, "waitpipe: object killed");
708 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
709 for( int i = 0; i < p_this->i_children ; i++ )
710 vlc_object_kill( p_this->pp_children[i] );
712 vlc_object_signal_unlocked( p_this );
713 vlc_mutex_unlock( &p_this->object_lock );
718 * find an object given its ID
720 * This function looks for the object whose i_object_id field is i_id. We
721 * use a dichotomy so that lookups are in log2(n).
722 *****************************************************************************/
723 void * vlc_object_get( int i_id )
726 vlc_object_t **pp_objects;
727 libvlc_global_data_t *p_libvlc_global = vlc_global();
729 vlc_mutex_lock( &structure_lock );
731 pp_objects = p_libvlc_global->pp_objects;
733 /* Perform our dichotomy */
734 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
736 i_middle = i_max / 2;
738 if( pp_objects[i_middle]->i_object_id > i_id )
742 else if( pp_objects[i_middle]->i_object_id < i_id )
746 pp_objects += i_middle;
751 /* This happens when there are only two remaining objects */
752 if( pp_objects[i_middle+1]->i_object_id == i_id
753 && pp_objects[i_middle+1]->p_internals->i_refcount > 0 )
755 vlc_object_yield_locked( pp_objects[i_middle+1] );
756 vlc_mutex_unlock( &structure_lock );
757 return pp_objects[i_middle+1];
762 else if( pp_objects[i_middle]->p_internals->i_refcount > 0 )
764 vlc_object_yield_locked( pp_objects[i_middle] );
765 vlc_mutex_unlock( &structure_lock );
766 return pp_objects[i_middle];
771 /* this means that i_max == i_middle, and since we have already
772 * tested pp_objects[i_middle]), p_found is properly set. */
777 vlc_mutex_unlock( &structure_lock );
782 ****************************************************************************
783 * find a typed object and increment its refcount
784 *****************************************************************************
785 * This function recursively looks for a given object type. i_mode can be one
786 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
787 *****************************************************************************/
788 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
790 vlc_object_t *p_found;
792 vlc_mutex_lock( &structure_lock );
794 /* If we are of the requested type ourselves, don't look further */
795 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type
796 && p_this->p_internals->i_refcount > 0 )
798 vlc_object_yield_locked( p_this );
799 vlc_mutex_unlock( &structure_lock );
803 /* Otherwise, recursively look for the object */
804 if( (i_mode & 0x000f) == FIND_ANYWHERE )
806 vlc_object_t *p_root = p_this;
809 while( p_root->p_parent != NULL &&
810 p_root != VLC_OBJECT( p_this->p_libvlc ) )
812 p_root = p_root->p_parent;
815 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
816 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
818 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
819 i_type, (i_mode & ~0x000f)|FIND_CHILD );
824 p_found = FindObject( p_this, i_type, i_mode );
827 vlc_mutex_unlock( &structure_lock );
833 ****************************************************************************
834 * find a named object and increment its refcount
835 *****************************************************************************
836 * This function recursively looks for a given object name. i_mode can be one
837 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
838 *****************************************************************************/
839 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
842 vlc_object_t *p_found;
844 vlc_mutex_lock( &structure_lock );
846 /* Avoid obvious freed object uses */
847 assert( p_this->p_internals->i_refcount > 0 );
849 /* If have the requested name ourselves, don't look further */
850 if( !(i_mode & FIND_STRICT)
851 && p_this->psz_object_name
852 && !strcmp( p_this->psz_object_name, psz_name )
853 && p_this->p_internals->i_refcount > 0 )
855 vlc_object_yield_locked( p_this );
856 vlc_mutex_unlock( &structure_lock );
860 /* Otherwise, recursively look for the object */
861 if( (i_mode & 0x000f) == FIND_ANYWHERE )
863 vlc_object_t *p_root = p_this;
866 while( p_root->p_parent != NULL &&
867 p_root != VLC_OBJECT( p_this->p_libvlc ) )
869 p_root = p_root->p_parent;
872 p_found = FindObjectName( p_root, psz_name,
873 (i_mode & ~0x000f)|FIND_CHILD );
874 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
876 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
877 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
882 p_found = FindObjectName( p_this, psz_name, i_mode );
885 vlc_mutex_unlock( &structure_lock );
891 ****************************************************************************
892 * increment an object refcount
893 *****************************************************************************/
895 /* When the structure_lock is locked */
896 static void vlc_object_yield_locked( vlc_object_t *p_this )
898 vlc_assert_locked (&structure_lock);
900 /* Avoid obvious freed object uses */
901 assert( p_this->p_internals->i_refcount > 0 );
903 /* Increment the counter */
904 p_this->p_internals->i_refcount++;
907 /* Public function */
908 void __vlc_object_yield( vlc_object_t *p_this )
910 vlc_mutex_lock( &structure_lock );
911 vlc_object_yield_locked( p_this );
912 vlc_mutex_unlock( &structure_lock );
916 /*****************************************************************************
917 * decrement an object refcount
918 * And destroy the object if its refcount reach zero.
919 *****************************************************************************/
920 void __vlc_object_release( vlc_object_t *p_this )
922 vlc_bool_t b_should_destroy;
924 vlc_mutex_lock( &structure_lock );
926 assert( p_this->p_internals->i_refcount > 0 );
927 p_this->p_internals->i_refcount--;
928 b_should_destroy = (p_this->p_internals->i_refcount == 0);
930 if( b_should_destroy )
932 /* Make sure this object can't be obtained via vlc_find_object now that
934 libvlc_global_data_t *p_libvlc_global = vlc_global();
937 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
938 * useless to try and recover anything if pp_objects gets smashed. */
939 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
940 p_libvlc_global->i_objects );
941 REMOVE_ELEM( p_libvlc_global->pp_objects,
942 p_libvlc_global->i_objects, i_index );
945 vlc_mutex_unlock( &structure_lock );
947 if( b_should_destroy )
948 vlc_object_destroy( p_this );
952 ****************************************************************************
953 * attach object to a parent object
954 *****************************************************************************
955 * This function sets p_this as a child of p_parent, and p_parent as a parent
956 * of p_this. This link can be undone using vlc_object_detach.
957 *****************************************************************************/
958 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
960 if( !p_this ) return;
962 vlc_mutex_lock( &structure_lock );
964 /* Avoid obvious freed object uses */
965 assert( p_this->p_internals->i_refcount > 0 );
967 /* Attach the parent to its child */
968 p_this->p_parent = p_parent;
970 /* Attach the child to its parent */
971 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
972 p_parent->i_children, p_this );
974 /* Climb up the tree to see whether we are connected with the root */
975 if( p_parent->p_internals->b_attached )
977 SetAttachment( p_this, VLC_TRUE );
980 vlc_mutex_unlock( &structure_lock );
984 ****************************************************************************
985 * detach object from its parent
986 *****************************************************************************
987 * This function removes all links between an object and its parent.
988 *****************************************************************************/
989 void __vlc_object_detach( vlc_object_t *p_this )
991 if( !p_this ) return;
993 vlc_mutex_lock( &structure_lock );
995 if( !p_this->p_parent )
997 msg_Err( p_this, "object is not attached" );
998 vlc_mutex_unlock( &structure_lock );
1002 /* Climb up the tree to see whether we are connected with the root */
1003 if( p_this->p_parent->p_internals->b_attached )
1005 SetAttachment( p_this, VLC_FALSE );
1008 DetachObject( p_this );
1009 vlc_mutex_unlock( &structure_lock );
1014 ****************************************************************************
1015 * find a list typed objects and increment their refcount
1016 *****************************************************************************
1017 * This function recursively looks for a given object type. i_mode can be one
1018 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
1019 *****************************************************************************/
1020 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
1023 vlc_object_t **pp_current, **pp_end;
1024 int i_count = 0, i_index = 0;
1025 libvlc_global_data_t *p_libvlc_global = vlc_global();
1027 vlc_mutex_lock( &structure_lock );
1029 /* Look for the objects */
1030 switch( i_mode & 0x000f )
1033 pp_current = p_libvlc_global->pp_objects;
1034 pp_end = pp_current + p_libvlc_global->i_objects;
1036 for( ; pp_current < pp_end ; pp_current++ )
1038 if( (*pp_current)->p_internals->b_attached
1039 && (*pp_current)->i_object_type == i_type )
1045 p_list = NewList( i_count );
1046 pp_current = p_libvlc_global->pp_objects;
1048 for( ; pp_current < pp_end ; pp_current++ )
1050 if( (*pp_current)->p_internals->b_attached
1051 && (*pp_current)->i_object_type == i_type )
1053 ListReplace( p_list, *pp_current, i_index );
1054 if( i_index < i_count ) i_index++;
1060 i_count = CountChildren( p_this, i_type );
1061 p_list = NewList( i_count );
1063 /* Check allocation was successful */
1064 if( p_list->i_count != i_count )
1066 msg_Err( p_this, "list allocation failed!" );
1067 p_list->i_count = 0;
1071 p_list->i_count = 0;
1072 ListChildren( p_list, p_this, i_type );
1076 msg_Err( p_this, "unimplemented!" );
1077 p_list = NewList( 0 );
1081 vlc_mutex_unlock( &structure_lock );
1086 /*****************************************************************************
1087 * DumpCommand: print the current vlc structure
1088 *****************************************************************************
1089 * This function prints either an ASCII tree showing the connections between
1090 * vlc objects, and additional information such as their refcount, thread ID,
1091 * etc. (command "tree"), or the same data as a simple list (command "list").
1092 *****************************************************************************/
1093 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1094 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1096 libvlc_global_data_t *p_libvlc_global = vlc_global();
1098 (void)oldval; (void)p_data;
1099 if( *psz_cmd == 'l' )
1101 vlc_mutex_lock( &structure_lock );
1103 vlc_object_t **pp_current, **pp_end;
1105 pp_current = p_libvlc_global->pp_objects;
1106 pp_end = pp_current + p_libvlc_global->i_objects;
1108 for( ; pp_current < pp_end ; pp_current++ )
1110 if( (*pp_current)->p_internals->b_attached )
1112 PrintObject( *pp_current, "" );
1116 printf( " o %.8i %s (not attached)\n",
1117 (*pp_current)->i_object_id,
1118 (*pp_current)->psz_object_type );
1122 vlc_mutex_unlock( &structure_lock );
1126 vlc_object_t *p_object = NULL;
1128 if( *newval.psz_string )
1131 int i_id = strtol( newval.psz_string, &end, 0 );
1132 if( end != newval.psz_string )
1133 p_object = vlc_object_get( i_id );
1136 /* try using the object's name to find it */
1137 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1140 /* Look in p_libvlc's children tree */
1141 p_object = vlc_object_find_name( p_libvlc,
1144 vlc_object_release( p_libvlc );
1148 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1149 p_object = vlc_object_find_name( p_this,
1161 vlc_mutex_lock( &structure_lock );
1163 if( *psz_cmd == 't' )
1165 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1168 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1171 DumpStructure( p_object, 0, psz_foo );
1173 else if( *psz_cmd == 'v' )
1178 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1180 PrintObject( p_object, "" );
1182 if( !p_object->p_internals->i_vars )
1183 printf( " `-o No variables\n" );
1184 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1186 variable_t *p_var = p_object->p_internals->p_vars + i;
1188 const char *psz_type = "unknown";
1189 switch( p_var->i_type & VLC_VAR_TYPE )
1191 #define MYCASE( type, nice ) \
1192 case VLC_VAR_ ## type: \
1195 MYCASE( VOID, "void" );
1196 MYCASE( BOOL, "bool" );
1197 MYCASE( INTEGER, "integer" );
1198 MYCASE( HOTKEY, "hotkey" );
1199 MYCASE( STRING, "string" );
1200 MYCASE( MODULE, "module" );
1201 MYCASE( FILE, "file" );
1202 MYCASE( DIRECTORY, "directory" );
1203 MYCASE( VARIABLE, "variable" );
1204 MYCASE( FLOAT, "float" );
1205 MYCASE( TIME, "time" );
1206 MYCASE( ADDRESS, "address" );
1207 MYCASE( MUTEX, "mutex" );
1208 MYCASE( LIST, "list" );
1211 printf( " %c-o \"%s\" (%s",
1212 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1213 p_var->psz_name, psz_type );
1214 if( p_var->psz_text )
1215 printf( ", %s", p_var->psz_text );
1217 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1218 printf( ", command" );
1219 if( p_var->i_entries )
1220 printf( ", %d callbacks", p_var->i_entries );
1221 switch( p_var->i_type & 0x00f0 )
1227 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1229 case VLC_VAR_INTEGER:
1230 printf( ": %d", p_var->val.i_int );
1232 case VLC_VAR_STRING:
1233 printf( ": \"%s\"", p_var->val.psz_string );
1236 printf( ": %f", p_var->val.f_float );
1239 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1241 case VLC_VAR_ADDRESS:
1242 printf( ": %p", p_var->val.p_address );
1252 vlc_mutex_unlock( &structure_lock );
1254 if( *newval.psz_string )
1256 vlc_object_release( p_object );
1263 /*****************************************************************************
1264 * vlc_list_release: free a list previously allocated by vlc_list_find
1265 *****************************************************************************
1266 * This function decreases the refcount of all objects in the list and
1268 *****************************************************************************/
1269 void vlc_list_release( vlc_list_t *p_list )
1273 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1275 vlc_object_release( p_list->p_values[i_index].p_object );
1278 free( p_list->p_values );
1282 /* Following functions are local */
1284 /*****************************************************************************
1285 * FindIndex: find the index of an object in an array of objects
1286 *****************************************************************************
1287 * This function assumes that p_this can be found in pp_objects. It will not
1288 * crash if p_this cannot be found, but will return a wrong value. It is your
1289 * duty to check the return value if you are not certain that the object could
1290 * be found for sure.
1291 *****************************************************************************/
1292 static int FindIndex( vlc_object_t *p_this,
1293 vlc_object_t **pp_objects, int i_count )
1295 int i_middle = i_count / 2;
1302 if( pp_objects[i_middle] == p_this )
1312 /* We take advantage of the sorted array */
1313 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1315 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1316 i_count - i_middle );
1320 return FindIndex( p_this, pp_objects, i_middle );
1324 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1327 vlc_object_t *p_tmp;
1329 switch( i_mode & 0x000f )
1332 p_tmp = p_this->p_parent;
1335 if( p_tmp->i_object_type == i_type
1336 && p_tmp->p_internals->i_refcount > 0 )
1338 vlc_object_yield_locked( p_tmp );
1343 return FindObject( p_tmp, i_type, i_mode );
1349 for( i = p_this->i_children; i--; )
1351 p_tmp = p_this->pp_children[i];
1352 if( p_tmp->i_object_type == i_type
1353 && p_tmp->p_internals->i_refcount > 0 )
1355 vlc_object_yield_locked( p_tmp );
1358 else if( p_tmp->i_children )
1360 p_tmp = FindObject( p_tmp, i_type, i_mode );
1370 /* Handled in vlc_object_find */
1377 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1378 const char *psz_name,
1382 vlc_object_t *p_tmp;
1384 switch( i_mode & 0x000f )
1387 p_tmp = p_this->p_parent;
1390 if( p_tmp->psz_object_name
1391 && !strcmp( p_tmp->psz_object_name, psz_name )
1392 && p_tmp->p_internals->i_refcount > 0 )
1394 vlc_object_yield_locked( p_tmp );
1399 return FindObjectName( p_tmp, psz_name, i_mode );
1405 for( i = p_this->i_children; i--; )
1407 p_tmp = p_this->pp_children[i];
1408 if( p_tmp->psz_object_name
1409 && !strcmp( p_tmp->psz_object_name, psz_name )
1410 && p_tmp->p_internals->i_refcount > 0 )
1412 vlc_object_yield_locked( p_tmp );
1415 else if( p_tmp->i_children )
1417 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1427 /* Handled in vlc_object_find */
1434 static void DetachObject( vlc_object_t *p_this )
1436 vlc_object_t *p_parent = p_this->p_parent;
1439 /* Remove p_this's parent */
1440 p_this->p_parent = NULL;
1442 /* Remove all of p_parent's children which are p_this */
1443 for( i_index = p_parent->i_children ; i_index-- ; )
1445 if( p_parent->pp_children[i_index] == p_this )
1447 p_parent->i_children--;
1448 for( i = i_index ; i < p_parent->i_children ; i++ )
1450 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1455 if( p_parent->i_children )
1457 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1458 p_parent->i_children * sizeof(vlc_object_t *) );
1462 free( p_parent->pp_children );
1463 p_parent->pp_children = NULL;
1467 /*****************************************************************************
1468 * SetAttachment: recursively set the b_attached flag of a subtree.
1469 *****************************************************************************
1470 * This function is used by the attach and detach functions to propagate
1471 * the b_attached flag in a subtree.
1472 *****************************************************************************/
1473 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1477 for( i_index = p_this->i_children ; i_index-- ; )
1479 SetAttachment( p_this->pp_children[i_index], b_attached );
1482 p_this->p_internals->b_attached = b_attached;
1485 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1487 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1491 if( p_this->psz_object_name )
1493 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1495 psz_name[48] = '\"';
1498 psz_children[0] = '\0';
1499 switch( p_this->i_children )
1504 strcpy( psz_children, ", 1 child" );
1507 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1511 psz_refcount[0] = '\0';
1512 if( p_this->p_internals->i_refcount > 0 )
1513 snprintf( psz_refcount, 19, ", refcount %u",
1514 p_this->p_internals->i_refcount );
1516 psz_thread[0] = '\0';
1517 if( p_this->p_internals->b_thread )
1518 snprintf( psz_thread, 29, " (thread %u)",
1519 #if defined(WIN32) || defined(UNDER_CE)
1520 (unsigned)p_this->p_internals->thread_id.id );
1522 (unsigned)p_this->p_internals->thread_id );
1525 psz_parent[0] = '\0';
1526 if( p_this->p_parent )
1527 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1529 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1530 p_this->i_object_id, p_this->psz_object_type,
1531 psz_name, psz_thread, psz_refcount, psz_children,
1535 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1538 char i_back = psz_foo[i_level];
1539 psz_foo[i_level] = '\0';
1541 PrintObject( p_this, psz_foo );
1543 psz_foo[i_level] = i_back;
1545 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1547 msg_Warn( p_this, "structure tree is too deep" );
1551 for( i = 0 ; i < p_this->i_children ; i++ )
1555 psz_foo[i_level-1] = ' ';
1557 if( psz_foo[i_level-2] == '`' )
1559 psz_foo[i_level-2] = ' ';
1563 if( i == p_this->i_children - 1 )
1565 psz_foo[i_level] = '`';
1569 psz_foo[i_level] = '|';
1572 psz_foo[i_level+1] = '-';
1573 psz_foo[i_level+2] = '\0';
1575 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1579 static vlc_list_t * NewList( int i_count )
1581 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1582 if( p_list == NULL )
1587 p_list->i_count = i_count;
1591 p_list->p_values = NULL;
1595 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1596 if( p_list->p_values == NULL )
1598 p_list->i_count = 0;
1605 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1608 if( p_list == NULL || i_index >= p_list->i_count )
1613 vlc_object_yield_locked( p_object );
1615 p_list->p_values[i_index].p_object = p_object;
1620 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1622 if( p_list == NULL )
1627 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1628 * sizeof( vlc_value_t ) );
1629 if( p_list->p_values == NULL )
1631 p_list->i_count = 0;
1635 vlc_object_yield_locked( p_object );
1637 p_list->p_values[p_list->i_count].p_object = p_object;
1643 static int CountChildren( vlc_object_t *p_this, int i_type )
1645 vlc_object_t *p_tmp;
1648 for( i = 0; i < p_this->i_children; i++ )
1650 p_tmp = p_this->pp_children[i];
1652 if( p_tmp->i_object_type == i_type )
1657 if( p_tmp->i_children )
1659 i_count += CountChildren( p_tmp, i_type );
1666 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1668 vlc_object_t *p_tmp;
1671 for( i = 0; i < p_this->i_children; i++ )
1673 p_tmp = p_this->pp_children[i];
1675 if( p_tmp->i_object_type == i_type )
1677 ListReplace( p_list, p_tmp, p_list->i_count++ );
1680 if( p_tmp->i_children )
1682 ListChildren( p_list, p_tmp, i_type );