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";
306 i_size = sizeof(spu_t);
307 psz_type = "subpicture";
309 case VLC_OBJECT_AOUT:
310 i_size = sizeof(aout_instance_t);
311 psz_type = "audio output";
313 case VLC_OBJECT_SOUT:
314 i_size = sizeof(sout_instance_t);
315 psz_type = "stream output";
318 i_size = sizeof( vlm_t );
319 psz_type = "vlm dameon";
322 i_size = sizeof( xml_t );
325 case VLC_OBJECT_OPENGL:
326 i_size = sizeof( vout_thread_t );
329 case VLC_OBJECT_ANNOUNCE:
330 i_size = sizeof( announce_handler_t );
331 psz_type = "announce";
333 case VLC_OBJECT_META_ENGINE:
334 i_size = sizeof( meta_engine_t );
335 psz_type = "meta engine";
337 case VLC_OBJECT_OSDMENU:
338 i_size = sizeof( osd_menu_t );
339 psz_type = "osd menu";
341 case VLC_OBJECT_INTERACTION:
342 i_size = sizeof( interaction_t );
343 psz_type = "interaction";
346 i_size = i_type > (int)sizeof(vlc_object_t)
347 ? i_type : (int)sizeof(vlc_object_t);
348 i_type = VLC_OBJECT_GENERIC;
349 psz_type = "generic";
353 return vlc_custom_create( p_this, i_size, i_type, psz_type );
358 ****************************************************************************
359 * Set the destructor of a vlc object
361 * This function sets the destructor of the vlc object. It will be called
362 * when the object is destroyed when the its refcount reaches 0.
363 * (It is called by the internal function vlc_object_destroy())
364 *****************************************************************************/
365 void __vlc_object_set_destructor( vlc_object_t *p_this,
366 vlc_destructor_t pf_destructor )
368 vlc_object_internals_t *p_priv = vlc_internals(p_this );
370 vlc_mutex_lock( &structure_lock );
371 p_priv->pf_destructor = pf_destructor;
372 vlc_mutex_unlock( &structure_lock );
376 ****************************************************************************
377 * Destroy a vlc object (Internal)
379 * This function destroys an object that has been previously allocated with
380 * vlc_object_create. The object's refcount must be zero and it must not be
381 * attached to other objects in any way.
382 *****************************************************************************/
383 static void vlc_object_destroy( vlc_object_t *p_this )
385 vlc_object_internals_t *p_priv = vlc_internals( p_this );
387 /* Automatically detach the object from its parents */
388 if( p_this->p_parent ) vlc_object_detach( p_this );
391 /* Send a kill to the object's thread if applicable */
392 vlc_object_kill( p_this );
394 /* If we are running on a thread, wait until it ends */
395 if( p_priv->b_thread )
396 vlc_thread_join( p_this );
398 /* Call the custom "subclass" destructor */
399 if( p_priv->pf_destructor )
400 p_priv->pf_destructor( p_this );
403 if( p_this->i_children )
408 "ERROR: cannot delete object (%i, %s) with %d children\n",
409 p_this->i_object_id, p_this->psz_object_name,
410 p_this->i_children );
412 for( i = 0; i < p_this->i_children; i++ )
415 "ERROR: Remaining children object "
416 "(id:%i, type:%s, name:%s)\n",
417 p_this->pp_children[i]->i_object_id,
418 p_this->pp_children[i]->psz_object_type,
419 p_this->pp_children[i]->psz_object_name );
425 /* Destroy the associated variables, starting from the end so that
426 * no memmove calls have to be done. */
427 while( p_priv->i_vars )
429 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
432 free( p_priv->p_vars );
433 vlc_mutex_destroy( &p_priv->var_lock );
435 free( p_this->psz_header );
437 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
439 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
442 if( p_global->i_objects > 0 )
445 for( i = 0; i < p_global->i_objects; i++ )
447 /* We are leaking this object */
449 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
450 p_global->pp_objects[i]->i_object_id,
451 p_global->pp_objects[i]->psz_object_type,
452 p_global->pp_objects[i]->psz_object_name );
455 /* Strongly abort, cause we want these to be fixed */
459 /* We are the global object ... no need to lock. */
460 free( p_global->pp_objects );
461 p_global->pp_objects = NULL;
463 vlc_mutex_destroy( &structure_lock );
466 #if defined(WIN32) || defined(UNDER_CE)
467 /* if object has an associated thread, close it now */
468 if( p_priv->thread_id.hThread )
469 CloseHandle(p_priv->thread_id.hThread);
472 vlc_mutex_destroy( &p_this->object_lock );
473 vlc_cond_destroy( &p_this->object_wait );
474 vlc_spin_destroy( &p_priv->spin );
475 if( p_priv->pipes[1] != -1 )
476 close( p_priv->pipes[1] );
477 if( p_priv->pipes[0] != -1 )
478 close( p_priv->pipes[0] );
480 /* global is not dynamically allocated by vlc_object_create */
481 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
486 /** Inter-object signaling */
488 void __vlc_object_lock( vlc_object_t *obj )
490 vlc_mutex_lock( &obj->object_lock );
493 void __vlc_object_unlock( vlc_object_t *obj )
495 vlc_assert_locked( &obj->object_lock );
496 vlc_mutex_unlock( &obj->object_lock );
500 # include <winsock2.h>
501 # include <ws2tcpip.h>
504 * select()-able pipes emulated using Winsock
506 static int pipe (int fd[2])
509 int addrlen = sizeof (addr);
511 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
512 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
513 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
516 memset (&addr, 0, sizeof (addr));
517 addr.sin_family = AF_INET;
518 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
519 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
520 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
522 || connect (c, (PSOCKADDR)&addr, addrlen))
525 a = accept (l, NULL, NULL);
526 if (a == INVALID_SOCKET)
537 if (l != INVALID_SOCKET)
539 if (c != INVALID_SOCKET)
545 #define read( a, b, c ) recv (a, b, c, 0)
547 #define write( a, b, c ) send (a, b, c, 0)
549 #define close( a ) closesocket (a)
553 * Returns the readable end of a pipe that becomes readable once termination
554 * of the object is requested (vlc_object_kill()).
555 * This can be used to wake-up out of a select() or poll() event loop, such
556 * typically when doing network I/O.
558 * Note that the pipe will remain the same for the lifetime of the object.
559 * DO NOT read the pipe nor close it yourself. Ever.
561 * @param obj object that would be "killed"
562 * @return a readable pipe descriptor, or -1 on error.
564 int __vlc_object_waitpipe( vlc_object_t *obj )
566 int pfd[2] = { -1, -1 };
567 struct vlc_object_internals_t *internals = obj->p_internals;
568 vlc_bool_t killed = VLC_FALSE;
570 vlc_spin_lock (&internals->spin);
571 if (internals->pipes[0] == -1)
573 /* This can only ever happen if someone killed us without locking: */
574 assert (internals->pipes[1] == -1);
575 vlc_spin_unlock (&internals->spin);
580 vlc_spin_lock (&internals->spin);
581 if (internals->pipes[0] == -1)
583 internals->pipes[0] = pfd[0];
584 internals->pipes[1] = pfd[1];
585 pfd[0] = pfd[1] = -1;
589 vlc_spin_unlock (&internals->spin);
593 /* Race condition: vlc_object_kill() already invoked! */
596 vlc_spin_lock (&internals->spin);
597 fd = internals->pipes[1];
598 internals->pipes[1] = -1;
599 vlc_spin_unlock (&internals->spin);
601 msg_Dbg (obj, "waitpipe: object already dying");
606 /* Race condition: two threads call pipe() - unlikely */
612 return internals->pipes[0];
617 * Waits for the object to be signaled (using vlc_object_signal()).
618 * If the object already has a signal pending, this function will return
619 * immediately. It is asserted that the caller holds the object lock.
621 * @return true if the object is dying and should terminate.
623 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
625 vlc_assert_locked( &obj->object_lock );
626 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
632 * Waits for the object to be signaled (using vlc_object_signal()), or for
634 * If the object already has a signal pending, this function will return
635 * immediately. It is asserted that the caller holds the object lock.
637 * @return negative if the object is dying and should terminate,
638 * positive if the the object has been signaled but is not dying,
639 * 0 if timeout has been reached.
641 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
645 vlc_assert_locked( &obj->object_lock );
646 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
647 if( v == 0 ) /* signaled */
648 return obj->b_die ? -1 : 1;
654 * Checks whether an object has been "killed".
655 * The object lock must be held.
657 * Typical code for an object thread could be:
659 vlc_object_lock (self);
661 while (vlc_object_alive (self))
665 if (vlc_object_wait (self))
670 ...deinitialization...
671 vlc_object_unlock (self);
674 * @return true iff the object has not been killed yet
676 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
678 vlc_assert_locked( &obj->object_lock );
684 * Signals an object for which the lock is held.
686 void __vlc_object_signal_unlocked( vlc_object_t *obj )
688 vlc_assert_locked (&obj->object_lock);
689 vlc_cond_signal( &obj->object_wait );
694 * Requests termination of an object.
695 * If the object is LibVLC, also request to terminate all its children.
697 void __vlc_object_kill( vlc_object_t *p_this )
699 struct vlc_object_internals_t *internals = p_this->p_internals;
702 vlc_mutex_lock( &p_this->object_lock );
703 p_this->b_die = VLC_TRUE;
705 vlc_spin_lock (&internals->spin);
706 fd = internals->pipes[1];
707 internals->pipes[1] = -1;
708 vlc_spin_unlock (&internals->spin);
712 msg_Dbg (p_this, "waitpipe: object killed");
716 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
717 for( int i = 0; i < p_this->i_children ; i++ )
718 vlc_object_kill( p_this->pp_children[i] );
720 vlc_object_signal_unlocked( p_this );
721 vlc_mutex_unlock( &p_this->object_lock );
726 * find an object given its ID
728 * This function looks for the object whose i_object_id field is i_id. We
729 * use a dichotomy so that lookups are in log2(n).
730 *****************************************************************************/
731 void * vlc_object_get( int i_id )
734 vlc_object_t **pp_objects;
735 libvlc_global_data_t *p_libvlc_global = vlc_global();
737 vlc_mutex_lock( &structure_lock );
739 pp_objects = p_libvlc_global->pp_objects;
741 /* Perform our dichotomy */
742 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
744 i_middle = i_max / 2;
746 if( pp_objects[i_middle]->i_object_id > i_id )
750 else if( pp_objects[i_middle]->i_object_id < i_id )
754 pp_objects += i_middle;
759 /* This happens when there are only two remaining objects */
760 if( pp_objects[i_middle+1]->i_object_id == i_id
761 && pp_objects[i_middle+1]->p_internals->i_refcount > 0 )
763 vlc_object_yield_locked( pp_objects[i_middle+1] );
764 vlc_mutex_unlock( &structure_lock );
765 return pp_objects[i_middle+1];
770 else if( pp_objects[i_middle]->p_internals->i_refcount > 0 )
772 vlc_object_yield_locked( pp_objects[i_middle] );
773 vlc_mutex_unlock( &structure_lock );
774 return pp_objects[i_middle];
779 /* this means that i_max == i_middle, and since we have already
780 * tested pp_objects[i_middle]), p_found is properly set. */
785 vlc_mutex_unlock( &structure_lock );
790 ****************************************************************************
791 * find a typed object and increment its refcount
792 *****************************************************************************
793 * This function recursively looks for a given object type. i_mode can be one
794 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
795 *****************************************************************************/
796 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
798 vlc_object_t *p_found;
800 vlc_mutex_lock( &structure_lock );
802 /* If we are of the requested type ourselves, don't look further */
803 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type
804 && p_this->p_internals->i_refcount > 0 )
806 vlc_object_yield_locked( p_this );
807 vlc_mutex_unlock( &structure_lock );
811 /* Otherwise, recursively look for the object */
812 if( (i_mode & 0x000f) == FIND_ANYWHERE )
814 vlc_object_t *p_root = p_this;
817 while( p_root->p_parent != NULL &&
818 p_root != VLC_OBJECT( p_this->p_libvlc ) )
820 p_root = p_root->p_parent;
823 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
824 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
826 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
827 i_type, (i_mode & ~0x000f)|FIND_CHILD );
832 p_found = FindObject( p_this, i_type, i_mode );
835 vlc_mutex_unlock( &structure_lock );
841 ****************************************************************************
842 * find a named object and increment its refcount
843 *****************************************************************************
844 * This function recursively looks for a given object name. i_mode can be one
845 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
846 *****************************************************************************/
847 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
850 vlc_object_t *p_found;
852 vlc_mutex_lock( &structure_lock );
854 /* Avoid obvious freed object uses */
855 assert( p_this->p_internals->i_refcount > 0 );
857 /* If have the requested name ourselves, don't look further */
858 if( !(i_mode & FIND_STRICT)
859 && p_this->psz_object_name
860 && !strcmp( p_this->psz_object_name, psz_name )
861 && p_this->p_internals->i_refcount > 0 )
863 vlc_object_yield_locked( p_this );
864 vlc_mutex_unlock( &structure_lock );
868 /* Otherwise, recursively look for the object */
869 if( (i_mode & 0x000f) == FIND_ANYWHERE )
871 vlc_object_t *p_root = p_this;
874 while( p_root->p_parent != NULL &&
875 p_root != VLC_OBJECT( p_this->p_libvlc ) )
877 p_root = p_root->p_parent;
880 p_found = FindObjectName( p_root, psz_name,
881 (i_mode & ~0x000f)|FIND_CHILD );
882 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
884 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
885 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
890 p_found = FindObjectName( p_this, psz_name, i_mode );
893 vlc_mutex_unlock( &structure_lock );
899 ****************************************************************************
900 * increment an object refcount
901 *****************************************************************************/
903 /* When the structure_lock is locked */
904 static void vlc_object_yield_locked( vlc_object_t *p_this )
906 vlc_assert_locked (&structure_lock);
908 /* Avoid obvious freed object uses */
909 assert( p_this->p_internals->i_refcount > 0 );
911 /* Increment the counter */
912 p_this->p_internals->i_refcount++;
915 /* Public function */
916 void __vlc_object_yield( vlc_object_t *p_this )
918 vlc_mutex_lock( &structure_lock );
919 vlc_object_yield_locked( p_this );
920 vlc_mutex_unlock( &structure_lock );
924 /*****************************************************************************
925 * decrement an object refcount
926 * And destroy the object if its refcount reach zero.
927 *****************************************************************************/
928 void __vlc_object_release( vlc_object_t *p_this )
930 vlc_bool_t b_should_destroy;
932 vlc_mutex_lock( &structure_lock );
934 assert( p_this->p_internals->i_refcount > 0 );
935 p_this->p_internals->i_refcount--;
936 b_should_destroy = (p_this->p_internals->i_refcount == 0);
938 if( b_should_destroy )
940 /* Make sure this object can't be obtained via vlc_find_object now that
942 libvlc_global_data_t *p_libvlc_global = vlc_global();
945 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
946 * useless to try and recover anything if pp_objects gets smashed. */
947 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
948 p_libvlc_global->i_objects );
949 REMOVE_ELEM( p_libvlc_global->pp_objects,
950 p_libvlc_global->i_objects, i_index );
953 vlc_mutex_unlock( &structure_lock );
955 if( b_should_destroy )
956 vlc_object_destroy( p_this );
960 ****************************************************************************
961 * attach object to a parent object
962 *****************************************************************************
963 * This function sets p_this as a child of p_parent, and p_parent as a parent
964 * of p_this. This link can be undone using vlc_object_detach.
965 *****************************************************************************/
966 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
968 if( !p_this ) return;
970 vlc_mutex_lock( &structure_lock );
972 /* Avoid obvious freed object uses */
973 assert( p_this->p_internals->i_refcount > 0 );
975 /* Attach the parent to its child */
976 p_this->p_parent = p_parent;
978 /* Attach the child to its parent */
979 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
980 p_parent->i_children, p_this );
982 /* Climb up the tree to see whether we are connected with the root */
983 if( p_parent->p_internals->b_attached )
985 SetAttachment( p_this, VLC_TRUE );
988 vlc_mutex_unlock( &structure_lock );
992 ****************************************************************************
993 * detach object from its parent
994 *****************************************************************************
995 * This function removes all links between an object and its parent.
996 *****************************************************************************/
997 void __vlc_object_detach( vlc_object_t *p_this )
999 if( !p_this ) return;
1001 vlc_mutex_lock( &structure_lock );
1003 if( !p_this->p_parent )
1005 msg_Err( p_this, "object is not attached" );
1006 vlc_mutex_unlock( &structure_lock );
1010 /* Climb up the tree to see whether we are connected with the root */
1011 if( p_this->p_parent->p_internals->b_attached )
1013 SetAttachment( p_this, VLC_FALSE );
1016 DetachObject( p_this );
1017 vlc_mutex_unlock( &structure_lock );
1022 ****************************************************************************
1023 * find a list typed objects and increment their refcount
1024 *****************************************************************************
1025 * This function recursively looks for a given object type. i_mode can be one
1026 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
1027 *****************************************************************************/
1028 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
1031 vlc_object_t **pp_current, **pp_end;
1032 int i_count = 0, i_index = 0;
1033 libvlc_global_data_t *p_libvlc_global = vlc_global();
1035 vlc_mutex_lock( &structure_lock );
1037 /* Look for the objects */
1038 switch( i_mode & 0x000f )
1041 pp_current = p_libvlc_global->pp_objects;
1042 pp_end = pp_current + p_libvlc_global->i_objects;
1044 for( ; pp_current < pp_end ; pp_current++ )
1046 if( (*pp_current)->p_internals->b_attached
1047 && (*pp_current)->i_object_type == i_type )
1053 p_list = NewList( i_count );
1054 pp_current = p_libvlc_global->pp_objects;
1056 for( ; pp_current < pp_end ; pp_current++ )
1058 if( (*pp_current)->p_internals->b_attached
1059 && (*pp_current)->i_object_type == i_type )
1061 ListReplace( p_list, *pp_current, i_index );
1062 if( i_index < i_count ) i_index++;
1068 i_count = CountChildren( p_this, i_type );
1069 p_list = NewList( i_count );
1071 /* Check allocation was successful */
1072 if( p_list->i_count != i_count )
1074 msg_Err( p_this, "list allocation failed!" );
1075 p_list->i_count = 0;
1079 p_list->i_count = 0;
1080 ListChildren( p_list, p_this, i_type );
1084 msg_Err( p_this, "unimplemented!" );
1085 p_list = NewList( 0 );
1089 vlc_mutex_unlock( &structure_lock );
1094 /*****************************************************************************
1095 * DumpCommand: print the current vlc structure
1096 *****************************************************************************
1097 * This function prints either an ASCII tree showing the connections between
1098 * vlc objects, and additional information such as their refcount, thread ID,
1099 * etc. (command "tree"), or the same data as a simple list (command "list").
1100 *****************************************************************************/
1101 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1102 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1104 libvlc_global_data_t *p_libvlc_global = vlc_global();
1106 (void)oldval; (void)p_data;
1107 if( *psz_cmd == 'l' )
1109 vlc_mutex_lock( &structure_lock );
1111 vlc_object_t **pp_current, **pp_end;
1113 pp_current = p_libvlc_global->pp_objects;
1114 pp_end = pp_current + p_libvlc_global->i_objects;
1116 for( ; pp_current < pp_end ; pp_current++ )
1118 if( (*pp_current)->p_internals->b_attached )
1120 PrintObject( *pp_current, "" );
1124 printf( " o %.8i %s (not attached)\n",
1125 (*pp_current)->i_object_id,
1126 (*pp_current)->psz_object_type );
1130 vlc_mutex_unlock( &structure_lock );
1134 vlc_object_t *p_object = NULL;
1136 if( *newval.psz_string )
1139 int i_id = strtol( newval.psz_string, &end, 0 );
1140 if( end != newval.psz_string )
1141 p_object = vlc_object_get( i_id );
1144 /* try using the object's name to find it */
1145 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1148 /* Look in p_libvlc's children tree */
1149 p_object = vlc_object_find_name( p_libvlc,
1152 vlc_object_release( p_libvlc );
1156 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1157 p_object = vlc_object_find_name( p_this,
1169 vlc_mutex_lock( &structure_lock );
1171 if( *psz_cmd == 't' )
1173 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1176 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1179 DumpStructure( p_object, 0, psz_foo );
1181 else if( *psz_cmd == 'v' )
1186 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1188 PrintObject( p_object, "" );
1190 if( !p_object->p_internals->i_vars )
1191 printf( " `-o No variables\n" );
1192 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1194 variable_t *p_var = p_object->p_internals->p_vars + i;
1196 const char *psz_type = "unknown";
1197 switch( p_var->i_type & VLC_VAR_TYPE )
1199 #define MYCASE( type, nice ) \
1200 case VLC_VAR_ ## type: \
1203 MYCASE( VOID, "void" );
1204 MYCASE( BOOL, "bool" );
1205 MYCASE( INTEGER, "integer" );
1206 MYCASE( HOTKEY, "hotkey" );
1207 MYCASE( STRING, "string" );
1208 MYCASE( MODULE, "module" );
1209 MYCASE( FILE, "file" );
1210 MYCASE( DIRECTORY, "directory" );
1211 MYCASE( VARIABLE, "variable" );
1212 MYCASE( FLOAT, "float" );
1213 MYCASE( TIME, "time" );
1214 MYCASE( ADDRESS, "address" );
1215 MYCASE( MUTEX, "mutex" );
1216 MYCASE( LIST, "list" );
1219 printf( " %c-o \"%s\" (%s",
1220 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1221 p_var->psz_name, psz_type );
1222 if( p_var->psz_text )
1223 printf( ", %s", p_var->psz_text );
1225 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1226 printf( ", command" );
1227 if( p_var->i_entries )
1228 printf( ", %d callbacks", p_var->i_entries );
1229 switch( p_var->i_type & 0x00f0 )
1235 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1237 case VLC_VAR_INTEGER:
1238 printf( ": %d", p_var->val.i_int );
1240 case VLC_VAR_STRING:
1241 printf( ": \"%s\"", p_var->val.psz_string );
1244 printf( ": %f", p_var->val.f_float );
1247 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1249 case VLC_VAR_ADDRESS:
1250 printf( ": %p", p_var->val.p_address );
1260 vlc_mutex_unlock( &structure_lock );
1262 if( *newval.psz_string )
1264 vlc_object_release( p_object );
1271 /*****************************************************************************
1272 * vlc_list_release: free a list previously allocated by vlc_list_find
1273 *****************************************************************************
1274 * This function decreases the refcount of all objects in the list and
1276 *****************************************************************************/
1277 void vlc_list_release( vlc_list_t *p_list )
1281 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1283 vlc_object_release( p_list->p_values[i_index].p_object );
1286 free( p_list->p_values );
1290 /* Following functions are local */
1292 /*****************************************************************************
1293 * FindIndex: find the index of an object in an array of objects
1294 *****************************************************************************
1295 * This function assumes that p_this can be found in pp_objects. It will not
1296 * crash if p_this cannot be found, but will return a wrong value. It is your
1297 * duty to check the return value if you are not certain that the object could
1298 * be found for sure.
1299 *****************************************************************************/
1300 static int FindIndex( vlc_object_t *p_this,
1301 vlc_object_t **pp_objects, int i_count )
1303 int i_middle = i_count / 2;
1310 if( pp_objects[i_middle] == p_this )
1320 /* We take advantage of the sorted array */
1321 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1323 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1324 i_count - i_middle );
1328 return FindIndex( p_this, pp_objects, i_middle );
1332 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1335 vlc_object_t *p_tmp;
1337 switch( i_mode & 0x000f )
1340 p_tmp = p_this->p_parent;
1343 if( p_tmp->i_object_type == i_type
1344 && p_tmp->p_internals->i_refcount > 0 )
1346 vlc_object_yield_locked( p_tmp );
1351 return FindObject( p_tmp, i_type, i_mode );
1357 for( i = p_this->i_children; i--; )
1359 p_tmp = p_this->pp_children[i];
1360 if( p_tmp->i_object_type == i_type
1361 && p_tmp->p_internals->i_refcount > 0 )
1363 vlc_object_yield_locked( p_tmp );
1366 else if( p_tmp->i_children )
1368 p_tmp = FindObject( p_tmp, i_type, i_mode );
1378 /* Handled in vlc_object_find */
1385 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1386 const char *psz_name,
1390 vlc_object_t *p_tmp;
1392 switch( i_mode & 0x000f )
1395 p_tmp = p_this->p_parent;
1398 if( p_tmp->psz_object_name
1399 && !strcmp( p_tmp->psz_object_name, psz_name )
1400 && p_tmp->p_internals->i_refcount > 0 )
1402 vlc_object_yield_locked( p_tmp );
1407 return FindObjectName( p_tmp, psz_name, i_mode );
1413 for( i = p_this->i_children; i--; )
1415 p_tmp = p_this->pp_children[i];
1416 if( p_tmp->psz_object_name
1417 && !strcmp( p_tmp->psz_object_name, psz_name )
1418 && p_tmp->p_internals->i_refcount > 0 )
1420 vlc_object_yield_locked( p_tmp );
1423 else if( p_tmp->i_children )
1425 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1435 /* Handled in vlc_object_find */
1442 static void DetachObject( vlc_object_t *p_this )
1444 vlc_object_t *p_parent = p_this->p_parent;
1447 /* Remove p_this's parent */
1448 p_this->p_parent = NULL;
1450 /* Remove all of p_parent's children which are p_this */
1451 for( i_index = p_parent->i_children ; i_index-- ; )
1453 if( p_parent->pp_children[i_index] == p_this )
1455 p_parent->i_children--;
1456 for( i = i_index ; i < p_parent->i_children ; i++ )
1458 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1463 if( p_parent->i_children )
1465 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1466 p_parent->i_children * sizeof(vlc_object_t *) );
1470 free( p_parent->pp_children );
1471 p_parent->pp_children = NULL;
1475 /*****************************************************************************
1476 * SetAttachment: recursively set the b_attached flag of a subtree.
1477 *****************************************************************************
1478 * This function is used by the attach and detach functions to propagate
1479 * the b_attached flag in a subtree.
1480 *****************************************************************************/
1481 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1485 for( i_index = p_this->i_children ; i_index-- ; )
1487 SetAttachment( p_this->pp_children[i_index], b_attached );
1490 p_this->p_internals->b_attached = b_attached;
1493 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1495 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1499 if( p_this->psz_object_name )
1501 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1503 psz_name[48] = '\"';
1506 psz_children[0] = '\0';
1507 switch( p_this->i_children )
1512 strcpy( psz_children, ", 1 child" );
1515 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1519 psz_refcount[0] = '\0';
1520 if( p_this->p_internals->i_refcount > 0 )
1521 snprintf( psz_refcount, 19, ", refcount %u",
1522 p_this->p_internals->i_refcount );
1524 psz_thread[0] = '\0';
1525 if( p_this->p_internals->b_thread )
1526 snprintf( psz_thread, 29, " (thread %u)",
1527 #if defined(WIN32) || defined(UNDER_CE)
1528 (unsigned)p_this->p_internals->thread_id.id );
1530 (unsigned)p_this->p_internals->thread_id );
1533 psz_parent[0] = '\0';
1534 if( p_this->p_parent )
1535 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1537 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1538 p_this->i_object_id, p_this->psz_object_type,
1539 psz_name, psz_thread, psz_refcount, psz_children,
1543 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1546 char i_back = psz_foo[i_level];
1547 psz_foo[i_level] = '\0';
1549 PrintObject( p_this, psz_foo );
1551 psz_foo[i_level] = i_back;
1553 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1555 msg_Warn( p_this, "structure tree is too deep" );
1559 for( i = 0 ; i < p_this->i_children ; i++ )
1563 psz_foo[i_level-1] = ' ';
1565 if( psz_foo[i_level-2] == '`' )
1567 psz_foo[i_level-2] = ' ';
1571 if( i == p_this->i_children - 1 )
1573 psz_foo[i_level] = '`';
1577 psz_foo[i_level] = '|';
1580 psz_foo[i_level+1] = '-';
1581 psz_foo[i_level+2] = '\0';
1583 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1587 static vlc_list_t * NewList( int i_count )
1589 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1590 if( p_list == NULL )
1595 p_list->i_count = i_count;
1599 p_list->p_values = NULL;
1603 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1604 if( p_list->p_values == NULL )
1606 p_list->i_count = 0;
1613 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1616 if( p_list == NULL || i_index >= p_list->i_count )
1621 vlc_object_yield_locked( p_object );
1623 p_list->p_values[i_index].p_object = p_object;
1628 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1630 if( p_list == NULL )
1635 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1636 * sizeof( vlc_value_t ) );
1637 if( p_list->p_values == NULL )
1639 p_list->i_count = 0;
1643 vlc_object_yield_locked( p_object );
1645 p_list->p_values[p_list->i_count].p_object = p_object;
1651 static int CountChildren( vlc_object_t *p_this, int i_type )
1653 vlc_object_t *p_tmp;
1656 for( i = 0; i < p_this->i_children; i++ )
1658 p_tmp = p_this->pp_children[i];
1660 if( p_tmp->i_object_type == i_type )
1665 if( p_tmp->i_children )
1667 i_count += CountChildren( p_tmp, i_type );
1674 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1676 vlc_object_t *p_tmp;
1679 for( i = 0; i < p_this->i_children; i++ )
1681 p_tmp = p_this->pp_children[i];
1683 if( p_tmp->i_object_type == i_type )
1685 ListReplace( p_list, p_tmp, p_list->i_count++ );
1688 if( p_tmp->i_children )
1690 ListChildren( p_list, p_tmp, i_type );