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_interface.h"
52 #include "vlc_codec.h"
53 #include "vlc_filter.h"
55 #include "variables.h"
61 # include <errno.h> /* ENOSYS */
65 /*****************************************************************************
67 *****************************************************************************/
69 const vlc_destructor_t kVLCDestructor = NULL;
71 /*****************************************************************************
73 *****************************************************************************/
74 static int DumpCommand( vlc_object_t *, char const *,
75 vlc_value_t, vlc_value_t, void * );
77 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
78 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
79 static void DetachObject ( vlc_object_t * );
80 static void PrintObject ( vlc_object_t *, const char * );
81 static void DumpStructure ( vlc_object_t *, int, char * );
82 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
83 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
85 static vlc_list_t * NewList ( int );
86 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
87 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
88 static int CountChildren ( vlc_object_t *, int );
89 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
91 static void vlc_object_destroy( vlc_object_t *p_this );
92 static void vlc_object_yield_locked( vlc_object_t *p_this );
94 /*****************************************************************************
95 * Local structure lock
96 *****************************************************************************/
97 static vlc_mutex_t structure_lock;
99 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
100 int i_type, const char *psz_type )
103 vlc_object_internals_t *p_priv;
105 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
109 if( i_type == VLC_OBJECT_GLOBAL )
113 assert (i_size >= sizeof (vlc_object_t));
114 p_new = (vlc_object_t *)(p_priv + 1);
117 p_new->p_internals = p_priv;
118 p_new->i_object_type = i_type;
119 p_new->psz_object_type = psz_type;
121 p_new->psz_object_name = NULL;
123 p_new->b_die = VLC_FALSE;
124 p_new->b_error = VLC_FALSE;
125 p_new->b_dead = VLC_FALSE;
126 p_priv->b_attached = VLC_FALSE;
127 p_new->b_force = VLC_FALSE;
129 p_new->psz_header = NULL;
131 p_new->i_flags |= p_this->i_flags
132 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
134 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
136 if( !p_priv->p_vars )
142 if( i_type == VLC_OBJECT_GLOBAL )
144 /* If i_type is global, then p_new is actually p_libvlc_global */
145 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
146 p_new->p_libvlc = NULL;
148 p_libvlc_global->i_counter = 0;
149 p_new->i_object_id = 0;
151 p_libvlc_global->i_objects = 1;
152 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
153 p_libvlc_global->pp_objects[0] = p_new;
154 p_priv->b_attached = VLC_TRUE;
155 vlc_mutex_init( p_new, &structure_lock );
159 libvlc_global_data_t *p_libvlc_global = vlc_global();
160 if( i_type == VLC_OBJECT_LIBVLC )
162 p_new->p_libvlc = (libvlc_int_t*)p_new;
163 p_priv->b_attached = VLC_TRUE;
167 p_new->p_libvlc = p_this->p_libvlc;
170 vlc_mutex_lock( &structure_lock );
172 p_libvlc_global->i_counter++;
173 p_new->i_object_id = p_libvlc_global->i_counter;
175 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
176 * useless to try and recover anything if pp_objects gets smashed. */
177 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
180 vlc_mutex_unlock( &structure_lock );
183 p_priv->i_refcount = 1;
184 p_priv->pf_destructor = kVLCDestructor;
185 p_priv->b_thread = VLC_FALSE;
186 p_new->p_parent = NULL;
187 p_new->pp_children = NULL;
188 p_new->i_children = 0;
190 p_new->p_private = NULL;
192 /* Initialize mutexes and condvars */
193 vlc_mutex_init( p_new, &p_new->object_lock );
194 vlc_cond_init( p_new, &p_new->object_wait );
195 vlc_mutex_init( p_new, &p_priv->var_lock );
196 vlc_spin_init( &p_priv->spin );
197 p_priv->pipes[0] = p_priv->pipes[1] = -1;
199 if( i_type == VLC_OBJECT_LIBVLC )
201 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
202 var_AddCallback( p_new, "list", DumpCommand, NULL );
203 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
204 var_AddCallback( p_new, "tree", DumpCommand, NULL );
205 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
206 var_AddCallback( p_new, "vars", DumpCommand, NULL );
214 * Allocates and initializes a vlc object.
216 * @param i_type known object type (all of them are negative integer values),
217 * or object byte size (always positive).
219 * @return the new object, or NULL on error.
221 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
223 const char * psz_type;
228 case VLC_OBJECT_LIBVLC:
229 i_size = sizeof(libvlc_int_t);
232 case VLC_OBJECT_INTF:
233 i_size = sizeof(intf_thread_t);
234 psz_type = "interface";
236 case VLC_OBJECT_DIALOGS:
237 i_size = sizeof(intf_thread_t);
238 psz_type = "dialogs";
240 case VLC_OBJECT_DEMUX:
241 i_size = sizeof(demux_t);
244 case VLC_OBJECT_ACCESS:
245 i_size = sizeof(access_t);
248 case VLC_OBJECT_DECODER:
249 i_size = sizeof(decoder_t);
250 psz_type = "decoder";
252 case VLC_OBJECT_PACKETIZER:
253 i_size = sizeof(decoder_t);
254 psz_type = "packetizer";
256 case VLC_OBJECT_ENCODER:
257 i_size = sizeof(encoder_t);
258 psz_type = "encoder";
260 case VLC_OBJECT_FILTER:
261 i_size = sizeof(filter_t);
264 case VLC_OBJECT_VOUT:
265 i_size = sizeof(vout_thread_t);
266 psz_type = "video output";
268 case VLC_OBJECT_AOUT:
269 i_size = sizeof(aout_instance_t);
270 psz_type = "audio output";
272 case VLC_OBJECT_SOUT:
273 i_size = sizeof(sout_instance_t);
274 psz_type = "stream output";
276 case VLC_OBJECT_OPENGL:
277 i_size = sizeof( vout_thread_t );
280 case VLC_OBJECT_ANNOUNCE:
281 i_size = sizeof( announce_handler_t );
282 psz_type = "announce";
284 case VLC_OBJECT_INTERACTION:
285 i_size = sizeof( interaction_t );
286 psz_type = "interaction";
289 i_size = i_type > (int)sizeof(vlc_object_t)
290 ? i_type : (int)sizeof(vlc_object_t);
291 i_type = VLC_OBJECT_GENERIC;
292 psz_type = "generic";
296 return vlc_custom_create( p_this, i_size, i_type, psz_type );
301 ****************************************************************************
302 * Set the destructor of a vlc object
304 * This function sets the destructor of the vlc object. It will be called
305 * when the object is destroyed when the its refcount reaches 0.
306 * (It is called by the internal function vlc_object_destroy())
307 *****************************************************************************/
308 void __vlc_object_set_destructor( vlc_object_t *p_this,
309 vlc_destructor_t pf_destructor )
311 vlc_object_internals_t *p_priv = vlc_internals(p_this );
313 vlc_mutex_lock( &structure_lock );
314 p_priv->pf_destructor = pf_destructor;
315 vlc_mutex_unlock( &structure_lock );
319 ****************************************************************************
320 * Destroy a vlc object (Internal)
322 * This function destroys an object that has been previously allocated with
323 * vlc_object_create. The object's refcount must be zero and it must not be
324 * attached to other objects in any way.
325 *****************************************************************************/
326 static void vlc_object_destroy( vlc_object_t *p_this )
328 vlc_object_internals_t *p_priv = vlc_internals( p_this );
330 /* Automatically detach the object from its parents */
331 if( p_this->p_parent ) vlc_object_detach( p_this );
334 /* Send a kill to the object's thread if applicable */
335 vlc_object_kill( p_this );
337 /* If we are running on a thread, wait until it ends */
338 if( p_priv->b_thread )
339 vlc_thread_join( p_this );
341 /* Call the custom "subclass" destructor */
342 if( p_priv->pf_destructor )
343 p_priv->pf_destructor( p_this );
346 if( p_this->i_children )
351 "ERROR: cannot delete object (%i, %s) with %d children\n",
352 p_this->i_object_id, p_this->psz_object_name,
353 p_this->i_children );
355 for( i = 0; i < p_this->i_children; i++ )
358 "ERROR: Remaining children object "
359 "(id:%i, type:%s, name:%s)\n",
360 p_this->pp_children[i]->i_object_id,
361 p_this->pp_children[i]->psz_object_type,
362 p_this->pp_children[i]->psz_object_name );
368 /* Destroy the associated variables, starting from the end so that
369 * no memmove calls have to be done. */
370 while( p_priv->i_vars )
372 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
375 free( p_priv->p_vars );
376 vlc_mutex_destroy( &p_priv->var_lock );
378 free( p_this->psz_header );
380 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
382 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
385 if( p_global->i_objects > 0 )
388 for( i = 0; i < p_global->i_objects; i++ )
390 /* We are leaking this object */
392 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
393 p_global->pp_objects[i]->i_object_id,
394 p_global->pp_objects[i]->psz_object_type,
395 p_global->pp_objects[i]->psz_object_name );
398 /* Strongly abort, cause we want these to be fixed */
402 /* We are the global object ... no need to lock. */
403 free( p_global->pp_objects );
404 p_global->pp_objects = NULL;
406 vlc_mutex_destroy( &structure_lock );
409 #if defined(WIN32) || defined(UNDER_CE)
410 /* if object has an associated thread, close it now */
411 if( p_priv->thread_id.hThread )
412 CloseHandle(p_priv->thread_id.hThread);
415 vlc_mutex_destroy( &p_this->object_lock );
416 vlc_cond_destroy( &p_this->object_wait );
417 vlc_spin_destroy( &p_priv->spin );
418 if( p_priv->pipes[1] != -1 )
419 close( p_priv->pipes[1] );
420 if( p_priv->pipes[0] != -1 )
421 close( p_priv->pipes[0] );
427 /** Inter-object signaling */
429 void __vlc_object_lock( vlc_object_t *obj )
431 vlc_mutex_lock( &obj->object_lock );
434 void __vlc_object_unlock( vlc_object_t *obj )
436 vlc_assert_locked( &obj->object_lock );
437 vlc_mutex_unlock( &obj->object_lock );
441 # include <winsock2.h>
442 # include <ws2tcpip.h>
445 * select()-able pipes emulated using Winsock
447 static int pipe (int fd[2])
450 int addrlen = sizeof (addr);
452 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
453 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
454 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
457 memset (&addr, 0, sizeof (addr));
458 addr.sin_family = AF_INET;
459 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
460 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
461 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
463 || connect (c, (PSOCKADDR)&addr, addrlen))
466 a = accept (l, NULL, NULL);
467 if (a == INVALID_SOCKET)
478 if (l != INVALID_SOCKET)
480 if (c != INVALID_SOCKET)
486 #define read( a, b, c ) recv (a, b, c, 0)
488 #define write( a, b, c ) send (a, b, c, 0)
490 #define close( a ) closesocket (a)
494 * Returns the readable end of a pipe that becomes readable once termination
495 * of the object is requested (vlc_object_kill()).
496 * This can be used to wake-up out of a select() or poll() event loop, such
497 * typically when doing network I/O.
499 * Note that the pipe will remain the same for the lifetime of the object.
500 * DO NOT read the pipe nor close it yourself. Ever.
502 * @param obj object that would be "killed"
503 * @return a readable pipe descriptor, or -1 on error.
505 int __vlc_object_waitpipe( vlc_object_t *obj )
507 int pfd[2] = { -1, -1 };
508 struct vlc_object_internals_t *internals = obj->p_internals;
509 vlc_bool_t killed = VLC_FALSE;
511 vlc_spin_lock (&internals->spin);
512 if (internals->pipes[0] == -1)
514 /* This can only ever happen if someone killed us without locking: */
515 assert (internals->pipes[1] == -1);
516 vlc_spin_unlock (&internals->spin);
521 vlc_spin_lock (&internals->spin);
522 if (internals->pipes[0] == -1)
524 internals->pipes[0] = pfd[0];
525 internals->pipes[1] = pfd[1];
526 pfd[0] = pfd[1] = -1;
530 vlc_spin_unlock (&internals->spin);
534 /* Race condition: vlc_object_kill() already invoked! */
537 vlc_spin_lock (&internals->spin);
538 fd = internals->pipes[1];
539 internals->pipes[1] = -1;
540 vlc_spin_unlock (&internals->spin);
542 msg_Dbg (obj, "waitpipe: object already dying");
547 /* Race condition: two threads call pipe() - unlikely */
553 return internals->pipes[0];
558 * Waits for the object to be signaled (using vlc_object_signal()).
559 * If the object already has a signal pending, this function will return
560 * immediately. It is asserted that the caller holds the object lock.
562 * @return true if the object is dying and should terminate.
564 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
566 vlc_assert_locked( &obj->object_lock );
567 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
573 * Waits for the object to be signaled (using vlc_object_signal()), or for
575 * If the object already has a signal pending, this function will return
576 * immediately. It is asserted that the caller holds the object lock.
578 * @return negative if the object is dying and should terminate,
579 * positive if the the object has been signaled but is not dying,
580 * 0 if timeout has been reached.
582 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
586 vlc_assert_locked( &obj->object_lock );
587 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
588 if( v == 0 ) /* signaled */
589 return obj->b_die ? -1 : 1;
595 * Checks whether an object has been "killed".
596 * The object lock must be held.
598 * Typical code for an object thread could be:
600 vlc_object_lock (self);
602 while (vlc_object_alive (self))
606 if (vlc_object_wait (self))
611 ...deinitialization...
612 vlc_object_unlock (self);
615 * @return true iff the object has not been killed yet
617 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
619 vlc_assert_locked( &obj->object_lock );
625 * Signals an object for which the lock is held.
627 void __vlc_object_signal_unlocked( vlc_object_t *obj )
629 vlc_assert_locked (&obj->object_lock);
630 vlc_cond_signal( &obj->object_wait );
635 * Requests termination of an object.
636 * If the object is LibVLC, also request to terminate all its children.
638 void __vlc_object_kill( vlc_object_t *p_this )
640 struct vlc_object_internals_t *internals = p_this->p_internals;
643 vlc_mutex_lock( &p_this->object_lock );
644 p_this->b_die = VLC_TRUE;
646 vlc_spin_lock (&internals->spin);
647 fd = internals->pipes[1];
648 internals->pipes[1] = -1;
649 vlc_spin_unlock (&internals->spin);
653 msg_Dbg (p_this, "waitpipe: object killed");
657 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
658 for( int i = 0; i < p_this->i_children ; i++ )
659 vlc_object_kill( p_this->pp_children[i] );
661 vlc_object_signal_unlocked( p_this );
662 vlc_mutex_unlock( &p_this->object_lock );
667 * find an object given its ID
669 * This function looks for the object whose i_object_id field is i_id. We
670 * use a dichotomy so that lookups are in log2(n).
671 *****************************************************************************/
672 void * vlc_object_get( int i_id )
675 vlc_object_t **pp_objects;
676 libvlc_global_data_t *p_libvlc_global = vlc_global();
678 vlc_mutex_lock( &structure_lock );
680 pp_objects = p_libvlc_global->pp_objects;
682 /* Perform our dichotomy */
683 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
685 i_middle = i_max / 2;
687 if( pp_objects[i_middle]->i_object_id > i_id )
691 else if( pp_objects[i_middle]->i_object_id < i_id )
695 pp_objects += i_middle;
700 /* This happens when there are only two remaining objects */
701 if( pp_objects[i_middle+1]->i_object_id == i_id
702 && pp_objects[i_middle+1]->p_internals->i_refcount > 0 )
704 vlc_object_yield_locked( pp_objects[i_middle+1] );
705 vlc_mutex_unlock( &structure_lock );
706 return pp_objects[i_middle+1];
711 else if( pp_objects[i_middle]->p_internals->i_refcount > 0 )
713 vlc_object_yield_locked( pp_objects[i_middle] );
714 vlc_mutex_unlock( &structure_lock );
715 return pp_objects[i_middle];
720 /* this means that i_max == i_middle, and since we have already
721 * tested pp_objects[i_middle]), p_found is properly set. */
726 vlc_mutex_unlock( &structure_lock );
731 ****************************************************************************
732 * find a typed object and increment its refcount
733 *****************************************************************************
734 * This function recursively looks for a given object type. i_mode can be one
735 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
736 *****************************************************************************/
737 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
739 vlc_object_t *p_found;
741 vlc_mutex_lock( &structure_lock );
743 /* If we are of the requested type ourselves, don't look further */
744 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type
745 && p_this->p_internals->i_refcount > 0 )
747 vlc_object_yield_locked( p_this );
748 vlc_mutex_unlock( &structure_lock );
752 /* Otherwise, recursively look for the object */
753 if( (i_mode & 0x000f) == FIND_ANYWHERE )
755 vlc_object_t *p_root = p_this;
758 while( p_root->p_parent != NULL &&
759 p_root != VLC_OBJECT( p_this->p_libvlc ) )
761 p_root = p_root->p_parent;
764 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
765 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
767 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
768 i_type, (i_mode & ~0x000f)|FIND_CHILD );
773 p_found = FindObject( p_this, i_type, i_mode );
776 vlc_mutex_unlock( &structure_lock );
782 ****************************************************************************
783 * find a named object and increment its refcount
784 *****************************************************************************
785 * This function recursively looks for a given object name. i_mode can be one
786 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
787 *****************************************************************************/
788 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
791 vlc_object_t *p_found;
793 vlc_mutex_lock( &structure_lock );
795 /* Avoid obvious freed object uses */
796 assert( p_this->p_internals->i_refcount > 0 );
798 /* If have the requested name ourselves, don't look further */
799 if( !(i_mode & FIND_STRICT)
800 && p_this->psz_object_name
801 && !strcmp( p_this->psz_object_name, psz_name )
802 && p_this->p_internals->i_refcount > 0 )
804 vlc_object_yield_locked( p_this );
805 vlc_mutex_unlock( &structure_lock );
809 /* Otherwise, recursively look for the object */
810 if( (i_mode & 0x000f) == FIND_ANYWHERE )
812 vlc_object_t *p_root = p_this;
815 while( p_root->p_parent != NULL &&
816 p_root != VLC_OBJECT( p_this->p_libvlc ) )
818 p_root = p_root->p_parent;
821 p_found = FindObjectName( p_root, psz_name,
822 (i_mode & ~0x000f)|FIND_CHILD );
823 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
825 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
826 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
831 p_found = FindObjectName( p_this, psz_name, i_mode );
834 vlc_mutex_unlock( &structure_lock );
840 ****************************************************************************
841 * increment an object refcount
842 *****************************************************************************/
844 /* When the structure_lock is locked */
845 static void vlc_object_yield_locked( vlc_object_t *p_this )
847 vlc_assert_locked (&structure_lock);
849 /* Avoid obvious freed object uses */
850 assert( p_this->p_internals->i_refcount > 0 );
852 /* Increment the counter */
853 p_this->p_internals->i_refcount++;
856 /* Public function */
857 void __vlc_object_yield( vlc_object_t *p_this )
859 vlc_mutex_lock( &structure_lock );
860 vlc_object_yield_locked( p_this );
861 vlc_mutex_unlock( &structure_lock );
865 /*****************************************************************************
866 * decrement an object refcount
867 * And destroy the object if its refcount reach zero.
868 *****************************************************************************/
869 void __vlc_object_release( vlc_object_t *p_this )
871 vlc_bool_t b_should_destroy;
873 vlc_mutex_lock( &structure_lock );
875 assert( p_this->p_internals->i_refcount > 0 );
876 p_this->p_internals->i_refcount--;
877 b_should_destroy = (p_this->p_internals->i_refcount == 0);
879 if( b_should_destroy )
881 /* Make sure this object can't be obtained via vlc_find_object now that
883 libvlc_global_data_t *p_libvlc_global = vlc_global();
886 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
887 * useless to try and recover anything if pp_objects gets smashed. */
888 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
889 p_libvlc_global->i_objects );
890 REMOVE_ELEM( p_libvlc_global->pp_objects,
891 p_libvlc_global->i_objects, i_index );
894 vlc_mutex_unlock( &structure_lock );
896 if( b_should_destroy )
897 vlc_object_destroy( p_this );
901 ****************************************************************************
902 * attach object to a parent object
903 *****************************************************************************
904 * This function sets p_this as a child of p_parent, and p_parent as a parent
905 * of p_this. This link can be undone using vlc_object_detach.
906 *****************************************************************************/
907 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
909 if( !p_this ) return;
911 vlc_mutex_lock( &structure_lock );
913 /* Avoid obvious freed object uses */
914 assert( p_this->p_internals->i_refcount > 0 );
916 /* Attach the parent to its child */
917 p_this->p_parent = p_parent;
919 /* Attach the child to its parent */
920 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
921 p_parent->i_children, p_this );
923 /* Climb up the tree to see whether we are connected with the root */
924 if( p_parent->p_internals->b_attached )
926 SetAttachment( p_this, VLC_TRUE );
929 vlc_mutex_unlock( &structure_lock );
933 ****************************************************************************
934 * detach object from its parent
935 *****************************************************************************
936 * This function removes all links between an object and its parent.
937 *****************************************************************************/
938 void __vlc_object_detach( vlc_object_t *p_this )
940 if( !p_this ) return;
942 vlc_mutex_lock( &structure_lock );
944 if( !p_this->p_parent )
946 msg_Err( p_this, "object is not attached" );
947 vlc_mutex_unlock( &structure_lock );
951 /* Climb up the tree to see whether we are connected with the root */
952 if( p_this->p_parent->p_internals->b_attached )
954 SetAttachment( p_this, VLC_FALSE );
957 DetachObject( p_this );
958 vlc_mutex_unlock( &structure_lock );
963 ****************************************************************************
964 * find a list typed objects and increment their refcount
965 *****************************************************************************
966 * This function recursively looks for a given object type. i_mode can be one
967 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
968 *****************************************************************************/
969 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
972 vlc_object_t **pp_current, **pp_end;
973 int i_count = 0, i_index = 0;
974 libvlc_global_data_t *p_libvlc_global = vlc_global();
976 vlc_mutex_lock( &structure_lock );
978 /* Look for the objects */
979 switch( i_mode & 0x000f )
982 pp_current = p_libvlc_global->pp_objects;
983 pp_end = pp_current + p_libvlc_global->i_objects;
985 for( ; pp_current < pp_end ; pp_current++ )
987 if( (*pp_current)->p_internals->b_attached
988 && (*pp_current)->i_object_type == i_type )
994 p_list = NewList( i_count );
995 pp_current = p_libvlc_global->pp_objects;
997 for( ; pp_current < pp_end ; pp_current++ )
999 if( (*pp_current)->p_internals->b_attached
1000 && (*pp_current)->i_object_type == i_type )
1002 ListReplace( p_list, *pp_current, i_index );
1003 if( i_index < i_count ) i_index++;
1009 i_count = CountChildren( p_this, i_type );
1010 p_list = NewList( i_count );
1012 /* Check allocation was successful */
1013 if( p_list->i_count != i_count )
1015 msg_Err( p_this, "list allocation failed!" );
1016 p_list->i_count = 0;
1020 p_list->i_count = 0;
1021 ListChildren( p_list, p_this, i_type );
1025 msg_Err( p_this, "unimplemented!" );
1026 p_list = NewList( 0 );
1030 vlc_mutex_unlock( &structure_lock );
1035 /*****************************************************************************
1036 * DumpCommand: print the current vlc structure
1037 *****************************************************************************
1038 * This function prints either an ASCII tree showing the connections between
1039 * vlc objects, and additional information such as their refcount, thread ID,
1040 * etc. (command "tree"), or the same data as a simple list (command "list").
1041 *****************************************************************************/
1042 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1043 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1045 libvlc_global_data_t *p_libvlc_global = vlc_global();
1047 (void)oldval; (void)p_data;
1048 if( *psz_cmd == 'l' )
1050 vlc_mutex_lock( &structure_lock );
1052 vlc_object_t **pp_current, **pp_end;
1054 pp_current = p_libvlc_global->pp_objects;
1055 pp_end = pp_current + p_libvlc_global->i_objects;
1057 for( ; pp_current < pp_end ; pp_current++ )
1059 if( (*pp_current)->p_internals->b_attached )
1061 PrintObject( *pp_current, "" );
1065 printf( " o %.8i %s (not attached)\n",
1066 (*pp_current)->i_object_id,
1067 (*pp_current)->psz_object_type );
1071 vlc_mutex_unlock( &structure_lock );
1075 vlc_object_t *p_object = NULL;
1077 if( *newval.psz_string )
1080 int i_id = strtol( newval.psz_string, &end, 0 );
1081 if( end != newval.psz_string )
1082 p_object = vlc_object_get( i_id );
1085 /* try using the object's name to find it */
1086 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1089 /* Look in p_libvlc's children tree */
1090 p_object = vlc_object_find_name( p_libvlc,
1093 vlc_object_release( p_libvlc );
1097 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1098 p_object = vlc_object_find_name( p_this,
1110 vlc_mutex_lock( &structure_lock );
1112 if( *psz_cmd == 't' )
1114 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1117 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1120 DumpStructure( p_object, 0, psz_foo );
1122 else if( *psz_cmd == 'v' )
1127 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1129 PrintObject( p_object, "" );
1131 if( !p_object->p_internals->i_vars )
1132 printf( " `-o No variables\n" );
1133 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1135 variable_t *p_var = p_object->p_internals->p_vars + i;
1137 const char *psz_type = "unknown";
1138 switch( p_var->i_type & VLC_VAR_TYPE )
1140 #define MYCASE( type, nice ) \
1141 case VLC_VAR_ ## type: \
1144 MYCASE( VOID, "void" );
1145 MYCASE( BOOL, "bool" );
1146 MYCASE( INTEGER, "integer" );
1147 MYCASE( HOTKEY, "hotkey" );
1148 MYCASE( STRING, "string" );
1149 MYCASE( MODULE, "module" );
1150 MYCASE( FILE, "file" );
1151 MYCASE( DIRECTORY, "directory" );
1152 MYCASE( VARIABLE, "variable" );
1153 MYCASE( FLOAT, "float" );
1154 MYCASE( TIME, "time" );
1155 MYCASE( ADDRESS, "address" );
1156 MYCASE( MUTEX, "mutex" );
1157 MYCASE( LIST, "list" );
1160 printf( " %c-o \"%s\" (%s",
1161 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1162 p_var->psz_name, psz_type );
1163 if( p_var->psz_text )
1164 printf( ", %s", p_var->psz_text );
1166 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1167 printf( ", command" );
1168 if( p_var->i_entries )
1169 printf( ", %d callbacks", p_var->i_entries );
1170 switch( p_var->i_type & 0x00f0 )
1176 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1178 case VLC_VAR_INTEGER:
1179 printf( ": %d", p_var->val.i_int );
1181 case VLC_VAR_STRING:
1182 printf( ": \"%s\"", p_var->val.psz_string );
1185 printf( ": %f", p_var->val.f_float );
1188 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1190 case VLC_VAR_ADDRESS:
1191 printf( ": %p", p_var->val.p_address );
1201 vlc_mutex_unlock( &structure_lock );
1203 if( *newval.psz_string )
1205 vlc_object_release( p_object );
1212 /*****************************************************************************
1213 * vlc_list_release: free a list previously allocated by vlc_list_find
1214 *****************************************************************************
1215 * This function decreases the refcount of all objects in the list and
1217 *****************************************************************************/
1218 void vlc_list_release( vlc_list_t *p_list )
1222 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1224 vlc_object_release( p_list->p_values[i_index].p_object );
1227 free( p_list->p_values );
1231 /* Following functions are local */
1233 /*****************************************************************************
1234 * FindIndex: find the index of an object in an array of objects
1235 *****************************************************************************
1236 * This function assumes that p_this can be found in pp_objects. It will not
1237 * crash if p_this cannot be found, but will return a wrong value. It is your
1238 * duty to check the return value if you are not certain that the object could
1239 * be found for sure.
1240 *****************************************************************************/
1241 static int FindIndex( vlc_object_t *p_this,
1242 vlc_object_t **pp_objects, int i_count )
1244 int i_middle = i_count / 2;
1251 if( pp_objects[i_middle] == p_this )
1261 /* We take advantage of the sorted array */
1262 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1264 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1265 i_count - i_middle );
1269 return FindIndex( p_this, pp_objects, i_middle );
1273 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1276 vlc_object_t *p_tmp;
1278 switch( i_mode & 0x000f )
1281 p_tmp = p_this->p_parent;
1284 if( p_tmp->i_object_type == i_type
1285 && p_tmp->p_internals->i_refcount > 0 )
1287 vlc_object_yield_locked( p_tmp );
1292 return FindObject( p_tmp, i_type, i_mode );
1298 for( i = p_this->i_children; i--; )
1300 p_tmp = p_this->pp_children[i];
1301 if( p_tmp->i_object_type == i_type
1302 && p_tmp->p_internals->i_refcount > 0 )
1304 vlc_object_yield_locked( p_tmp );
1307 else if( p_tmp->i_children )
1309 p_tmp = FindObject( p_tmp, i_type, i_mode );
1319 /* Handled in vlc_object_find */
1326 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1327 const char *psz_name,
1331 vlc_object_t *p_tmp;
1333 switch( i_mode & 0x000f )
1336 p_tmp = p_this->p_parent;
1339 if( p_tmp->psz_object_name
1340 && !strcmp( p_tmp->psz_object_name, psz_name )
1341 && p_tmp->p_internals->i_refcount > 0 )
1343 vlc_object_yield_locked( p_tmp );
1348 return FindObjectName( p_tmp, psz_name, i_mode );
1354 for( i = p_this->i_children; i--; )
1356 p_tmp = p_this->pp_children[i];
1357 if( p_tmp->psz_object_name
1358 && !strcmp( p_tmp->psz_object_name, psz_name )
1359 && p_tmp->p_internals->i_refcount > 0 )
1361 vlc_object_yield_locked( p_tmp );
1364 else if( p_tmp->i_children )
1366 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1376 /* Handled in vlc_object_find */
1383 static void DetachObject( vlc_object_t *p_this )
1385 vlc_object_t *p_parent = p_this->p_parent;
1388 /* Remove p_this's parent */
1389 p_this->p_parent = NULL;
1391 /* Remove all of p_parent's children which are p_this */
1392 for( i_index = p_parent->i_children ; i_index-- ; )
1394 if( p_parent->pp_children[i_index] == p_this )
1396 p_parent->i_children--;
1397 for( i = i_index ; i < p_parent->i_children ; i++ )
1399 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1404 if( p_parent->i_children )
1406 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1407 p_parent->i_children * sizeof(vlc_object_t *) );
1411 free( p_parent->pp_children );
1412 p_parent->pp_children = NULL;
1416 /*****************************************************************************
1417 * SetAttachment: recursively set the b_attached flag of a subtree.
1418 *****************************************************************************
1419 * This function is used by the attach and detach functions to propagate
1420 * the b_attached flag in a subtree.
1421 *****************************************************************************/
1422 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1426 for( i_index = p_this->i_children ; i_index-- ; )
1428 SetAttachment( p_this->pp_children[i_index], b_attached );
1431 p_this->p_internals->b_attached = b_attached;
1434 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1436 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1440 if( p_this->psz_object_name )
1442 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1444 psz_name[48] = '\"';
1447 psz_children[0] = '\0';
1448 switch( p_this->i_children )
1453 strcpy( psz_children, ", 1 child" );
1456 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1460 psz_refcount[0] = '\0';
1461 if( p_this->p_internals->i_refcount > 0 )
1462 snprintf( psz_refcount, 19, ", refcount %u",
1463 p_this->p_internals->i_refcount );
1465 psz_thread[0] = '\0';
1466 if( p_this->p_internals->b_thread )
1467 snprintf( psz_thread, 29, " (thread %u)",
1468 #if defined(WIN32) || defined(UNDER_CE)
1469 (unsigned)p_this->p_internals->thread_id.id );
1471 (unsigned)p_this->p_internals->thread_id );
1474 psz_parent[0] = '\0';
1475 if( p_this->p_parent )
1476 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1478 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1479 p_this->i_object_id, p_this->psz_object_type,
1480 psz_name, psz_thread, psz_refcount, psz_children,
1484 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1487 char i_back = psz_foo[i_level];
1488 psz_foo[i_level] = '\0';
1490 PrintObject( p_this, psz_foo );
1492 psz_foo[i_level] = i_back;
1494 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1496 msg_Warn( p_this, "structure tree is too deep" );
1500 for( i = 0 ; i < p_this->i_children ; i++ )
1504 psz_foo[i_level-1] = ' ';
1506 if( psz_foo[i_level-2] == '`' )
1508 psz_foo[i_level-2] = ' ';
1512 if( i == p_this->i_children - 1 )
1514 psz_foo[i_level] = '`';
1518 psz_foo[i_level] = '|';
1521 psz_foo[i_level+1] = '-';
1522 psz_foo[i_level+2] = '\0';
1524 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1528 static vlc_list_t * NewList( int i_count )
1530 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1531 if( p_list == NULL )
1536 p_list->i_count = i_count;
1540 p_list->p_values = NULL;
1544 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1545 if( p_list->p_values == NULL )
1547 p_list->i_count = 0;
1554 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1557 if( p_list == NULL || i_index >= p_list->i_count )
1562 vlc_object_yield_locked( p_object );
1564 p_list->p_values[i_index].p_object = p_object;
1569 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1571 if( p_list == NULL )
1576 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1577 * sizeof( vlc_value_t ) );
1578 if( p_list->p_values == NULL )
1580 p_list->i_count = 0;
1584 vlc_object_yield_locked( p_object );
1586 p_list->p_values[p_list->i_count].p_object = p_object;
1592 static int CountChildren( vlc_object_t *p_this, int i_type )
1594 vlc_object_t *p_tmp;
1597 for( i = 0; i < p_this->i_children; i++ )
1599 p_tmp = p_this->pp_children[i];
1601 if( p_tmp->i_object_type == i_type )
1606 if( p_tmp->i_children )
1608 i_count += CountChildren( p_tmp, i_type );
1615 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1617 vlc_object_t *p_tmp;
1620 for( i = 0; i < p_this->i_children; i++ )
1622 p_tmp = p_this->pp_children[i];
1624 if( p_tmp->i_object_type == i_type )
1626 ListReplace( p_list, p_tmp, p_list->i_count++ );
1629 if( p_tmp->i_children )
1631 ListChildren( p_list, p_tmp, i_type );