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_OSDMENU:
321 i_size = sizeof( osd_menu_t );
322 psz_type = "osd menu";
324 case VLC_OBJECT_INTERACTION:
325 i_size = sizeof( interaction_t );
326 psz_type = "interaction";
329 i_size = i_type > (int)sizeof(vlc_object_t)
330 ? i_type : (int)sizeof(vlc_object_t);
331 i_type = VLC_OBJECT_GENERIC;
332 psz_type = "generic";
336 return vlc_custom_create( p_this, i_size, i_type, psz_type );
341 ****************************************************************************
342 * Set the destructor of a vlc object
344 * This function sets the destructor of the vlc object. It will be called
345 * when the object is destroyed when the its refcount reaches 0.
346 * (It is called by the internal function vlc_object_destroy())
347 *****************************************************************************/
348 void __vlc_object_set_destructor( vlc_object_t *p_this,
349 vlc_destructor_t pf_destructor )
351 vlc_object_internals_t *p_priv = vlc_internals(p_this );
353 vlc_mutex_lock( &structure_lock );
354 p_priv->pf_destructor = pf_destructor;
355 vlc_mutex_unlock( &structure_lock );
359 ****************************************************************************
360 * Destroy a vlc object (Internal)
362 * This function destroys an object that has been previously allocated with
363 * vlc_object_create. The object's refcount must be zero and it must not be
364 * attached to other objects in any way.
365 *****************************************************************************/
366 static void vlc_object_destroy( vlc_object_t *p_this )
368 vlc_object_internals_t *p_priv = vlc_internals( p_this );
370 /* Automatically detach the object from its parents */
371 if( p_this->p_parent ) vlc_object_detach( p_this );
374 /* Send a kill to the object's thread if applicable */
375 vlc_object_kill( p_this );
377 /* If we are running on a thread, wait until it ends */
378 if( p_priv->b_thread )
379 vlc_thread_join( p_this );
381 /* Call the custom "subclass" destructor */
382 if( p_priv->pf_destructor )
383 p_priv->pf_destructor( p_this );
386 if( p_this->i_children )
391 "ERROR: cannot delete object (%i, %s) with %d children\n",
392 p_this->i_object_id, p_this->psz_object_name,
393 p_this->i_children );
395 for( i = 0; i < p_this->i_children; i++ )
398 "ERROR: Remaining children object "
399 "(id:%i, type:%s, name:%s)\n",
400 p_this->pp_children[i]->i_object_id,
401 p_this->pp_children[i]->psz_object_type,
402 p_this->pp_children[i]->psz_object_name );
408 /* Destroy the associated variables, starting from the end so that
409 * no memmove calls have to be done. */
410 while( p_priv->i_vars )
412 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
415 free( p_priv->p_vars );
416 vlc_mutex_destroy( &p_priv->var_lock );
418 free( p_this->psz_header );
420 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
422 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
425 if( p_global->i_objects > 0 )
428 for( i = 0; i < p_global->i_objects; i++ )
430 /* We are leaking this object */
432 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
433 p_global->pp_objects[i]->i_object_id,
434 p_global->pp_objects[i]->psz_object_type,
435 p_global->pp_objects[i]->psz_object_name );
438 /* Strongly abort, cause we want these to be fixed */
442 /* We are the global object ... no need to lock. */
443 free( p_global->pp_objects );
444 p_global->pp_objects = NULL;
446 vlc_mutex_destroy( &structure_lock );
449 #if defined(WIN32) || defined(UNDER_CE)
450 /* if object has an associated thread, close it now */
451 if( p_priv->thread_id.hThread )
452 CloseHandle(p_priv->thread_id.hThread);
455 vlc_mutex_destroy( &p_this->object_lock );
456 vlc_cond_destroy( &p_this->object_wait );
457 vlc_spin_destroy( &p_priv->spin );
458 if( p_priv->pipes[1] != -1 )
459 close( p_priv->pipes[1] );
460 if( p_priv->pipes[0] != -1 )
461 close( p_priv->pipes[0] );
463 /* global is not dynamically allocated by vlc_object_create */
464 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
469 /** Inter-object signaling */
471 void __vlc_object_lock( vlc_object_t *obj )
473 vlc_mutex_lock( &obj->object_lock );
476 void __vlc_object_unlock( vlc_object_t *obj )
478 vlc_assert_locked( &obj->object_lock );
479 vlc_mutex_unlock( &obj->object_lock );
483 # include <winsock2.h>
484 # include <ws2tcpip.h>
487 * select()-able pipes emulated using Winsock
489 static int pipe (int fd[2])
492 int addrlen = sizeof (addr);
494 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
495 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
496 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
499 memset (&addr, 0, sizeof (addr));
500 addr.sin_family = AF_INET;
501 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
502 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
503 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
505 || connect (c, (PSOCKADDR)&addr, addrlen))
508 a = accept (l, NULL, NULL);
509 if (a == INVALID_SOCKET)
520 if (l != INVALID_SOCKET)
522 if (c != INVALID_SOCKET)
528 #define read( a, b, c ) recv (a, b, c, 0)
530 #define write( a, b, c ) send (a, b, c, 0)
532 #define close( a ) closesocket (a)
536 * Returns the readable end of a pipe that becomes readable once termination
537 * of the object is requested (vlc_object_kill()).
538 * This can be used to wake-up out of a select() or poll() event loop, such
539 * typically when doing network I/O.
541 * Note that the pipe will remain the same for the lifetime of the object.
542 * DO NOT read the pipe nor close it yourself. Ever.
544 * @param obj object that would be "killed"
545 * @return a readable pipe descriptor, or -1 on error.
547 int __vlc_object_waitpipe( vlc_object_t *obj )
549 int pfd[2] = { -1, -1 };
550 struct vlc_object_internals_t *internals = obj->p_internals;
551 vlc_bool_t killed = VLC_FALSE;
553 vlc_spin_lock (&internals->spin);
554 if (internals->pipes[0] == -1)
556 /* This can only ever happen if someone killed us without locking: */
557 assert (internals->pipes[1] == -1);
558 vlc_spin_unlock (&internals->spin);
563 vlc_spin_lock (&internals->spin);
564 if (internals->pipes[0] == -1)
566 internals->pipes[0] = pfd[0];
567 internals->pipes[1] = pfd[1];
568 pfd[0] = pfd[1] = -1;
572 vlc_spin_unlock (&internals->spin);
576 /* Race condition: vlc_object_kill() already invoked! */
579 vlc_spin_lock (&internals->spin);
580 fd = internals->pipes[1];
581 internals->pipes[1] = -1;
582 vlc_spin_unlock (&internals->spin);
584 msg_Dbg (obj, "waitpipe: object already dying");
589 /* Race condition: two threads call pipe() - unlikely */
595 return internals->pipes[0];
600 * Waits for the object to be signaled (using vlc_object_signal()).
601 * If the object already has a signal pending, this function will return
602 * immediately. It is asserted that the caller holds the object lock.
604 * @return true if the object is dying and should terminate.
606 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
608 vlc_assert_locked( &obj->object_lock );
609 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
615 * Waits for the object to be signaled (using vlc_object_signal()), or for
617 * If the object already has a signal pending, this function will return
618 * immediately. It is asserted that the caller holds the object lock.
620 * @return negative if the object is dying and should terminate,
621 * positive if the the object has been signaled but is not dying,
622 * 0 if timeout has been reached.
624 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
628 vlc_assert_locked( &obj->object_lock );
629 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
630 if( v == 0 ) /* signaled */
631 return obj->b_die ? -1 : 1;
637 * Checks whether an object has been "killed".
638 * The object lock must be held.
640 * Typical code for an object thread could be:
642 vlc_object_lock (self);
644 while (vlc_object_alive (self))
648 if (vlc_object_wait (self))
653 ...deinitialization...
654 vlc_object_unlock (self);
657 * @return true iff the object has not been killed yet
659 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
661 vlc_assert_locked( &obj->object_lock );
667 * Signals an object for which the lock is held.
669 void __vlc_object_signal_unlocked( vlc_object_t *obj )
671 vlc_assert_locked (&obj->object_lock);
672 vlc_cond_signal( &obj->object_wait );
677 * Requests termination of an object.
678 * If the object is LibVLC, also request to terminate all its children.
680 void __vlc_object_kill( vlc_object_t *p_this )
682 struct vlc_object_internals_t *internals = p_this->p_internals;
685 vlc_mutex_lock( &p_this->object_lock );
686 p_this->b_die = VLC_TRUE;
688 vlc_spin_lock (&internals->spin);
689 fd = internals->pipes[1];
690 internals->pipes[1] = -1;
691 vlc_spin_unlock (&internals->spin);
695 msg_Dbg (p_this, "waitpipe: object killed");
699 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
700 for( int i = 0; i < p_this->i_children ; i++ )
701 vlc_object_kill( p_this->pp_children[i] );
703 vlc_object_signal_unlocked( p_this );
704 vlc_mutex_unlock( &p_this->object_lock );
709 * find an object given its ID
711 * This function looks for the object whose i_object_id field is i_id. We
712 * use a dichotomy so that lookups are in log2(n).
713 *****************************************************************************/
714 void * vlc_object_get( int i_id )
717 vlc_object_t **pp_objects;
718 libvlc_global_data_t *p_libvlc_global = vlc_global();
720 vlc_mutex_lock( &structure_lock );
722 pp_objects = p_libvlc_global->pp_objects;
724 /* Perform our dichotomy */
725 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
727 i_middle = i_max / 2;
729 if( pp_objects[i_middle]->i_object_id > i_id )
733 else if( pp_objects[i_middle]->i_object_id < i_id )
737 pp_objects += i_middle;
742 /* This happens when there are only two remaining objects */
743 if( pp_objects[i_middle+1]->i_object_id == i_id
744 && pp_objects[i_middle+1]->p_internals->i_refcount > 0 )
746 vlc_object_yield_locked( pp_objects[i_middle+1] );
747 vlc_mutex_unlock( &structure_lock );
748 return pp_objects[i_middle+1];
753 else if( pp_objects[i_middle]->p_internals->i_refcount > 0 )
755 vlc_object_yield_locked( pp_objects[i_middle] );
756 vlc_mutex_unlock( &structure_lock );
757 return pp_objects[i_middle];
762 /* this means that i_max == i_middle, and since we have already
763 * tested pp_objects[i_middle]), p_found is properly set. */
768 vlc_mutex_unlock( &structure_lock );
773 ****************************************************************************
774 * find a typed object and increment its refcount
775 *****************************************************************************
776 * This function recursively looks for a given object type. i_mode can be one
777 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
778 *****************************************************************************/
779 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
781 vlc_object_t *p_found;
783 vlc_mutex_lock( &structure_lock );
785 /* If we are of the requested type ourselves, don't look further */
786 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type
787 && p_this->p_internals->i_refcount > 0 )
789 vlc_object_yield_locked( p_this );
790 vlc_mutex_unlock( &structure_lock );
794 /* Otherwise, recursively look for the object */
795 if( (i_mode & 0x000f) == FIND_ANYWHERE )
797 vlc_object_t *p_root = p_this;
800 while( p_root->p_parent != NULL &&
801 p_root != VLC_OBJECT( p_this->p_libvlc ) )
803 p_root = p_root->p_parent;
806 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
807 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
809 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
810 i_type, (i_mode & ~0x000f)|FIND_CHILD );
815 p_found = FindObject( p_this, i_type, i_mode );
818 vlc_mutex_unlock( &structure_lock );
824 ****************************************************************************
825 * find a named object and increment its refcount
826 *****************************************************************************
827 * This function recursively looks for a given object name. i_mode can be one
828 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
829 *****************************************************************************/
830 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
833 vlc_object_t *p_found;
835 vlc_mutex_lock( &structure_lock );
837 /* Avoid obvious freed object uses */
838 assert( p_this->p_internals->i_refcount > 0 );
840 /* If have the requested name ourselves, don't look further */
841 if( !(i_mode & FIND_STRICT)
842 && p_this->psz_object_name
843 && !strcmp( p_this->psz_object_name, psz_name )
844 && p_this->p_internals->i_refcount > 0 )
846 vlc_object_yield_locked( p_this );
847 vlc_mutex_unlock( &structure_lock );
851 /* Otherwise, recursively look for the object */
852 if( (i_mode & 0x000f) == FIND_ANYWHERE )
854 vlc_object_t *p_root = p_this;
857 while( p_root->p_parent != NULL &&
858 p_root != VLC_OBJECT( p_this->p_libvlc ) )
860 p_root = p_root->p_parent;
863 p_found = FindObjectName( p_root, psz_name,
864 (i_mode & ~0x000f)|FIND_CHILD );
865 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
867 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
868 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
873 p_found = FindObjectName( p_this, psz_name, i_mode );
876 vlc_mutex_unlock( &structure_lock );
882 ****************************************************************************
883 * increment an object refcount
884 *****************************************************************************/
886 /* When the structure_lock is locked */
887 static void vlc_object_yield_locked( vlc_object_t *p_this )
889 vlc_assert_locked (&structure_lock);
891 /* Avoid obvious freed object uses */
892 assert( p_this->p_internals->i_refcount > 0 );
894 /* Increment the counter */
895 p_this->p_internals->i_refcount++;
898 /* Public function */
899 void __vlc_object_yield( vlc_object_t *p_this )
901 vlc_mutex_lock( &structure_lock );
902 vlc_object_yield_locked( p_this );
903 vlc_mutex_unlock( &structure_lock );
907 /*****************************************************************************
908 * decrement an object refcount
909 * And destroy the object if its refcount reach zero.
910 *****************************************************************************/
911 void __vlc_object_release( vlc_object_t *p_this )
913 vlc_bool_t b_should_destroy;
915 vlc_mutex_lock( &structure_lock );
917 assert( p_this->p_internals->i_refcount > 0 );
918 p_this->p_internals->i_refcount--;
919 b_should_destroy = (p_this->p_internals->i_refcount == 0);
921 if( b_should_destroy )
923 /* Make sure this object can't be obtained via vlc_find_object now that
925 libvlc_global_data_t *p_libvlc_global = vlc_global();
928 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
929 * useless to try and recover anything if pp_objects gets smashed. */
930 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
931 p_libvlc_global->i_objects );
932 REMOVE_ELEM( p_libvlc_global->pp_objects,
933 p_libvlc_global->i_objects, i_index );
936 vlc_mutex_unlock( &structure_lock );
938 if( b_should_destroy )
939 vlc_object_destroy( p_this );
943 ****************************************************************************
944 * attach object to a parent object
945 *****************************************************************************
946 * This function sets p_this as a child of p_parent, and p_parent as a parent
947 * of p_this. This link can be undone using vlc_object_detach.
948 *****************************************************************************/
949 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
951 if( !p_this ) return;
953 vlc_mutex_lock( &structure_lock );
955 /* Avoid obvious freed object uses */
956 assert( p_this->p_internals->i_refcount > 0 );
958 /* Attach the parent to its child */
959 p_this->p_parent = p_parent;
961 /* Attach the child to its parent */
962 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
963 p_parent->i_children, p_this );
965 /* Climb up the tree to see whether we are connected with the root */
966 if( p_parent->p_internals->b_attached )
968 SetAttachment( p_this, VLC_TRUE );
971 vlc_mutex_unlock( &structure_lock );
975 ****************************************************************************
976 * detach object from its parent
977 *****************************************************************************
978 * This function removes all links between an object and its parent.
979 *****************************************************************************/
980 void __vlc_object_detach( vlc_object_t *p_this )
982 if( !p_this ) return;
984 vlc_mutex_lock( &structure_lock );
986 if( !p_this->p_parent )
988 msg_Err( p_this, "object is not attached" );
989 vlc_mutex_unlock( &structure_lock );
993 /* Climb up the tree to see whether we are connected with the root */
994 if( p_this->p_parent->p_internals->b_attached )
996 SetAttachment( p_this, VLC_FALSE );
999 DetachObject( p_this );
1000 vlc_mutex_unlock( &structure_lock );
1005 ****************************************************************************
1006 * find a list typed objects and increment their refcount
1007 *****************************************************************************
1008 * This function recursively looks for a given object type. i_mode can be one
1009 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
1010 *****************************************************************************/
1011 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
1014 vlc_object_t **pp_current, **pp_end;
1015 int i_count = 0, i_index = 0;
1016 libvlc_global_data_t *p_libvlc_global = vlc_global();
1018 vlc_mutex_lock( &structure_lock );
1020 /* Look for the objects */
1021 switch( i_mode & 0x000f )
1024 pp_current = p_libvlc_global->pp_objects;
1025 pp_end = pp_current + p_libvlc_global->i_objects;
1027 for( ; pp_current < pp_end ; pp_current++ )
1029 if( (*pp_current)->p_internals->b_attached
1030 && (*pp_current)->i_object_type == i_type )
1036 p_list = NewList( i_count );
1037 pp_current = p_libvlc_global->pp_objects;
1039 for( ; pp_current < pp_end ; pp_current++ )
1041 if( (*pp_current)->p_internals->b_attached
1042 && (*pp_current)->i_object_type == i_type )
1044 ListReplace( p_list, *pp_current, i_index );
1045 if( i_index < i_count ) i_index++;
1051 i_count = CountChildren( p_this, i_type );
1052 p_list = NewList( i_count );
1054 /* Check allocation was successful */
1055 if( p_list->i_count != i_count )
1057 msg_Err( p_this, "list allocation failed!" );
1058 p_list->i_count = 0;
1062 p_list->i_count = 0;
1063 ListChildren( p_list, p_this, i_type );
1067 msg_Err( p_this, "unimplemented!" );
1068 p_list = NewList( 0 );
1072 vlc_mutex_unlock( &structure_lock );
1077 /*****************************************************************************
1078 * DumpCommand: print the current vlc structure
1079 *****************************************************************************
1080 * This function prints either an ASCII tree showing the connections between
1081 * vlc objects, and additional information such as their refcount, thread ID,
1082 * etc. (command "tree"), or the same data as a simple list (command "list").
1083 *****************************************************************************/
1084 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1085 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1087 libvlc_global_data_t *p_libvlc_global = vlc_global();
1089 (void)oldval; (void)p_data;
1090 if( *psz_cmd == 'l' )
1092 vlc_mutex_lock( &structure_lock );
1094 vlc_object_t **pp_current, **pp_end;
1096 pp_current = p_libvlc_global->pp_objects;
1097 pp_end = pp_current + p_libvlc_global->i_objects;
1099 for( ; pp_current < pp_end ; pp_current++ )
1101 if( (*pp_current)->p_internals->b_attached )
1103 PrintObject( *pp_current, "" );
1107 printf( " o %.8i %s (not attached)\n",
1108 (*pp_current)->i_object_id,
1109 (*pp_current)->psz_object_type );
1113 vlc_mutex_unlock( &structure_lock );
1117 vlc_object_t *p_object = NULL;
1119 if( *newval.psz_string )
1122 int i_id = strtol( newval.psz_string, &end, 0 );
1123 if( end != newval.psz_string )
1124 p_object = vlc_object_get( i_id );
1127 /* try using the object's name to find it */
1128 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1131 /* Look in p_libvlc's children tree */
1132 p_object = vlc_object_find_name( p_libvlc,
1135 vlc_object_release( p_libvlc );
1139 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1140 p_object = vlc_object_find_name( p_this,
1152 vlc_mutex_lock( &structure_lock );
1154 if( *psz_cmd == 't' )
1156 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1159 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1162 DumpStructure( p_object, 0, psz_foo );
1164 else if( *psz_cmd == 'v' )
1169 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1171 PrintObject( p_object, "" );
1173 if( !p_object->p_internals->i_vars )
1174 printf( " `-o No variables\n" );
1175 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1177 variable_t *p_var = p_object->p_internals->p_vars + i;
1179 const char *psz_type = "unknown";
1180 switch( p_var->i_type & VLC_VAR_TYPE )
1182 #define MYCASE( type, nice ) \
1183 case VLC_VAR_ ## type: \
1186 MYCASE( VOID, "void" );
1187 MYCASE( BOOL, "bool" );
1188 MYCASE( INTEGER, "integer" );
1189 MYCASE( HOTKEY, "hotkey" );
1190 MYCASE( STRING, "string" );
1191 MYCASE( MODULE, "module" );
1192 MYCASE( FILE, "file" );
1193 MYCASE( DIRECTORY, "directory" );
1194 MYCASE( VARIABLE, "variable" );
1195 MYCASE( FLOAT, "float" );
1196 MYCASE( TIME, "time" );
1197 MYCASE( ADDRESS, "address" );
1198 MYCASE( MUTEX, "mutex" );
1199 MYCASE( LIST, "list" );
1202 printf( " %c-o \"%s\" (%s",
1203 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1204 p_var->psz_name, psz_type );
1205 if( p_var->psz_text )
1206 printf( ", %s", p_var->psz_text );
1208 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1209 printf( ", command" );
1210 if( p_var->i_entries )
1211 printf( ", %d callbacks", p_var->i_entries );
1212 switch( p_var->i_type & 0x00f0 )
1218 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1220 case VLC_VAR_INTEGER:
1221 printf( ": %d", p_var->val.i_int );
1223 case VLC_VAR_STRING:
1224 printf( ": \"%s\"", p_var->val.psz_string );
1227 printf( ": %f", p_var->val.f_float );
1230 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1232 case VLC_VAR_ADDRESS:
1233 printf( ": %p", p_var->val.p_address );
1243 vlc_mutex_unlock( &structure_lock );
1245 if( *newval.psz_string )
1247 vlc_object_release( p_object );
1254 /*****************************************************************************
1255 * vlc_list_release: free a list previously allocated by vlc_list_find
1256 *****************************************************************************
1257 * This function decreases the refcount of all objects in the list and
1259 *****************************************************************************/
1260 void vlc_list_release( vlc_list_t *p_list )
1264 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1266 vlc_object_release( p_list->p_values[i_index].p_object );
1269 free( p_list->p_values );
1273 /* Following functions are local */
1275 /*****************************************************************************
1276 * FindIndex: find the index of an object in an array of objects
1277 *****************************************************************************
1278 * This function assumes that p_this can be found in pp_objects. It will not
1279 * crash if p_this cannot be found, but will return a wrong value. It is your
1280 * duty to check the return value if you are not certain that the object could
1281 * be found for sure.
1282 *****************************************************************************/
1283 static int FindIndex( vlc_object_t *p_this,
1284 vlc_object_t **pp_objects, int i_count )
1286 int i_middle = i_count / 2;
1293 if( pp_objects[i_middle] == p_this )
1303 /* We take advantage of the sorted array */
1304 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1306 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1307 i_count - i_middle );
1311 return FindIndex( p_this, pp_objects, i_middle );
1315 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1318 vlc_object_t *p_tmp;
1320 switch( i_mode & 0x000f )
1323 p_tmp = p_this->p_parent;
1326 if( p_tmp->i_object_type == i_type
1327 && p_tmp->p_internals->i_refcount > 0 )
1329 vlc_object_yield_locked( p_tmp );
1334 return FindObject( p_tmp, i_type, i_mode );
1340 for( i = p_this->i_children; i--; )
1342 p_tmp = p_this->pp_children[i];
1343 if( p_tmp->i_object_type == i_type
1344 && p_tmp->p_internals->i_refcount > 0 )
1346 vlc_object_yield_locked( p_tmp );
1349 else if( p_tmp->i_children )
1351 p_tmp = FindObject( p_tmp, i_type, i_mode );
1361 /* Handled in vlc_object_find */
1368 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1369 const char *psz_name,
1373 vlc_object_t *p_tmp;
1375 switch( i_mode & 0x000f )
1378 p_tmp = p_this->p_parent;
1381 if( p_tmp->psz_object_name
1382 && !strcmp( p_tmp->psz_object_name, psz_name )
1383 && p_tmp->p_internals->i_refcount > 0 )
1385 vlc_object_yield_locked( p_tmp );
1390 return FindObjectName( p_tmp, psz_name, i_mode );
1396 for( i = p_this->i_children; i--; )
1398 p_tmp = p_this->pp_children[i];
1399 if( p_tmp->psz_object_name
1400 && !strcmp( p_tmp->psz_object_name, psz_name )
1401 && p_tmp->p_internals->i_refcount > 0 )
1403 vlc_object_yield_locked( p_tmp );
1406 else if( p_tmp->i_children )
1408 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1418 /* Handled in vlc_object_find */
1425 static void DetachObject( vlc_object_t *p_this )
1427 vlc_object_t *p_parent = p_this->p_parent;
1430 /* Remove p_this's parent */
1431 p_this->p_parent = NULL;
1433 /* Remove all of p_parent's children which are p_this */
1434 for( i_index = p_parent->i_children ; i_index-- ; )
1436 if( p_parent->pp_children[i_index] == p_this )
1438 p_parent->i_children--;
1439 for( i = i_index ; i < p_parent->i_children ; i++ )
1441 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1446 if( p_parent->i_children )
1448 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1449 p_parent->i_children * sizeof(vlc_object_t *) );
1453 free( p_parent->pp_children );
1454 p_parent->pp_children = NULL;
1458 /*****************************************************************************
1459 * SetAttachment: recursively set the b_attached flag of a subtree.
1460 *****************************************************************************
1461 * This function is used by the attach and detach functions to propagate
1462 * the b_attached flag in a subtree.
1463 *****************************************************************************/
1464 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1468 for( i_index = p_this->i_children ; i_index-- ; )
1470 SetAttachment( p_this->pp_children[i_index], b_attached );
1473 p_this->p_internals->b_attached = b_attached;
1476 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1478 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1482 if( p_this->psz_object_name )
1484 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1486 psz_name[48] = '\"';
1489 psz_children[0] = '\0';
1490 switch( p_this->i_children )
1495 strcpy( psz_children, ", 1 child" );
1498 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1502 psz_refcount[0] = '\0';
1503 if( p_this->p_internals->i_refcount > 0 )
1504 snprintf( psz_refcount, 19, ", refcount %u",
1505 p_this->p_internals->i_refcount );
1507 psz_thread[0] = '\0';
1508 if( p_this->p_internals->b_thread )
1509 snprintf( psz_thread, 29, " (thread %u)",
1510 #if defined(WIN32) || defined(UNDER_CE)
1511 (unsigned)p_this->p_internals->thread_id.id );
1513 (unsigned)p_this->p_internals->thread_id );
1516 psz_parent[0] = '\0';
1517 if( p_this->p_parent )
1518 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1520 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1521 p_this->i_object_id, p_this->psz_object_type,
1522 psz_name, psz_thread, psz_refcount, psz_children,
1526 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1529 char i_back = psz_foo[i_level];
1530 psz_foo[i_level] = '\0';
1532 PrintObject( p_this, psz_foo );
1534 psz_foo[i_level] = i_back;
1536 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1538 msg_Warn( p_this, "structure tree is too deep" );
1542 for( i = 0 ; i < p_this->i_children ; i++ )
1546 psz_foo[i_level-1] = ' ';
1548 if( psz_foo[i_level-2] == '`' )
1550 psz_foo[i_level-2] = ' ';
1554 if( i == p_this->i_children - 1 )
1556 psz_foo[i_level] = '`';
1560 psz_foo[i_level] = '|';
1563 psz_foo[i_level+1] = '-';
1564 psz_foo[i_level+2] = '\0';
1566 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1570 static vlc_list_t * NewList( int i_count )
1572 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1573 if( p_list == NULL )
1578 p_list->i_count = i_count;
1582 p_list->p_values = NULL;
1586 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1587 if( p_list->p_values == NULL )
1589 p_list->i_count = 0;
1596 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1599 if( p_list == NULL || i_index >= p_list->i_count )
1604 vlc_object_yield_locked( p_object );
1606 p_list->p_values[i_index].p_object = p_object;
1611 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1613 if( p_list == NULL )
1618 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1619 * sizeof( vlc_value_t ) );
1620 if( p_list->p_values == NULL )
1622 p_list->i_count = 0;
1626 vlc_object_yield_locked( p_object );
1628 p_list->p_values[p_list->i_count].p_object = p_object;
1634 static int CountChildren( vlc_object_t *p_this, int i_type )
1636 vlc_object_t *p_tmp;
1639 for( i = 0; i < p_this->i_children; i++ )
1641 p_tmp = p_this->pp_children[i];
1643 if( p_tmp->i_object_type == i_type )
1648 if( p_tmp->i_children )
1650 i_count += CountChildren( p_tmp, i_type );
1657 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1659 vlc_object_t *p_tmp;
1662 for( i = 0; i < p_this->i_children; i++ )
1664 p_tmp = p_this->pp_children[i];
1666 if( p_tmp->i_object_type == i_type )
1668 ListReplace( p_list, p_tmp, p_list->i_count++ );
1671 if( p_tmp->i_children )
1673 ListChildren( p_list, p_tmp, i_type );