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"
64 #include "variables.h"
70 # include <errno.h> /* ENOSYS */
74 /*****************************************************************************
76 *****************************************************************************/
78 const vlc_destructor_t kVLCDestructor = NULL;
80 /*****************************************************************************
82 *****************************************************************************/
83 static int DumpCommand( vlc_object_t *, char const *,
84 vlc_value_t, vlc_value_t, void * );
86 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
87 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
88 static void DetachObject ( vlc_object_t * );
89 static void PrintObject ( vlc_object_t *, const char * );
90 static void DumpStructure ( vlc_object_t *, int, char * );
91 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
92 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
94 static vlc_list_t * NewList ( int );
95 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
96 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
97 static int CountChildren ( vlc_object_t *, int );
98 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
100 static void vlc_object_destroy( vlc_object_t *p_this );
101 static void vlc_object_yield_locked( vlc_object_t *p_this );
103 /*****************************************************************************
104 * Local structure lock
105 *****************************************************************************/
106 static vlc_mutex_t structure_lock;
107 static vlc_object_internals_t global_internals;
109 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
110 int i_type, const char *psz_type )
113 vlc_object_internals_t *p_priv;
115 if( i_type == VLC_OBJECT_GLOBAL )
118 p_priv = &global_internals;
119 memset( p_priv, 0, sizeof( *p_priv ) );
123 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
127 p_new = (vlc_object_t *)(p_priv + 1);
130 p_new->p_internals = p_priv;
131 p_new->i_object_type = i_type;
132 p_new->psz_object_type = psz_type;
134 p_new->psz_object_name = NULL;
136 p_new->b_die = VLC_FALSE;
137 p_new->b_error = VLC_FALSE;
138 p_new->b_dead = VLC_FALSE;
139 p_priv->b_attached = VLC_FALSE;
140 p_new->b_force = VLC_FALSE;
142 p_new->psz_header = NULL;
144 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
145 p_new->i_flags |= OBJECT_FLAGS_NODBG;
146 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
147 p_new->i_flags |= OBJECT_FLAGS_QUIET;
148 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
149 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
151 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
153 if( !p_priv->p_vars )
155 if( i_type != VLC_OBJECT_GLOBAL )
160 if( i_type == VLC_OBJECT_GLOBAL )
162 /* If i_type is global, then p_new is actually p_libvlc_global */
163 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
164 p_new->p_libvlc = NULL;
166 p_libvlc_global->i_counter = 0;
167 p_new->i_object_id = 0;
169 p_libvlc_global->i_objects = 1;
170 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
171 p_libvlc_global->pp_objects[0] = p_new;
172 p_priv->b_attached = VLC_TRUE;
176 libvlc_global_data_t *p_libvlc_global = vlc_global();
177 if( i_type == VLC_OBJECT_LIBVLC )
179 p_new->p_libvlc = (libvlc_int_t*)p_new;
180 p_priv->b_attached = VLC_TRUE;
184 p_new->p_libvlc = p_this->p_libvlc;
187 vlc_mutex_lock( &structure_lock );
189 p_libvlc_global->i_counter++;
190 p_new->i_object_id = p_libvlc_global->i_counter;
192 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
193 * useless to try and recover anything if pp_objects gets smashed. */
194 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
197 vlc_mutex_unlock( &structure_lock );
200 p_priv->i_refcount = 1;
201 p_priv->pf_destructor = kVLCDestructor;
202 p_priv->b_thread = VLC_FALSE;
203 p_new->p_parent = NULL;
204 p_new->pp_children = NULL;
205 p_new->i_children = 0;
207 p_new->p_private = NULL;
209 /* Initialize mutexes and condvars */
210 vlc_mutex_init( p_new, &p_new->object_lock );
211 vlc_cond_init( p_new, &p_new->object_wait );
212 vlc_mutex_init( p_new, &p_priv->var_lock );
213 vlc_spin_init( &p_priv->spin );
214 p_priv->pipes[0] = p_priv->pipes[1] = -1;
216 if( i_type == VLC_OBJECT_GLOBAL )
218 vlc_mutex_init( p_new, &structure_lock );
221 if( i_type == VLC_OBJECT_LIBVLC )
223 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
224 var_AddCallback( p_new, "list", DumpCommand, NULL );
225 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
226 var_AddCallback( p_new, "tree", DumpCommand, NULL );
227 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
228 var_AddCallback( p_new, "vars", DumpCommand, NULL );
236 * Allocates and initializes a vlc object.
238 * @param i_type known object type (all of them are negative integer values),
239 * or object byte size (always positive).
241 * @return the new object, or NULL on error.
243 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
245 const char * psz_type;
250 case VLC_OBJECT_GLOBAL:
251 i_size = sizeof(libvlc_global_data_t);
254 case VLC_OBJECT_LIBVLC:
255 i_size = sizeof(libvlc_int_t);
258 case VLC_OBJECT_INTF:
259 i_size = sizeof(intf_thread_t);
260 psz_type = "interface";
262 case VLC_OBJECT_DIALOGS:
263 i_size = sizeof(intf_thread_t);
264 psz_type = "dialogs";
266 case VLC_OBJECT_PLAYLIST:
267 i_size = sizeof(playlist_t);
268 psz_type = "playlist";
271 i_size = sizeof(services_discovery_t);
272 psz_type = "services discovery";
274 case VLC_OBJECT_INPUT:
275 i_size = sizeof(input_thread_t);
278 case VLC_OBJECT_DEMUX:
279 i_size = sizeof(demux_t);
282 case VLC_OBJECT_ACCESS:
283 i_size = sizeof(access_t);
286 case VLC_OBJECT_DECODER:
287 i_size = sizeof(decoder_t);
288 psz_type = "decoder";
290 case VLC_OBJECT_PACKETIZER:
291 i_size = sizeof(decoder_t);
292 psz_type = "packetizer";
294 case VLC_OBJECT_ENCODER:
295 i_size = sizeof(encoder_t);
296 psz_type = "encoder";
298 case VLC_OBJECT_FILTER:
299 i_size = sizeof(filter_t);
302 case VLC_OBJECT_VOUT:
303 i_size = sizeof(vout_thread_t);
304 psz_type = "video output";
307 i_size = sizeof(spu_t);
308 psz_type = "subpicture";
310 case VLC_OBJECT_AOUT:
311 i_size = sizeof(aout_instance_t);
312 psz_type = "audio output";
314 case VLC_OBJECT_SOUT:
315 i_size = sizeof(sout_instance_t);
316 psz_type = "stream output";
319 i_size = sizeof( vlm_t );
320 psz_type = "vlm dameon";
323 i_size = sizeof( xml_t );
326 case VLC_OBJECT_OPENGL:
327 i_size = sizeof( vout_thread_t );
330 case VLC_OBJECT_ANNOUNCE:
331 i_size = sizeof( announce_handler_t );
332 psz_type = "announce";
334 case VLC_OBJECT_META_ENGINE:
335 i_size = sizeof( meta_engine_t );
336 psz_type = "meta engine";
338 case VLC_OBJECT_OSDMENU:
339 i_size = sizeof( osd_menu_t );
340 psz_type = "osd menu";
342 case VLC_OBJECT_INTERACTION:
343 i_size = sizeof( interaction_t );
344 psz_type = "interaction";
347 i_size = i_type > (int)sizeof(vlc_object_t)
348 ? i_type : (int)sizeof(vlc_object_t);
349 i_type = VLC_OBJECT_GENERIC;
350 psz_type = "generic";
354 return vlc_custom_create( p_this, i_size, i_type, psz_type );
359 ****************************************************************************
360 * Set the destructor of a vlc object
362 * This function sets the destructor of the vlc object. It will be called
363 * when the object is destroyed when the its refcount reaches 0.
364 * (It is called by the internal function vlc_object_destroy())
365 *****************************************************************************/
366 void __vlc_object_set_destructor( vlc_object_t *p_this,
367 vlc_destructor_t pf_destructor )
369 vlc_object_internals_t *p_priv = vlc_internals(p_this );
371 vlc_mutex_lock( &structure_lock );
372 p_priv->pf_destructor = pf_destructor;
373 vlc_mutex_unlock( &structure_lock );
377 ****************************************************************************
378 * Destroy a vlc object (Internal)
380 * This function destroys an object that has been previously allocated with
381 * vlc_object_create. The object's refcount must be zero and it must not be
382 * attached to other objects in any way.
383 *****************************************************************************/
384 static void vlc_object_destroy( vlc_object_t *p_this )
386 vlc_object_internals_t *p_priv = vlc_internals( p_this );
388 /* Automatically detach the object from its parents */
389 if( p_this->p_parent ) vlc_object_detach( p_this );
392 /* Send a kill to the object's thread if applicable */
393 vlc_object_kill( p_this );
395 /* If we are running on a thread, wait until it ends */
396 if( p_priv->b_thread )
397 vlc_thread_join( p_this );
399 /* Call the custom "subclass" destructor */
400 if( p_priv->pf_destructor )
401 p_priv->pf_destructor( p_this );
404 if( p_this->i_children )
409 "ERROR: cannot delete object (%i, %s) with %d children\n",
410 p_this->i_object_id, p_this->psz_object_name,
411 p_this->i_children );
413 for( i = 0; i < p_this->i_children; i++ )
416 "ERROR: Remaining children object "
417 "(id:%i, type:%s, name:%s)\n",
418 p_this->pp_children[i]->i_object_id,
419 p_this->pp_children[i]->psz_object_type,
420 p_this->pp_children[i]->psz_object_name );
426 /* Destroy the associated variables, starting from the end so that
427 * no memmove calls have to be done. */
428 while( p_priv->i_vars )
430 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
433 free( p_priv->p_vars );
434 vlc_mutex_destroy( &p_priv->var_lock );
436 free( p_this->psz_header );
438 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
440 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
443 if( p_global->i_objects > 0 )
446 for( i = 0; i < p_global->i_objects; i++ )
448 /* We are leaking this object */
450 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
451 p_global->pp_objects[i]->i_object_id,
452 p_global->pp_objects[i]->psz_object_type,
453 p_global->pp_objects[i]->psz_object_name );
456 /* Strongly abort, cause we want these to be fixed */
460 /* We are the global object ... no need to lock. */
461 free( p_global->pp_objects );
462 p_global->pp_objects = NULL;
464 vlc_mutex_destroy( &structure_lock );
467 #if defined(WIN32) || defined(UNDER_CE)
468 /* if object has an associated thread, close it now */
469 if( p_priv->thread_id.hThread )
470 CloseHandle(p_priv->thread_id.hThread);
473 vlc_mutex_destroy( &p_this->object_lock );
474 vlc_cond_destroy( &p_this->object_wait );
475 vlc_spin_destroy( &p_priv->spin );
476 if( p_priv->pipes[1] != -1 )
477 close( p_priv->pipes[1] );
478 if( p_priv->pipes[0] != -1 )
479 close( p_priv->pipes[0] );
481 /* global is not dynamically allocated by vlc_object_create */
482 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
487 /** Inter-object signaling */
489 void __vlc_object_lock( vlc_object_t *obj )
491 vlc_mutex_lock( &obj->object_lock );
494 void __vlc_object_unlock( vlc_object_t *obj )
496 vlc_assert_locked( &obj->object_lock );
497 vlc_mutex_unlock( &obj->object_lock );
501 # include <winsock2.h>
502 # include <ws2tcpip.h>
505 * select()-able pipes emulated using Winsock
507 static int pipe (int fd[2])
510 int addrlen = sizeof (addr);
512 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
513 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
514 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
517 memset (&addr, 0, sizeof (addr));
518 addr.sin_family = AF_INET;
519 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
520 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
521 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
523 || connect (c, (PSOCKADDR)&addr, addrlen))
526 a = accept (l, NULL, NULL);
527 if (a == INVALID_SOCKET)
538 if (l != INVALID_SOCKET)
540 if (c != INVALID_SOCKET)
546 #define read( a, b, c ) recv (a, b, c, 0)
548 #define write( a, b, c ) send (a, b, c, 0)
550 #define close( a ) closesocket (a)
554 * Returns the readable end of a pipe that becomes readable once termination
555 * of the object is requested (vlc_object_kill()).
556 * This can be used to wake-up out of a select() or poll() event loop, such
557 * typically when doing network I/O.
559 * Note that the pipe will remain the same for the lifetime of the object.
560 * DO NOT read the pipe nor close it yourself. Ever.
562 * @param obj object that would be "killed"
563 * @return a readable pipe descriptor, or -1 on error.
565 int __vlc_object_waitpipe( vlc_object_t *obj )
567 int pfd[2] = { -1, -1 };
568 struct vlc_object_internals_t *internals = obj->p_internals;
569 vlc_bool_t killed = VLC_FALSE;
571 vlc_spin_lock (&internals->spin);
572 if (internals->pipes[0] == -1)
574 /* This can only ever happen if someone killed us without locking: */
575 assert (internals->pipes[1] == -1);
576 vlc_spin_unlock (&internals->spin);
581 vlc_spin_lock (&internals->spin);
582 if (internals->pipes[0] == -1)
584 internals->pipes[0] = pfd[0];
585 internals->pipes[1] = pfd[1];
586 pfd[0] = pfd[1] = -1;
590 vlc_spin_unlock (&internals->spin);
594 /* Race condition: vlc_object_kill() already invoked! */
597 vlc_spin_lock (&internals->spin);
598 fd = internals->pipes[1];
599 internals->pipes[1] = -1;
600 vlc_spin_unlock (&internals->spin);
602 msg_Dbg (obj, "waitpipe: object already dying");
607 /* Race condition: two threads call pipe() - unlikely */
613 return internals->pipes[0];
618 * Waits for the object to be signaled (using vlc_object_signal()).
619 * If the object already has a signal pending, this function will return
620 * immediately. It is asserted that the caller holds the object lock.
622 * @return true if the object is dying and should terminate.
624 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
626 vlc_assert_locked( &obj->object_lock );
627 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
633 * Waits for the object to be signaled (using vlc_object_signal()), or for
635 * If the object already has a signal pending, this function will return
636 * immediately. It is asserted that the caller holds the object lock.
638 * @return negative if the object is dying and should terminate,
639 * positive if the the object has been signaled but is not dying,
640 * 0 if timeout has been reached.
642 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
646 vlc_assert_locked( &obj->object_lock );
647 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
648 if( v == 0 ) /* signaled */
649 return obj->b_die ? -1 : 1;
655 * Checks whether an object has been "killed".
656 * The object lock must be held.
658 * Typical code for an object thread could be:
660 vlc_object_lock (self);
662 while (vlc_object_alive (self))
666 if (vlc_object_wait (self))
671 ...deinitialization...
672 vlc_object_unlock (self);
675 * @return true iff the object has not been killed yet
677 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
679 vlc_assert_locked( &obj->object_lock );
685 * Signals an object for which the lock is held.
687 void __vlc_object_signal_unlocked( vlc_object_t *obj )
689 vlc_assert_locked (&obj->object_lock);
690 vlc_cond_signal( &obj->object_wait );
695 * Requests termination of an object.
696 * If the object is LibVLC, also request to terminate all its children.
698 void __vlc_object_kill( vlc_object_t *p_this )
700 struct vlc_object_internals_t *internals = p_this->p_internals;
703 vlc_mutex_lock( &p_this->object_lock );
704 p_this->b_die = VLC_TRUE;
706 vlc_spin_lock (&internals->spin);
707 fd = internals->pipes[1];
708 internals->pipes[1] = -1;
709 vlc_spin_unlock (&internals->spin);
713 msg_Dbg (p_this, "waitpipe: object killed");
717 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
718 for( int i = 0; i < p_this->i_children ; i++ )
719 vlc_object_kill( p_this->pp_children[i] );
721 vlc_object_signal_unlocked( p_this );
722 vlc_mutex_unlock( &p_this->object_lock );
727 * find an object given its ID
729 * This function looks for the object whose i_object_id field is i_id. We
730 * use a dichotomy so that lookups are in log2(n).
731 *****************************************************************************/
732 void * vlc_object_get( int i_id )
735 vlc_object_t **pp_objects;
736 libvlc_global_data_t *p_libvlc_global = vlc_global();
738 vlc_mutex_lock( &structure_lock );
740 pp_objects = p_libvlc_global->pp_objects;
742 /* Perform our dichotomy */
743 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
745 i_middle = i_max / 2;
747 if( pp_objects[i_middle]->i_object_id > i_id )
751 else if( pp_objects[i_middle]->i_object_id < i_id )
755 pp_objects += i_middle;
760 /* This happens when there are only two remaining objects */
761 if( pp_objects[i_middle+1]->i_object_id == i_id
762 && pp_objects[i_middle+1]->p_internals->i_refcount > 0 )
764 vlc_object_yield_locked( pp_objects[i_middle+1] );
765 vlc_mutex_unlock( &structure_lock );
766 return pp_objects[i_middle+1];
771 else if( pp_objects[i_middle]->p_internals->i_refcount > 0 )
773 vlc_object_yield_locked( pp_objects[i_middle] );
774 vlc_mutex_unlock( &structure_lock );
775 return pp_objects[i_middle];
780 /* this means that i_max == i_middle, and since we have already
781 * tested pp_objects[i_middle]), p_found is properly set. */
786 vlc_mutex_unlock( &structure_lock );
791 ****************************************************************************
792 * find a typed object and increment its refcount
793 *****************************************************************************
794 * This function recursively looks for a given object type. i_mode can be one
795 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
796 *****************************************************************************/
797 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
799 vlc_object_t *p_found;
801 vlc_mutex_lock( &structure_lock );
803 /* If we are of the requested type ourselves, don't look further */
804 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type
805 && p_this->p_internals->i_refcount > 0 )
807 vlc_object_yield_locked( p_this );
808 vlc_mutex_unlock( &structure_lock );
812 /* Otherwise, recursively look for the object */
813 if( (i_mode & 0x000f) == FIND_ANYWHERE )
815 vlc_object_t *p_root = p_this;
818 while( p_root->p_parent != NULL &&
819 p_root != VLC_OBJECT( p_this->p_libvlc ) )
821 p_root = p_root->p_parent;
824 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
825 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
827 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
828 i_type, (i_mode & ~0x000f)|FIND_CHILD );
833 p_found = FindObject( p_this, i_type, i_mode );
836 vlc_mutex_unlock( &structure_lock );
842 ****************************************************************************
843 * find a named object and increment its refcount
844 *****************************************************************************
845 * This function recursively looks for a given object name. i_mode can be one
846 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
847 *****************************************************************************/
848 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
851 vlc_object_t *p_found;
853 vlc_mutex_lock( &structure_lock );
855 /* Avoid obvious freed object uses */
856 assert( p_this->p_internals->i_refcount > 0 );
858 /* If have the requested name ourselves, don't look further */
859 if( !(i_mode & FIND_STRICT)
860 && p_this->psz_object_name
861 && !strcmp( p_this->psz_object_name, psz_name )
862 && p_this->p_internals->i_refcount > 0 )
864 vlc_object_yield_locked( p_this );
865 vlc_mutex_unlock( &structure_lock );
869 /* Otherwise, recursively look for the object */
870 if( (i_mode & 0x000f) == FIND_ANYWHERE )
872 vlc_object_t *p_root = p_this;
875 while( p_root->p_parent != NULL &&
876 p_root != VLC_OBJECT( p_this->p_libvlc ) )
878 p_root = p_root->p_parent;
881 p_found = FindObjectName( p_root, psz_name,
882 (i_mode & ~0x000f)|FIND_CHILD );
883 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
885 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
886 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
891 p_found = FindObjectName( p_this, psz_name, i_mode );
894 vlc_mutex_unlock( &structure_lock );
900 ****************************************************************************
901 * increment an object refcount
902 *****************************************************************************/
904 /* When the structure_lock is locked */
905 static void vlc_object_yield_locked( vlc_object_t *p_this )
907 vlc_assert_locked (&structure_lock);
909 /* Avoid obvious freed object uses */
910 assert( p_this->p_internals->i_refcount > 0 );
912 /* Increment the counter */
913 p_this->p_internals->i_refcount++;
916 /* Public function */
917 void __vlc_object_yield( vlc_object_t *p_this )
919 vlc_mutex_lock( &structure_lock );
920 vlc_object_yield_locked( p_this );
921 vlc_mutex_unlock( &structure_lock );
925 /*****************************************************************************
926 * decrement an object refcount
927 * And destroy the object if its refcount reach zero.
928 *****************************************************************************/
929 void __vlc_object_release( vlc_object_t *p_this )
931 vlc_bool_t b_should_destroy;
933 vlc_mutex_lock( &structure_lock );
935 assert( p_this->p_internals->i_refcount > 0 );
936 p_this->p_internals->i_refcount--;
937 b_should_destroy = (p_this->p_internals->i_refcount == 0);
939 if( b_should_destroy )
941 /* Make sure this object can't be obtained via vlc_find_object now that
943 libvlc_global_data_t *p_libvlc_global = vlc_global();
946 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
947 * useless to try and recover anything if pp_objects gets smashed. */
948 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
949 p_libvlc_global->i_objects );
950 REMOVE_ELEM( p_libvlc_global->pp_objects,
951 p_libvlc_global->i_objects, i_index );
954 vlc_mutex_unlock( &structure_lock );
956 if( b_should_destroy )
957 vlc_object_destroy( p_this );
961 ****************************************************************************
962 * attach object to a parent object
963 *****************************************************************************
964 * This function sets p_this as a child of p_parent, and p_parent as a parent
965 * of p_this. This link can be undone using vlc_object_detach.
966 *****************************************************************************/
967 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
969 if( !p_this ) return;
971 vlc_mutex_lock( &structure_lock );
973 /* Avoid obvious freed object uses */
974 assert( p_this->p_internals->i_refcount > 0 );
976 /* Attach the parent to its child */
977 p_this->p_parent = p_parent;
979 /* Attach the child to its parent */
980 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
981 p_parent->i_children, p_this );
983 /* Climb up the tree to see whether we are connected with the root */
984 if( p_parent->p_internals->b_attached )
986 SetAttachment( p_this, VLC_TRUE );
989 vlc_mutex_unlock( &structure_lock );
993 ****************************************************************************
994 * detach object from its parent
995 *****************************************************************************
996 * This function removes all links between an object and its parent.
997 *****************************************************************************/
998 void __vlc_object_detach( vlc_object_t *p_this )
1000 if( !p_this ) return;
1002 vlc_mutex_lock( &structure_lock );
1004 if( !p_this->p_parent )
1006 msg_Err( p_this, "object is not attached" );
1007 vlc_mutex_unlock( &structure_lock );
1011 /* Climb up the tree to see whether we are connected with the root */
1012 if( p_this->p_parent->p_internals->b_attached )
1014 SetAttachment( p_this, VLC_FALSE );
1017 DetachObject( p_this );
1018 vlc_mutex_unlock( &structure_lock );
1023 ****************************************************************************
1024 * find a list typed objects and increment their refcount
1025 *****************************************************************************
1026 * This function recursively looks for a given object type. i_mode can be one
1027 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
1028 *****************************************************************************/
1029 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
1032 vlc_object_t **pp_current, **pp_end;
1033 int i_count = 0, i_index = 0;
1034 libvlc_global_data_t *p_libvlc_global = vlc_global();
1036 vlc_mutex_lock( &structure_lock );
1038 /* Look for the objects */
1039 switch( i_mode & 0x000f )
1042 pp_current = p_libvlc_global->pp_objects;
1043 pp_end = pp_current + p_libvlc_global->i_objects;
1045 for( ; pp_current < pp_end ; pp_current++ )
1047 if( (*pp_current)->p_internals->b_attached
1048 && (*pp_current)->i_object_type == i_type )
1054 p_list = NewList( i_count );
1055 pp_current = p_libvlc_global->pp_objects;
1057 for( ; pp_current < pp_end ; pp_current++ )
1059 if( (*pp_current)->p_internals->b_attached
1060 && (*pp_current)->i_object_type == i_type )
1062 ListReplace( p_list, *pp_current, i_index );
1063 if( i_index < i_count ) i_index++;
1069 i_count = CountChildren( p_this, i_type );
1070 p_list = NewList( i_count );
1072 /* Check allocation was successful */
1073 if( p_list->i_count != i_count )
1075 msg_Err( p_this, "list allocation failed!" );
1076 p_list->i_count = 0;
1080 p_list->i_count = 0;
1081 ListChildren( p_list, p_this, i_type );
1085 msg_Err( p_this, "unimplemented!" );
1086 p_list = NewList( 0 );
1090 vlc_mutex_unlock( &structure_lock );
1095 /*****************************************************************************
1096 * DumpCommand: print the current vlc structure
1097 *****************************************************************************
1098 * This function prints either an ASCII tree showing the connections between
1099 * vlc objects, and additional information such as their refcount, thread ID,
1100 * etc. (command "tree"), or the same data as a simple list (command "list").
1101 *****************************************************************************/
1102 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1103 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1105 libvlc_global_data_t *p_libvlc_global = vlc_global();
1107 (void)oldval; (void)p_data;
1108 if( *psz_cmd == 'l' )
1110 vlc_mutex_lock( &structure_lock );
1112 vlc_object_t **pp_current, **pp_end;
1114 pp_current = p_libvlc_global->pp_objects;
1115 pp_end = pp_current + p_libvlc_global->i_objects;
1117 for( ; pp_current < pp_end ; pp_current++ )
1119 if( (*pp_current)->p_internals->b_attached )
1121 PrintObject( *pp_current, "" );
1125 printf( " o %.8i %s (not attached)\n",
1126 (*pp_current)->i_object_id,
1127 (*pp_current)->psz_object_type );
1131 vlc_mutex_unlock( &structure_lock );
1135 vlc_object_t *p_object = NULL;
1137 if( *newval.psz_string )
1140 int i_id = strtol( newval.psz_string, &end, 0 );
1141 if( end != newval.psz_string )
1142 p_object = vlc_object_get( i_id );
1145 /* try using the object's name to find it */
1146 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1149 /* Look in p_libvlc's children tree */
1150 p_object = vlc_object_find_name( p_libvlc,
1153 vlc_object_release( p_libvlc );
1157 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1158 p_object = vlc_object_find_name( p_this,
1170 vlc_mutex_lock( &structure_lock );
1172 if( *psz_cmd == 't' )
1174 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1177 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1180 DumpStructure( p_object, 0, psz_foo );
1182 else if( *psz_cmd == 'v' )
1187 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1189 PrintObject( p_object, "" );
1191 if( !p_object->p_internals->i_vars )
1192 printf( " `-o No variables\n" );
1193 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1195 variable_t *p_var = p_object->p_internals->p_vars + i;
1197 const char *psz_type = "unknown";
1198 switch( p_var->i_type & VLC_VAR_TYPE )
1200 #define MYCASE( type, nice ) \
1201 case VLC_VAR_ ## type: \
1204 MYCASE( VOID, "void" );
1205 MYCASE( BOOL, "bool" );
1206 MYCASE( INTEGER, "integer" );
1207 MYCASE( HOTKEY, "hotkey" );
1208 MYCASE( STRING, "string" );
1209 MYCASE( MODULE, "module" );
1210 MYCASE( FILE, "file" );
1211 MYCASE( DIRECTORY, "directory" );
1212 MYCASE( VARIABLE, "variable" );
1213 MYCASE( FLOAT, "float" );
1214 MYCASE( TIME, "time" );
1215 MYCASE( ADDRESS, "address" );
1216 MYCASE( MUTEX, "mutex" );
1217 MYCASE( LIST, "list" );
1220 printf( " %c-o \"%s\" (%s",
1221 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1222 p_var->psz_name, psz_type );
1223 if( p_var->psz_text )
1224 printf( ", %s", p_var->psz_text );
1226 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1227 printf( ", command" );
1228 if( p_var->i_entries )
1229 printf( ", %d callbacks", p_var->i_entries );
1230 switch( p_var->i_type & 0x00f0 )
1236 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1238 case VLC_VAR_INTEGER:
1239 printf( ": %d", p_var->val.i_int );
1241 case VLC_VAR_STRING:
1242 printf( ": \"%s\"", p_var->val.psz_string );
1245 printf( ": %f", p_var->val.f_float );
1248 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1250 case VLC_VAR_ADDRESS:
1251 printf( ": %p", p_var->val.p_address );
1261 vlc_mutex_unlock( &structure_lock );
1263 if( *newval.psz_string )
1265 vlc_object_release( p_object );
1272 /*****************************************************************************
1273 * vlc_list_release: free a list previously allocated by vlc_list_find
1274 *****************************************************************************
1275 * This function decreases the refcount of all objects in the list and
1277 *****************************************************************************/
1278 void vlc_list_release( vlc_list_t *p_list )
1282 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1284 vlc_object_release( p_list->p_values[i_index].p_object );
1287 free( p_list->p_values );
1291 /* Following functions are local */
1293 /*****************************************************************************
1294 * FindIndex: find the index of an object in an array of objects
1295 *****************************************************************************
1296 * This function assumes that p_this can be found in pp_objects. It will not
1297 * crash if p_this cannot be found, but will return a wrong value. It is your
1298 * duty to check the return value if you are not certain that the object could
1299 * be found for sure.
1300 *****************************************************************************/
1301 static int FindIndex( vlc_object_t *p_this,
1302 vlc_object_t **pp_objects, int i_count )
1304 int i_middle = i_count / 2;
1311 if( pp_objects[i_middle] == p_this )
1321 /* We take advantage of the sorted array */
1322 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1324 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1325 i_count - i_middle );
1329 return FindIndex( p_this, pp_objects, i_middle );
1333 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1336 vlc_object_t *p_tmp;
1338 switch( i_mode & 0x000f )
1341 p_tmp = p_this->p_parent;
1344 if( p_tmp->i_object_type == i_type
1345 && p_tmp->p_internals->i_refcount > 0 )
1347 vlc_object_yield_locked( p_tmp );
1352 return FindObject( p_tmp, i_type, i_mode );
1358 for( i = p_this->i_children; i--; )
1360 p_tmp = p_this->pp_children[i];
1361 if( p_tmp->i_object_type == i_type
1362 && p_tmp->p_internals->i_refcount > 0 )
1364 vlc_object_yield_locked( p_tmp );
1367 else if( p_tmp->i_children )
1369 p_tmp = FindObject( p_tmp, i_type, i_mode );
1379 /* Handled in vlc_object_find */
1386 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1387 const char *psz_name,
1391 vlc_object_t *p_tmp;
1393 switch( i_mode & 0x000f )
1396 p_tmp = p_this->p_parent;
1399 if( p_tmp->psz_object_name
1400 && !strcmp( p_tmp->psz_object_name, psz_name )
1401 && p_tmp->p_internals->i_refcount > 0 )
1403 vlc_object_yield_locked( p_tmp );
1408 return FindObjectName( p_tmp, psz_name, i_mode );
1414 for( i = p_this->i_children; i--; )
1416 p_tmp = p_this->pp_children[i];
1417 if( p_tmp->psz_object_name
1418 && !strcmp( p_tmp->psz_object_name, psz_name )
1419 && p_tmp->p_internals->i_refcount > 0 )
1421 vlc_object_yield_locked( p_tmp );
1424 else if( p_tmp->i_children )
1426 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1436 /* Handled in vlc_object_find */
1443 static void DetachObject( vlc_object_t *p_this )
1445 vlc_object_t *p_parent = p_this->p_parent;
1448 /* Remove p_this's parent */
1449 p_this->p_parent = NULL;
1451 /* Remove all of p_parent's children which are p_this */
1452 for( i_index = p_parent->i_children ; i_index-- ; )
1454 if( p_parent->pp_children[i_index] == p_this )
1456 p_parent->i_children--;
1457 for( i = i_index ; i < p_parent->i_children ; i++ )
1459 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1464 if( p_parent->i_children )
1466 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1467 p_parent->i_children * sizeof(vlc_object_t *) );
1471 free( p_parent->pp_children );
1472 p_parent->pp_children = NULL;
1476 /*****************************************************************************
1477 * SetAttachment: recursively set the b_attached flag of a subtree.
1478 *****************************************************************************
1479 * This function is used by the attach and detach functions to propagate
1480 * the b_attached flag in a subtree.
1481 *****************************************************************************/
1482 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1486 for( i_index = p_this->i_children ; i_index-- ; )
1488 SetAttachment( p_this->pp_children[i_index], b_attached );
1491 p_this->p_internals->b_attached = b_attached;
1494 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1496 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1500 if( p_this->psz_object_name )
1502 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1504 psz_name[48] = '\"';
1507 psz_children[0] = '\0';
1508 switch( p_this->i_children )
1513 strcpy( psz_children, ", 1 child" );
1516 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1520 psz_refcount[0] = '\0';
1521 if( p_this->p_internals->i_refcount > 0 )
1522 snprintf( psz_refcount, 19, ", refcount %u",
1523 p_this->p_internals->i_refcount );
1525 psz_thread[0] = '\0';
1526 if( p_this->p_internals->b_thread )
1527 snprintf( psz_thread, 29, " (thread %u)",
1528 #if defined(WIN32) || defined(UNDER_CE)
1529 (unsigned)p_this->p_internals->thread_id.id );
1531 (unsigned)p_this->p_internals->thread_id );
1534 psz_parent[0] = '\0';
1535 if( p_this->p_parent )
1536 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1538 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1539 p_this->i_object_id, p_this->psz_object_type,
1540 psz_name, psz_thread, psz_refcount, psz_children,
1544 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1547 char i_back = psz_foo[i_level];
1548 psz_foo[i_level] = '\0';
1550 PrintObject( p_this, psz_foo );
1552 psz_foo[i_level] = i_back;
1554 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1556 msg_Warn( p_this, "structure tree is too deep" );
1560 for( i = 0 ; i < p_this->i_children ; i++ )
1564 psz_foo[i_level-1] = ' ';
1566 if( psz_foo[i_level-2] == '`' )
1568 psz_foo[i_level-2] = ' ';
1572 if( i == p_this->i_children - 1 )
1574 psz_foo[i_level] = '`';
1578 psz_foo[i_level] = '|';
1581 psz_foo[i_level+1] = '-';
1582 psz_foo[i_level+2] = '\0';
1584 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1588 static vlc_list_t * NewList( int i_count )
1590 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1591 if( p_list == NULL )
1596 p_list->i_count = i_count;
1600 p_list->p_values = NULL;
1604 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1605 if( p_list->p_values == NULL )
1607 p_list->i_count = 0;
1614 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1617 if( p_list == NULL || i_index >= p_list->i_count )
1622 vlc_object_yield_locked( p_object );
1624 p_list->p_values[i_index].p_object = p_object;
1629 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1631 if( p_list == NULL )
1636 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1637 * sizeof( vlc_value_t ) );
1638 if( p_list->p_values == NULL )
1640 p_list->i_count = 0;
1644 vlc_object_yield_locked( p_object );
1646 p_list->p_values[p_list->i_count].p_object = p_object;
1652 static int CountChildren( vlc_object_t *p_this, int i_type )
1654 vlc_object_t *p_tmp;
1657 for( i = 0; i < p_this->i_children; i++ )
1659 p_tmp = p_this->pp_children[i];
1661 if( p_tmp->i_object_type == i_type )
1666 if( p_tmp->i_children )
1668 i_count += CountChildren( p_tmp, i_type );
1675 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1677 vlc_object_t *p_tmp;
1680 for( i = 0; i < p_this->i_children; i++ )
1682 p_tmp = p_this->pp_children[i];
1684 if( p_tmp->i_object_type == i_type )
1686 ListReplace( p_list, p_tmp, p_list->i_count++ );
1689 if( p_tmp->i_children )
1691 ListChildren( p_list, p_tmp, i_type );