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 *****************************************************************************/
68 static int DumpCommand( vlc_object_t *, char const *,
69 vlc_value_t, vlc_value_t, void * );
71 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
72 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
73 static void PrintObject ( vlc_object_t *, const char * );
74 static void DumpStructure ( vlc_object_t *, int, char * );
75 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
77 static vlc_list_t * NewList ( int );
78 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
79 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
80 static int CountChildren ( vlc_object_t *, int );
81 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
83 static void vlc_object_destroy( vlc_object_t *p_this );
84 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
86 /*****************************************************************************
87 * Local structure lock
88 *****************************************************************************/
89 static vlc_mutex_t structure_lock;
91 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
92 int i_type, const char *psz_type )
95 vlc_object_internals_t *p_priv;
98 * VLC objects are laid out as follow:
99 * - first the LibVLC-private per-object data,
100 * - then VLC_COMMON members from vlc_object_t,
101 * - finally, the type-specific data (if any).
103 * This function initializes the LibVLC and common data,
104 * and zeroes the rest.
106 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
110 assert (i_size >= sizeof (vlc_object_t));
111 p_new = (vlc_object_t *)(p_priv + 1);
113 p_new->i_object_type = i_type;
114 p_new->psz_object_type = psz_type;
115 p_new->psz_object_name = NULL;
117 p_new->b_die = false;
118 p_new->b_error = false;
119 p_new->b_dead = false;
120 p_new->b_force = false;
122 p_new->psz_header = NULL;
125 p_new->i_flags = p_this->i_flags
126 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
128 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
130 if( !p_priv->p_vars )
136 libvlc_global_data_t *p_libvlc_global;
139 /* Only the global root object is created out of the blue */
140 p_libvlc_global = (libvlc_global_data_t *)p_new;
141 p_new->p_libvlc = NULL;
143 p_libvlc_global->i_counter = 0;
144 p_libvlc_global->i_objects = 0;
145 p_libvlc_global->pp_objects = NULL;
146 vlc_mutex_init( &structure_lock );
150 p_libvlc_global = vlc_global();
151 if( i_type == VLC_OBJECT_LIBVLC )
152 p_new->p_libvlc = (libvlc_int_t*)p_new;
154 p_new->p_libvlc = p_this->p_libvlc;
157 vlc_spin_init( &p_priv->ref_spin );
158 p_priv->i_refcount = 1;
159 p_priv->pf_destructor = NULL;
160 p_priv->b_thread = false;
161 p_new->p_parent = NULL;
162 p_new->pp_children = NULL;
163 p_new->i_children = 0;
165 p_new->p_private = NULL;
167 /* Initialize mutexes and condvars */
168 vlc_mutex_init( &p_new->object_lock );
169 vlc_cond_init( p_new, &p_new->object_wait );
170 vlc_mutex_init( &p_priv->var_lock );
171 vlc_spin_init( &p_priv->spin );
172 p_priv->pipes[0] = p_priv->pipes[1] = -1;
174 vlc_mutex_lock( &structure_lock );
175 p_new->i_object_id = p_libvlc_global->i_counter++;
176 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
177 * useless to try and recover anything if pp_objects gets smashed. */
178 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
180 vlc_mutex_unlock( &structure_lock );
182 if( i_type == VLC_OBJECT_LIBVLC )
184 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
185 var_AddCallback( p_new, "list", DumpCommand, NULL );
186 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
187 var_AddCallback( p_new, "tree", DumpCommand, NULL );
188 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
189 var_AddCallback( p_new, "vars", DumpCommand, NULL );
197 * Allocates and initializes a vlc object.
199 * @param i_type known object type (all of them are negative integer values),
200 * or object byte size (always positive).
202 * @return the new object, or NULL on error.
204 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
206 const char * psz_type;
211 case VLC_OBJECT_LIBVLC:
212 i_size = sizeof(libvlc_int_t);
215 case VLC_OBJECT_INTF:
216 i_size = sizeof(intf_thread_t);
217 psz_type = "interface";
219 case VLC_OBJECT_DIALOGS:
220 i_size = sizeof(intf_thread_t);
221 psz_type = "dialogs";
223 case VLC_OBJECT_DEMUX:
224 i_size = sizeof(demux_t);
227 case VLC_OBJECT_ACCESS:
228 i_size = sizeof(access_t);
231 case VLC_OBJECT_DECODER:
232 i_size = sizeof(decoder_t);
233 psz_type = "decoder";
235 case VLC_OBJECT_PACKETIZER:
236 i_size = sizeof(decoder_t);
237 psz_type = "packetizer";
239 case VLC_OBJECT_ENCODER:
240 i_size = sizeof(encoder_t);
241 psz_type = "encoder";
243 case VLC_OBJECT_FILTER:
244 i_size = sizeof(filter_t);
247 case VLC_OBJECT_VOUT:
248 i_size = sizeof(vout_thread_t);
249 psz_type = "video output";
251 case VLC_OBJECT_AOUT:
252 i_size = sizeof(aout_instance_t);
253 psz_type = "audio output";
255 case VLC_OBJECT_SOUT:
256 i_size = sizeof(sout_instance_t);
257 psz_type = "stream output";
259 case VLC_OBJECT_OPENGL:
260 i_size = sizeof( vout_thread_t );
263 case VLC_OBJECT_ANNOUNCE:
264 i_size = sizeof( announce_handler_t );
265 psz_type = "announce";
267 case VLC_OBJECT_INTERACTION:
268 i_size = sizeof( interaction_t );
269 psz_type = "interaction";
272 i_size = i_type > (int)sizeof(vlc_object_t)
273 ? i_type : (int)sizeof(vlc_object_t);
274 i_type = VLC_OBJECT_GENERIC;
275 psz_type = "generic";
279 return vlc_custom_create( p_this, i_size, i_type, psz_type );
284 ****************************************************************************
285 * Set the destructor of a vlc object
287 * This function sets the destructor of the vlc object. It will be called
288 * when the object is destroyed when the its refcount reaches 0.
289 * (It is called by the internal function vlc_object_destroy())
290 *****************************************************************************/
291 void __vlc_object_set_destructor( vlc_object_t *p_this,
292 vlc_destructor_t pf_destructor )
294 vlc_object_internals_t *p_priv = vlc_internals(p_this );
295 p_priv->pf_destructor = pf_destructor;
299 ****************************************************************************
300 * Destroy a vlc object (Internal)
302 * This function destroys an object that has been previously allocated with
303 * vlc_object_create. The object's refcount must be zero and it must not be
304 * attached to other objects in any way.
305 *****************************************************************************/
306 static void vlc_object_destroy( vlc_object_t *p_this )
308 vlc_object_internals_t *p_priv = vlc_internals( p_this );
310 /* Objects are always detached beforehand */
311 assert( !p_this->p_parent );
313 /* Send a kill to the object's thread if applicable */
314 vlc_object_kill( p_this );
316 /* If we are running on a thread, wait until it ends */
317 if( p_priv->b_thread )
318 vlc_thread_join( p_this );
320 /* Call the custom "subclass" destructor */
321 if( p_priv->pf_destructor )
322 p_priv->pf_destructor( p_this );
325 if( p_this->i_children )
330 "ERROR: cannot delete object (%i, %s) with %d children\n",
331 p_this->i_object_id, p_this->psz_object_name,
332 p_this->i_children );
334 for( i = 0; i < p_this->i_children; i++ )
337 "ERROR: Remaining children object "
338 "(id:%i, type:%s, name:%s)\n",
339 p_this->pp_children[i]->i_object_id,
340 p_this->pp_children[i]->psz_object_type,
341 p_this->pp_children[i]->psz_object_name );
345 /* Dump libvlc object to ease debugging */
346 vlc_object_dump( p_this->p_libvlc );
351 /* Destroy the associated variables, starting from the end so that
352 * no memmove calls have to be done. */
353 while( p_priv->i_vars )
355 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
358 free( p_priv->p_vars );
359 vlc_mutex_destroy( &p_priv->var_lock );
361 free( p_this->psz_header );
363 if( p_this->p_libvlc == NULL )
365 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
368 assert( p_global == vlc_global() );
370 if( p_global->i_objects > 0 )
373 for( i = 0; i < p_global->i_objects; i++ )
375 /* We are leaking this object */
377 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
378 p_global->pp_objects[i]->i_object_id,
379 p_global->pp_objects[i]->psz_object_type,
380 p_global->pp_objects[i]->psz_object_name );
382 /* Dump libvlc object to ease debugging */
383 vlc_object_dump( p_global->pp_objects[i] );
388 /* Dump libvlc object to ease debugging */
389 vlc_object_dump( p_this );
391 /* Strongly abort, cause we want these to be fixed */
396 /* We are the global object ... no need to lock. */
397 free( p_global->pp_objects );
398 p_global->pp_objects = NULL;
400 vlc_mutex_destroy( &structure_lock );
403 FREENULL( p_this->psz_object_name );
405 #if defined(WIN32) || defined(UNDER_CE)
406 /* if object has an associated thread, close it now */
407 if( p_priv->thread_id )
408 CloseHandle(p_priv->thread_id);
411 vlc_spin_destroy( &p_priv->ref_spin );
412 vlc_mutex_destroy( &p_this->object_lock );
413 vlc_cond_destroy( &p_this->object_wait );
414 vlc_spin_destroy( &p_priv->spin );
415 if( p_priv->pipes[1] != -1 )
416 close( p_priv->pipes[1] );
417 if( p_priv->pipes[0] != -1 )
418 close( p_priv->pipes[0] );
424 /** Inter-object signaling */
426 void __vlc_object_lock( vlc_object_t *obj )
428 vlc_mutex_lock( &obj->object_lock );
431 void __vlc_object_unlock( vlc_object_t *obj )
433 vlc_assert_locked( &obj->object_lock );
434 vlc_mutex_unlock( &obj->object_lock );
438 # include <winsock2.h>
439 # include <ws2tcpip.h>
442 * select()-able pipes emulated using Winsock
444 static int pipe (int fd[2])
447 int addrlen = sizeof (addr);
449 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
450 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
451 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
454 memset (&addr, 0, sizeof (addr));
455 addr.sin_family = AF_INET;
456 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
457 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
458 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
460 || connect (c, (PSOCKADDR)&addr, addrlen))
463 a = accept (l, NULL, NULL);
464 if (a == INVALID_SOCKET)
475 if (l != INVALID_SOCKET)
477 if (c != INVALID_SOCKET)
483 #define read( a, b, c ) recv (a, b, c, 0)
485 #define write( a, b, c ) send (a, b, c, 0)
487 #define close( a ) closesocket (a)
491 * Returns the readable end of a pipe that becomes readable once termination
492 * of the object is requested (vlc_object_kill()).
493 * This can be used to wake-up out of a select() or poll() event loop, such
494 * typically when doing network I/O.
496 * Note that the pipe will remain the same for the lifetime of the object.
497 * DO NOT read the pipe nor close it yourself. Ever.
499 * @param obj object that would be "killed"
500 * @return a readable pipe descriptor, or -1 on error.
502 int __vlc_object_waitpipe( vlc_object_t *obj )
504 int pfd[2] = { -1, -1 };
505 vlc_object_internals_t *internals = vlc_internals( obj );
508 vlc_spin_lock (&internals->spin);
509 if (internals->pipes[0] == -1)
511 /* This can only ever happen if someone killed us without locking: */
512 assert (internals->pipes[1] == -1);
513 vlc_spin_unlock (&internals->spin);
518 vlc_spin_lock (&internals->spin);
519 if (internals->pipes[0] == -1)
521 internals->pipes[0] = pfd[0];
522 internals->pipes[1] = pfd[1];
523 pfd[0] = pfd[1] = -1;
527 vlc_spin_unlock (&internals->spin);
531 /* Race condition: vlc_object_kill() already invoked! */
534 vlc_spin_lock (&internals->spin);
535 fd = internals->pipes[1];
536 internals->pipes[1] = -1;
537 vlc_spin_unlock (&internals->spin);
539 msg_Dbg (obj, "waitpipe: object already dying");
544 /* Race condition: two threads call pipe() - unlikely */
550 return internals->pipes[0];
555 * Waits for the object to be signaled (using vlc_object_signal()).
556 * If the object already has a signal pending, this function will return
557 * immediately. It is asserted that the caller holds the object lock.
559 * @return true if the object is dying and should terminate.
561 bool __vlc_object_wait( vlc_object_t *obj )
563 vlc_assert_locked( &obj->object_lock );
564 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
570 * Waits for the object to be signaled (using vlc_object_signal()), or for
572 * If the object already has a signal pending, this function will return
573 * immediately. It is asserted that the caller holds the object lock.
575 * @return negative if the object is dying and should terminate,
576 * positive if the the object has been signaled but is not dying,
577 * 0 if timeout has been reached.
579 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
583 vlc_assert_locked( &obj->object_lock );
584 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
585 if( v == 0 ) /* signaled */
586 return obj->b_die ? -1 : 1;
592 * Checks whether an object has been "killed".
593 * The object lock must be held.
595 * Typical code for an object thread could be:
597 vlc_object_lock (self);
599 while (vlc_object_alive (self))
603 if (vlc_object_wait (self))
608 ...deinitialization...
609 vlc_object_unlock (self);
612 * @return true iff the object has not been killed yet
614 bool __vlc_object_alive( vlc_object_t *obj )
616 vlc_assert_locked( &obj->object_lock );
622 * Signals an object for which the lock is held.
624 void __vlc_object_signal_unlocked( vlc_object_t *obj )
626 vlc_assert_locked (&obj->object_lock);
627 vlc_cond_signal( &obj->object_wait );
632 * Requests termination of an object.
633 * If the object is LibVLC, also request to terminate all its children.
635 void __vlc_object_kill( vlc_object_t *p_this )
637 vlc_object_internals_t *internals = vlc_internals( p_this );
640 vlc_mutex_lock( &p_this->object_lock );
641 p_this->b_die = true;
643 vlc_spin_lock (&internals->spin);
644 fd = internals->pipes[1];
645 internals->pipes[1] = -1;
646 vlc_spin_unlock (&internals->spin);
650 msg_Dbg (p_this, "waitpipe: object killed");
654 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
655 for( int i = 0; i < p_this->i_children ; i++ )
656 vlc_object_kill( p_this->pp_children[i] );
658 vlc_object_signal_unlocked( p_this );
659 vlc_mutex_unlock( &p_this->object_lock );
664 * find an object given its ID
666 * This function looks for the object whose i_object_id field is i_id. We
667 * use a dichotomy so that lookups are in log2(n).
668 *****************************************************************************/
669 void * vlc_object_get( int i_id )
672 vlc_object_t **pp_objects;
673 libvlc_global_data_t *p_libvlc_global = vlc_global();
674 vlc_object_t *obj = NULL;
676 vlc_mutex_lock( &structure_lock );
678 pp_objects = p_libvlc_global->pp_objects;
680 /* Perform our dichotomy */
681 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
683 i_middle = i_max / 2;
685 if( pp_objects[i_middle]->i_object_id > i_id )
689 else if( pp_objects[i_middle]->i_object_id < i_id )
693 pp_objects += i_middle;
698 /* This happens when there are only two remaining objects */
699 if( pp_objects[i_middle+1]->i_object_id == i_id )
701 vlc_object_yield( pp_objects[i_middle+1] );
702 obj = pp_objects[i_middle+1];
709 vlc_object_yield( pp_objects[i_middle] );
710 obj = pp_objects[i_middle];
715 vlc_mutex_unlock( &structure_lock );
720 ****************************************************************************
721 * find a typed object and increment its refcount
722 *****************************************************************************
723 * This function recursively looks for a given object type. i_mode can be one
724 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
725 *****************************************************************************/
726 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
728 vlc_object_t *p_found;
730 /* If we are of the requested type ourselves, don't look further */
731 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
733 vlc_object_yield( p_this );
737 vlc_mutex_lock( &structure_lock );
739 /* Otherwise, recursively look for the object */
740 if( (i_mode & 0x000f) == FIND_ANYWHERE )
742 vlc_object_t *p_root = p_this;
745 while( p_root->p_parent != NULL &&
746 p_root != VLC_OBJECT( p_this->p_libvlc ) )
748 p_root = p_root->p_parent;
751 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
752 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
754 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
755 i_type, (i_mode & ~0x000f)|FIND_CHILD );
760 p_found = FindObject( p_this, i_type, i_mode );
763 vlc_mutex_unlock( &structure_lock );
769 ****************************************************************************
770 * find a named object and increment its refcount
771 *****************************************************************************
772 * This function recursively looks for a given object name. i_mode can be one
773 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
774 *****************************************************************************/
775 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
778 vlc_object_t *p_found;
780 /* If have the requested name ourselves, don't look further */
781 if( !(i_mode & FIND_STRICT)
782 && p_this->psz_object_name
783 && !strcmp( p_this->psz_object_name, psz_name ) )
785 vlc_object_yield( p_this );
789 vlc_mutex_lock( &structure_lock );
791 /* Otherwise, recursively look for the object */
792 if( (i_mode & 0x000f) == FIND_ANYWHERE )
794 vlc_object_t *p_root = p_this;
797 while( p_root->p_parent != NULL &&
798 p_root != VLC_OBJECT( p_this->p_libvlc ) )
800 p_root = p_root->p_parent;
803 p_found = FindObjectName( p_root, psz_name,
804 (i_mode & ~0x000f)|FIND_CHILD );
805 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
807 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
808 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
813 p_found = FindObjectName( p_this, psz_name, i_mode );
816 vlc_mutex_unlock( &structure_lock );
822 * Increment an object reference counter.
824 void __vlc_object_yield( vlc_object_t *p_this )
826 vlc_object_internals_t *internals = vlc_internals( p_this );
828 vlc_spin_lock( &internals->ref_spin );
829 /* Avoid obvious freed object uses */
830 assert( internals->i_refcount > 0 );
831 /* Increment the counter */
832 internals->i_refcount++;
833 vlc_spin_unlock( &internals->ref_spin );
836 /*****************************************************************************
837 * decrement an object refcount
838 * And destroy the object if its refcount reach zero.
839 *****************************************************************************/
840 void __vlc_object_release( vlc_object_t *p_this )
842 vlc_object_internals_t *internals = vlc_internals( p_this );
844 vlc_spin_lock( &internals->ref_spin );
845 assert( internals->i_refcount > 0 );
847 if( --internals->i_refcount )
850 /* There are still other references to the object */
851 vlc_spin_unlock( &internals->ref_spin );
854 vlc_spin_unlock( &internals->ref_spin );
856 /* Slow path : object destruction */
857 /* Remember that we cannot hold the spin while waiting on the mutex */
858 vlc_mutex_lock( &structure_lock );
860 /* Remove the object from the table
861 * so that it cannot be encountered by vlc_object_get() */
862 libvlc_global_data_t *p_libvlc_global = vlc_global();
865 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
866 p_libvlc_global->i_objects );
867 REMOVE_ELEM( p_libvlc_global->pp_objects,
868 p_libvlc_global->i_objects, i_index );
870 /* Detach from parent to protect against FIND_CHILDREN */
871 if (p_this->p_parent)
872 vlc_object_detach_unlocked (p_this);
873 /* Detach from children to protect against FIND_PARENT */
874 for (int i = 0; i < p_this->i_children; i++)
875 vlc_object_detach_unlocked( p_this->pp_children[i] );
877 vlc_mutex_unlock( &structure_lock );
879 vlc_object_destroy( p_this );
883 ****************************************************************************
884 * attach object to a parent object
885 *****************************************************************************
886 * This function sets p_this as a child of p_parent, and p_parent as a parent
887 * of p_this. This link can be undone using vlc_object_detach.
888 *****************************************************************************/
889 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
891 if( !p_this ) return;
893 vlc_mutex_lock( &structure_lock );
895 /* Attach the parent to its child */
896 assert (!p_this->p_parent);
897 p_this->p_parent = p_parent;
899 /* Attach the child to its parent */
900 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
901 p_parent->i_children, p_this );
903 vlc_mutex_unlock( &structure_lock );
907 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
909 assert (p_this->p_parent);
911 vlc_object_t *p_parent = p_this->p_parent;
914 /* Remove p_this's parent */
915 p_this->p_parent = NULL;
917 /* Remove all of p_parent's children which are p_this */
918 for( i_index = p_parent->i_children ; i_index-- ; )
920 if( p_parent->pp_children[i_index] == p_this )
922 p_parent->i_children--;
923 for( i = i_index ; i < p_parent->i_children ; i++ )
925 p_parent->pp_children[i] = p_parent->pp_children[i+1];
930 if( p_parent->i_children )
932 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
933 p_parent->i_children * sizeof(vlc_object_t *) );
937 /* Special case - don't realloc() to zero to avoid leaking */
938 free( p_parent->pp_children );
939 p_parent->pp_children = NULL;
945 ****************************************************************************
946 * detach object from its parent
947 *****************************************************************************
948 * This function removes all links between an object and its parent.
949 *****************************************************************************/
950 void __vlc_object_detach( vlc_object_t *p_this )
952 if( !p_this ) return;
954 vlc_mutex_lock( &structure_lock );
956 if( !p_this->p_parent )
958 msg_Err( p_this, "object is not attached" );
959 vlc_mutex_unlock( &structure_lock );
963 vlc_object_detach_unlocked( p_this );
964 vlc_mutex_unlock( &structure_lock );
969 ****************************************************************************
970 * find a list typed objects and increment their refcount
971 *****************************************************************************
972 * This function recursively looks for a given object type. i_mode can be one
973 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
974 *****************************************************************************/
975 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
980 /* Look for the objects */
981 switch( i_mode & 0x000f )
984 return vlc_list_find (vlc_global (), i_type, FIND_CHILD);
987 vlc_mutex_lock( &structure_lock );
988 i_count = CountChildren( p_this, i_type );
989 p_list = NewList( i_count );
991 /* Check allocation was successful */
992 if( p_list->i_count != i_count )
994 msg_Err( p_this, "list allocation failed!" );
1000 ListChildren( p_list, p_this, i_type );
1001 vlc_mutex_unlock( &structure_lock );
1005 msg_Err( p_this, "unimplemented!" );
1006 p_list = NewList( 0 );
1013 /*****************************************************************************
1014 * DumpCommand: print the current vlc structure
1015 *****************************************************************************
1016 * This function prints either an ASCII tree showing the connections between
1017 * vlc objects, and additional information such as their refcount, thread ID,
1018 * etc. (command "tree"), or the same data as a simple list (command "list").
1019 *****************************************************************************/
1020 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1021 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1023 libvlc_global_data_t *p_libvlc_global = vlc_global();
1025 (void)oldval; (void)p_data;
1026 if( *psz_cmd == 'l' )
1028 vlc_mutex_lock( &structure_lock );
1030 vlc_object_t **pp_current, **pp_end;
1032 pp_current = p_libvlc_global->pp_objects;
1033 pp_end = pp_current + p_libvlc_global->i_objects;
1035 for( ; pp_current < pp_end ; pp_current++ )
1036 PrintObject( *pp_current, "" );
1038 vlc_mutex_unlock( &structure_lock );
1042 vlc_object_t *p_object = NULL;
1044 if( *newval.psz_string )
1047 int i_id = strtol( newval.psz_string, &end, 0 );
1048 if( end != newval.psz_string )
1049 p_object = vlc_object_get( i_id );
1052 /* try using the object's name to find it */
1053 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1056 /* Look in p_libvlc's children tree */
1057 p_object = vlc_object_find_name( p_libvlc,
1060 vlc_object_release( p_libvlc );
1064 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1065 p_object = vlc_object_find_name( p_this,
1077 vlc_mutex_lock( &structure_lock );
1079 if( *psz_cmd == 't' )
1081 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1084 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1087 DumpStructure( p_object, 0, psz_foo );
1089 else if( *psz_cmd == 'v' )
1094 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1096 PrintObject( p_object, "" );
1098 if( !vlc_internals( p_object )->i_vars )
1099 printf( " `-o No variables\n" );
1100 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1102 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1104 const char *psz_type = "unknown";
1105 switch( p_var->i_type & VLC_VAR_TYPE )
1107 #define MYCASE( type, nice ) \
1108 case VLC_VAR_ ## type: \
1111 MYCASE( VOID, "void" );
1112 MYCASE( BOOL, "bool" );
1113 MYCASE( INTEGER, "integer" );
1114 MYCASE( HOTKEY, "hotkey" );
1115 MYCASE( STRING, "string" );
1116 MYCASE( MODULE, "module" );
1117 MYCASE( FILE, "file" );
1118 MYCASE( DIRECTORY, "directory" );
1119 MYCASE( VARIABLE, "variable" );
1120 MYCASE( FLOAT, "float" );
1121 MYCASE( TIME, "time" );
1122 MYCASE( ADDRESS, "address" );
1123 MYCASE( MUTEX, "mutex" );
1124 MYCASE( LIST, "list" );
1127 printf( " %c-o \"%s\" (%s",
1128 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1129 p_var->psz_name, psz_type );
1130 if( p_var->psz_text )
1131 printf( ", %s", p_var->psz_text );
1133 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1134 printf( ", command" );
1135 if( p_var->i_entries )
1136 printf( ", %d callbacks", p_var->i_entries );
1137 switch( p_var->i_type & 0x00f0 )
1143 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1145 case VLC_VAR_INTEGER:
1146 printf( ": %d", p_var->val.i_int );
1148 case VLC_VAR_STRING:
1149 printf( ": \"%s\"", p_var->val.psz_string );
1152 printf( ": %f", p_var->val.f_float );
1155 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1157 case VLC_VAR_ADDRESS:
1158 printf( ": %p", p_var->val.p_address );
1168 vlc_mutex_unlock( &structure_lock );
1170 if( *newval.psz_string )
1172 vlc_object_release( p_object );
1179 /*****************************************************************************
1180 * vlc_list_release: free a list previously allocated by vlc_list_find
1181 *****************************************************************************
1182 * This function decreases the refcount of all objects in the list and
1184 *****************************************************************************/
1185 void vlc_list_release( vlc_list_t *p_list )
1189 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1191 vlc_object_release( p_list->p_values[i_index].p_object );
1194 free( p_list->p_values );
1198 /*****************************************************************************
1199 * dump an object. (Debug function)
1200 *****************************************************************************/
1201 void __vlc_object_dump( vlc_object_t *p_this )
1203 vlc_mutex_lock( &structure_lock );
1204 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1206 DumpStructure( p_this, 0, psz_foo );
1207 vlc_mutex_unlock( &structure_lock );
1210 /* Following functions are local */
1212 /*****************************************************************************
1213 * FindIndex: find the index of an object in an array of objects
1214 *****************************************************************************
1215 * This function assumes that p_this can be found in pp_objects. It will not
1216 * crash if p_this cannot be found, but will return a wrong value. It is your
1217 * duty to check the return value if you are not certain that the object could
1218 * be found for sure.
1219 *****************************************************************************/
1220 static int FindIndex( vlc_object_t *p_this,
1221 vlc_object_t **pp_objects, int i_count )
1223 int i_middle = i_count / 2;
1230 if( pp_objects[i_middle] == p_this )
1240 /* We take advantage of the sorted array */
1241 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1243 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1244 i_count - i_middle );
1248 return FindIndex( p_this, pp_objects, i_middle );
1252 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1255 vlc_object_t *p_tmp;
1257 switch( i_mode & 0x000f )
1260 p_tmp = p_this->p_parent;
1263 if( p_tmp->i_object_type == i_type )
1265 vlc_object_yield( p_tmp );
1270 return FindObject( p_tmp, i_type, i_mode );
1276 for( i = p_this->i_children; i--; )
1278 p_tmp = p_this->pp_children[i];
1279 if( p_tmp->i_object_type == i_type )
1281 vlc_object_yield( p_tmp );
1284 else if( p_tmp->i_children )
1286 p_tmp = FindObject( p_tmp, i_type, i_mode );
1296 /* Handled in vlc_object_find */
1303 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1304 const char *psz_name,
1308 vlc_object_t *p_tmp;
1310 switch( i_mode & 0x000f )
1313 p_tmp = p_this->p_parent;
1316 if( p_tmp->psz_object_name
1317 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1319 vlc_object_yield( p_tmp );
1324 return FindObjectName( p_tmp, psz_name, i_mode );
1330 for( i = p_this->i_children; i--; )
1332 p_tmp = p_this->pp_children[i];
1333 if( p_tmp->psz_object_name
1334 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1336 vlc_object_yield( p_tmp );
1339 else if( p_tmp->i_children )
1341 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1351 /* Handled in vlc_object_find */
1359 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1361 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1364 memset( &psz_name, 0, sizeof(psz_name) );
1365 if( p_this->psz_object_name )
1367 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1369 psz_name[48] = '\"';
1372 psz_children[0] = '\0';
1373 switch( p_this->i_children )
1378 strcpy( psz_children, ", 1 child" );
1381 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1385 psz_refcount[0] = '\0';
1386 if( vlc_internals( p_this )->i_refcount > 0 )
1387 snprintf( psz_refcount, 19, ", refcount %u",
1388 vlc_internals( p_this )->i_refcount );
1390 psz_thread[0] = '\0';
1391 if( vlc_internals( p_this )->b_thread )
1392 snprintf( psz_thread, 29, " (thread %lu)",
1393 (unsigned long)vlc_internals( p_this )->thread_id );
1395 psz_parent[0] = '\0';
1396 if( p_this->p_parent )
1397 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1399 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1400 p_this->i_object_id, p_this->psz_object_type,
1401 psz_name, psz_thread, psz_refcount, psz_children,
1405 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1408 char i_back = psz_foo[i_level];
1409 psz_foo[i_level] = '\0';
1411 PrintObject( p_this, psz_foo );
1413 psz_foo[i_level] = i_back;
1415 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1417 msg_Warn( p_this, "structure tree is too deep" );
1421 for( i = 0 ; i < p_this->i_children ; i++ )
1425 psz_foo[i_level-1] = ' ';
1427 if( psz_foo[i_level-2] == '`' )
1429 psz_foo[i_level-2] = ' ';
1433 if( i == p_this->i_children - 1 )
1435 psz_foo[i_level] = '`';
1439 psz_foo[i_level] = '|';
1442 psz_foo[i_level+1] = '-';
1443 psz_foo[i_level+2] = '\0';
1445 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1449 static vlc_list_t * NewList( int i_count )
1451 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1452 if( p_list == NULL )
1457 p_list->i_count = i_count;
1461 p_list->p_values = NULL;
1465 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1466 if( p_list->p_values == NULL )
1468 p_list->i_count = 0;
1475 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1478 if( p_list == NULL || i_index >= p_list->i_count )
1483 vlc_object_yield( p_object );
1485 p_list->p_values[i_index].p_object = p_object;
1490 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1492 if( p_list == NULL )
1497 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1498 * sizeof( vlc_value_t ) );
1499 if( p_list->p_values == NULL )
1501 p_list->i_count = 0;
1505 vlc_object_yield( p_object );
1507 p_list->p_values[p_list->i_count].p_object = p_object;
1513 static int CountChildren( vlc_object_t *p_this, int i_type )
1515 vlc_object_t *p_tmp;
1518 for( i = 0; i < p_this->i_children; i++ )
1520 p_tmp = p_this->pp_children[i];
1522 if( p_tmp->i_object_type == i_type )
1527 if( p_tmp->i_children )
1529 i_count += CountChildren( p_tmp, i_type );
1536 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1538 vlc_object_t *p_tmp;
1541 for( i = 0; i < p_this->i_children; i++ )
1543 p_tmp = p_this->pp_children[i];
1545 if( p_tmp->i_object_type == i_type )
1547 ListReplace( p_list, p_tmp, p_list->i_count++ );
1550 if( p_tmp->i_children )
1552 ListChildren( p_list, p_tmp, i_type );