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"
65 #include "variables.h"
71 # include <errno.h> /* ENOSYS */
75 /*****************************************************************************
77 *****************************************************************************/
79 const vlc_destructor_t kVLCDestructor = NULL;
81 /*****************************************************************************
83 *****************************************************************************/
84 static int DumpCommand( vlc_object_t *, char const *,
85 vlc_value_t, vlc_value_t, void * );
87 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
88 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
89 static void DetachObject ( vlc_object_t * );
90 static void PrintObject ( vlc_object_t *, const char * );
91 static void DumpStructure ( vlc_object_t *, int, char * );
92 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
93 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
95 static vlc_list_t * NewList ( int );
96 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
97 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
98 static int CountChildren ( vlc_object_t *, int );
99 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
101 static void vlc_object_destroy( vlc_object_t *p_this );
102 static void vlc_object_yield_locked( vlc_object_t *p_this );
104 /*****************************************************************************
105 * Local structure lock
106 *****************************************************************************/
107 static vlc_mutex_t structure_lock;
108 static vlc_object_internals_t global_internals;
110 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
111 int i_type, const char *psz_type )
114 vlc_object_internals_t *p_priv;
116 if( i_type == VLC_OBJECT_GLOBAL )
119 p_priv = &global_internals;
120 memset( p_priv, 0, sizeof( *p_priv ) );
124 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
128 p_new = (vlc_object_t *)(p_priv + 1);
131 p_new->p_internals = p_priv;
132 p_new->i_object_type = i_type;
133 p_new->psz_object_type = psz_type;
135 p_new->psz_object_name = NULL;
137 p_new->b_die = VLC_FALSE;
138 p_new->b_error = VLC_FALSE;
139 p_new->b_dead = VLC_FALSE;
140 p_priv->b_attached = VLC_FALSE;
141 p_new->b_force = VLC_FALSE;
143 p_new->psz_header = NULL;
145 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
146 p_new->i_flags |= OBJECT_FLAGS_NODBG;
147 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
148 p_new->i_flags |= OBJECT_FLAGS_QUIET;
149 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
150 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
152 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
154 if( !p_priv->p_vars )
156 if( i_type != VLC_OBJECT_GLOBAL )
161 if( i_type == VLC_OBJECT_GLOBAL )
163 /* If i_type is global, then p_new is actually p_libvlc_global */
164 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
165 p_new->p_libvlc = NULL;
167 p_libvlc_global->i_counter = 0;
168 p_new->i_object_id = 0;
170 p_libvlc_global->i_objects = 1;
171 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
172 p_libvlc_global->pp_objects[0] = p_new;
173 p_priv->b_attached = VLC_TRUE;
177 libvlc_global_data_t *p_libvlc_global = vlc_global();
178 if( i_type == VLC_OBJECT_LIBVLC )
180 p_new->p_libvlc = (libvlc_int_t*)p_new;
181 p_priv->b_attached = VLC_TRUE;
185 p_new->p_libvlc = p_this->p_libvlc;
188 vlc_mutex_lock( &structure_lock );
190 p_libvlc_global->i_counter++;
191 p_new->i_object_id = p_libvlc_global->i_counter;
193 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
194 * useless to try and recover anything if pp_objects gets smashed. */
195 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
198 vlc_mutex_unlock( &structure_lock );
201 p_priv->i_refcount = 1;
202 p_priv->pf_destructor = kVLCDestructor;
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( vod_t );
324 psz_type = "vod server";
327 i_size = sizeof( xml_t );
330 case VLC_OBJECT_OPENGL:
331 i_size = sizeof( vout_thread_t );
334 case VLC_OBJECT_ANNOUNCE:
335 i_size = sizeof( announce_handler_t );
336 psz_type = "announce";
338 case VLC_OBJECT_META_ENGINE:
339 i_size = sizeof( meta_engine_t );
340 psz_type = "meta engine";
342 case VLC_OBJECT_OSDMENU:
343 i_size = sizeof( osd_menu_t );
344 psz_type = "osd menu";
346 case VLC_OBJECT_INTERACTION:
347 i_size = sizeof( interaction_t );
348 psz_type = "interaction";
351 i_size = i_type > (int)sizeof(vlc_object_t)
352 ? i_type : (int)sizeof(vlc_object_t);
353 i_type = VLC_OBJECT_GENERIC;
354 psz_type = "generic";
358 return vlc_custom_create( p_this, i_size, i_type, psz_type );
363 ****************************************************************************
364 * Set the destructor of a vlc object
366 * This function sets the destructor of the vlc object. It will be called
367 * when the object is destroyed when the its refcount reaches 0.
368 * (It is called by the internal function vlc_object_destroy())
369 *****************************************************************************/
370 void __vlc_object_set_destructor( vlc_object_t *p_this,
371 vlc_destructor_t pf_destructor )
373 vlc_object_internals_t *p_priv = vlc_internals(p_this );
375 vlc_mutex_lock( &structure_lock );
376 p_priv->pf_destructor = pf_destructor;
377 vlc_mutex_unlock( &structure_lock );
381 ****************************************************************************
382 * Destroy a vlc object (Internal)
384 * This function destroys an object that has been previously allocated with
385 * vlc_object_create. The object's refcount must be zero and it must not be
386 * attached to other objects in any way.
387 *****************************************************************************/
388 static void vlc_object_destroy( vlc_object_t *p_this )
390 vlc_object_internals_t *p_priv = vlc_internals( p_this );
393 if( p_this->i_children )
398 "ERROR: cannot delete object (%i, %s) with %d children\n",
399 p_this->i_object_id, p_this->psz_object_name,
400 p_this->i_children );
402 for( i = 0; i < p_this->i_children; i++ )
405 "ERROR: Remaining children object "
406 "(id:%i, type:%s, name:%s)\n",
407 p_this->pp_children[i]->i_object_id,
408 p_this->pp_children[i]->psz_object_type,
409 p_this->pp_children[i]->psz_object_name );
415 if( p_this->p_parent )
418 "ERROR: cannot delete object (id:%i, type:%s, name:%s) "
419 "with a parent (id:%i, type:%s, name:%s)\n",
420 p_this->i_object_id, p_this->psz_object_type,
421 p_this->psz_object_name, p_this->p_parent->i_object_id,
422 p_this->p_parent->psz_object_type,
423 p_this->p_parent->psz_object_name );
428 /* Call the custom "subclass" destructor */
429 if( p_priv->pf_destructor )
430 p_priv->pf_destructor( p_this );
433 /* Destroy the associated variables, starting from the end so that
434 * no memmove calls have to be done. */
435 while( p_priv->i_vars )
437 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
440 free( p_priv->p_vars );
441 vlc_mutex_destroy( &p_priv->var_lock );
443 free( p_this->psz_header );
445 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
447 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
450 if( p_global->i_objects > 0 )
453 for( i = 0; i < p_global->i_objects; i++ )
455 /* We are leaking this object */
457 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
458 p_global->pp_objects[i]->i_object_id,
459 p_global->pp_objects[i]->psz_object_type,
460 p_global->pp_objects[i]->psz_object_name );
463 /* Strongly abort, cause we want these to be fixed */
467 /* We are the global object ... no need to lock. */
468 free( p_global->pp_objects );
469 p_global->pp_objects = NULL;
471 vlc_mutex_destroy( &structure_lock );
474 #if defined(WIN32) || defined(UNDER_CE)
475 /* if object has an associated thread, close it now */
476 if( p_priv->thread_id.hThread )
477 CloseHandle(p_priv->thread_id.hThread);
480 vlc_mutex_destroy( &p_this->object_lock );
481 vlc_cond_destroy( &p_this->object_wait );
482 vlc_spin_destroy( &p_priv->spin );
483 if( p_priv->pipes[1] != -1 )
484 close( p_priv->pipes[1] );
485 if( p_priv->pipes[0] != -1 )
486 close( p_priv->pipes[0] );
488 /* global is not dynamically allocated by vlc_object_create */
489 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
494 /** Inter-object signaling */
496 void __vlc_object_lock( vlc_object_t *obj )
498 vlc_mutex_lock( &obj->object_lock );
501 void __vlc_object_unlock( vlc_object_t *obj )
503 vlc_assert_locked( &obj->object_lock );
504 vlc_mutex_unlock( &obj->object_lock );
508 # include <winsock2.h>
509 # include <ws2tcpip.h>
512 * select()-able pipes emulated using Winsock
514 static int pipe (int fd[2])
517 int addrlen = sizeof (addr);
519 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
520 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
521 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
524 memset (&addr, 0, sizeof (addr));
525 addr.sin_family = AF_INET;
526 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
527 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
528 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
530 || connect (c, (PSOCKADDR)&addr, addrlen))
533 a = accept (l, NULL, NULL);
534 if (a == INVALID_SOCKET)
545 if (l != INVALID_SOCKET)
547 if (c != INVALID_SOCKET)
553 #define read( a, b, c ) recv (a, b, c, 0)
555 #define write( a, b, c ) send (a, b, c, 0)
557 #define close( a ) closesocket (a)
561 * Returns the readable end of a pipe that becomes readable once termination
562 * of the object is requested (vlc_object_kill()).
563 * This can be used to wake-up out of a select() or poll() event loop, such
564 * typically when doing network I/O.
566 * Note that the pipe will remain the same for the lifetime of the object.
567 * DO NOT read the pipe nor close it yourself. Ever.
569 * @param obj object that would be "killed"
570 * @return a readable pipe descriptor, or -1 on error.
572 int __vlc_object_waitpipe( vlc_object_t *obj )
574 int pfd[2] = { -1, -1 };
575 struct vlc_object_internals_t *internals = obj->p_internals;
576 vlc_bool_t killed = VLC_FALSE;
578 vlc_spin_lock (&internals->spin);
579 if (internals->pipes[0] == -1)
581 /* This can only ever happen if someone killed us without locking: */
582 assert (internals->pipes[1] == -1);
583 vlc_spin_unlock (&internals->spin);
588 vlc_spin_lock (&internals->spin);
589 if (internals->pipes[0] == -1)
591 internals->pipes[0] = pfd[0];
592 internals->pipes[1] = pfd[1];
593 pfd[0] = pfd[1] = -1;
597 vlc_spin_unlock (&internals->spin);
601 /* Race condition: vlc_object_kill() already invoked! */
604 vlc_spin_lock (&internals->spin);
605 fd = internals->pipes[1];
606 internals->pipes[1] = -1;
607 vlc_spin_unlock (&internals->spin);
609 msg_Dbg (obj, "waitpipe: object already dying");
614 /* Race condition: two threads call pipe() - unlikely */
620 return internals->pipes[0];
625 * Waits for the object to be signaled (using vlc_object_signal()).
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 true if the object is dying and should terminate.
631 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
633 vlc_assert_locked( &obj->object_lock );
634 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
640 * Waits for the object to be signaled (using vlc_object_signal()), or for
642 * If the object already has a signal pending, this function will return
643 * immediately. It is asserted that the caller holds the object lock.
645 * @return negative if the object is dying and should terminate,
646 * positive if the the object has been signaled but is not dying,
647 * 0 if timeout has been reached.
649 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
653 vlc_assert_locked( &obj->object_lock );
654 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
655 if( v == 0 ) /* signaled */
656 return obj->b_die ? -1 : 1;
662 * Checks whether an object has been "killed".
663 * The object lock must be held.
665 * Typical code for an object thread could be:
667 vlc_object_lock (self);
669 while (vlc_object_alive (self))
673 if (vlc_object_wait (self))
678 ...deinitialization...
679 vlc_object_unlock (self);
682 * @return true iff the object has not been killed yet
684 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
686 vlc_assert_locked( &obj->object_lock );
692 * Signals an object for which the lock is held.
694 void __vlc_object_signal_unlocked( vlc_object_t *obj )
696 vlc_assert_locked (&obj->object_lock);
697 vlc_cond_signal( &obj->object_wait );
702 * Requests termination of an object.
703 * If the object is LibVLC, also request to terminate all its children.
705 void __vlc_object_kill( vlc_object_t *p_this )
707 struct vlc_object_internals_t *internals = p_this->p_internals;
710 vlc_mutex_lock( &p_this->object_lock );
711 p_this->b_die = VLC_TRUE;
713 vlc_spin_lock (&internals->spin);
714 fd = internals->pipes[1];
715 internals->pipes[1] = -1;
716 vlc_spin_unlock (&internals->spin);
720 msg_Dbg (p_this, "waitpipe: object killed");
724 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
725 for( int i = 0; i < p_this->i_children ; i++ )
726 vlc_object_kill( p_this->pp_children[i] );
728 vlc_object_signal_unlocked( p_this );
729 vlc_mutex_unlock( &p_this->object_lock );
734 * find an object given its ID
736 * This function looks for the object whose i_object_id field is i_id. We
737 * use a dichotomy so that lookups are in log2(n).
738 *****************************************************************************/
739 void * vlc_object_get( int i_id )
742 vlc_object_t **pp_objects;
743 libvlc_global_data_t *p_libvlc_global = vlc_global();
745 vlc_mutex_lock( &structure_lock );
747 pp_objects = p_libvlc_global->pp_objects;
749 /* Perform our dichotomy */
750 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
752 i_middle = i_max / 2;
754 if( pp_objects[i_middle]->i_object_id > i_id )
758 else if( pp_objects[i_middle]->i_object_id < i_id )
762 pp_objects += i_middle;
767 /* This happens when there are only two remaining objects */
768 if( pp_objects[i_middle+1]->i_object_id == i_id )
770 vlc_object_yield_locked( pp_objects[i_middle+1] );
771 vlc_mutex_unlock( &structure_lock );
772 return pp_objects[i_middle+1];
779 vlc_object_yield_locked( pp_objects[i_middle] );
780 vlc_mutex_unlock( &structure_lock );
781 return pp_objects[i_middle];
786 /* this means that i_max == i_middle, and since we have already
787 * tested pp_objects[i_middle]), p_found is properly set. */
792 vlc_mutex_unlock( &structure_lock );
797 ****************************************************************************
798 * find a typed object and increment its refcount
799 *****************************************************************************
800 * This function recursively looks for a given object type. i_mode can be one
801 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
802 *****************************************************************************/
803 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
805 vlc_object_t *p_found;
807 vlc_mutex_lock( &structure_lock );
809 /* Avoid obvious freed object uses */
810 assert( p_this->p_internals->i_refcount > 0 );
812 /* If we are of the requested type ourselves, don't look further */
813 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
815 vlc_object_yield_locked( p_this );
816 vlc_mutex_unlock( &structure_lock );
820 /* Otherwise, recursively look for the object */
821 if( (i_mode & 0x000f) == FIND_ANYWHERE )
823 vlc_object_t *p_root = p_this;
826 while( p_root->p_parent != NULL &&
827 p_root != VLC_OBJECT( p_this->p_libvlc ) )
829 p_root = p_root->p_parent;
832 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
833 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
835 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
836 i_type, (i_mode & ~0x000f)|FIND_CHILD );
841 p_found = FindObject( p_this, i_type, i_mode );
844 vlc_mutex_unlock( &structure_lock );
850 ****************************************************************************
851 * find a named object and increment its refcount
852 *****************************************************************************
853 * This function recursively looks for a given object name. i_mode can be one
854 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
855 *****************************************************************************/
856 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
859 vlc_object_t *p_found;
861 vlc_mutex_lock( &structure_lock );
863 /* Avoid obvious freed object uses */
864 assert( p_this->p_internals->i_refcount > 0 );
866 /* If have the requested name ourselves, don't look further */
867 if( !(i_mode & FIND_STRICT)
868 && p_this->psz_object_name
869 && !strcmp( p_this->psz_object_name, psz_name ) )
871 vlc_object_yield_locked( p_this );
872 vlc_mutex_unlock( &structure_lock );
876 /* Otherwise, recursively look for the object */
877 if( (i_mode & 0x000f) == FIND_ANYWHERE )
879 vlc_object_t *p_root = p_this;
882 while( p_root->p_parent != NULL &&
883 p_root != VLC_OBJECT( p_this->p_libvlc ) )
885 p_root = p_root->p_parent;
888 p_found = FindObjectName( p_root, psz_name,
889 (i_mode & ~0x000f)|FIND_CHILD );
890 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
892 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
893 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
898 p_found = FindObjectName( p_this, psz_name, i_mode );
901 vlc_mutex_unlock( &structure_lock );
907 ****************************************************************************
908 * increment an object refcount
909 *****************************************************************************/
911 /* When the structure_lock is locked */
912 static void vlc_object_yield_locked( vlc_object_t *p_this )
914 vlc_assert_locked (&structure_lock);
916 /* Avoid obvious freed object uses */
917 assert( p_this->p_internals->i_refcount > 0 );
919 /* Increment the counter */
920 p_this->p_internals->i_refcount++;
923 /* Public function */
924 void __vlc_object_yield( vlc_object_t *p_this )
926 vlc_mutex_lock( &structure_lock );
927 vlc_object_yield_locked( p_this );
928 vlc_mutex_unlock( &structure_lock );
932 /*****************************************************************************
933 * decrement an object refcount
934 * And destroy the object if its refcount reach zero.
935 *****************************************************************************/
936 void __vlc_object_release( vlc_object_t *p_this )
938 vlc_bool_t b_should_destroy;
940 vlc_mutex_lock( &structure_lock );
942 assert( p_this->p_internals->i_refcount > 0 );
943 p_this->p_internals->i_refcount--;
944 b_should_destroy = (p_this->p_internals->i_refcount == 0);
946 if( b_should_destroy )
948 /* Make sure this object can't be obtained via vlc_find_object now that
950 libvlc_global_data_t *p_libvlc_global = vlc_global();
953 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
954 * useless to try and recover anything if pp_objects gets smashed. */
955 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
956 p_libvlc_global->i_objects );
957 REMOVE_ELEM( p_libvlc_global->pp_objects,
958 p_libvlc_global->i_objects, i_index );
961 vlc_mutex_unlock( &structure_lock );
963 if( b_should_destroy )
964 vlc_object_destroy( p_this );
968 ****************************************************************************
969 * attach object to a parent object
970 *****************************************************************************
971 * This function sets p_this as a child of p_parent, and p_parent as a parent
972 * of p_this. This link can be undone using vlc_object_detach.
973 *****************************************************************************/
974 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
976 if( !p_this ) return;
978 vlc_mutex_lock( &structure_lock );
980 /* Avoid obvious freed object uses */
981 assert( p_this->p_internals->i_refcount > 0 );
983 /* Attach the parent to its child */
984 p_this->p_parent = p_parent;
986 /* Attach the child to its parent */
987 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
988 p_parent->i_children, p_this );
990 /* Climb up the tree to see whether we are connected with the root */
991 if( p_parent->p_internals->b_attached )
993 SetAttachment( p_this, VLC_TRUE );
996 vlc_mutex_unlock( &structure_lock );
1000 ****************************************************************************
1001 * detach object from its parent
1002 *****************************************************************************
1003 * This function removes all links between an object and its parent.
1004 *****************************************************************************/
1005 void __vlc_object_detach( vlc_object_t *p_this )
1007 if( !p_this ) return;
1009 vlc_mutex_lock( &structure_lock );
1011 /* Avoid obvious freed object uses */
1012 assert( p_this->p_internals->i_refcount > 0 );
1014 if( !p_this->p_parent )
1016 msg_Err( p_this, "object is not attached" );
1017 vlc_mutex_unlock( &structure_lock );
1021 /* Climb up the tree to see whether we are connected with the root */
1022 if( p_this->p_parent->p_internals->b_attached )
1024 SetAttachment( p_this, VLC_FALSE );
1027 DetachObject( p_this );
1028 vlc_mutex_unlock( &structure_lock );
1033 ****************************************************************************
1034 * find a list typed objects and increment their refcount
1035 *****************************************************************************
1036 * This function recursively looks for a given object type. i_mode can be one
1037 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
1038 *****************************************************************************/
1039 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
1042 vlc_object_t **pp_current, **pp_end;
1043 int i_count = 0, i_index = 0;
1044 libvlc_global_data_t *p_libvlc_global = vlc_global();
1046 vlc_mutex_lock( &structure_lock );
1048 /* Avoid obvious freed object uses */
1049 assert( p_this->p_internals->i_refcount > 0 );
1051 /* Look for the objects */
1052 switch( i_mode & 0x000f )
1055 pp_current = p_libvlc_global->pp_objects;
1056 pp_end = pp_current + p_libvlc_global->i_objects;
1058 for( ; pp_current < pp_end ; pp_current++ )
1060 if( (*pp_current)->p_internals->b_attached
1061 && (*pp_current)->i_object_type == i_type )
1067 p_list = NewList( i_count );
1068 pp_current = p_libvlc_global->pp_objects;
1070 for( ; pp_current < pp_end ; pp_current++ )
1072 if( (*pp_current)->p_internals->b_attached
1073 && (*pp_current)->i_object_type == i_type )
1075 ListReplace( p_list, *pp_current, i_index );
1076 if( i_index < i_count ) i_index++;
1082 i_count = CountChildren( p_this, i_type );
1083 p_list = NewList( i_count );
1085 /* Check allocation was successful */
1086 if( p_list->i_count != i_count )
1088 msg_Err( p_this, "list allocation failed!" );
1089 p_list->i_count = 0;
1093 p_list->i_count = 0;
1094 ListChildren( p_list, p_this, i_type );
1098 msg_Err( p_this, "unimplemented!" );
1099 p_list = NewList( 0 );
1103 vlc_mutex_unlock( &structure_lock );
1108 /*****************************************************************************
1109 * DumpCommand: print the current vlc structure
1110 *****************************************************************************
1111 * This function prints either an ASCII tree showing the connections between
1112 * vlc objects, and additional information such as their refcount, thread ID,
1113 * etc. (command "tree"), or the same data as a simple list (command "list").
1114 *****************************************************************************/
1115 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1116 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1118 libvlc_global_data_t *p_libvlc_global = vlc_global();
1120 (void)oldval; (void)p_data;
1121 if( *psz_cmd == 'l' )
1123 vlc_mutex_lock( &structure_lock );
1125 vlc_object_t **pp_current, **pp_end;
1127 pp_current = p_libvlc_global->pp_objects;
1128 pp_end = pp_current + p_libvlc_global->i_objects;
1130 for( ; pp_current < pp_end ; pp_current++ )
1132 if( (*pp_current)->p_internals->b_attached )
1134 PrintObject( *pp_current, "" );
1138 printf( " o %.8i %s (not attached)\n",
1139 (*pp_current)->i_object_id,
1140 (*pp_current)->psz_object_type );
1144 vlc_mutex_unlock( &structure_lock );
1148 vlc_object_t *p_object = NULL;
1150 if( *newval.psz_string )
1153 int i_id = strtol( newval.psz_string, &end, 0 );
1154 if( end != newval.psz_string )
1155 p_object = vlc_object_get( i_id );
1158 /* try using the object's name to find it */
1159 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1162 /* Look in p_libvlc's children tree */
1163 p_object = vlc_object_find_name( p_libvlc,
1166 vlc_object_release( p_libvlc );
1170 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1171 p_object = vlc_object_find_name( p_this,
1183 vlc_mutex_lock( &structure_lock );
1185 if( *psz_cmd == 't' )
1187 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1190 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1193 DumpStructure( p_object, 0, psz_foo );
1195 else if( *psz_cmd == 'v' )
1200 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1202 PrintObject( p_object, "" );
1204 if( !p_object->p_internals->i_vars )
1205 printf( " `-o No variables\n" );
1206 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1208 variable_t *p_var = p_object->p_internals->p_vars + i;
1210 const char *psz_type = "unknown";
1211 switch( p_var->i_type & VLC_VAR_TYPE )
1213 #define MYCASE( type, nice ) \
1214 case VLC_VAR_ ## type: \
1217 MYCASE( VOID, "void" );
1218 MYCASE( BOOL, "bool" );
1219 MYCASE( INTEGER, "integer" );
1220 MYCASE( HOTKEY, "hotkey" );
1221 MYCASE( STRING, "string" );
1222 MYCASE( MODULE, "module" );
1223 MYCASE( FILE, "file" );
1224 MYCASE( DIRECTORY, "directory" );
1225 MYCASE( VARIABLE, "variable" );
1226 MYCASE( FLOAT, "float" );
1227 MYCASE( TIME, "time" );
1228 MYCASE( ADDRESS, "address" );
1229 MYCASE( MUTEX, "mutex" );
1230 MYCASE( LIST, "list" );
1233 printf( " %c-o \"%s\" (%s",
1234 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1235 p_var->psz_name, psz_type );
1236 if( p_var->psz_text )
1237 printf( ", %s", p_var->psz_text );
1239 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1240 printf( ", command" );
1241 if( p_var->i_entries )
1242 printf( ", %d callbacks", p_var->i_entries );
1243 switch( p_var->i_type & 0x00f0 )
1249 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1251 case VLC_VAR_INTEGER:
1252 printf( ": %d", p_var->val.i_int );
1254 case VLC_VAR_STRING:
1255 printf( ": \"%s\"", p_var->val.psz_string );
1258 printf( ": %f", p_var->val.f_float );
1261 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1263 case VLC_VAR_ADDRESS:
1264 printf( ": %p", p_var->val.p_address );
1274 vlc_mutex_unlock( &structure_lock );
1276 if( *newval.psz_string )
1278 vlc_object_release( p_object );
1285 /*****************************************************************************
1286 * vlc_list_release: free a list previously allocated by vlc_list_find
1287 *****************************************************************************
1288 * This function decreases the refcount of all objects in the list and
1290 *****************************************************************************/
1291 void vlc_list_release( vlc_list_t *p_list )
1295 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1297 vlc_object_release( p_list->p_values[i_index].p_object );
1300 free( p_list->p_values );
1304 /* Following functions are local */
1306 /*****************************************************************************
1307 * FindIndex: find the index of an object in an array of objects
1308 *****************************************************************************
1309 * This function assumes that p_this can be found in pp_objects. It will not
1310 * crash if p_this cannot be found, but will return a wrong value. It is your
1311 * duty to check the return value if you are not certain that the object could
1312 * be found for sure.
1313 *****************************************************************************/
1314 static int FindIndex( vlc_object_t *p_this,
1315 vlc_object_t **pp_objects, int i_count )
1317 int i_middle = i_count / 2;
1324 if( pp_objects[i_middle] == p_this )
1334 /* We take advantage of the sorted array */
1335 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1337 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1338 i_count - i_middle );
1342 return FindIndex( p_this, pp_objects, i_middle );
1346 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1349 vlc_object_t *p_tmp;
1351 switch( i_mode & 0x000f )
1354 p_tmp = p_this->p_parent;
1357 if( p_tmp->i_object_type == i_type )
1359 vlc_object_yield_locked( p_tmp );
1364 return FindObject( p_tmp, i_type, i_mode );
1370 for( i = p_this->i_children; i--; )
1372 p_tmp = p_this->pp_children[i];
1373 if( p_tmp->i_object_type == i_type )
1375 vlc_object_yield_locked( p_tmp );
1378 else if( p_tmp->i_children )
1380 p_tmp = FindObject( p_tmp, i_type, i_mode );
1390 /* Handled in vlc_object_find */
1397 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1398 const char *psz_name,
1402 vlc_object_t *p_tmp;
1404 switch( i_mode & 0x000f )
1407 p_tmp = p_this->p_parent;
1410 if( p_tmp->psz_object_name
1411 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1413 vlc_object_yield_locked( p_tmp );
1418 return FindObjectName( p_tmp, psz_name, i_mode );
1424 for( i = p_this->i_children; i--; )
1426 p_tmp = p_this->pp_children[i];
1427 if( p_tmp->psz_object_name
1428 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1430 vlc_object_yield_locked( p_tmp );
1433 else if( p_tmp->i_children )
1435 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1445 /* Handled in vlc_object_find */
1452 static void DetachObject( vlc_object_t *p_this )
1454 vlc_object_t *p_parent = p_this->p_parent;
1457 /* Remove p_this's parent */
1458 p_this->p_parent = NULL;
1460 /* Remove all of p_parent's children which are p_this */
1461 for( i_index = p_parent->i_children ; i_index-- ; )
1463 if( p_parent->pp_children[i_index] == p_this )
1465 p_parent->i_children--;
1466 for( i = i_index ; i < p_parent->i_children ; i++ )
1468 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1473 if( p_parent->i_children )
1475 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1476 p_parent->i_children * sizeof(vlc_object_t *) );
1480 free( p_parent->pp_children );
1481 p_parent->pp_children = NULL;
1485 /*****************************************************************************
1486 * SetAttachment: recursively set the b_attached flag of a subtree.
1487 *****************************************************************************
1488 * This function is used by the attach and detach functions to propagate
1489 * the b_attached flag in a subtree.
1490 *****************************************************************************/
1491 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1495 for( i_index = p_this->i_children ; i_index-- ; )
1497 SetAttachment( p_this->pp_children[i_index], b_attached );
1500 p_this->p_internals->b_attached = b_attached;
1503 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1505 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1509 if( p_this->psz_object_name )
1511 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1513 psz_name[48] = '\"';
1516 psz_children[0] = '\0';
1517 switch( p_this->i_children )
1522 strcpy( psz_children, ", 1 child" );
1525 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1529 psz_refcount[0] = '\0';
1530 if( p_this->p_internals->i_refcount > 0 )
1531 snprintf( psz_refcount, 19, ", refcount %u",
1532 p_this->p_internals->i_refcount );
1534 psz_thread[0] = '\0';
1535 if( p_this->p_internals->b_thread )
1536 snprintf( psz_thread, 29, " (thread %u)",
1537 #if defined(WIN32) || defined(UNDER_CE)
1538 (unsigned)p_this->p_internals->thread_id.id );
1540 (unsigned)p_this->p_internals->thread_id );
1543 psz_parent[0] = '\0';
1544 if( p_this->p_parent )
1545 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1547 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1548 p_this->i_object_id, p_this->psz_object_type,
1549 psz_name, psz_thread, psz_refcount, psz_children,
1553 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1556 char i_back = psz_foo[i_level];
1557 psz_foo[i_level] = '\0';
1559 PrintObject( p_this, psz_foo );
1561 psz_foo[i_level] = i_back;
1563 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1565 msg_Warn( p_this, "structure tree is too deep" );
1569 for( i = 0 ; i < p_this->i_children ; i++ )
1573 psz_foo[i_level-1] = ' ';
1575 if( psz_foo[i_level-2] == '`' )
1577 psz_foo[i_level-2] = ' ';
1581 if( i == p_this->i_children - 1 )
1583 psz_foo[i_level] = '`';
1587 psz_foo[i_level] = '|';
1590 psz_foo[i_level+1] = '-';
1591 psz_foo[i_level+2] = '\0';
1593 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1597 static vlc_list_t * NewList( int i_count )
1599 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1600 if( p_list == NULL )
1605 p_list->i_count = i_count;
1609 p_list->p_values = NULL;
1613 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1614 if( p_list->p_values == NULL )
1616 p_list->i_count = 0;
1623 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1626 if( p_list == NULL || i_index >= p_list->i_count )
1631 vlc_object_yield_locked( p_object );
1633 p_list->p_values[i_index].p_object = p_object;
1638 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1640 if( p_list == NULL )
1645 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1646 * sizeof( vlc_value_t ) );
1647 if( p_list->p_values == NULL )
1649 p_list->i_count = 0;
1653 vlc_object_yield_locked( p_object );
1655 p_list->p_values[p_list->i_count].p_object = p_object;
1661 static int CountChildren( vlc_object_t *p_this, int i_type )
1663 vlc_object_t *p_tmp;
1666 for( i = 0; i < p_this->i_children; i++ )
1668 p_tmp = p_this->pp_children[i];
1670 if( p_tmp->i_object_type == i_type )
1675 if( p_tmp->i_children )
1677 i_count += CountChildren( p_tmp, i_type );
1684 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1686 vlc_object_t *p_tmp;
1689 for( i = 0; i < p_this->i_children; i++ )
1691 p_tmp = p_this->pp_children[i];
1693 if( p_tmp->i_object_type == i_type )
1695 ListReplace( p_list, p_tmp, p_list->i_count++ );
1698 if( p_tmp->i_children )
1700 ListChildren( p_list, p_tmp, i_type );