1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2007 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";
270 i_size = sizeof(services_discovery_t);
271 psz_type = "services discovery";
273 case VLC_OBJECT_INPUT:
274 i_size = sizeof(input_thread_t);
277 case VLC_OBJECT_DEMUX:
278 i_size = sizeof(demux_t);
281 case VLC_OBJECT_ACCESS:
282 i_size = sizeof(access_t);
285 case VLC_OBJECT_DECODER:
286 i_size = sizeof(decoder_t);
287 psz_type = "decoder";
289 case VLC_OBJECT_PACKETIZER:
290 i_size = sizeof(decoder_t);
291 psz_type = "packetizer";
293 case VLC_OBJECT_ENCODER:
294 i_size = sizeof(encoder_t);
295 psz_type = "encoder";
297 case VLC_OBJECT_FILTER:
298 i_size = sizeof(filter_t);
301 case VLC_OBJECT_VOUT:
302 i_size = sizeof(vout_thread_t);
303 psz_type = "video output";
305 case VLC_OBJECT_AOUT:
306 i_size = sizeof(aout_instance_t);
307 psz_type = "audio output";
309 case VLC_OBJECT_SOUT:
310 i_size = sizeof(sout_instance_t);
311 psz_type = "stream output";
314 i_size = sizeof( vlm_t );
315 psz_type = "vlm dameon";
318 i_size = sizeof( xml_t );
321 case VLC_OBJECT_OPENGL:
322 i_size = sizeof( vout_thread_t );
325 case VLC_OBJECT_ANNOUNCE:
326 i_size = sizeof( announce_handler_t );
327 psz_type = "announce";
329 case VLC_OBJECT_META_ENGINE:
330 i_size = sizeof( meta_engine_t );
331 psz_type = "meta engine";
333 case VLC_OBJECT_OSDMENU:
334 i_size = sizeof( osd_menu_t );
335 psz_type = "osd menu";
337 case VLC_OBJECT_INTERACTION:
338 i_size = sizeof( interaction_t );
339 psz_type = "interaction";
342 i_size = i_type > (int)sizeof(vlc_object_t)
343 ? i_type : (int)sizeof(vlc_object_t);
344 i_type = VLC_OBJECT_GENERIC;
345 psz_type = "generic";
349 return vlc_custom_create( p_this, i_size, i_type, psz_type );
354 ****************************************************************************
355 * Set the destructor of a vlc object
357 * This function sets the destructor of the vlc object. It will be called
358 * when the object is destroyed when the its refcount reaches 0.
359 * (It is called by the internal function vlc_object_destroy())
360 *****************************************************************************/
361 void __vlc_object_set_destructor( vlc_object_t *p_this,
362 vlc_destructor_t pf_destructor )
364 vlc_object_internals_t *p_priv = vlc_internals(p_this );
366 vlc_mutex_lock( &structure_lock );
367 p_priv->pf_destructor = pf_destructor;
368 vlc_mutex_unlock( &structure_lock );
372 ****************************************************************************
373 * Destroy a vlc object (Internal)
375 * This function destroys an object that has been previously allocated with
376 * vlc_object_create. The object's refcount must be zero and it must not be
377 * attached to other objects in any way.
378 *****************************************************************************/
379 static void vlc_object_destroy( vlc_object_t *p_this )
381 vlc_object_internals_t *p_priv = vlc_internals( p_this );
383 /* Automatically detach the object from its parents */
384 if( p_this->p_parent ) vlc_object_detach( p_this );
387 /* Send a kill to the object's thread if applicable */
388 vlc_object_kill( p_this );
390 /* If we are running on a thread, wait until it ends */
391 if( p_priv->b_thread )
392 vlc_thread_join( p_this );
394 /* Call the custom "subclass" destructor */
395 if( p_priv->pf_destructor )
396 p_priv->pf_destructor( p_this );
399 if( p_this->i_children )
404 "ERROR: cannot delete object (%i, %s) with %d children\n",
405 p_this->i_object_id, p_this->psz_object_name,
406 p_this->i_children );
408 for( i = 0; i < p_this->i_children; i++ )
411 "ERROR: Remaining children object "
412 "(id:%i, type:%s, name:%s)\n",
413 p_this->pp_children[i]->i_object_id,
414 p_this->pp_children[i]->psz_object_type,
415 p_this->pp_children[i]->psz_object_name );
421 /* Destroy the associated variables, starting from the end so that
422 * no memmove calls have to be done. */
423 while( p_priv->i_vars )
425 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
428 free( p_priv->p_vars );
429 vlc_mutex_destroy( &p_priv->var_lock );
431 free( p_this->psz_header );
433 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
435 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
438 if( p_global->i_objects > 0 )
441 for( i = 0; i < p_global->i_objects; i++ )
443 /* We are leaking this object */
445 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
446 p_global->pp_objects[i]->i_object_id,
447 p_global->pp_objects[i]->psz_object_type,
448 p_global->pp_objects[i]->psz_object_name );
451 /* Strongly abort, cause we want these to be fixed */
455 /* We are the global object ... no need to lock. */
456 free( p_global->pp_objects );
457 p_global->pp_objects = NULL;
459 vlc_mutex_destroy( &structure_lock );
462 #if defined(WIN32) || defined(UNDER_CE)
463 /* if object has an associated thread, close it now */
464 if( p_priv->thread_id.hThread )
465 CloseHandle(p_priv->thread_id.hThread);
468 vlc_mutex_destroy( &p_this->object_lock );
469 vlc_cond_destroy( &p_this->object_wait );
470 vlc_spin_destroy( &p_priv->spin );
471 if( p_priv->pipes[1] != -1 )
472 close( p_priv->pipes[1] );
473 if( p_priv->pipes[0] != -1 )
474 close( p_priv->pipes[0] );
476 /* global is not dynamically allocated by vlc_object_create */
477 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
482 /** Inter-object signaling */
484 void __vlc_object_lock( vlc_object_t *obj )
486 vlc_mutex_lock( &obj->object_lock );
489 void __vlc_object_unlock( vlc_object_t *obj )
491 vlc_assert_locked( &obj->object_lock );
492 vlc_mutex_unlock( &obj->object_lock );
496 # include <winsock2.h>
497 # include <ws2tcpip.h>
500 * select()-able pipes emulated using Winsock
502 static int pipe (int fd[2])
505 int addrlen = sizeof (addr);
507 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
508 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
509 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
512 memset (&addr, 0, sizeof (addr));
513 addr.sin_family = AF_INET;
514 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
515 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
516 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
518 || connect (c, (PSOCKADDR)&addr, addrlen))
521 a = accept (l, NULL, NULL);
522 if (a == INVALID_SOCKET)
533 if (l != INVALID_SOCKET)
535 if (c != INVALID_SOCKET)
541 #define read( a, b, c ) recv (a, b, c, 0)
543 #define write( a, b, c ) send (a, b, c, 0)
545 #define close( a ) closesocket (a)
549 * Returns the readable end of a pipe that becomes readable once termination
550 * of the object is requested (vlc_object_kill()).
551 * This can be used to wake-up out of a select() or poll() event loop, such
552 * typically when doing network I/O.
554 * Note that the pipe will remain the same for the lifetime of the object.
555 * DO NOT read the pipe nor close it yourself. Ever.
557 * @param obj object that would be "killed"
558 * @return a readable pipe descriptor, or -1 on error.
560 int __vlc_object_waitpipe( vlc_object_t *obj )
562 int pfd[2] = { -1, -1 };
563 struct vlc_object_internals_t *internals = obj->p_internals;
564 vlc_bool_t killed = VLC_FALSE;
566 vlc_spin_lock (&internals->spin);
567 if (internals->pipes[0] == -1)
569 /* This can only ever happen if someone killed us without locking: */
570 assert (internals->pipes[1] == -1);
571 vlc_spin_unlock (&internals->spin);
576 vlc_spin_lock (&internals->spin);
577 if (internals->pipes[0] == -1)
579 internals->pipes[0] = pfd[0];
580 internals->pipes[1] = pfd[1];
581 pfd[0] = pfd[1] = -1;
585 vlc_spin_unlock (&internals->spin);
589 /* Race condition: vlc_object_kill() already invoked! */
592 vlc_spin_lock (&internals->spin);
593 fd = internals->pipes[1];
594 internals->pipes[1] = -1;
595 vlc_spin_unlock (&internals->spin);
597 msg_Dbg (obj, "waitpipe: object already dying");
602 /* Race condition: two threads call pipe() - unlikely */
608 return internals->pipes[0];
613 * Waits for the object to be signaled (using vlc_object_signal()).
614 * If the object already has a signal pending, this function will return
615 * immediately. It is asserted that the caller holds the object lock.
617 * @return true if the object is dying and should terminate.
619 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
621 vlc_assert_locked( &obj->object_lock );
622 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
628 * Waits for the object to be signaled (using vlc_object_signal()), or for
630 * If the object already has a signal pending, this function will return
631 * immediately. It is asserted that the caller holds the object lock.
633 * @return negative if the object is dying and should terminate,
634 * positive if the the object has been signaled but is not dying,
635 * 0 if timeout has been reached.
637 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
641 vlc_assert_locked( &obj->object_lock );
642 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
643 if( v == 0 ) /* signaled */
644 return obj->b_die ? -1 : 1;
650 * Checks whether an object has been "killed".
651 * The object lock must be held.
653 * Typical code for an object thread could be:
655 vlc_object_lock (self);
657 while (vlc_object_alive (self))
661 if (vlc_object_wait (self))
666 ...deinitialization...
667 vlc_object_unlock (self);
670 * @return true iff the object has not been killed yet
672 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
674 vlc_assert_locked( &obj->object_lock );
680 * Signals an object for which the lock is held.
682 void __vlc_object_signal_unlocked( vlc_object_t *obj )
684 vlc_assert_locked (&obj->object_lock);
685 vlc_cond_signal( &obj->object_wait );
690 * Requests termination of an object.
691 * If the object is LibVLC, also request to terminate all its children.
693 void __vlc_object_kill( vlc_object_t *p_this )
695 struct vlc_object_internals_t *internals = p_this->p_internals;
698 vlc_mutex_lock( &p_this->object_lock );
699 p_this->b_die = VLC_TRUE;
701 vlc_spin_lock (&internals->spin);
702 fd = internals->pipes[1];
703 internals->pipes[1] = -1;
704 vlc_spin_unlock (&internals->spin);
708 msg_Dbg (p_this, "waitpipe: object killed");
712 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
713 for( int i = 0; i < p_this->i_children ; i++ )
714 vlc_object_kill( p_this->pp_children[i] );
716 vlc_object_signal_unlocked( p_this );
717 vlc_mutex_unlock( &p_this->object_lock );
722 * find an object given its ID
724 * This function looks for the object whose i_object_id field is i_id. We
725 * use a dichotomy so that lookups are in log2(n).
726 *****************************************************************************/
727 void * vlc_object_get( int i_id )
730 vlc_object_t **pp_objects;
731 libvlc_global_data_t *p_libvlc_global = vlc_global();
733 vlc_mutex_lock( &structure_lock );
735 pp_objects = p_libvlc_global->pp_objects;
737 /* Perform our dichotomy */
738 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
740 i_middle = i_max / 2;
742 if( pp_objects[i_middle]->i_object_id > i_id )
746 else if( pp_objects[i_middle]->i_object_id < i_id )
750 pp_objects += i_middle;
755 /* This happens when there are only two remaining objects */
756 if( pp_objects[i_middle+1]->i_object_id == i_id
757 && pp_objects[i_middle+1]->p_internals->i_refcount > 0 )
759 vlc_object_yield_locked( pp_objects[i_middle+1] );
760 vlc_mutex_unlock( &structure_lock );
761 return pp_objects[i_middle+1];
766 else if( pp_objects[i_middle]->p_internals->i_refcount > 0 )
768 vlc_object_yield_locked( pp_objects[i_middle] );
769 vlc_mutex_unlock( &structure_lock );
770 return pp_objects[i_middle];
775 /* this means that i_max == i_middle, and since we have already
776 * tested pp_objects[i_middle]), p_found is properly set. */
781 vlc_mutex_unlock( &structure_lock );
786 ****************************************************************************
787 * find a typed object and increment its refcount
788 *****************************************************************************
789 * This function recursively looks for a given object type. i_mode can be one
790 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
791 *****************************************************************************/
792 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
794 vlc_object_t *p_found;
796 vlc_mutex_lock( &structure_lock );
798 /* If we are of the requested type ourselves, don't look further */
799 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type
800 && p_this->p_internals->i_refcount > 0 )
802 vlc_object_yield_locked( p_this );
803 vlc_mutex_unlock( &structure_lock );
807 /* Otherwise, recursively look for the object */
808 if( (i_mode & 0x000f) == FIND_ANYWHERE )
810 vlc_object_t *p_root = p_this;
813 while( p_root->p_parent != NULL &&
814 p_root != VLC_OBJECT( p_this->p_libvlc ) )
816 p_root = p_root->p_parent;
819 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
820 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
822 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
823 i_type, (i_mode & ~0x000f)|FIND_CHILD );
828 p_found = FindObject( p_this, i_type, i_mode );
831 vlc_mutex_unlock( &structure_lock );
837 ****************************************************************************
838 * find a named object and increment its refcount
839 *****************************************************************************
840 * This function recursively looks for a given object name. i_mode can be one
841 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
842 *****************************************************************************/
843 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
846 vlc_object_t *p_found;
848 vlc_mutex_lock( &structure_lock );
850 /* Avoid obvious freed object uses */
851 assert( p_this->p_internals->i_refcount > 0 );
853 /* If have the requested name ourselves, don't look further */
854 if( !(i_mode & FIND_STRICT)
855 && p_this->psz_object_name
856 && !strcmp( p_this->psz_object_name, psz_name )
857 && p_this->p_internals->i_refcount > 0 )
859 vlc_object_yield_locked( p_this );
860 vlc_mutex_unlock( &structure_lock );
864 /* Otherwise, recursively look for the object */
865 if( (i_mode & 0x000f) == FIND_ANYWHERE )
867 vlc_object_t *p_root = p_this;
870 while( p_root->p_parent != NULL &&
871 p_root != VLC_OBJECT( p_this->p_libvlc ) )
873 p_root = p_root->p_parent;
876 p_found = FindObjectName( p_root, psz_name,
877 (i_mode & ~0x000f)|FIND_CHILD );
878 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
880 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
881 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
886 p_found = FindObjectName( p_this, psz_name, i_mode );
889 vlc_mutex_unlock( &structure_lock );
895 ****************************************************************************
896 * increment an object refcount
897 *****************************************************************************/
899 /* When the structure_lock is locked */
900 static void vlc_object_yield_locked( vlc_object_t *p_this )
902 vlc_assert_locked (&structure_lock);
904 /* Avoid obvious freed object uses */
905 assert( p_this->p_internals->i_refcount > 0 );
907 /* Increment the counter */
908 p_this->p_internals->i_refcount++;
911 /* Public function */
912 void __vlc_object_yield( vlc_object_t *p_this )
914 vlc_mutex_lock( &structure_lock );
915 vlc_object_yield_locked( p_this );
916 vlc_mutex_unlock( &structure_lock );
920 /*****************************************************************************
921 * decrement an object refcount
922 * And destroy the object if its refcount reach zero.
923 *****************************************************************************/
924 void __vlc_object_release( vlc_object_t *p_this )
926 vlc_bool_t b_should_destroy;
928 vlc_mutex_lock( &structure_lock );
930 assert( p_this->p_internals->i_refcount > 0 );
931 p_this->p_internals->i_refcount--;
932 b_should_destroy = (p_this->p_internals->i_refcount == 0);
934 if( b_should_destroy )
936 /* Make sure this object can't be obtained via vlc_find_object now that
938 libvlc_global_data_t *p_libvlc_global = vlc_global();
941 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
942 * useless to try and recover anything if pp_objects gets smashed. */
943 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
944 p_libvlc_global->i_objects );
945 REMOVE_ELEM( p_libvlc_global->pp_objects,
946 p_libvlc_global->i_objects, i_index );
949 vlc_mutex_unlock( &structure_lock );
951 if( b_should_destroy )
952 vlc_object_destroy( p_this );
956 ****************************************************************************
957 * attach object to a parent object
958 *****************************************************************************
959 * This function sets p_this as a child of p_parent, and p_parent as a parent
960 * of p_this. This link can be undone using vlc_object_detach.
961 *****************************************************************************/
962 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
964 if( !p_this ) return;
966 vlc_mutex_lock( &structure_lock );
968 /* Avoid obvious freed object uses */
969 assert( p_this->p_internals->i_refcount > 0 );
971 /* Attach the parent to its child */
972 p_this->p_parent = p_parent;
974 /* Attach the child to its parent */
975 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
976 p_parent->i_children, p_this );
978 /* Climb up the tree to see whether we are connected with the root */
979 if( p_parent->p_internals->b_attached )
981 SetAttachment( p_this, VLC_TRUE );
984 vlc_mutex_unlock( &structure_lock );
988 ****************************************************************************
989 * detach object from its parent
990 *****************************************************************************
991 * This function removes all links between an object and its parent.
992 *****************************************************************************/
993 void __vlc_object_detach( vlc_object_t *p_this )
995 if( !p_this ) return;
997 vlc_mutex_lock( &structure_lock );
999 if( !p_this->p_parent )
1001 msg_Err( p_this, "object is not attached" );
1002 vlc_mutex_unlock( &structure_lock );
1006 /* Climb up the tree to see whether we are connected with the root */
1007 if( p_this->p_parent->p_internals->b_attached )
1009 SetAttachment( p_this, VLC_FALSE );
1012 DetachObject( p_this );
1013 vlc_mutex_unlock( &structure_lock );
1018 ****************************************************************************
1019 * find a list typed objects and increment their refcount
1020 *****************************************************************************
1021 * This function recursively looks for a given object type. i_mode can be one
1022 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
1023 *****************************************************************************/
1024 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
1027 vlc_object_t **pp_current, **pp_end;
1028 int i_count = 0, i_index = 0;
1029 libvlc_global_data_t *p_libvlc_global = vlc_global();
1031 vlc_mutex_lock( &structure_lock );
1033 /* Look for the objects */
1034 switch( i_mode & 0x000f )
1037 pp_current = p_libvlc_global->pp_objects;
1038 pp_end = pp_current + p_libvlc_global->i_objects;
1040 for( ; pp_current < pp_end ; pp_current++ )
1042 if( (*pp_current)->p_internals->b_attached
1043 && (*pp_current)->i_object_type == i_type )
1049 p_list = NewList( i_count );
1050 pp_current = p_libvlc_global->pp_objects;
1052 for( ; pp_current < pp_end ; pp_current++ )
1054 if( (*pp_current)->p_internals->b_attached
1055 && (*pp_current)->i_object_type == i_type )
1057 ListReplace( p_list, *pp_current, i_index );
1058 if( i_index < i_count ) i_index++;
1064 i_count = CountChildren( p_this, i_type );
1065 p_list = NewList( i_count );
1067 /* Check allocation was successful */
1068 if( p_list->i_count != i_count )
1070 msg_Err( p_this, "list allocation failed!" );
1071 p_list->i_count = 0;
1075 p_list->i_count = 0;
1076 ListChildren( p_list, p_this, i_type );
1080 msg_Err( p_this, "unimplemented!" );
1081 p_list = NewList( 0 );
1085 vlc_mutex_unlock( &structure_lock );
1090 /*****************************************************************************
1091 * DumpCommand: print the current vlc structure
1092 *****************************************************************************
1093 * This function prints either an ASCII tree showing the connections between
1094 * vlc objects, and additional information such as their refcount, thread ID,
1095 * etc. (command "tree"), or the same data as a simple list (command "list").
1096 *****************************************************************************/
1097 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1098 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1100 libvlc_global_data_t *p_libvlc_global = vlc_global();
1102 (void)oldval; (void)p_data;
1103 if( *psz_cmd == 'l' )
1105 vlc_mutex_lock( &structure_lock );
1107 vlc_object_t **pp_current, **pp_end;
1109 pp_current = p_libvlc_global->pp_objects;
1110 pp_end = pp_current + p_libvlc_global->i_objects;
1112 for( ; pp_current < pp_end ; pp_current++ )
1114 if( (*pp_current)->p_internals->b_attached )
1116 PrintObject( *pp_current, "" );
1120 printf( " o %.8i %s (not attached)\n",
1121 (*pp_current)->i_object_id,
1122 (*pp_current)->psz_object_type );
1126 vlc_mutex_unlock( &structure_lock );
1130 vlc_object_t *p_object = NULL;
1132 if( *newval.psz_string )
1135 int i_id = strtol( newval.psz_string, &end, 0 );
1136 if( end != newval.psz_string )
1137 p_object = vlc_object_get( i_id );
1140 /* try using the object's name to find it */
1141 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1144 /* Look in p_libvlc's children tree */
1145 p_object = vlc_object_find_name( p_libvlc,
1148 vlc_object_release( p_libvlc );
1152 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1153 p_object = vlc_object_find_name( p_this,
1165 vlc_mutex_lock( &structure_lock );
1167 if( *psz_cmd == 't' )
1169 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1172 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1175 DumpStructure( p_object, 0, psz_foo );
1177 else if( *psz_cmd == 'v' )
1182 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1184 PrintObject( p_object, "" );
1186 if( !p_object->p_internals->i_vars )
1187 printf( " `-o No variables\n" );
1188 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1190 variable_t *p_var = p_object->p_internals->p_vars + i;
1192 const char *psz_type = "unknown";
1193 switch( p_var->i_type & VLC_VAR_TYPE )
1195 #define MYCASE( type, nice ) \
1196 case VLC_VAR_ ## type: \
1199 MYCASE( VOID, "void" );
1200 MYCASE( BOOL, "bool" );
1201 MYCASE( INTEGER, "integer" );
1202 MYCASE( HOTKEY, "hotkey" );
1203 MYCASE( STRING, "string" );
1204 MYCASE( MODULE, "module" );
1205 MYCASE( FILE, "file" );
1206 MYCASE( DIRECTORY, "directory" );
1207 MYCASE( VARIABLE, "variable" );
1208 MYCASE( FLOAT, "float" );
1209 MYCASE( TIME, "time" );
1210 MYCASE( ADDRESS, "address" );
1211 MYCASE( MUTEX, "mutex" );
1212 MYCASE( LIST, "list" );
1215 printf( " %c-o \"%s\" (%s",
1216 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1217 p_var->psz_name, psz_type );
1218 if( p_var->psz_text )
1219 printf( ", %s", p_var->psz_text );
1221 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1222 printf( ", command" );
1223 if( p_var->i_entries )
1224 printf( ", %d callbacks", p_var->i_entries );
1225 switch( p_var->i_type & 0x00f0 )
1231 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1233 case VLC_VAR_INTEGER:
1234 printf( ": %d", p_var->val.i_int );
1236 case VLC_VAR_STRING:
1237 printf( ": \"%s\"", p_var->val.psz_string );
1240 printf( ": %f", p_var->val.f_float );
1243 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1245 case VLC_VAR_ADDRESS:
1246 printf( ": %p", p_var->val.p_address );
1256 vlc_mutex_unlock( &structure_lock );
1258 if( *newval.psz_string )
1260 vlc_object_release( p_object );
1267 /*****************************************************************************
1268 * vlc_list_release: free a list previously allocated by vlc_list_find
1269 *****************************************************************************
1270 * This function decreases the refcount of all objects in the list and
1272 *****************************************************************************/
1273 void vlc_list_release( vlc_list_t *p_list )
1277 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1279 vlc_object_release( p_list->p_values[i_index].p_object );
1282 free( p_list->p_values );
1286 /* Following functions are local */
1288 /*****************************************************************************
1289 * FindIndex: find the index of an object in an array of objects
1290 *****************************************************************************
1291 * This function assumes that p_this can be found in pp_objects. It will not
1292 * crash if p_this cannot be found, but will return a wrong value. It is your
1293 * duty to check the return value if you are not certain that the object could
1294 * be found for sure.
1295 *****************************************************************************/
1296 static int FindIndex( vlc_object_t *p_this,
1297 vlc_object_t **pp_objects, int i_count )
1299 int i_middle = i_count / 2;
1306 if( pp_objects[i_middle] == p_this )
1316 /* We take advantage of the sorted array */
1317 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1319 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1320 i_count - i_middle );
1324 return FindIndex( p_this, pp_objects, i_middle );
1328 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1331 vlc_object_t *p_tmp;
1333 switch( i_mode & 0x000f )
1336 p_tmp = p_this->p_parent;
1339 if( p_tmp->i_object_type == i_type
1340 && p_tmp->p_internals->i_refcount > 0 )
1342 vlc_object_yield_locked( p_tmp );
1347 return FindObject( p_tmp, i_type, i_mode );
1353 for( i = p_this->i_children; i--; )
1355 p_tmp = p_this->pp_children[i];
1356 if( p_tmp->i_object_type == i_type
1357 && p_tmp->p_internals->i_refcount > 0 )
1359 vlc_object_yield_locked( p_tmp );
1362 else if( p_tmp->i_children )
1364 p_tmp = FindObject( p_tmp, i_type, i_mode );
1374 /* Handled in vlc_object_find */
1381 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1382 const char *psz_name,
1386 vlc_object_t *p_tmp;
1388 switch( i_mode & 0x000f )
1391 p_tmp = p_this->p_parent;
1394 if( p_tmp->psz_object_name
1395 && !strcmp( p_tmp->psz_object_name, psz_name )
1396 && p_tmp->p_internals->i_refcount > 0 )
1398 vlc_object_yield_locked( p_tmp );
1403 return FindObjectName( p_tmp, psz_name, i_mode );
1409 for( i = p_this->i_children; i--; )
1411 p_tmp = p_this->pp_children[i];
1412 if( p_tmp->psz_object_name
1413 && !strcmp( p_tmp->psz_object_name, psz_name )
1414 && p_tmp->p_internals->i_refcount > 0 )
1416 vlc_object_yield_locked( p_tmp );
1419 else if( p_tmp->i_children )
1421 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1431 /* Handled in vlc_object_find */
1438 static void DetachObject( vlc_object_t *p_this )
1440 vlc_object_t *p_parent = p_this->p_parent;
1443 /* Remove p_this's parent */
1444 p_this->p_parent = NULL;
1446 /* Remove all of p_parent's children which are p_this */
1447 for( i_index = p_parent->i_children ; i_index-- ; )
1449 if( p_parent->pp_children[i_index] == p_this )
1451 p_parent->i_children--;
1452 for( i = i_index ; i < p_parent->i_children ; i++ )
1454 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1459 if( p_parent->i_children )
1461 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1462 p_parent->i_children * sizeof(vlc_object_t *) );
1466 free( p_parent->pp_children );
1467 p_parent->pp_children = NULL;
1471 /*****************************************************************************
1472 * SetAttachment: recursively set the b_attached flag of a subtree.
1473 *****************************************************************************
1474 * This function is used by the attach and detach functions to propagate
1475 * the b_attached flag in a subtree.
1476 *****************************************************************************/
1477 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1481 for( i_index = p_this->i_children ; i_index-- ; )
1483 SetAttachment( p_this->pp_children[i_index], b_attached );
1486 p_this->p_internals->b_attached = b_attached;
1489 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1491 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1495 if( p_this->psz_object_name )
1497 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1499 psz_name[48] = '\"';
1502 psz_children[0] = '\0';
1503 switch( p_this->i_children )
1508 strcpy( psz_children, ", 1 child" );
1511 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1515 psz_refcount[0] = '\0';
1516 if( p_this->p_internals->i_refcount > 0 )
1517 snprintf( psz_refcount, 19, ", refcount %u",
1518 p_this->p_internals->i_refcount );
1520 psz_thread[0] = '\0';
1521 if( p_this->p_internals->b_thread )
1522 snprintf( psz_thread, 29, " (thread %u)",
1523 #if defined(WIN32) || defined(UNDER_CE)
1524 (unsigned)p_this->p_internals->thread_id.id );
1526 (unsigned)p_this->p_internals->thread_id );
1529 psz_parent[0] = '\0';
1530 if( p_this->p_parent )
1531 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1533 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1534 p_this->i_object_id, p_this->psz_object_type,
1535 psz_name, psz_thread, psz_refcount, psz_children,
1539 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1542 char i_back = psz_foo[i_level];
1543 psz_foo[i_level] = '\0';
1545 PrintObject( p_this, psz_foo );
1547 psz_foo[i_level] = i_back;
1549 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1551 msg_Warn( p_this, "structure tree is too deep" );
1555 for( i = 0 ; i < p_this->i_children ; i++ )
1559 psz_foo[i_level-1] = ' ';
1561 if( psz_foo[i_level-2] == '`' )
1563 psz_foo[i_level-2] = ' ';
1567 if( i == p_this->i_children - 1 )
1569 psz_foo[i_level] = '`';
1573 psz_foo[i_level] = '|';
1576 psz_foo[i_level+1] = '-';
1577 psz_foo[i_level+2] = '\0';
1579 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1583 static vlc_list_t * NewList( int i_count )
1585 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1586 if( p_list == NULL )
1591 p_list->i_count = i_count;
1595 p_list->p_values = NULL;
1599 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1600 if( p_list->p_values == NULL )
1602 p_list->i_count = 0;
1609 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1612 if( p_list == NULL || i_index >= p_list->i_count )
1617 vlc_object_yield_locked( p_object );
1619 p_list->p_values[i_index].p_object = p_object;
1624 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1626 if( p_list == NULL )
1631 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1632 * sizeof( vlc_value_t ) );
1633 if( p_list->p_values == NULL )
1635 p_list->i_count = 0;
1639 vlc_object_yield_locked( p_object );
1641 p_list->p_values[p_list->i_count].p_object = p_object;
1647 static int CountChildren( vlc_object_t *p_this, int i_type )
1649 vlc_object_t *p_tmp;
1652 for( i = 0; i < p_this->i_children; i++ )
1654 p_tmp = p_this->pp_children[i];
1656 if( p_tmp->i_object_type == i_type )
1661 if( p_tmp->i_children )
1663 i_count += CountChildren( p_tmp, i_type );
1670 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1672 vlc_object_t *p_tmp;
1675 for( i = 0; i < p_this->i_children; i++ )
1677 p_tmp = p_this->pp_children[i];
1679 if( p_tmp->i_object_type == i_type )
1681 ListReplace( p_list, p_tmp, p_list->i_count++ );
1684 if( p_tmp->i_children )
1686 ListChildren( p_list, p_tmp, i_type );