1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
6 * Authors: Samuel Hocevar <sam@zoy.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
25 * This file contains the functions to handle the vlc_object_t type
29 /*****************************************************************************
31 *****************************************************************************/
36 #include <vlc_common.h>
38 #include "../libvlc.h"
41 #include "audio_output/aout_internal.h"
43 #include <vlc_access.h>
44 #include <vlc_demux.h>
45 #include <vlc_stream.h>
48 #include "stream_output/stream_output.h"
50 #include "vlc_interface.h"
51 #include "vlc_codec.h"
52 #include "vlc_filter.h"
54 #include "variables.h"
60 # include <errno.h> /* ENOSYS */
64 /*****************************************************************************
66 *****************************************************************************/
67 static int DumpCommand( vlc_object_t *, char const *,
68 vlc_value_t, vlc_value_t, void * );
70 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
71 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
72 static void PrintObject ( vlc_object_t *, const char * );
73 static void DumpStructure ( vlc_object_t *, int, char * );
75 static vlc_list_t * NewList ( int );
76 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
77 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
78 static int CountChildren ( vlc_object_t *, int );
79 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
81 static void vlc_object_destroy( vlc_object_t *p_this );
82 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
84 #ifdef LIBVLC_REFCHECK
85 static vlc_threadvar_t held_objects;
86 typedef struct held_list_t
88 struct held_list_t *next;
91 static void held_objects_destroy (void *);
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 assert (i_size >= sizeof (vlc_object_t));
119 p_new = (vlc_object_t *)(p_priv + 1);
121 p_new->i_object_type = i_type;
122 p_new->psz_object_type = psz_type;
123 p_new->psz_object_name = NULL;
125 p_new->b_die = false;
126 p_new->b_error = false;
127 p_new->b_dead = false;
128 p_new->b_force = false;
130 p_new->psz_header = NULL;
133 p_new->i_flags = p_this->i_flags
134 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
136 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
138 if( !p_priv->p_vars )
144 libvlc_global_data_t *p_libvlc_global;
147 /* Only the global root object is created out of the blue */
148 p_libvlc_global = (libvlc_global_data_t *)p_new;
149 p_new->p_libvlc = NULL;
151 p_libvlc_global->i_counter = 0;
152 p_priv->next = p_priv->prev = p_new;
153 vlc_mutex_init( &structure_lock );
154 #ifdef LIBVLC_REFCHECK
155 /* TODO: use the destruction callback to track ref leaks */
156 vlc_threadvar_create( &held_objects, held_objects_destroy );
161 p_libvlc_global = vlc_global();
162 if( i_type == VLC_OBJECT_LIBVLC )
163 p_new->p_libvlc = (libvlc_int_t*)p_new;
165 p_new->p_libvlc = p_this->p_libvlc;
168 vlc_spin_init( &p_priv->ref_spin );
169 p_priv->i_refcount = 1;
170 p_priv->pf_destructor = NULL;
171 p_priv->b_thread = false;
172 p_new->p_parent = NULL;
173 p_priv->pp_children = NULL;
174 p_priv->i_children = 0;
176 p_new->p_private = NULL;
178 /* Initialize mutexes and condvars */
179 vlc_mutex_init( &p_priv->lock );
180 vlc_cond_init( p_new, &p_priv->wait );
181 vlc_mutex_init( &p_priv->var_lock );
182 vlc_spin_init( &p_priv->spin );
183 p_priv->pipes[0] = p_priv->pipes[1] = -1;
185 p_priv->next = VLC_OBJECT (p_libvlc_global);
186 #if !defined (LIBVLC_REFCHECK)
188 #elif defined (LIBVLC_USE_PTHREAD)
189 p_priv->creator_id = pthread_self ();
190 #elif defined (WIN32)
191 p_priv->creator_id = GetCurrentThreadId ();
193 vlc_mutex_lock( &structure_lock );
194 p_priv->prev = vlc_internals (p_libvlc_global)->prev;
195 vlc_internals (p_libvlc_global)->prev = p_new;
196 vlc_internals (p_priv->prev)->next = p_new;
197 p_new->i_object_id = p_libvlc_global->i_counter++;
198 vlc_mutex_unlock( &structure_lock );
200 if( i_type == VLC_OBJECT_LIBVLC )
202 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
203 var_AddCallback( p_new, "list", DumpCommand, NULL );
204 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
205 var_AddCallback( p_new, "tree", DumpCommand, NULL );
206 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
207 var_AddCallback( p_new, "vars", DumpCommand, NULL );
215 * Allocates and initializes a vlc object.
217 * @param i_type known object type (all of them are negative integer values),
218 * or object byte size (always positive).
220 * @return the new object, or NULL on error.
222 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
224 const char * psz_type;
229 case VLC_OBJECT_LIBVLC:
230 i_size = sizeof(libvlc_int_t);
233 case VLC_OBJECT_INTF:
234 i_size = sizeof(intf_thread_t);
235 psz_type = "interface";
237 case VLC_OBJECT_DIALOGS:
238 i_size = sizeof(intf_thread_t);
239 psz_type = "dialogs";
241 case VLC_OBJECT_DECODER:
242 i_size = sizeof(decoder_t);
243 psz_type = "decoder";
245 case VLC_OBJECT_PACKETIZER:
246 i_size = sizeof(decoder_t);
247 psz_type = "packetizer";
249 case VLC_OBJECT_ENCODER:
250 i_size = sizeof(encoder_t);
251 psz_type = "encoder";
253 case VLC_OBJECT_FILTER:
254 i_size = sizeof(filter_t);
257 case VLC_OBJECT_VOUT:
258 i_size = sizeof(vout_thread_t);
259 psz_type = "video output";
261 case VLC_OBJECT_AOUT:
262 i_size = sizeof(aout_instance_t);
263 psz_type = "audio output";
265 case VLC_OBJECT_OPENGL:
266 i_size = sizeof( vout_thread_t );
269 case VLC_OBJECT_ANNOUNCE:
270 i_size = sizeof( announce_handler_t );
271 psz_type = "announce";
274 i_size = i_type > (int)sizeof(vlc_object_t)
275 ? i_type : (int)sizeof(vlc_object_t);
276 i_type = VLC_OBJECT_GENERIC;
277 psz_type = "generic";
281 return vlc_custom_create( p_this, i_size, i_type, psz_type );
286 ****************************************************************************
287 * Set the destructor of a vlc object
289 * This function sets the destructor of the vlc object. It will be called
290 * when the object is destroyed when the its refcount reaches 0.
291 * (It is called by the internal function vlc_object_destroy())
292 *****************************************************************************/
293 void __vlc_object_set_destructor( vlc_object_t *p_this,
294 vlc_destructor_t pf_destructor )
296 vlc_object_internals_t *p_priv = vlc_internals(p_this );
297 p_priv->pf_destructor = pf_destructor;
301 ****************************************************************************
302 * Destroy a vlc object (Internal)
304 * This function destroys an object that has been previously allocated with
305 * vlc_object_create. The object's refcount must be zero and it must not be
306 * attached to other objects in any way.
307 *****************************************************************************/
308 static void vlc_object_destroy( vlc_object_t *p_this )
310 vlc_object_internals_t *p_priv = vlc_internals( p_this );
312 /* Objects are always detached beforehand */
313 assert( !p_this->p_parent );
315 /* Send a kill to the object's thread if applicable */
316 vlc_object_kill( p_this );
318 /* If we are running on a thread, wait until it ends */
319 if( p_priv->b_thread )
320 vlc_thread_join( p_this );
322 /* Call the custom "subclass" destructor */
323 if( p_priv->pf_destructor )
324 p_priv->pf_destructor( p_this );
326 /* Destroy the associated variables, starting from the end so that
327 * no memmove calls have to be done. */
328 while( p_priv->i_vars )
330 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
333 free( p_priv->p_vars );
334 vlc_mutex_destroy( &p_priv->var_lock );
336 free( p_this->psz_header );
338 if( p_this->p_libvlc == NULL )
340 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
343 assert( p_global == vlc_global() );
345 if (p_priv->next != p_this)
347 vlc_object_t *leaked = p_priv->next, *first = leaked;
350 /* We are leaking this object */
352 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
353 leaked->i_object_id, leaked->psz_object_type,
354 leaked->psz_object_name );
355 /* Dump libvlc object to ease debugging */
356 vlc_object_dump( leaked );
358 leaked = vlc_internals (leaked)->next;
360 while (leaked != first);
362 /* Dump global object to ease debugging */
363 vlc_object_dump( p_this );
364 /* Strongly abort, cause we want these to be fixed */
369 /* We are the global object ... no need to lock. */
370 vlc_mutex_destroy( &structure_lock );
371 #ifdef LIBVLC_REFCHECK
372 held_objects_destroy( vlc_threadvar_get( &held_objects ) );
373 vlc_threadvar_delete( &held_objects );
377 FREENULL( p_this->psz_object_name );
379 #if defined(WIN32) || defined(UNDER_CE)
380 /* if object has an associated thread, close it now */
381 if( p_priv->thread_id )
382 CloseHandle(p_priv->thread_id);
385 vlc_spin_destroy( &p_priv->ref_spin );
386 vlc_mutex_destroy( &p_priv->lock );
387 vlc_cond_destroy( &p_priv->wait );
388 vlc_spin_destroy( &p_priv->spin );
389 if( p_priv->pipes[1] != -1 )
390 close( p_priv->pipes[1] );
391 if( p_priv->pipes[0] != -1 )
392 close( p_priv->pipes[0] );
398 /** Inter-object signaling */
400 void __vlc_object_lock( vlc_object_t *obj )
402 vlc_mutex_lock( &(vlc_internals(obj)->lock) );
405 void __vlc_object_unlock( vlc_object_t *obj )
407 vlc_assert_locked( &(vlc_internals(obj)->lock) );
408 vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
412 # include <winsock2.h>
413 # include <ws2tcpip.h>
416 * select()-able pipes emulated using Winsock
418 static int pipe (int fd[2])
421 int addrlen = sizeof (addr);
423 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
424 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
425 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
428 memset (&addr, 0, sizeof (addr));
429 addr.sin_family = AF_INET;
430 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
431 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
432 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
434 || connect (c, (PSOCKADDR)&addr, addrlen))
437 a = accept (l, NULL, NULL);
438 if (a == INVALID_SOCKET)
449 if (l != INVALID_SOCKET)
451 if (c != INVALID_SOCKET)
457 #define read( a, b, c ) recv (a, b, c, 0)
459 #define write( a, b, c ) send (a, b, c, 0)
461 #define close( a ) closesocket (a)
465 * Returns the readable end of a pipe that becomes readable once termination
466 * of the object is requested (vlc_object_kill()).
467 * This can be used to wake-up out of a select() or poll() event loop, such
468 * typically when doing network I/O.
470 * Note that the pipe will remain the same for the lifetime of the object.
471 * DO NOT read the pipe nor close it yourself. Ever.
473 * @param obj object that would be "killed"
474 * @return a readable pipe descriptor, or -1 on error.
476 int __vlc_object_waitpipe( vlc_object_t *obj )
478 int pfd[2] = { -1, -1 };
479 vlc_object_internals_t *internals = vlc_internals( obj );
482 vlc_spin_lock (&internals->spin);
483 if (internals->pipes[0] == -1)
485 /* This can only ever happen if someone killed us without locking: */
486 assert (internals->pipes[1] == -1);
487 vlc_spin_unlock (&internals->spin);
492 vlc_spin_lock (&internals->spin);
493 if (internals->pipes[0] == -1)
495 internals->pipes[0] = pfd[0];
496 internals->pipes[1] = pfd[1];
497 pfd[0] = pfd[1] = -1;
501 vlc_spin_unlock (&internals->spin);
505 /* Race condition: vlc_object_kill() already invoked! */
508 vlc_spin_lock (&internals->spin);
509 fd = internals->pipes[1];
510 internals->pipes[1] = -1;
511 vlc_spin_unlock (&internals->spin);
513 msg_Dbg (obj, "waitpipe: object already dying");
518 /* Race condition: two threads call pipe() - unlikely */
524 return internals->pipes[0];
529 * Waits for the object to be signaled (using vlc_object_signal()).
530 * It is assumed that the caller has locked the object. This function will
531 * unlock the object, and lock it again before returning.
532 * If the object was signaled before the caller locked the object, it is
533 * undefined whether the signal will be lost or will wake the process.
535 * @return true if the object is dying and should terminate.
537 void __vlc_object_wait( vlc_object_t *obj )
539 vlc_object_internals_t *priv = vlc_internals( obj );
540 vlc_assert_locked( &priv->lock);
541 vlc_cond_wait( &priv->wait, &priv->lock );
546 * Waits for the object to be signaled (using vlc_object_signal()), or for
547 * a timer to expire. It is asserted that the caller holds the object lock.
549 * @return 0 if the object was signaled before the timer expiration, or
550 * ETIMEDOUT if the timer expired without any signal.
552 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
554 vlc_object_internals_t *priv = vlc_internals( obj );
555 vlc_assert_locked( &priv->lock);
556 return vlc_cond_timedwait( &priv->wait, &priv->lock, deadline );
561 * Checks whether an object has been "killed".
562 * The object lock must be held.
564 * Typical code for an object thread could be:
566 vlc_object_lock (self);
568 while (vlc_object_alive (self))
572 vlc_object_wait (self);
576 ...deinitialization...
577 vlc_object_unlock (self);
580 * @return true iff the object has not been killed yet
582 bool __vlc_object_alive( vlc_object_t *obj )
584 vlc_assert_locked( &(vlc_internals(obj)->lock) );
590 * Signals an object for which the lock is held.
591 * At least one thread currently sleeping in vlc_object_wait() or
592 * vlc_object_timedwait() will wake up, assuming that there is at least one
593 * such thread in the first place. Otherwise, it is undefined whether the
594 * signal will be lost or will wake up one or more thread later.
596 void __vlc_object_signal_unlocked( vlc_object_t *obj )
598 vlc_assert_locked (&(vlc_internals(obj)->lock));
599 vlc_cond_signal( &(vlc_internals(obj)->wait) );
604 * Requests termination of an object.
605 * If the object is LibVLC, also request to terminate all its children.
607 void __vlc_object_kill( vlc_object_t *p_this )
609 vlc_object_internals_t *internals = vlc_internals( p_this );
612 vlc_object_lock( p_this );
613 p_this->b_die = true;
615 vlc_spin_lock (&internals->spin);
616 fd = internals->pipes[1];
617 internals->pipes[1] = -1;
618 vlc_spin_unlock (&internals->spin);
622 msg_Dbg (p_this, "waitpipe: object killed");
626 vlc_object_signal_unlocked( p_this );
627 vlc_object_unlock( p_this );
629 if (p_this->i_object_type == VLC_OBJECT_LIBVLC)
631 vlc_list_t *children = vlc_list_children (p_this);
632 for (int i = 0; i < children->i_count; i++)
633 vlc_object_kill (children->p_values[i].p_object);
634 vlc_list_release (children);
640 * Find an object given its ID.
642 * This function looks for the object whose i_object_id field is i_id.
643 * This function is slow, and often used to hide bugs. Do not use it.
644 * If you need to retain reference to an object, yield the object pointer with
645 * vlc_object_yield(), use the pointer as your reference, and call
646 * vlc_object_release() when you're done.
648 void * vlc_object_get( int i_id )
650 libvlc_global_data_t *p_libvlc_global = vlc_global();
651 vlc_object_t *obj = NULL;
653 vlc_object_t *caller = vlc_threadobj ();
656 msg_Dbg (caller, "uses deprecated vlc_object_get(%d)", i_id);
658 fprintf (stderr, "main thread uses deprecated vlc_object_get(%d)\n",
661 vlc_mutex_lock( &structure_lock );
663 for( obj = vlc_internals (p_libvlc_global)->next;
664 obj != VLC_OBJECT (p_libvlc_global);
665 obj = vlc_internals (obj)->next )
667 if( obj->i_object_id == i_id )
669 vlc_object_yield( obj );
676 msg_Warn (caller, "wants non-existing object %d", i_id);
678 fprintf (stderr, "main thread wants non-existing object %d\n", i_id);
681 vlc_mutex_unlock( &structure_lock );
686 ****************************************************************************
687 * find a typed object and increment its refcount
688 *****************************************************************************
689 * This function recursively looks for a given object type. i_mode can be one
690 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
691 *****************************************************************************/
692 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
694 vlc_object_t *p_found;
696 /* If we are of the requested type ourselves, don't look further */
697 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
699 vlc_object_yield( p_this );
703 /* Otherwise, recursively look for the object */
704 if ((i_mode & 0x000f) == FIND_ANYWHERE)
707 if (i_type == VLC_OBJECT_PLAYLIST)
708 msg_Err (p_this, "using vlc_object_find(VLC_OBJECT_PLAYLIST) "
709 "instead of pl_Yield()");
711 return vlc_object_find (p_this->p_libvlc, i_type,
712 (i_mode & ~0x000f)|FIND_CHILD);
715 vlc_mutex_lock( &structure_lock );
716 p_found = FindObject( p_this, i_type, i_mode );
717 vlc_mutex_unlock( &structure_lock );
722 ****************************************************************************
723 * find a named object and increment its refcount
724 *****************************************************************************
725 * This function recursively looks for a given object name. i_mode can be one
726 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
727 *****************************************************************************/
728 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
731 vlc_object_t *p_found;
733 /* If have the requested name ourselves, don't look further */
734 if( !(i_mode & FIND_STRICT)
735 && p_this->psz_object_name
736 && !strcmp( p_this->psz_object_name, psz_name ) )
738 vlc_object_yield( p_this );
742 vlc_mutex_lock( &structure_lock );
744 /* Otherwise, recursively look for the object */
745 if( (i_mode & 0x000f) == FIND_ANYWHERE )
747 vlc_object_t *p_root = p_this;
750 while( p_root->p_parent != NULL &&
751 p_root != VLC_OBJECT( p_this->p_libvlc ) )
753 p_root = p_root->p_parent;
756 p_found = FindObjectName( p_root, psz_name,
757 (i_mode & ~0x000f)|FIND_CHILD );
758 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
760 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
761 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
766 p_found = FindObjectName( p_this, psz_name, i_mode );
769 vlc_mutex_unlock( &structure_lock );
775 * Increment an object reference counter.
777 void __vlc_object_yield( vlc_object_t *p_this )
779 vlc_object_internals_t *internals = vlc_internals( p_this );
781 vlc_spin_lock( &internals->ref_spin );
782 /* Avoid obvious freed object uses */
783 assert( internals->i_refcount > 0 );
784 /* Increment the counter */
785 internals->i_refcount++;
786 vlc_spin_unlock( &internals->ref_spin );
787 #ifdef LIBVLC_REFCHECK
788 /* Update the list of referenced objects */
789 /* Using TLS, so no need to lock */
790 /* The following line may leak memory if a thread leaks objects. */
791 held_list_t *newhead = malloc (sizeof (*newhead));
792 held_list_t *oldhead = vlc_threadvar_get (&held_objects);
793 newhead->next = oldhead;
794 newhead->obj = p_this;
795 vlc_threadvar_set (&held_objects, newhead);
799 /*****************************************************************************
800 * decrement an object refcount
801 * And destroy the object if its refcount reach zero.
802 *****************************************************************************/
803 void __vlc_object_release( vlc_object_t *p_this )
805 vlc_object_internals_t *internals = vlc_internals( p_this );
806 bool b_should_destroy;
808 #ifdef LIBVLC_REFCHECK
809 /* Update the list of referenced objects */
810 /* Using TLS, so no need to lock */
811 for (held_list_t *hlcur = vlc_threadvar_get (&held_objects),
814 hlprev = hlcur, hlcur = hlcur->next)
816 if (hlcur->obj == p_this)
819 vlc_threadvar_set (&held_objects, hlcur->next);
821 hlprev->next = hlcur->next;
826 /* TODO: what if releasing without references? */
829 vlc_spin_lock( &internals->ref_spin );
830 assert( internals->i_refcount > 0 );
832 if( internals->i_refcount > 1 )
835 /* There are still other references to the object */
836 internals->i_refcount--;
837 vlc_spin_unlock( &internals->ref_spin );
840 vlc_spin_unlock( &internals->ref_spin );
843 /* Remember that we cannot hold the spin while waiting on the mutex */
844 vlc_mutex_lock( &structure_lock );
845 /* Take the spin again. Note that another thread may have yielded the
846 * object in the (very short) mean time. */
847 vlc_spin_lock( &internals->ref_spin );
848 b_should_destroy = --internals->i_refcount == 0;
849 vlc_spin_unlock( &internals->ref_spin );
851 if( b_should_destroy )
853 /* Remove the object from object list
854 * so that it cannot be encountered by vlc_object_get() */
855 vlc_internals (internals->next)->prev = internals->prev;
856 vlc_internals (internals->prev)->next = internals->next;
858 /* Detach from parent to protect against FIND_CHILDREN */
859 vlc_object_detach_unlocked (p_this);
860 /* Detach from children to protect against FIND_PARENT */
861 for (int i = 0; i < internals->i_children; i++)
862 internals->pp_children[i]->p_parent = NULL;
865 vlc_mutex_unlock( &structure_lock );
867 if( b_should_destroy )
869 free( internals->pp_children );
870 internals->pp_children = NULL;
871 internals->i_children = 0;
872 vlc_object_destroy( p_this );
877 ****************************************************************************
878 * attach object to a parent object
879 *****************************************************************************
880 * This function sets p_this as a child of p_parent, and p_parent as a parent
881 * of p_this. This link can be undone using vlc_object_detach.
882 *****************************************************************************/
883 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
885 if( !p_this ) return;
887 vlc_mutex_lock( &structure_lock );
889 /* Attach the parent to its child */
890 assert (!p_this->p_parent);
891 p_this->p_parent = p_parent;
893 /* Attach the child to its parent */
894 vlc_object_internals_t *priv = vlc_internals( p_parent );
895 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
898 vlc_mutex_unlock( &structure_lock );
902 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
904 vlc_assert_locked (&structure_lock);
906 if (p_this->p_parent == NULL)
909 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
913 /* Remove p_this's parent */
914 p_this->p_parent = NULL;
916 /* Remove all of p_parent's children which are p_this */
917 for( i_index = priv->i_children ; i_index-- ; )
919 if( priv->pp_children[i_index] == p_this )
922 for( i = i_index ; i < priv->i_children ; i++ )
923 priv->pp_children[i] = priv->pp_children[i+1];
927 if( priv->i_children )
929 priv->pp_children = (vlc_object_t **)realloc( priv->pp_children,
930 priv->i_children * sizeof(vlc_object_t *) );
934 /* Special case - don't realloc() to zero to avoid leaking */
935 free( priv->pp_children );
936 priv->pp_children = NULL;
942 ****************************************************************************
943 * detach object from its parent
944 *****************************************************************************
945 * This function removes all links between an object and its parent.
946 *****************************************************************************/
947 void __vlc_object_detach( vlc_object_t *p_this )
949 if( !p_this ) return;
951 vlc_mutex_lock( &structure_lock );
952 if( !p_this->p_parent )
953 msg_Err( p_this, "object is not attached" );
955 vlc_object_detach_unlocked( p_this );
956 vlc_mutex_unlock( &structure_lock );
961 ****************************************************************************
962 * find a list typed objects and increment their refcount
963 *****************************************************************************
964 * This function recursively looks for a given object type. i_mode can be one
965 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
966 *****************************************************************************/
967 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
972 /* Look for the objects */
973 switch( i_mode & 0x000f )
976 /* Modules should probably not be object, and the module should perhaps
977 * not be shared across LibVLC instances. In the mean time, this ugly
978 * hack is brought to you by Courmisch. */
979 if (i_type == VLC_OBJECT_MODULE)
980 return vlc_list_find ((vlc_object_t *)vlc_global ()->p_module_bank,
982 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
985 vlc_mutex_lock( &structure_lock );
986 i_count = CountChildren( p_this, i_type );
987 p_list = NewList( i_count );
989 /* Check allocation was successful */
990 if( p_list->i_count != i_count )
992 vlc_mutex_unlock( &structure_lock );
993 msg_Err( p_this, "list allocation failed!" );
999 ListChildren( p_list, p_this, i_type );
1000 vlc_mutex_unlock( &structure_lock );
1004 msg_Err( p_this, "unimplemented!" );
1005 p_list = NewList( 0 );
1013 * Gets the list of children of an objects, and increment their reference
1015 * @return a list (possibly empty) or NULL in case of error.
1017 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
1020 vlc_object_internals_t *priv = vlc_internals( obj );
1022 vlc_mutex_lock( &structure_lock );
1023 l = NewList( priv->i_children );
1024 for (int i = 0; i < l->i_count; i++)
1026 vlc_object_yield( priv->pp_children[i] );
1027 l->p_values[i].p_object = priv->pp_children[i];
1029 vlc_mutex_unlock( &structure_lock );
1033 /*****************************************************************************
1034 * DumpCommand: print the current vlc structure
1035 *****************************************************************************
1036 * This function prints either an ASCII tree showing the connections between
1037 * vlc objects, and additional information such as their refcount, thread ID,
1038 * etc. (command "tree"), or the same data as a simple list (command "list").
1039 *****************************************************************************/
1040 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1041 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1043 (void)oldval; (void)p_data;
1044 if( *psz_cmd == 'l' )
1046 vlc_object_t *root = VLC_OBJECT (vlc_global ()), *cur = root;
1048 vlc_mutex_lock( &structure_lock );
1051 PrintObject (cur, "");
1052 cur = vlc_internals (cur)->next;
1054 while (cur != root);
1055 vlc_mutex_unlock( &structure_lock );
1059 vlc_object_t *p_object = NULL;
1061 if( *newval.psz_string )
1064 int i_id = strtol( newval.psz_string, &end, 0 );
1065 if( end != newval.psz_string )
1066 p_object = vlc_object_get( i_id );
1068 /* try using the object's name to find it */
1069 p_object = vlc_object_find_name( p_this, newval.psz_string,
1078 vlc_mutex_lock( &structure_lock );
1080 if( *psz_cmd == 't' )
1082 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1085 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1088 DumpStructure( p_object, 0, psz_foo );
1090 else if( *psz_cmd == 'v' )
1095 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1097 PrintObject( p_object, "" );
1099 if( !vlc_internals( p_object )->i_vars )
1100 printf( " `-o No variables\n" );
1101 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1103 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1105 const char *psz_type = "unknown";
1106 switch( p_var->i_type & VLC_VAR_TYPE )
1108 #define MYCASE( type, nice ) \
1109 case VLC_VAR_ ## type: \
1112 MYCASE( VOID, "void" );
1113 MYCASE( BOOL, "bool" );
1114 MYCASE( INTEGER, "integer" );
1115 MYCASE( HOTKEY, "hotkey" );
1116 MYCASE( STRING, "string" );
1117 MYCASE( MODULE, "module" );
1118 MYCASE( FILE, "file" );
1119 MYCASE( DIRECTORY, "directory" );
1120 MYCASE( VARIABLE, "variable" );
1121 MYCASE( FLOAT, "float" );
1122 MYCASE( TIME, "time" );
1123 MYCASE( ADDRESS, "address" );
1124 MYCASE( MUTEX, "mutex" );
1125 MYCASE( LIST, "list" );
1128 printf( " %c-o \"%s\" (%s",
1129 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1130 p_var->psz_name, psz_type );
1131 if( p_var->psz_text )
1132 printf( ", %s", p_var->psz_text );
1134 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1135 printf( ", command" );
1136 if( p_var->i_entries )
1137 printf( ", %d callbacks", p_var->i_entries );
1138 switch( p_var->i_type & 0x00f0 )
1144 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1146 case VLC_VAR_INTEGER:
1147 printf( ": %d", p_var->val.i_int );
1149 case VLC_VAR_STRING:
1150 printf( ": \"%s\"", p_var->val.psz_string );
1153 printf( ": %f", p_var->val.f_float );
1156 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1158 case VLC_VAR_ADDRESS:
1159 printf( ": %p", p_var->val.p_address );
1169 vlc_mutex_unlock( &structure_lock );
1171 if( *newval.psz_string )
1173 vlc_object_release( p_object );
1180 /*****************************************************************************
1181 * vlc_list_release: free a list previously allocated by vlc_list_find
1182 *****************************************************************************
1183 * This function decreases the refcount of all objects in the list and
1185 *****************************************************************************/
1186 void vlc_list_release( vlc_list_t *p_list )
1190 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1192 vlc_object_release( p_list->p_values[i_index].p_object );
1195 free( p_list->p_values );
1199 /*****************************************************************************
1200 * dump an object. (Debug function)
1201 *****************************************************************************/
1202 void __vlc_object_dump( vlc_object_t *p_this )
1204 vlc_mutex_lock( &structure_lock );
1205 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1207 DumpStructure( p_this, 0, psz_foo );
1208 vlc_mutex_unlock( &structure_lock );
1211 /* Following functions are local */
1213 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1216 vlc_object_t *p_tmp;
1218 switch( i_mode & 0x000f )
1221 p_tmp = p_this->p_parent;
1224 if( p_tmp->i_object_type == i_type )
1226 vlc_object_yield( p_tmp );
1231 return FindObject( p_tmp, i_type, i_mode );
1237 for( i = vlc_internals( p_this )->i_children; i--; )
1239 p_tmp = vlc_internals( p_this )->pp_children[i];
1240 if( p_tmp->i_object_type == i_type )
1242 vlc_object_yield( p_tmp );
1245 else if( vlc_internals( p_tmp )->i_children )
1247 p_tmp = FindObject( p_tmp, i_type, i_mode );
1257 /* Handled in vlc_object_find */
1264 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1265 const char *psz_name,
1269 vlc_object_t *p_tmp;
1271 switch( i_mode & 0x000f )
1274 p_tmp = p_this->p_parent;
1277 if( p_tmp->psz_object_name
1278 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1280 vlc_object_yield( p_tmp );
1285 return FindObjectName( p_tmp, psz_name, i_mode );
1291 for( i = vlc_internals( p_this )->i_children; i--; )
1293 p_tmp = vlc_internals( p_this )->pp_children[i];
1294 if( p_tmp->psz_object_name
1295 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1297 vlc_object_yield( p_tmp );
1300 else if( vlc_internals( p_tmp )->i_children )
1302 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1312 /* Handled in vlc_object_find */
1320 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1322 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1325 memset( &psz_name, 0, sizeof(psz_name) );
1326 if( p_this->psz_object_name )
1328 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1330 psz_name[48] = '\"';
1333 psz_children[0] = '\0';
1334 switch( vlc_internals( p_this )->i_children )
1339 strcpy( psz_children, ", 1 child" );
1342 snprintf( psz_children, 19, ", %i children",
1343 vlc_internals( p_this )->i_children );
1347 psz_refcount[0] = '\0';
1348 if( vlc_internals( p_this )->i_refcount > 0 )
1349 snprintf( psz_refcount, 19, ", refcount %u",
1350 vlc_internals( p_this )->i_refcount );
1352 psz_thread[0] = '\0';
1353 if( vlc_internals( p_this )->b_thread )
1354 snprintf( psz_thread, 29, " (thread %lu)",
1355 (unsigned long)vlc_internals( p_this )->thread_id );
1357 psz_parent[0] = '\0';
1358 if( p_this->p_parent )
1359 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1361 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1362 p_this->i_object_id, p_this->psz_object_type,
1363 psz_name, psz_thread, psz_refcount, psz_children,
1367 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1370 char i_back = psz_foo[i_level];
1371 psz_foo[i_level] = '\0';
1373 PrintObject( p_this, psz_foo );
1375 psz_foo[i_level] = i_back;
1377 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1379 msg_Warn( p_this, "structure tree is too deep" );
1383 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1387 psz_foo[i_level-1] = ' ';
1389 if( psz_foo[i_level-2] == '`' )
1391 psz_foo[i_level-2] = ' ';
1395 if( i == vlc_internals( p_this )->i_children - 1 )
1397 psz_foo[i_level] = '`';
1401 psz_foo[i_level] = '|';
1404 psz_foo[i_level+1] = '-';
1405 psz_foo[i_level+2] = '\0';
1407 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1412 static vlc_list_t * NewList( int i_count )
1414 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1415 if( p_list == NULL )
1420 p_list->i_count = i_count;
1424 p_list->p_values = NULL;
1428 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1429 if( p_list->p_values == NULL )
1431 p_list->i_count = 0;
1438 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1441 if( p_list == NULL || i_index >= p_list->i_count )
1446 vlc_object_yield( p_object );
1448 p_list->p_values[i_index].p_object = p_object;
1453 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1455 if( p_list == NULL )
1460 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1461 * sizeof( vlc_value_t ) );
1462 if( p_list->p_values == NULL )
1464 p_list->i_count = 0;
1468 vlc_object_yield( p_object );
1470 p_list->p_values[p_list->i_count].p_object = p_object;
1476 static int CountChildren( vlc_object_t *p_this, int i_type )
1478 vlc_object_t *p_tmp;
1481 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1483 p_tmp = vlc_internals( p_this )->pp_children[i];
1485 if( p_tmp->i_object_type == i_type )
1489 i_count += CountChildren( p_tmp, i_type );
1495 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1497 vlc_object_t *p_tmp;
1500 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1502 p_tmp = vlc_internals( p_this )->pp_children[i];
1504 if( p_tmp->i_object_type == i_type )
1505 ListReplace( p_list, p_tmp, p_list->i_count++ );
1507 ListChildren( p_list, p_tmp, i_type );
1511 #ifdef LIBVLC_REFCHECK
1512 # if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE)
1513 # include <execinfo.h>
1516 void vlc_refcheck (vlc_object_t *obj)
1518 static unsigned errors = 0;
1522 /* Anyone can use the root object (though it should not exist) */
1523 if (obj == VLC_OBJECT (vlc_global ()))
1526 /* Anyone can use its libvlc instance object */
1527 if (obj == VLC_OBJECT (obj->p_libvlc))
1530 /* The thread that created the object holds the initial reference */
1531 vlc_object_internals_t *priv = vlc_internals (obj);
1532 #if defined (LIBVLC_USE_PTHREAD)
1533 if (pthread_equal (priv->creator_id, pthread_self ()))
1535 if (priv->creator_id == GetCurrentThreadId ())
1541 /* A thread can use its own object without references! */
1542 vlc_object_t *caller = vlc_threadobj ();
1546 /* The calling thread is younger than the object.
1547 * Access could be valid through cross-thread synchronization;
1548 * we would need better accounting. */
1549 if (caller && (caller->i_object_id > obj->i_object_id))
1553 vlc_spin_lock (&priv->ref_spin);
1554 refs = priv->i_refcount;
1555 vlc_spin_unlock (&priv->ref_spin);
1557 for (held_list_t *hlcur = vlc_threadvar_get (&held_objects);
1558 hlcur != NULL; hlcur = hlcur->next)
1559 if (hlcur->obj == obj)
1562 fprintf (stderr, "The %s %s thread object is accessing...\n"
1563 "the %s %s object without references.\n",
1564 caller && caller->psz_object_name
1565 ? caller->psz_object_name : "unnamed",
1566 caller ? caller->psz_object_type : "main",
1567 obj->psz_object_name ? obj->psz_object_name : "unnamed",
1568 obj->psz_object_type);
1571 #ifdef HAVE_BACKTRACE
1573 int stackdepth = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
1574 backtrace_symbols_fd (stack, stackdepth, 2);
1577 if (++errors == 100)
1578 fprintf (stderr, "Too many reference errors!\n");
1581 static void held_objects_destroy (void *data)
1584 held_list_t *hl = vlc_threadvar_get (&held_objects);
1585 vlc_object_t *caller = vlc_threadobj ();
1589 held_list_t *buf = hl->next;
1590 vlc_object_t *obj = hl->obj;
1592 fprintf (stderr, "The %s %s thread object leaked a reference to...\n"
1593 "the %s %s object.\n",
1594 caller && caller->psz_object_name
1595 ? caller->psz_object_name : "unnamed",
1596 caller ? caller->psz_object_type : "main",
1597 obj->psz_object_name ? obj->psz_object_name : "unnamed",
1598 obj->psz_object_type);