1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
26 * This file contains the functions to handle the vlc_object_t type
30 /*****************************************************************************
32 *****************************************************************************/
39 #include "../libvlc.h"
42 #include "audio_output/aout_internal.h"
44 #include <vlc_access.h>
45 #include <vlc_demux.h>
46 #include <vlc_stream.h>
49 #include "stream_output/stream_output.h"
51 #include "vlc_playlist.h"
52 #include "vlc_interface.h"
53 #include "vlc_codec.h"
54 #include "vlc_filter.h"
56 #include "vlc_httpd.h"
58 #include "input/vlm_internal.h"
62 #include "variables.h"
68 # include <errno.h> /* ENOSYS */
72 /*****************************************************************************
74 *****************************************************************************/
76 const vlc_destructor_t kVLCDestructor = NULL;
78 /*****************************************************************************
80 *****************************************************************************/
81 static int DumpCommand( vlc_object_t *, char const *,
82 vlc_value_t, vlc_value_t, void * );
84 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
85 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
86 static void DetachObject ( vlc_object_t * );
87 static void PrintObject ( vlc_object_t *, const char * );
88 static void DumpStructure ( vlc_object_t *, int, char * );
89 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
90 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
92 static vlc_list_t * NewList ( int );
93 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
94 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
95 static int CountChildren ( vlc_object_t *, int );
96 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
98 static void vlc_object_destroy( vlc_object_t *p_this );
99 static void vlc_object_yield_locked( vlc_object_t *p_this );
101 /*****************************************************************************
102 * Local structure lock
103 *****************************************************************************/
104 static vlc_mutex_t structure_lock;
105 static vlc_object_internals_t global_internals;
107 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
108 int i_type, const char *psz_type )
111 vlc_object_internals_t *p_priv;
113 if( i_type == VLC_OBJECT_GLOBAL )
116 p_priv = &global_internals;
117 memset( p_priv, 0, sizeof( *p_priv ) );
121 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
125 p_new = (vlc_object_t *)(p_priv + 1);
128 p_new->p_internals = p_priv;
129 p_new->i_object_type = i_type;
130 p_new->psz_object_type = psz_type;
132 p_new->psz_object_name = NULL;
134 p_new->b_die = VLC_FALSE;
135 p_new->b_error = VLC_FALSE;
136 p_new->b_dead = VLC_FALSE;
137 p_priv->b_attached = VLC_FALSE;
138 p_new->b_force = VLC_FALSE;
140 p_new->psz_header = NULL;
142 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
143 p_new->i_flags |= OBJECT_FLAGS_NODBG;
144 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
145 p_new->i_flags |= OBJECT_FLAGS_QUIET;
146 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
147 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
149 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
151 if( !p_priv->p_vars )
153 if( i_type != VLC_OBJECT_GLOBAL )
158 if( i_type == VLC_OBJECT_GLOBAL )
160 /* If i_type is global, then p_new is actually p_libvlc_global */
161 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
162 p_new->p_libvlc = NULL;
164 p_libvlc_global->i_counter = 0;
165 p_new->i_object_id = 0;
167 p_libvlc_global->i_objects = 1;
168 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
169 p_libvlc_global->pp_objects[0] = p_new;
170 p_priv->b_attached = VLC_TRUE;
174 libvlc_global_data_t *p_libvlc_global = vlc_global();
175 if( i_type == VLC_OBJECT_LIBVLC )
177 p_new->p_libvlc = (libvlc_int_t*)p_new;
178 p_priv->b_attached = VLC_TRUE;
182 p_new->p_libvlc = p_this->p_libvlc;
185 vlc_mutex_lock( &structure_lock );
187 p_libvlc_global->i_counter++;
188 p_new->i_object_id = p_libvlc_global->i_counter;
190 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
191 * useless to try and recover anything if pp_objects gets smashed. */
192 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
195 vlc_mutex_unlock( &structure_lock );
198 p_priv->i_refcount = 1;
199 p_priv->pf_destructor = kVLCDestructor;
200 p_priv->b_thread = VLC_FALSE;
201 p_new->p_parent = NULL;
202 p_new->pp_children = NULL;
203 p_new->i_children = 0;
205 p_new->p_private = NULL;
207 /* Initialize mutexes and condvars */
208 vlc_mutex_init( p_new, &p_new->object_lock );
209 vlc_cond_init( p_new, &p_new->object_wait );
210 vlc_mutex_init( p_new, &p_priv->var_lock );
211 vlc_spin_init( &p_priv->spin );
212 p_priv->pipes[0] = p_priv->pipes[1] = -1;
214 if( i_type == VLC_OBJECT_GLOBAL )
216 vlc_mutex_init( p_new, &structure_lock );
219 if( i_type == VLC_OBJECT_LIBVLC )
221 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
222 var_AddCallback( p_new, "list", DumpCommand, NULL );
223 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
224 var_AddCallback( p_new, "tree", DumpCommand, NULL );
225 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
226 var_AddCallback( p_new, "vars", DumpCommand, NULL );
234 * Allocates and initializes a vlc object.
236 * @param i_type known object type (all of them are negative integer values),
237 * or object byte size (always positive).
239 * @return the new object, or NULL on error.
241 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
243 const char * psz_type;
248 case VLC_OBJECT_GLOBAL:
249 i_size = sizeof(libvlc_global_data_t);
252 case VLC_OBJECT_LIBVLC:
253 i_size = sizeof(libvlc_int_t);
256 case VLC_OBJECT_INTF:
257 i_size = sizeof(intf_thread_t);
258 psz_type = "interface";
260 case VLC_OBJECT_DIALOGS:
261 i_size = sizeof(intf_thread_t);
262 psz_type = "dialogs";
264 case VLC_OBJECT_PLAYLIST:
265 i_size = sizeof(playlist_t);
266 psz_type = "playlist";
268 case VLC_OBJECT_INPUT:
269 i_size = sizeof(input_thread_t);
272 case VLC_OBJECT_DEMUX:
273 i_size = sizeof(demux_t);
276 case VLC_OBJECT_ACCESS:
277 i_size = sizeof(access_t);
280 case VLC_OBJECT_DECODER:
281 i_size = sizeof(decoder_t);
282 psz_type = "decoder";
284 case VLC_OBJECT_PACKETIZER:
285 i_size = sizeof(decoder_t);
286 psz_type = "packetizer";
288 case VLC_OBJECT_ENCODER:
289 i_size = sizeof(encoder_t);
290 psz_type = "encoder";
292 case VLC_OBJECT_FILTER:
293 i_size = sizeof(filter_t);
296 case VLC_OBJECT_VOUT:
297 i_size = sizeof(vout_thread_t);
298 psz_type = "video output";
300 case VLC_OBJECT_AOUT:
301 i_size = sizeof(aout_instance_t);
302 psz_type = "audio output";
304 case VLC_OBJECT_SOUT:
305 i_size = sizeof(sout_instance_t);
306 psz_type = "stream output";
309 i_size = sizeof( vlm_t );
310 psz_type = "vlm dameon";
312 case VLC_OBJECT_OPENGL:
313 i_size = sizeof( vout_thread_t );
316 case VLC_OBJECT_ANNOUNCE:
317 i_size = sizeof( announce_handler_t );
318 psz_type = "announce";
320 case VLC_OBJECT_META_ENGINE:
321 i_size = sizeof( meta_engine_t );
322 psz_type = "meta engine";
324 case VLC_OBJECT_OSDMENU:
325 i_size = sizeof( osd_menu_t );
326 psz_type = "osd menu";
328 case VLC_OBJECT_INTERACTION:
329 i_size = sizeof( interaction_t );
330 psz_type = "interaction";
333 i_size = i_type > (int)sizeof(vlc_object_t)
334 ? i_type : (int)sizeof(vlc_object_t);
335 i_type = VLC_OBJECT_GENERIC;
336 psz_type = "generic";
340 return vlc_custom_create( p_this, i_size, i_type, psz_type );
345 ****************************************************************************
346 * Set the destructor of a vlc object
348 * This function sets the destructor of the vlc object. It will be called
349 * when the object is destroyed when the its refcount reaches 0.
350 * (It is called by the internal function vlc_object_destroy())
351 *****************************************************************************/
352 void __vlc_object_set_destructor( vlc_object_t *p_this,
353 vlc_destructor_t pf_destructor )
355 vlc_object_internals_t *p_priv = vlc_internals(p_this );
357 vlc_mutex_lock( &structure_lock );
358 p_priv->pf_destructor = pf_destructor;
359 vlc_mutex_unlock( &structure_lock );
363 ****************************************************************************
364 * Destroy a vlc object (Internal)
366 * This function destroys an object that has been previously allocated with
367 * vlc_object_create. The object's refcount must be zero and it must not be
368 * attached to other objects in any way.
369 *****************************************************************************/
370 static void vlc_object_destroy( vlc_object_t *p_this )
372 vlc_object_internals_t *p_priv = vlc_internals( p_this );
374 /* Automatically detach the object from its parents */
375 if( p_this->p_parent ) vlc_object_detach( p_this );
378 /* Send a kill to the object's thread if applicable */
379 vlc_object_kill( p_this );
381 /* If we are running on a thread, wait until it ends */
382 if( p_priv->b_thread )
383 vlc_thread_join( p_this );
385 /* Call the custom "subclass" destructor */
386 if( p_priv->pf_destructor )
387 p_priv->pf_destructor( p_this );
390 if( p_this->i_children )
395 "ERROR: cannot delete object (%i, %s) with %d children\n",
396 p_this->i_object_id, p_this->psz_object_name,
397 p_this->i_children );
399 for( i = 0; i < p_this->i_children; i++ )
402 "ERROR: Remaining children object "
403 "(id:%i, type:%s, name:%s)\n",
404 p_this->pp_children[i]->i_object_id,
405 p_this->pp_children[i]->psz_object_type,
406 p_this->pp_children[i]->psz_object_name );
412 /* Destroy the associated variables, starting from the end so that
413 * no memmove calls have to be done. */
414 while( p_priv->i_vars )
416 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
419 free( p_priv->p_vars );
420 vlc_mutex_destroy( &p_priv->var_lock );
422 free( p_this->psz_header );
424 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
426 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
429 if( p_global->i_objects > 0 )
432 for( i = 0; i < p_global->i_objects; i++ )
434 /* We are leaking this object */
436 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
437 p_global->pp_objects[i]->i_object_id,
438 p_global->pp_objects[i]->psz_object_type,
439 p_global->pp_objects[i]->psz_object_name );
442 /* Strongly abort, cause we want these to be fixed */
446 /* We are the global object ... no need to lock. */
447 free( p_global->pp_objects );
448 p_global->pp_objects = NULL;
450 vlc_mutex_destroy( &structure_lock );
453 #if defined(WIN32) || defined(UNDER_CE)
454 /* if object has an associated thread, close it now */
455 if( p_priv->thread_id.hThread )
456 CloseHandle(p_priv->thread_id.hThread);
459 vlc_mutex_destroy( &p_this->object_lock );
460 vlc_cond_destroy( &p_this->object_wait );
461 vlc_spin_destroy( &p_priv->spin );
462 if( p_priv->pipes[1] != -1 )
463 close( p_priv->pipes[1] );
464 if( p_priv->pipes[0] != -1 )
465 close( p_priv->pipes[0] );
467 /* global is not dynamically allocated by vlc_object_create */
468 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
473 /** Inter-object signaling */
475 void __vlc_object_lock( vlc_object_t *obj )
477 vlc_mutex_lock( &obj->object_lock );
480 void __vlc_object_unlock( vlc_object_t *obj )
482 vlc_assert_locked( &obj->object_lock );
483 vlc_mutex_unlock( &obj->object_lock );
487 # include <winsock2.h>
488 # include <ws2tcpip.h>
491 * select()-able pipes emulated using Winsock
493 static int pipe (int fd[2])
496 int addrlen = sizeof (addr);
498 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
499 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
500 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
503 memset (&addr, 0, sizeof (addr));
504 addr.sin_family = AF_INET;
505 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
506 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
507 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
509 || connect (c, (PSOCKADDR)&addr, addrlen))
512 a = accept (l, NULL, NULL);
513 if (a == INVALID_SOCKET)
524 if (l != INVALID_SOCKET)
526 if (c != INVALID_SOCKET)
532 #define read( a, b, c ) recv (a, b, c, 0)
534 #define write( a, b, c ) send (a, b, c, 0)
536 #define close( a ) closesocket (a)
540 * Returns the readable end of a pipe that becomes readable once termination
541 * of the object is requested (vlc_object_kill()).
542 * This can be used to wake-up out of a select() or poll() event loop, such
543 * typically when doing network I/O.
545 * Note that the pipe will remain the same for the lifetime of the object.
546 * DO NOT read the pipe nor close it yourself. Ever.
548 * @param obj object that would be "killed"
549 * @return a readable pipe descriptor, or -1 on error.
551 int __vlc_object_waitpipe( vlc_object_t *obj )
553 int pfd[2] = { -1, -1 };
554 struct vlc_object_internals_t *internals = obj->p_internals;
555 vlc_bool_t killed = VLC_FALSE;
557 vlc_spin_lock (&internals->spin);
558 if (internals->pipes[0] == -1)
560 /* This can only ever happen if someone killed us without locking: */
561 assert (internals->pipes[1] == -1);
562 vlc_spin_unlock (&internals->spin);
567 vlc_spin_lock (&internals->spin);
568 if (internals->pipes[0] == -1)
570 internals->pipes[0] = pfd[0];
571 internals->pipes[1] = pfd[1];
572 pfd[0] = pfd[1] = -1;
576 vlc_spin_unlock (&internals->spin);
580 /* Race condition: vlc_object_kill() already invoked! */
583 vlc_spin_lock (&internals->spin);
584 fd = internals->pipes[1];
585 internals->pipes[1] = -1;
586 vlc_spin_unlock (&internals->spin);
588 msg_Dbg (obj, "waitpipe: object already dying");
593 /* Race condition: two threads call pipe() - unlikely */
599 return internals->pipes[0];
604 * Waits for the object to be signaled (using vlc_object_signal()).
605 * If the object already has a signal pending, this function will return
606 * immediately. It is asserted that the caller holds the object lock.
608 * @return true if the object is dying and should terminate.
610 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
612 vlc_assert_locked( &obj->object_lock );
613 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
619 * Waits for the object to be signaled (using vlc_object_signal()), or for
621 * If the object already has a signal pending, this function will return
622 * immediately. It is asserted that the caller holds the object lock.
624 * @return negative if the object is dying and should terminate,
625 * positive if the the object has been signaled but is not dying,
626 * 0 if timeout has been reached.
628 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
632 vlc_assert_locked( &obj->object_lock );
633 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
634 if( v == 0 ) /* signaled */
635 return obj->b_die ? -1 : 1;
641 * Checks whether an object has been "killed".
642 * The object lock must be held.
644 * Typical code for an object thread could be:
646 vlc_object_lock (self);
648 while (vlc_object_alive (self))
652 if (vlc_object_wait (self))
657 ...deinitialization...
658 vlc_object_unlock (self);
661 * @return true iff the object has not been killed yet
663 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
665 vlc_assert_locked( &obj->object_lock );
671 * Signals an object for which the lock is held.
673 void __vlc_object_signal_unlocked( vlc_object_t *obj )
675 vlc_assert_locked (&obj->object_lock);
676 vlc_cond_signal( &obj->object_wait );
681 * Requests termination of an object.
682 * If the object is LibVLC, also request to terminate all its children.
684 void __vlc_object_kill( vlc_object_t *p_this )
686 struct vlc_object_internals_t *internals = p_this->p_internals;
689 vlc_mutex_lock( &p_this->object_lock );
690 p_this->b_die = VLC_TRUE;
692 vlc_spin_lock (&internals->spin);
693 fd = internals->pipes[1];
694 internals->pipes[1] = -1;
695 vlc_spin_unlock (&internals->spin);
699 msg_Dbg (p_this, "waitpipe: object killed");
703 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
704 for( int i = 0; i < p_this->i_children ; i++ )
705 vlc_object_kill( p_this->pp_children[i] );
707 vlc_object_signal_unlocked( p_this );
708 vlc_mutex_unlock( &p_this->object_lock );
713 * find an object given its ID
715 * This function looks for the object whose i_object_id field is i_id. We
716 * use a dichotomy so that lookups are in log2(n).
717 *****************************************************************************/
718 void * vlc_object_get( int i_id )
721 vlc_object_t **pp_objects;
722 libvlc_global_data_t *p_libvlc_global = vlc_global();
724 vlc_mutex_lock( &structure_lock );
726 pp_objects = p_libvlc_global->pp_objects;
728 /* Perform our dichotomy */
729 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
731 i_middle = i_max / 2;
733 if( pp_objects[i_middle]->i_object_id > i_id )
737 else if( pp_objects[i_middle]->i_object_id < i_id )
741 pp_objects += i_middle;
746 /* This happens when there are only two remaining objects */
747 if( pp_objects[i_middle+1]->i_object_id == i_id
748 && pp_objects[i_middle+1]->p_internals->i_refcount > 0 )
750 vlc_object_yield_locked( pp_objects[i_middle+1] );
751 vlc_mutex_unlock( &structure_lock );
752 return pp_objects[i_middle+1];
757 else if( pp_objects[i_middle]->p_internals->i_refcount > 0 )
759 vlc_object_yield_locked( pp_objects[i_middle] );
760 vlc_mutex_unlock( &structure_lock );
761 return pp_objects[i_middle];
766 /* this means that i_max == i_middle, and since we have already
767 * tested pp_objects[i_middle]), p_found is properly set. */
772 vlc_mutex_unlock( &structure_lock );
777 ****************************************************************************
778 * find a typed object and increment its refcount
779 *****************************************************************************
780 * This function recursively looks for a given object type. i_mode can be one
781 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
782 *****************************************************************************/
783 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
785 vlc_object_t *p_found;
787 vlc_mutex_lock( &structure_lock );
789 /* If we are of the requested type ourselves, don't look further */
790 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type
791 && p_this->p_internals->i_refcount > 0 )
793 vlc_object_yield_locked( p_this );
794 vlc_mutex_unlock( &structure_lock );
798 /* Otherwise, recursively look for the object */
799 if( (i_mode & 0x000f) == FIND_ANYWHERE )
801 vlc_object_t *p_root = p_this;
804 while( p_root->p_parent != NULL &&
805 p_root != VLC_OBJECT( p_this->p_libvlc ) )
807 p_root = p_root->p_parent;
810 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
811 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
813 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
814 i_type, (i_mode & ~0x000f)|FIND_CHILD );
819 p_found = FindObject( p_this, i_type, i_mode );
822 vlc_mutex_unlock( &structure_lock );
828 ****************************************************************************
829 * find a named object and increment its refcount
830 *****************************************************************************
831 * This function recursively looks for a given object name. i_mode can be one
832 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
833 *****************************************************************************/
834 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
837 vlc_object_t *p_found;
839 vlc_mutex_lock( &structure_lock );
841 /* Avoid obvious freed object uses */
842 assert( p_this->p_internals->i_refcount > 0 );
844 /* If have the requested name ourselves, don't look further */
845 if( !(i_mode & FIND_STRICT)
846 && p_this->psz_object_name
847 && !strcmp( p_this->psz_object_name, psz_name )
848 && p_this->p_internals->i_refcount > 0 )
850 vlc_object_yield_locked( p_this );
851 vlc_mutex_unlock( &structure_lock );
855 /* Otherwise, recursively look for the object */
856 if( (i_mode & 0x000f) == FIND_ANYWHERE )
858 vlc_object_t *p_root = p_this;
861 while( p_root->p_parent != NULL &&
862 p_root != VLC_OBJECT( p_this->p_libvlc ) )
864 p_root = p_root->p_parent;
867 p_found = FindObjectName( p_root, psz_name,
868 (i_mode & ~0x000f)|FIND_CHILD );
869 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
871 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
872 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
877 p_found = FindObjectName( p_this, psz_name, i_mode );
880 vlc_mutex_unlock( &structure_lock );
886 ****************************************************************************
887 * increment an object refcount
888 *****************************************************************************/
890 /* When the structure_lock is locked */
891 static void vlc_object_yield_locked( vlc_object_t *p_this )
893 vlc_assert_locked (&structure_lock);
895 /* Avoid obvious freed object uses */
896 assert( p_this->p_internals->i_refcount > 0 );
898 /* Increment the counter */
899 p_this->p_internals->i_refcount++;
902 /* Public function */
903 void __vlc_object_yield( vlc_object_t *p_this )
905 vlc_mutex_lock( &structure_lock );
906 vlc_object_yield_locked( p_this );
907 vlc_mutex_unlock( &structure_lock );
911 /*****************************************************************************
912 * decrement an object refcount
913 * And destroy the object if its refcount reach zero.
914 *****************************************************************************/
915 void __vlc_object_release( vlc_object_t *p_this )
917 vlc_bool_t b_should_destroy;
919 vlc_mutex_lock( &structure_lock );
921 assert( p_this->p_internals->i_refcount > 0 );
922 p_this->p_internals->i_refcount--;
923 b_should_destroy = (p_this->p_internals->i_refcount == 0);
925 if( b_should_destroy )
927 /* Make sure this object can't be obtained via vlc_find_object now that
929 libvlc_global_data_t *p_libvlc_global = vlc_global();
932 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
933 * useless to try and recover anything if pp_objects gets smashed. */
934 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
935 p_libvlc_global->i_objects );
936 REMOVE_ELEM( p_libvlc_global->pp_objects,
937 p_libvlc_global->i_objects, i_index );
940 vlc_mutex_unlock( &structure_lock );
942 if( b_should_destroy )
943 vlc_object_destroy( p_this );
947 ****************************************************************************
948 * attach object to a parent object
949 *****************************************************************************
950 * This function sets p_this as a child of p_parent, and p_parent as a parent
951 * of p_this. This link can be undone using vlc_object_detach.
952 *****************************************************************************/
953 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
955 if( !p_this ) return;
957 vlc_mutex_lock( &structure_lock );
959 /* Avoid obvious freed object uses */
960 assert( p_this->p_internals->i_refcount > 0 );
962 /* Attach the parent to its child */
963 p_this->p_parent = p_parent;
965 /* Attach the child to its parent */
966 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
967 p_parent->i_children, p_this );
969 /* Climb up the tree to see whether we are connected with the root */
970 if( p_parent->p_internals->b_attached )
972 SetAttachment( p_this, VLC_TRUE );
975 vlc_mutex_unlock( &structure_lock );
979 ****************************************************************************
980 * detach object from its parent
981 *****************************************************************************
982 * This function removes all links between an object and its parent.
983 *****************************************************************************/
984 void __vlc_object_detach( vlc_object_t *p_this )
986 if( !p_this ) return;
988 vlc_mutex_lock( &structure_lock );
990 if( !p_this->p_parent )
992 msg_Err( p_this, "object is not attached" );
993 vlc_mutex_unlock( &structure_lock );
997 /* Climb up the tree to see whether we are connected with the root */
998 if( p_this->p_parent->p_internals->b_attached )
1000 SetAttachment( p_this, VLC_FALSE );
1003 DetachObject( p_this );
1004 vlc_mutex_unlock( &structure_lock );
1009 ****************************************************************************
1010 * find a list typed objects and increment their refcount
1011 *****************************************************************************
1012 * This function recursively looks for a given object type. i_mode can be one
1013 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
1014 *****************************************************************************/
1015 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
1018 vlc_object_t **pp_current, **pp_end;
1019 int i_count = 0, i_index = 0;
1020 libvlc_global_data_t *p_libvlc_global = vlc_global();
1022 vlc_mutex_lock( &structure_lock );
1024 /* Look for the objects */
1025 switch( i_mode & 0x000f )
1028 pp_current = p_libvlc_global->pp_objects;
1029 pp_end = pp_current + p_libvlc_global->i_objects;
1031 for( ; pp_current < pp_end ; pp_current++ )
1033 if( (*pp_current)->p_internals->b_attached
1034 && (*pp_current)->i_object_type == i_type )
1040 p_list = NewList( i_count );
1041 pp_current = p_libvlc_global->pp_objects;
1043 for( ; pp_current < pp_end ; pp_current++ )
1045 if( (*pp_current)->p_internals->b_attached
1046 && (*pp_current)->i_object_type == i_type )
1048 ListReplace( p_list, *pp_current, i_index );
1049 if( i_index < i_count ) i_index++;
1055 i_count = CountChildren( p_this, i_type );
1056 p_list = NewList( i_count );
1058 /* Check allocation was successful */
1059 if( p_list->i_count != i_count )
1061 msg_Err( p_this, "list allocation failed!" );
1062 p_list->i_count = 0;
1066 p_list->i_count = 0;
1067 ListChildren( p_list, p_this, i_type );
1071 msg_Err( p_this, "unimplemented!" );
1072 p_list = NewList( 0 );
1076 vlc_mutex_unlock( &structure_lock );
1081 /*****************************************************************************
1082 * DumpCommand: print the current vlc structure
1083 *****************************************************************************
1084 * This function prints either an ASCII tree showing the connections between
1085 * vlc objects, and additional information such as their refcount, thread ID,
1086 * etc. (command "tree"), or the same data as a simple list (command "list").
1087 *****************************************************************************/
1088 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1089 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1091 libvlc_global_data_t *p_libvlc_global = vlc_global();
1093 (void)oldval; (void)p_data;
1094 if( *psz_cmd == 'l' )
1096 vlc_mutex_lock( &structure_lock );
1098 vlc_object_t **pp_current, **pp_end;
1100 pp_current = p_libvlc_global->pp_objects;
1101 pp_end = pp_current + p_libvlc_global->i_objects;
1103 for( ; pp_current < pp_end ; pp_current++ )
1105 if( (*pp_current)->p_internals->b_attached )
1107 PrintObject( *pp_current, "" );
1111 printf( " o %.8i %s (not attached)\n",
1112 (*pp_current)->i_object_id,
1113 (*pp_current)->psz_object_type );
1117 vlc_mutex_unlock( &structure_lock );
1121 vlc_object_t *p_object = NULL;
1123 if( *newval.psz_string )
1126 int i_id = strtol( newval.psz_string, &end, 0 );
1127 if( end != newval.psz_string )
1128 p_object = vlc_object_get( i_id );
1131 /* try using the object's name to find it */
1132 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1135 /* Look in p_libvlc's children tree */
1136 p_object = vlc_object_find_name( p_libvlc,
1139 vlc_object_release( p_libvlc );
1143 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1144 p_object = vlc_object_find_name( p_this,
1156 vlc_mutex_lock( &structure_lock );
1158 if( *psz_cmd == 't' )
1160 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1163 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1166 DumpStructure( p_object, 0, psz_foo );
1168 else if( *psz_cmd == 'v' )
1173 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1175 PrintObject( p_object, "" );
1177 if( !p_object->p_internals->i_vars )
1178 printf( " `-o No variables\n" );
1179 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1181 variable_t *p_var = p_object->p_internals->p_vars + i;
1183 const char *psz_type = "unknown";
1184 switch( p_var->i_type & VLC_VAR_TYPE )
1186 #define MYCASE( type, nice ) \
1187 case VLC_VAR_ ## type: \
1190 MYCASE( VOID, "void" );
1191 MYCASE( BOOL, "bool" );
1192 MYCASE( INTEGER, "integer" );
1193 MYCASE( HOTKEY, "hotkey" );
1194 MYCASE( STRING, "string" );
1195 MYCASE( MODULE, "module" );
1196 MYCASE( FILE, "file" );
1197 MYCASE( DIRECTORY, "directory" );
1198 MYCASE( VARIABLE, "variable" );
1199 MYCASE( FLOAT, "float" );
1200 MYCASE( TIME, "time" );
1201 MYCASE( ADDRESS, "address" );
1202 MYCASE( MUTEX, "mutex" );
1203 MYCASE( LIST, "list" );
1206 printf( " %c-o \"%s\" (%s",
1207 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1208 p_var->psz_name, psz_type );
1209 if( p_var->psz_text )
1210 printf( ", %s", p_var->psz_text );
1212 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1213 printf( ", command" );
1214 if( p_var->i_entries )
1215 printf( ", %d callbacks", p_var->i_entries );
1216 switch( p_var->i_type & 0x00f0 )
1222 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1224 case VLC_VAR_INTEGER:
1225 printf( ": %d", p_var->val.i_int );
1227 case VLC_VAR_STRING:
1228 printf( ": \"%s\"", p_var->val.psz_string );
1231 printf( ": %f", p_var->val.f_float );
1234 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1236 case VLC_VAR_ADDRESS:
1237 printf( ": %p", p_var->val.p_address );
1247 vlc_mutex_unlock( &structure_lock );
1249 if( *newval.psz_string )
1251 vlc_object_release( p_object );
1258 /*****************************************************************************
1259 * vlc_list_release: free a list previously allocated by vlc_list_find
1260 *****************************************************************************
1261 * This function decreases the refcount of all objects in the list and
1263 *****************************************************************************/
1264 void vlc_list_release( vlc_list_t *p_list )
1268 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1270 vlc_object_release( p_list->p_values[i_index].p_object );
1273 free( p_list->p_values );
1277 /* Following functions are local */
1279 /*****************************************************************************
1280 * FindIndex: find the index of an object in an array of objects
1281 *****************************************************************************
1282 * This function assumes that p_this can be found in pp_objects. It will not
1283 * crash if p_this cannot be found, but will return a wrong value. It is your
1284 * duty to check the return value if you are not certain that the object could
1285 * be found for sure.
1286 *****************************************************************************/
1287 static int FindIndex( vlc_object_t *p_this,
1288 vlc_object_t **pp_objects, int i_count )
1290 int i_middle = i_count / 2;
1297 if( pp_objects[i_middle] == p_this )
1307 /* We take advantage of the sorted array */
1308 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1310 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1311 i_count - i_middle );
1315 return FindIndex( p_this, pp_objects, i_middle );
1319 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1322 vlc_object_t *p_tmp;
1324 switch( i_mode & 0x000f )
1327 p_tmp = p_this->p_parent;
1330 if( p_tmp->i_object_type == i_type
1331 && p_tmp->p_internals->i_refcount > 0 )
1333 vlc_object_yield_locked( p_tmp );
1338 return FindObject( p_tmp, i_type, i_mode );
1344 for( i = p_this->i_children; i--; )
1346 p_tmp = p_this->pp_children[i];
1347 if( p_tmp->i_object_type == i_type
1348 && p_tmp->p_internals->i_refcount > 0 )
1350 vlc_object_yield_locked( p_tmp );
1353 else if( p_tmp->i_children )
1355 p_tmp = FindObject( p_tmp, i_type, i_mode );
1365 /* Handled in vlc_object_find */
1372 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1373 const char *psz_name,
1377 vlc_object_t *p_tmp;
1379 switch( i_mode & 0x000f )
1382 p_tmp = p_this->p_parent;
1385 if( p_tmp->psz_object_name
1386 && !strcmp( p_tmp->psz_object_name, psz_name )
1387 && p_tmp->p_internals->i_refcount > 0 )
1389 vlc_object_yield_locked( p_tmp );
1394 return FindObjectName( p_tmp, psz_name, i_mode );
1400 for( i = p_this->i_children; i--; )
1402 p_tmp = p_this->pp_children[i];
1403 if( p_tmp->psz_object_name
1404 && !strcmp( p_tmp->psz_object_name, psz_name )
1405 && p_tmp->p_internals->i_refcount > 0 )
1407 vlc_object_yield_locked( p_tmp );
1410 else if( p_tmp->i_children )
1412 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1422 /* Handled in vlc_object_find */
1429 static void DetachObject( vlc_object_t *p_this )
1431 vlc_object_t *p_parent = p_this->p_parent;
1434 /* Remove p_this's parent */
1435 p_this->p_parent = NULL;
1437 /* Remove all of p_parent's children which are p_this */
1438 for( i_index = p_parent->i_children ; i_index-- ; )
1440 if( p_parent->pp_children[i_index] == p_this )
1442 p_parent->i_children--;
1443 for( i = i_index ; i < p_parent->i_children ; i++ )
1445 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1450 if( p_parent->i_children )
1452 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1453 p_parent->i_children * sizeof(vlc_object_t *) );
1457 free( p_parent->pp_children );
1458 p_parent->pp_children = NULL;
1462 /*****************************************************************************
1463 * SetAttachment: recursively set the b_attached flag of a subtree.
1464 *****************************************************************************
1465 * This function is used by the attach and detach functions to propagate
1466 * the b_attached flag in a subtree.
1467 *****************************************************************************/
1468 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1472 for( i_index = p_this->i_children ; i_index-- ; )
1474 SetAttachment( p_this->pp_children[i_index], b_attached );
1477 p_this->p_internals->b_attached = b_attached;
1480 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1482 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1486 if( p_this->psz_object_name )
1488 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1490 psz_name[48] = '\"';
1493 psz_children[0] = '\0';
1494 switch( p_this->i_children )
1499 strcpy( psz_children, ", 1 child" );
1502 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1506 psz_refcount[0] = '\0';
1507 if( p_this->p_internals->i_refcount > 0 )
1508 snprintf( psz_refcount, 19, ", refcount %u",
1509 p_this->p_internals->i_refcount );
1511 psz_thread[0] = '\0';
1512 if( p_this->p_internals->b_thread )
1513 snprintf( psz_thread, 29, " (thread %u)",
1514 #if defined(WIN32) || defined(UNDER_CE)
1515 (unsigned)p_this->p_internals->thread_id.id );
1517 (unsigned)p_this->p_internals->thread_id );
1520 psz_parent[0] = '\0';
1521 if( p_this->p_parent )
1522 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1524 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1525 p_this->i_object_id, p_this->psz_object_type,
1526 psz_name, psz_thread, psz_refcount, psz_children,
1530 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1533 char i_back = psz_foo[i_level];
1534 psz_foo[i_level] = '\0';
1536 PrintObject( p_this, psz_foo );
1538 psz_foo[i_level] = i_back;
1540 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1542 msg_Warn( p_this, "structure tree is too deep" );
1546 for( i = 0 ; i < p_this->i_children ; i++ )
1550 psz_foo[i_level-1] = ' ';
1552 if( psz_foo[i_level-2] == '`' )
1554 psz_foo[i_level-2] = ' ';
1558 if( i == p_this->i_children - 1 )
1560 psz_foo[i_level] = '`';
1564 psz_foo[i_level] = '|';
1567 psz_foo[i_level+1] = '-';
1568 psz_foo[i_level+2] = '\0';
1570 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1574 static vlc_list_t * NewList( int i_count )
1576 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1577 if( p_list == NULL )
1582 p_list->i_count = i_count;
1586 p_list->p_values = NULL;
1590 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1591 if( p_list->p_values == NULL )
1593 p_list->i_count = 0;
1600 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1603 if( p_list == NULL || i_index >= p_list->i_count )
1608 vlc_object_yield_locked( p_object );
1610 p_list->p_values[i_index].p_object = p_object;
1615 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1617 if( p_list == NULL )
1622 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1623 * sizeof( vlc_value_t ) );
1624 if( p_list->p_values == NULL )
1626 p_list->i_count = 0;
1630 vlc_object_yield_locked( p_object );
1632 p_list->p_values[p_list->i_count].p_object = p_object;
1638 static int CountChildren( vlc_object_t *p_this, int i_type )
1640 vlc_object_t *p_tmp;
1643 for( i = 0; i < p_this->i_children; i++ )
1645 p_tmp = p_this->pp_children[i];
1647 if( p_tmp->i_object_type == i_type )
1652 if( p_tmp->i_children )
1654 i_count += CountChildren( p_tmp, i_type );
1661 static void ListChildren( vlc_list_t *p_list, 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 )
1672 ListReplace( p_list, p_tmp, p_list->i_count++ );
1675 if( p_tmp->i_children )
1677 ListChildren( p_list, p_tmp, i_type );