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 *, bool );
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;
106 * VLC objects are laid out as follow:
107 * - first the LibVLC-private per-object data,
108 * - then VLC_COMMON members from vlc_object_t,
109 * - finally, the type-specific data (if any).
111 * This function initializes the LibVLC and common data,
112 * and zeroes the rest.
114 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
118 if( i_type == VLC_OBJECT_GLOBAL )
122 assert (i_size >= sizeof (vlc_object_t));
123 p_new = (vlc_object_t *)(p_priv + 1);
126 p_new->p_internals = p_priv;
127 p_new->i_object_type = i_type;
128 p_new->psz_object_type = psz_type;
130 p_new->psz_object_name = NULL;
132 p_new->b_die = false;
133 p_new->b_error = false;
134 p_new->b_dead = false;
135 p_priv->b_attached = false;
136 p_new->b_force = false;
138 p_new->psz_header = NULL;
140 p_new->i_flags |= p_this->i_flags
141 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
143 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
145 if( !p_priv->p_vars )
151 if( i_type == VLC_OBJECT_GLOBAL )
153 /* If i_type is global, then p_new is actually p_libvlc_global */
154 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
155 p_new->p_libvlc = NULL;
157 p_libvlc_global->i_counter = 0;
158 p_new->i_object_id = 0;
160 p_libvlc_global->i_objects = 1;
161 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
162 p_libvlc_global->pp_objects[0] = p_new;
163 p_priv->b_attached = true;
164 vlc_mutex_init( p_new, &structure_lock );
168 libvlc_global_data_t *p_libvlc_global = vlc_global();
169 if( i_type == VLC_OBJECT_LIBVLC )
171 p_new->p_libvlc = (libvlc_int_t*)p_new;
172 p_priv->b_attached = true;
176 p_new->p_libvlc = p_this->p_libvlc;
179 vlc_mutex_lock( &structure_lock );
181 p_libvlc_global->i_counter++;
182 p_new->i_object_id = p_libvlc_global->i_counter;
184 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
185 * useless to try and recover anything if pp_objects gets smashed. */
186 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
189 vlc_mutex_unlock( &structure_lock );
192 p_priv->i_refcount = 1;
193 p_priv->pf_destructor = kVLCDestructor;
194 p_priv->b_thread = false;
195 p_new->p_parent = NULL;
196 p_new->pp_children = NULL;
197 p_new->i_children = 0;
199 p_new->p_private = NULL;
201 /* Initialize mutexes and condvars */
202 vlc_mutex_init( p_new, &p_new->object_lock );
203 vlc_cond_init( p_new, &p_new->object_wait );
204 vlc_mutex_init( p_new, &p_priv->var_lock );
205 vlc_spin_init( &p_priv->spin );
206 p_priv->pipes[0] = p_priv->pipes[1] = -1;
208 if( i_type == VLC_OBJECT_LIBVLC )
210 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
211 var_AddCallback( p_new, "list", DumpCommand, NULL );
212 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
213 var_AddCallback( p_new, "tree", DumpCommand, NULL );
214 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
215 var_AddCallback( p_new, "vars", DumpCommand, NULL );
223 * Allocates and initializes a vlc object.
225 * @param i_type known object type (all of them are negative integer values),
226 * or object byte size (always positive).
228 * @return the new object, or NULL on error.
230 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
232 const char * psz_type;
237 case VLC_OBJECT_LIBVLC:
238 i_size = sizeof(libvlc_int_t);
241 case VLC_OBJECT_INTF:
242 i_size = sizeof(intf_thread_t);
243 psz_type = "interface";
245 case VLC_OBJECT_DIALOGS:
246 i_size = sizeof(intf_thread_t);
247 psz_type = "dialogs";
249 case VLC_OBJECT_DEMUX:
250 i_size = sizeof(demux_t);
253 case VLC_OBJECT_ACCESS:
254 i_size = sizeof(access_t);
257 case VLC_OBJECT_DECODER:
258 i_size = sizeof(decoder_t);
259 psz_type = "decoder";
261 case VLC_OBJECT_PACKETIZER:
262 i_size = sizeof(decoder_t);
263 psz_type = "packetizer";
265 case VLC_OBJECT_ENCODER:
266 i_size = sizeof(encoder_t);
267 psz_type = "encoder";
269 case VLC_OBJECT_FILTER:
270 i_size = sizeof(filter_t);
273 case VLC_OBJECT_VOUT:
274 i_size = sizeof(vout_thread_t);
275 psz_type = "video output";
277 case VLC_OBJECT_AOUT:
278 i_size = sizeof(aout_instance_t);
279 psz_type = "audio output";
281 case VLC_OBJECT_SOUT:
282 i_size = sizeof(sout_instance_t);
283 psz_type = "stream output";
285 case VLC_OBJECT_OPENGL:
286 i_size = sizeof( vout_thread_t );
289 case VLC_OBJECT_ANNOUNCE:
290 i_size = sizeof( announce_handler_t );
291 psz_type = "announce";
293 case VLC_OBJECT_INTERACTION:
294 i_size = sizeof( interaction_t );
295 psz_type = "interaction";
298 i_size = i_type > (int)sizeof(vlc_object_t)
299 ? i_type : (int)sizeof(vlc_object_t);
300 i_type = VLC_OBJECT_GENERIC;
301 psz_type = "generic";
305 return vlc_custom_create( p_this, i_size, i_type, psz_type );
310 ****************************************************************************
311 * Set the destructor of a vlc object
313 * This function sets the destructor of the vlc object. It will be called
314 * when the object is destroyed when the its refcount reaches 0.
315 * (It is called by the internal function vlc_object_destroy())
316 *****************************************************************************/
317 void __vlc_object_set_destructor( vlc_object_t *p_this,
318 vlc_destructor_t pf_destructor )
320 vlc_object_internals_t *p_priv = vlc_internals(p_this );
322 vlc_mutex_lock( &structure_lock );
323 p_priv->pf_destructor = pf_destructor;
324 vlc_mutex_unlock( &structure_lock );
328 ****************************************************************************
329 * Destroy a vlc object (Internal)
331 * This function destroys an object that has been previously allocated with
332 * vlc_object_create. The object's refcount must be zero and it must not be
333 * attached to other objects in any way.
334 *****************************************************************************/
335 static void vlc_object_destroy( vlc_object_t *p_this )
337 vlc_object_internals_t *p_priv = vlc_internals( p_this );
339 /* Automatically detach the object from its parents */
340 if( p_this->p_parent ) vlc_object_detach( p_this );
343 /* Send a kill to the object's thread if applicable */
344 vlc_object_kill( p_this );
346 /* If we are running on a thread, wait until it ends */
347 if( p_priv->b_thread )
348 vlc_thread_join( p_this );
350 /* Call the custom "subclass" destructor */
351 if( p_priv->pf_destructor )
352 p_priv->pf_destructor( p_this );
355 if( p_this->i_children )
360 "ERROR: cannot delete object (%i, %s) with %d children\n",
361 p_this->i_object_id, p_this->psz_object_name,
362 p_this->i_children );
364 for( i = 0; i < p_this->i_children; i++ )
367 "ERROR: Remaining children object "
368 "(id:%i, type:%s, name:%s)\n",
369 p_this->pp_children[i]->i_object_id,
370 p_this->pp_children[i]->psz_object_type,
371 p_this->pp_children[i]->psz_object_name );
375 /* Dump libvlc object to ease debugging */
376 vlc_object_dump( p_this->p_libvlc );
381 /* Destroy the associated variables, starting from the end so that
382 * no memmove calls have to be done. */
383 while( p_priv->i_vars )
385 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
388 free( p_priv->p_vars );
389 vlc_mutex_destroy( &p_priv->var_lock );
391 free( p_this->psz_header );
393 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
395 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
398 if( p_global->i_objects > 0 )
401 for( i = 0; i < p_global->i_objects; i++ )
403 /* We are leaking this object */
405 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
406 p_global->pp_objects[i]->i_object_id,
407 p_global->pp_objects[i]->psz_object_type,
408 p_global->pp_objects[i]->psz_object_name );
410 /* Dump libvlc object to ease debugging */
411 vlc_object_dump( p_global->pp_objects[i] );
416 /* Dump libvlc object to ease debugging */
417 vlc_object_dump( p_this );
419 /* Strongly abort, cause we want these to be fixed */
423 /* We are the global object ... no need to lock. */
424 free( p_global->pp_objects );
425 p_global->pp_objects = NULL;
427 vlc_mutex_destroy( &structure_lock );
430 #if defined(WIN32) || defined(UNDER_CE)
431 /* if object has an associated thread, close it now */
432 if( p_priv->thread_id.hThread )
433 CloseHandle(p_priv->thread_id.hThread);
436 vlc_mutex_destroy( &p_this->object_lock );
437 vlc_cond_destroy( &p_this->object_wait );
438 vlc_spin_destroy( &p_priv->spin );
439 if( p_priv->pipes[1] != -1 )
440 close( p_priv->pipes[1] );
441 if( p_priv->pipes[0] != -1 )
442 close( p_priv->pipes[0] );
448 /** Inter-object signaling */
450 void __vlc_object_lock( vlc_object_t *obj )
452 vlc_mutex_lock( &obj->object_lock );
455 void __vlc_object_unlock( vlc_object_t *obj )
457 vlc_assert_locked( &obj->object_lock );
458 vlc_mutex_unlock( &obj->object_lock );
462 # include <winsock2.h>
463 # include <ws2tcpip.h>
466 * select()-able pipes emulated using Winsock
468 static int pipe (int fd[2])
471 int addrlen = sizeof (addr);
473 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
474 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
475 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
478 memset (&addr, 0, sizeof (addr));
479 addr.sin_family = AF_INET;
480 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
481 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
482 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
484 || connect (c, (PSOCKADDR)&addr, addrlen))
487 a = accept (l, NULL, NULL);
488 if (a == INVALID_SOCKET)
499 if (l != INVALID_SOCKET)
501 if (c != INVALID_SOCKET)
507 #define read( a, b, c ) recv (a, b, c, 0)
509 #define write( a, b, c ) send (a, b, c, 0)
511 #define close( a ) closesocket (a)
515 * Returns the readable end of a pipe that becomes readable once termination
516 * of the object is requested (vlc_object_kill()).
517 * This can be used to wake-up out of a select() or poll() event loop, such
518 * typically when doing network I/O.
520 * Note that the pipe will remain the same for the lifetime of the object.
521 * DO NOT read the pipe nor close it yourself. Ever.
523 * @param obj object that would be "killed"
524 * @return a readable pipe descriptor, or -1 on error.
526 int __vlc_object_waitpipe( vlc_object_t *obj )
528 int pfd[2] = { -1, -1 };
529 struct vlc_object_internals_t *internals = obj->p_internals;
532 vlc_spin_lock (&internals->spin);
533 if (internals->pipes[0] == -1)
535 /* This can only ever happen if someone killed us without locking: */
536 assert (internals->pipes[1] == -1);
537 vlc_spin_unlock (&internals->spin);
542 vlc_spin_lock (&internals->spin);
543 if (internals->pipes[0] == -1)
545 internals->pipes[0] = pfd[0];
546 internals->pipes[1] = pfd[1];
547 pfd[0] = pfd[1] = -1;
551 vlc_spin_unlock (&internals->spin);
555 /* Race condition: vlc_object_kill() already invoked! */
558 vlc_spin_lock (&internals->spin);
559 fd = internals->pipes[1];
560 internals->pipes[1] = -1;
561 vlc_spin_unlock (&internals->spin);
563 msg_Dbg (obj, "waitpipe: object already dying");
568 /* Race condition: two threads call pipe() - unlikely */
574 return internals->pipes[0];
579 * Waits for the object to be signaled (using vlc_object_signal()).
580 * If the object already has a signal pending, this function will return
581 * immediately. It is asserted that the caller holds the object lock.
583 * @return true if the object is dying and should terminate.
585 bool __vlc_object_wait( vlc_object_t *obj )
587 vlc_assert_locked( &obj->object_lock );
588 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
594 * Waits for the object to be signaled (using vlc_object_signal()), or for
596 * If the object already has a signal pending, this function will return
597 * immediately. It is asserted that the caller holds the object lock.
599 * @return negative if the object is dying and should terminate,
600 * positive if the the object has been signaled but is not dying,
601 * 0 if timeout has been reached.
603 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
607 vlc_assert_locked( &obj->object_lock );
608 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
609 if( v == 0 ) /* signaled */
610 return obj->b_die ? -1 : 1;
616 * Checks whether an object has been "killed".
617 * The object lock must be held.
619 * Typical code for an object thread could be:
621 vlc_object_lock (self);
623 while (vlc_object_alive (self))
627 if (vlc_object_wait (self))
632 ...deinitialization...
633 vlc_object_unlock (self);
636 * @return true iff the object has not been killed yet
638 bool __vlc_object_alive( vlc_object_t *obj )
640 vlc_assert_locked( &obj->object_lock );
646 * Signals an object for which the lock is held.
648 void __vlc_object_signal_unlocked( vlc_object_t *obj )
650 vlc_assert_locked (&obj->object_lock);
651 vlc_cond_signal( &obj->object_wait );
656 * Requests termination of an object.
657 * If the object is LibVLC, also request to terminate all its children.
659 void __vlc_object_kill( vlc_object_t *p_this )
661 struct vlc_object_internals_t *internals = p_this->p_internals;
664 vlc_mutex_lock( &p_this->object_lock );
665 p_this->b_die = true;
667 vlc_spin_lock (&internals->spin);
668 fd = internals->pipes[1];
669 internals->pipes[1] = -1;
670 vlc_spin_unlock (&internals->spin);
674 msg_Dbg (p_this, "waitpipe: object killed");
678 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
679 for( int i = 0; i < p_this->i_children ; i++ )
680 vlc_object_kill( p_this->pp_children[i] );
682 vlc_object_signal_unlocked( p_this );
683 vlc_mutex_unlock( &p_this->object_lock );
688 * find an object given its ID
690 * This function looks for the object whose i_object_id field is i_id. We
691 * use a dichotomy so that lookups are in log2(n).
692 *****************************************************************************/
693 void * vlc_object_get( int i_id )
696 vlc_object_t **pp_objects;
697 libvlc_global_data_t *p_libvlc_global = vlc_global();
699 vlc_mutex_lock( &structure_lock );
701 pp_objects = p_libvlc_global->pp_objects;
703 /* Perform our dichotomy */
704 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
706 i_middle = i_max / 2;
708 if( pp_objects[i_middle]->i_object_id > i_id )
712 else if( pp_objects[i_middle]->i_object_id < i_id )
716 pp_objects += i_middle;
721 /* This happens when there are only two remaining objects */
722 if( pp_objects[i_middle+1]->i_object_id == i_id
723 && pp_objects[i_middle+1]->p_internals->i_refcount > 0 )
725 vlc_object_yield_locked( pp_objects[i_middle+1] );
726 vlc_mutex_unlock( &structure_lock );
727 return pp_objects[i_middle+1];
732 else if( pp_objects[i_middle]->p_internals->i_refcount > 0 )
734 vlc_object_yield_locked( pp_objects[i_middle] );
735 vlc_mutex_unlock( &structure_lock );
736 return pp_objects[i_middle];
741 /* this means that i_max == i_middle, and since we have already
742 * tested pp_objects[i_middle]), p_found is properly set. */
747 vlc_mutex_unlock( &structure_lock );
752 ****************************************************************************
753 * find a typed object and increment its refcount
754 *****************************************************************************
755 * This function recursively looks for a given object type. i_mode can be one
756 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
757 *****************************************************************************/
758 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
760 vlc_object_t *p_found;
762 vlc_mutex_lock( &structure_lock );
764 /* If we are of the requested type ourselves, don't look further */
765 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type
766 && p_this->p_internals->i_refcount > 0 )
768 vlc_object_yield_locked( p_this );
769 vlc_mutex_unlock( &structure_lock );
773 /* Otherwise, recursively look for the object */
774 if( (i_mode & 0x000f) == FIND_ANYWHERE )
776 vlc_object_t *p_root = p_this;
779 while( p_root->p_parent != NULL &&
780 p_root != VLC_OBJECT( p_this->p_libvlc ) )
782 p_root = p_root->p_parent;
785 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
786 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
788 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
789 i_type, (i_mode & ~0x000f)|FIND_CHILD );
794 p_found = FindObject( p_this, i_type, i_mode );
797 vlc_mutex_unlock( &structure_lock );
803 ****************************************************************************
804 * find a named object and increment its refcount
805 *****************************************************************************
806 * This function recursively looks for a given object name. i_mode can be one
807 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
808 *****************************************************************************/
809 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
812 vlc_object_t *p_found;
814 vlc_mutex_lock( &structure_lock );
816 /* Avoid obvious freed object uses */
817 assert( p_this->p_internals->i_refcount > 0 );
819 /* If have the requested name ourselves, don't look further */
820 if( !(i_mode & FIND_STRICT)
821 && p_this->psz_object_name
822 && !strcmp( p_this->psz_object_name, psz_name )
823 && p_this->p_internals->i_refcount > 0 )
825 vlc_object_yield_locked( p_this );
826 vlc_mutex_unlock( &structure_lock );
830 /* Otherwise, recursively look for the object */
831 if( (i_mode & 0x000f) == FIND_ANYWHERE )
833 vlc_object_t *p_root = p_this;
836 while( p_root->p_parent != NULL &&
837 p_root != VLC_OBJECT( p_this->p_libvlc ) )
839 p_root = p_root->p_parent;
842 p_found = FindObjectName( p_root, psz_name,
843 (i_mode & ~0x000f)|FIND_CHILD );
844 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
846 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
847 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
852 p_found = FindObjectName( p_this, psz_name, i_mode );
855 vlc_mutex_unlock( &structure_lock );
861 ****************************************************************************
862 * increment an object refcount
863 *****************************************************************************/
865 /* When the structure_lock is locked */
866 static void vlc_object_yield_locked( vlc_object_t *p_this )
868 vlc_assert_locked (&structure_lock);
870 /* Avoid obvious freed object uses */
871 assert( p_this->p_internals->i_refcount > 0 );
873 /* Increment the counter */
874 p_this->p_internals->i_refcount++;
877 /* Public function */
878 void __vlc_object_yield( vlc_object_t *p_this )
880 vlc_mutex_lock( &structure_lock );
881 vlc_object_yield_locked( p_this );
882 vlc_mutex_unlock( &structure_lock );
886 /*****************************************************************************
887 * decrement an object refcount
888 * And destroy the object if its refcount reach zero.
889 *****************************************************************************/
890 void __vlc_object_release( vlc_object_t *p_this )
892 bool b_should_destroy;
894 vlc_mutex_lock( &structure_lock );
896 assert( p_this->p_internals->i_refcount > 0 );
897 p_this->p_internals->i_refcount--;
898 b_should_destroy = (p_this->p_internals->i_refcount == 0);
900 if( b_should_destroy )
902 /* Make sure this object can't be obtained via vlc_find_object now that
904 libvlc_global_data_t *p_libvlc_global = vlc_global();
907 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
908 * useless to try and recover anything if pp_objects gets smashed. */
909 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
910 p_libvlc_global->i_objects );
911 REMOVE_ELEM( p_libvlc_global->pp_objects,
912 p_libvlc_global->i_objects, i_index );
915 vlc_mutex_unlock( &structure_lock );
917 if( b_should_destroy )
918 vlc_object_destroy( p_this );
922 ****************************************************************************
923 * attach object to a parent object
924 *****************************************************************************
925 * This function sets p_this as a child of p_parent, and p_parent as a parent
926 * of p_this. This link can be undone using vlc_object_detach.
927 *****************************************************************************/
928 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
930 if( !p_this ) return;
932 vlc_mutex_lock( &structure_lock );
934 /* Avoid obvious freed object uses */
935 assert( p_this->p_internals->i_refcount > 0 );
937 /* Attach the parent to its child */
938 p_this->p_parent = p_parent;
940 /* Attach the child to its parent */
941 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
942 p_parent->i_children, p_this );
944 /* Climb up the tree to see whether we are connected with the root */
945 if( p_parent->p_internals->b_attached )
947 SetAttachment( p_this, true );
950 vlc_mutex_unlock( &structure_lock );
954 ****************************************************************************
955 * detach object from its parent
956 *****************************************************************************
957 * This function removes all links between an object and its parent.
958 *****************************************************************************/
959 void __vlc_object_detach( vlc_object_t *p_this )
961 if( !p_this ) return;
963 vlc_mutex_lock( &structure_lock );
965 if( !p_this->p_parent )
967 msg_Err( p_this, "object is not attached" );
968 vlc_mutex_unlock( &structure_lock );
972 /* Climb up the tree to see whether we are connected with the root */
973 if( p_this->p_parent->p_internals->b_attached )
975 SetAttachment( p_this, false );
978 DetachObject( p_this );
979 vlc_mutex_unlock( &structure_lock );
984 ****************************************************************************
985 * find a list typed objects and increment their refcount
986 *****************************************************************************
987 * This function recursively looks for a given object type. i_mode can be one
988 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
989 *****************************************************************************/
990 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
993 vlc_object_t **pp_current, **pp_end;
994 int i_count = 0, i_index = 0;
995 libvlc_global_data_t *p_libvlc_global = vlc_global();
997 vlc_mutex_lock( &structure_lock );
999 /* Look for the objects */
1000 switch( i_mode & 0x000f )
1003 pp_current = p_libvlc_global->pp_objects;
1004 pp_end = pp_current + p_libvlc_global->i_objects;
1006 for( ; pp_current < pp_end ; pp_current++ )
1008 if( (*pp_current)->p_internals->b_attached
1009 && (*pp_current)->i_object_type == i_type )
1015 p_list = NewList( i_count );
1016 pp_current = p_libvlc_global->pp_objects;
1018 for( ; pp_current < pp_end ; pp_current++ )
1020 if( (*pp_current)->p_internals->b_attached
1021 && (*pp_current)->i_object_type == i_type )
1023 ListReplace( p_list, *pp_current, i_index );
1024 if( i_index < i_count ) i_index++;
1030 i_count = CountChildren( p_this, i_type );
1031 p_list = NewList( i_count );
1033 /* Check allocation was successful */
1034 if( p_list->i_count != i_count )
1036 msg_Err( p_this, "list allocation failed!" );
1037 p_list->i_count = 0;
1041 p_list->i_count = 0;
1042 ListChildren( p_list, p_this, i_type );
1046 msg_Err( p_this, "unimplemented!" );
1047 p_list = NewList( 0 );
1051 vlc_mutex_unlock( &structure_lock );
1056 /*****************************************************************************
1057 * DumpCommand: print the current vlc structure
1058 *****************************************************************************
1059 * This function prints either an ASCII tree showing the connections between
1060 * vlc objects, and additional information such as their refcount, thread ID,
1061 * etc. (command "tree"), or the same data as a simple list (command "list").
1062 *****************************************************************************/
1063 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1064 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1066 libvlc_global_data_t *p_libvlc_global = vlc_global();
1068 (void)oldval; (void)p_data;
1069 if( *psz_cmd == 'l' )
1071 vlc_mutex_lock( &structure_lock );
1073 vlc_object_t **pp_current, **pp_end;
1075 pp_current = p_libvlc_global->pp_objects;
1076 pp_end = pp_current + p_libvlc_global->i_objects;
1078 for( ; pp_current < pp_end ; pp_current++ )
1080 if( (*pp_current)->p_internals->b_attached )
1082 PrintObject( *pp_current, "" );
1086 printf( " o %.8i %s (not attached)\n",
1087 (*pp_current)->i_object_id,
1088 (*pp_current)->psz_object_type );
1092 vlc_mutex_unlock( &structure_lock );
1096 vlc_object_t *p_object = NULL;
1098 if( *newval.psz_string )
1101 int i_id = strtol( newval.psz_string, &end, 0 );
1102 if( end != newval.psz_string )
1103 p_object = vlc_object_get( i_id );
1106 /* try using the object's name to find it */
1107 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1110 /* Look in p_libvlc's children tree */
1111 p_object = vlc_object_find_name( p_libvlc,
1114 vlc_object_release( p_libvlc );
1118 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1119 p_object = vlc_object_find_name( p_this,
1131 vlc_mutex_lock( &structure_lock );
1133 if( *psz_cmd == 't' )
1135 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1138 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1141 DumpStructure( p_object, 0, psz_foo );
1143 else if( *psz_cmd == 'v' )
1148 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1150 PrintObject( p_object, "" );
1152 if( !p_object->p_internals->i_vars )
1153 printf( " `-o No variables\n" );
1154 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1156 variable_t *p_var = p_object->p_internals->p_vars + i;
1158 const char *psz_type = "unknown";
1159 switch( p_var->i_type & VLC_VAR_TYPE )
1161 #define MYCASE( type, nice ) \
1162 case VLC_VAR_ ## type: \
1165 MYCASE( VOID, "void" );
1166 MYCASE( BOOL, "bool" );
1167 MYCASE( INTEGER, "integer" );
1168 MYCASE( HOTKEY, "hotkey" );
1169 MYCASE( STRING, "string" );
1170 MYCASE( MODULE, "module" );
1171 MYCASE( FILE, "file" );
1172 MYCASE( DIRECTORY, "directory" );
1173 MYCASE( VARIABLE, "variable" );
1174 MYCASE( FLOAT, "float" );
1175 MYCASE( TIME, "time" );
1176 MYCASE( ADDRESS, "address" );
1177 MYCASE( MUTEX, "mutex" );
1178 MYCASE( LIST, "list" );
1181 printf( " %c-o \"%s\" (%s",
1182 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1183 p_var->psz_name, psz_type );
1184 if( p_var->psz_text )
1185 printf( ", %s", p_var->psz_text );
1187 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1188 printf( ", command" );
1189 if( p_var->i_entries )
1190 printf( ", %d callbacks", p_var->i_entries );
1191 switch( p_var->i_type & 0x00f0 )
1197 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1199 case VLC_VAR_INTEGER:
1200 printf( ": %d", p_var->val.i_int );
1202 case VLC_VAR_STRING:
1203 printf( ": \"%s\"", p_var->val.psz_string );
1206 printf( ": %f", p_var->val.f_float );
1209 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1211 case VLC_VAR_ADDRESS:
1212 printf( ": %p", p_var->val.p_address );
1222 vlc_mutex_unlock( &structure_lock );
1224 if( *newval.psz_string )
1226 vlc_object_release( p_object );
1233 /*****************************************************************************
1234 * vlc_list_release: free a list previously allocated by vlc_list_find
1235 *****************************************************************************
1236 * This function decreases the refcount of all objects in the list and
1238 *****************************************************************************/
1239 void vlc_list_release( vlc_list_t *p_list )
1243 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1245 vlc_object_release( p_list->p_values[i_index].p_object );
1248 free( p_list->p_values );
1252 /*****************************************************************************
1253 * dump an object. (Debug function)
1254 *****************************************************************************/
1255 void __vlc_object_dump( vlc_object_t *p_this )
1257 vlc_mutex_lock( &structure_lock );
1258 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1260 DumpStructure( p_this, 0, psz_foo );
1261 vlc_mutex_unlock( &structure_lock );
1264 /* Following functions are local */
1266 /*****************************************************************************
1267 * FindIndex: find the index of an object in an array of objects
1268 *****************************************************************************
1269 * This function assumes that p_this can be found in pp_objects. It will not
1270 * crash if p_this cannot be found, but will return a wrong value. It is your
1271 * duty to check the return value if you are not certain that the object could
1272 * be found for sure.
1273 *****************************************************************************/
1274 static int FindIndex( vlc_object_t *p_this,
1275 vlc_object_t **pp_objects, int i_count )
1277 int i_middle = i_count / 2;
1284 if( pp_objects[i_middle] == p_this )
1294 /* We take advantage of the sorted array */
1295 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1297 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1298 i_count - i_middle );
1302 return FindIndex( p_this, pp_objects, i_middle );
1306 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1309 vlc_object_t *p_tmp;
1311 switch( i_mode & 0x000f )
1314 p_tmp = p_this->p_parent;
1317 if( p_tmp->i_object_type == i_type
1318 && p_tmp->p_internals->i_refcount > 0 )
1320 vlc_object_yield_locked( p_tmp );
1325 return FindObject( p_tmp, i_type, i_mode );
1331 for( i = p_this->i_children; i--; )
1333 p_tmp = p_this->pp_children[i];
1334 if( p_tmp->i_object_type == i_type
1335 && p_tmp->p_internals->i_refcount > 0 )
1337 vlc_object_yield_locked( p_tmp );
1340 else if( p_tmp->i_children )
1342 p_tmp = FindObject( p_tmp, i_type, i_mode );
1352 /* Handled in vlc_object_find */
1359 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1360 const char *psz_name,
1364 vlc_object_t *p_tmp;
1366 switch( i_mode & 0x000f )
1369 p_tmp = p_this->p_parent;
1372 if( p_tmp->psz_object_name
1373 && !strcmp( p_tmp->psz_object_name, psz_name )
1374 && p_tmp->p_internals->i_refcount > 0 )
1376 vlc_object_yield_locked( p_tmp );
1381 return FindObjectName( p_tmp, psz_name, i_mode );
1387 for( i = p_this->i_children; i--; )
1389 p_tmp = p_this->pp_children[i];
1390 if( p_tmp->psz_object_name
1391 && !strcmp( p_tmp->psz_object_name, psz_name )
1392 && p_tmp->p_internals->i_refcount > 0 )
1394 vlc_object_yield_locked( p_tmp );
1397 else if( p_tmp->i_children )
1399 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1409 /* Handled in vlc_object_find */
1416 static void DetachObject( vlc_object_t *p_this )
1418 vlc_object_t *p_parent = p_this->p_parent;
1421 /* Remove p_this's parent */
1422 p_this->p_parent = NULL;
1424 /* Remove all of p_parent's children which are p_this */
1425 for( i_index = p_parent->i_children ; i_index-- ; )
1427 if( p_parent->pp_children[i_index] == p_this )
1429 p_parent->i_children--;
1430 for( i = i_index ; i < p_parent->i_children ; i++ )
1432 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1437 if( p_parent->i_children )
1439 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1440 p_parent->i_children * sizeof(vlc_object_t *) );
1444 free( p_parent->pp_children );
1445 p_parent->pp_children = NULL;
1449 /*****************************************************************************
1450 * SetAttachment: recursively set the b_attached flag of a subtree.
1451 *****************************************************************************
1452 * This function is used by the attach and detach functions to propagate
1453 * the b_attached flag in a subtree.
1454 *****************************************************************************/
1455 static void SetAttachment( vlc_object_t *p_this, bool b_attached )
1459 for( i_index = p_this->i_children ; i_index-- ; )
1461 SetAttachment( p_this->pp_children[i_index], b_attached );
1464 p_this->p_internals->b_attached = b_attached;
1467 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1469 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1472 memset( &psz_name, 0, sizeof(psz_name) );
1473 if( p_this->psz_object_name )
1475 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1477 psz_name[48] = '\"';
1480 psz_children[0] = '\0';
1481 switch( p_this->i_children )
1486 strcpy( psz_children, ", 1 child" );
1489 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1493 psz_refcount[0] = '\0';
1494 if( p_this->p_internals->i_refcount > 0 )
1495 snprintf( psz_refcount, 19, ", refcount %u",
1496 p_this->p_internals->i_refcount );
1498 psz_thread[0] = '\0';
1499 if( p_this->p_internals->b_thread )
1500 snprintf( psz_thread, 29, " (thread %u)",
1501 #if defined(WIN32) || defined(UNDER_CE)
1502 (unsigned)p_this->p_internals->thread_id.id );
1504 (unsigned)p_this->p_internals->thread_id );
1507 psz_parent[0] = '\0';
1508 if( p_this->p_parent )
1509 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1511 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1512 p_this->i_object_id, p_this->psz_object_type,
1513 psz_name, psz_thread, psz_refcount, psz_children,
1517 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1520 char i_back = psz_foo[i_level];
1521 psz_foo[i_level] = '\0';
1523 PrintObject( p_this, psz_foo );
1525 psz_foo[i_level] = i_back;
1527 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1529 msg_Warn( p_this, "structure tree is too deep" );
1533 for( i = 0 ; i < p_this->i_children ; i++ )
1537 psz_foo[i_level-1] = ' ';
1539 if( psz_foo[i_level-2] == '`' )
1541 psz_foo[i_level-2] = ' ';
1545 if( i == p_this->i_children - 1 )
1547 psz_foo[i_level] = '`';
1551 psz_foo[i_level] = '|';
1554 psz_foo[i_level+1] = '-';
1555 psz_foo[i_level+2] = '\0';
1557 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1561 static vlc_list_t * NewList( int i_count )
1563 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1564 if( p_list == NULL )
1569 p_list->i_count = i_count;
1573 p_list->p_values = NULL;
1577 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1578 if( p_list->p_values == NULL )
1580 p_list->i_count = 0;
1587 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1590 if( p_list == NULL || i_index >= p_list->i_count )
1595 vlc_object_yield_locked( p_object );
1597 p_list->p_values[i_index].p_object = p_object;
1602 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1604 if( p_list == NULL )
1609 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1610 * sizeof( vlc_value_t ) );
1611 if( p_list->p_values == NULL )
1613 p_list->i_count = 0;
1617 vlc_object_yield_locked( p_object );
1619 p_list->p_values[p_list->i_count].p_object = p_object;
1625 static int CountChildren( vlc_object_t *p_this, int i_type )
1627 vlc_object_t *p_tmp;
1630 for( i = 0; i < p_this->i_children; i++ )
1632 p_tmp = p_this->pp_children[i];
1634 if( p_tmp->i_object_type == i_type )
1639 if( p_tmp->i_children )
1641 i_count += CountChildren( p_tmp, i_type );
1648 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1650 vlc_object_t *p_tmp;
1653 for( i = 0; i < p_this->i_children; i++ )
1655 p_tmp = p_this->pp_children[i];
1657 if( p_tmp->i_object_type == i_type )
1659 ListReplace( p_list, p_tmp, p_list->i_count++ );
1662 if( p_tmp->i_children )
1664 ListChildren( p_list, p_tmp, i_type );