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_INTF:
230 i_size = sizeof(intf_thread_t);
231 psz_type = "interface";
233 case VLC_OBJECT_DECODER:
234 i_size = sizeof(decoder_t);
235 psz_type = "decoder";
237 case VLC_OBJECT_PACKETIZER:
238 i_size = sizeof(decoder_t);
239 psz_type = "packetizer";
241 case VLC_OBJECT_ENCODER:
242 i_size = sizeof(encoder_t);
243 psz_type = "encoder";
245 case VLC_OBJECT_AOUT:
246 i_size = sizeof(aout_instance_t);
247 psz_type = "audio output";
249 case VLC_OBJECT_OPENGL:
250 i_size = sizeof( vout_thread_t );
253 case VLC_OBJECT_ANNOUNCE:
254 i_size = sizeof( announce_handler_t );
255 psz_type = "announce";
258 assert( i_type > 0 ); /* unknown type?! */
260 i_type = VLC_OBJECT_GENERIC;
261 psz_type = "generic";
265 return vlc_custom_create( p_this, i_size, i_type, psz_type );
270 ****************************************************************************
271 * Set the destructor of a vlc object
273 * This function sets the destructor of the vlc object. It will be called
274 * when the object is destroyed when the its refcount reaches 0.
275 * (It is called by the internal function vlc_object_destroy())
276 *****************************************************************************/
277 void __vlc_object_set_destructor( vlc_object_t *p_this,
278 vlc_destructor_t pf_destructor )
280 vlc_object_internals_t *p_priv = vlc_internals(p_this );
281 p_priv->pf_destructor = pf_destructor;
285 ****************************************************************************
286 * Destroy a vlc object (Internal)
288 * This function destroys an object that has been previously allocated with
289 * vlc_object_create. The object's refcount must be zero and it must not be
290 * attached to other objects in any way.
291 *****************************************************************************/
292 static void vlc_object_destroy( vlc_object_t *p_this )
294 vlc_object_internals_t *p_priv = vlc_internals( p_this );
296 /* Objects are always detached beforehand */
297 assert( !p_this->p_parent );
299 /* Send a kill to the object's thread if applicable */
300 vlc_object_kill( p_this );
302 /* If we are running on a thread, wait until it ends */
303 if( p_priv->b_thread )
305 msg_Warn (p_this->p_libvlc, /* do NOT use a dead object for logging! */
306 "object %d destroyed while thread alive (VLC might crash)",
307 p_this->i_object_id);
308 vlc_thread_join( p_this );
311 /* Call the custom "subclass" destructor */
312 if( p_priv->pf_destructor )
313 p_priv->pf_destructor( p_this );
315 /* Destroy the associated variables, starting from the end so that
316 * no memmove calls have to be done. */
317 while( p_priv->i_vars )
319 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
322 free( p_priv->p_vars );
323 vlc_mutex_destroy( &p_priv->var_lock );
325 free( p_this->psz_header );
327 if( p_this->p_libvlc == NULL )
330 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
332 assert( p_global == vlc_global() );
334 if (p_priv->next != p_this)
336 vlc_object_t *leaked = p_priv->next, *first = leaked;
339 /* We are leaking this object */
341 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
342 leaked->i_object_id, leaked->psz_object_type,
343 leaked->psz_object_name );
344 /* Dump libvlc object to ease debugging */
345 vlc_object_dump( leaked );
347 leaked = vlc_internals (leaked)->next;
349 while (leaked != first);
351 /* Dump global object to ease debugging */
352 vlc_object_dump( p_this );
353 /* Strongly abort, cause we want these to be fixed */
358 /* We are the global object ... no need to lock. */
359 vlc_mutex_destroy( &structure_lock );
360 #ifdef LIBVLC_REFCHECK
361 held_objects_destroy( vlc_threadvar_get( &held_objects ) );
362 vlc_threadvar_delete( &held_objects );
366 FREENULL( p_this->psz_object_name );
368 #if defined(WIN32) || defined(UNDER_CE)
369 /* if object has an associated thread, close it now */
370 if( p_priv->thread_id )
371 CloseHandle(p_priv->thread_id);
374 vlc_spin_destroy( &p_priv->ref_spin );
375 vlc_mutex_destroy( &p_priv->lock );
376 vlc_cond_destroy( &p_priv->wait );
377 vlc_spin_destroy( &p_priv->spin );
378 if( p_priv->pipes[1] != -1 )
379 close( p_priv->pipes[1] );
380 if( p_priv->pipes[0] != -1 )
381 close( p_priv->pipes[0] );
387 /** Inter-object signaling */
389 void __vlc_object_lock( vlc_object_t *obj )
391 vlc_mutex_lock( &(vlc_internals(obj)->lock) );
394 void __vlc_object_unlock( vlc_object_t *obj )
396 vlc_assert_locked( &(vlc_internals(obj)->lock) );
397 vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
401 # include <winsock2.h>
402 # include <ws2tcpip.h>
405 * select()-able pipes emulated using Winsock
407 static int pipe (int fd[2])
410 int addrlen = sizeof (addr);
412 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
413 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
414 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
417 memset (&addr, 0, sizeof (addr));
418 addr.sin_family = AF_INET;
419 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
420 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
421 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
423 || connect (c, (PSOCKADDR)&addr, addrlen))
426 a = accept (l, NULL, NULL);
427 if (a == INVALID_SOCKET)
438 if (l != INVALID_SOCKET)
440 if (c != INVALID_SOCKET)
446 #define read( a, b, c ) recv (a, b, c, 0)
448 #define write( a, b, c ) send (a, b, c, 0)
450 #define close( a ) closesocket (a)
454 * Returns the readable end of a pipe that becomes readable once termination
455 * of the object is requested (vlc_object_kill()).
456 * This can be used to wake-up out of a select() or poll() event loop, such
457 * typically when doing network I/O.
459 * Note that the pipe will remain the same for the lifetime of the object.
460 * DO NOT read the pipe nor close it yourself. Ever.
462 * @param obj object that would be "killed"
463 * @return a readable pipe descriptor, or -1 on error.
465 int __vlc_object_waitpipe( vlc_object_t *obj )
467 int pfd[2] = { -1, -1 };
468 vlc_object_internals_t *internals = vlc_internals( obj );
471 vlc_spin_lock (&internals->spin);
472 if (internals->pipes[0] == -1)
474 /* This can only ever happen if someone killed us without locking: */
475 assert (internals->pipes[1] == -1);
476 vlc_spin_unlock (&internals->spin);
481 vlc_spin_lock (&internals->spin);
482 if (internals->pipes[0] == -1)
484 internals->pipes[0] = pfd[0];
485 internals->pipes[1] = pfd[1];
486 pfd[0] = pfd[1] = -1;
490 vlc_spin_unlock (&internals->spin);
494 /* Race condition: vlc_object_kill() already invoked! */
497 vlc_spin_lock (&internals->spin);
498 fd = internals->pipes[1];
499 internals->pipes[1] = -1;
500 vlc_spin_unlock (&internals->spin);
502 msg_Dbg (obj, "waitpipe: object already dying");
507 /* Race condition: two threads call pipe() - unlikely */
513 return internals->pipes[0];
518 * Waits for the object to be signaled (using vlc_object_signal()).
519 * It is assumed that the caller has locked the object. This function will
520 * unlock the object, and lock it again before returning.
521 * If the object was signaled before the caller locked the object, it is
522 * undefined whether the signal will be lost or will wake the process.
524 * @return true if the object is dying and should terminate.
526 void __vlc_object_wait( vlc_object_t *obj )
528 vlc_object_internals_t *priv = vlc_internals( obj );
529 vlc_assert_locked( &priv->lock);
530 vlc_cond_wait( &priv->wait, &priv->lock );
535 * Waits for the object to be signaled (using vlc_object_signal()), or for
536 * a timer to expire. It is asserted that the caller holds the object lock.
538 * @return 0 if the object was signaled before the timer expiration, or
539 * ETIMEDOUT if the timer expired without any signal.
541 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
543 vlc_object_internals_t *priv = vlc_internals( obj );
544 vlc_assert_locked( &priv->lock);
545 return vlc_cond_timedwait( &priv->wait, &priv->lock, deadline );
550 * Checks whether an object has been "killed".
551 * The object lock must be held.
553 * Typical code for an object thread could be:
555 vlc_object_lock (self);
557 while (vlc_object_alive (self))
561 vlc_object_wait (self);
565 ...deinitialization...
566 vlc_object_unlock (self);
569 * @return true iff the object has not been killed yet
571 bool __vlc_object_alive( vlc_object_t *obj )
573 vlc_assert_locked( &(vlc_internals(obj)->lock) );
579 * Signals an object for which the lock is held.
580 * At least one thread currently sleeping in vlc_object_wait() or
581 * vlc_object_timedwait() will wake up, assuming that there is at least one
582 * such thread in the first place. Otherwise, it is undefined whether the
583 * signal will be lost or will wake up one or more thread later.
585 void __vlc_object_signal_unlocked( vlc_object_t *obj )
587 vlc_assert_locked (&(vlc_internals(obj)->lock));
588 vlc_cond_signal( &(vlc_internals(obj)->wait) );
593 * Requests termination of an object.
594 * If the object is LibVLC, also request to terminate all its children.
596 void __vlc_object_kill( vlc_object_t *p_this )
598 vlc_object_internals_t *priv = vlc_internals( p_this );
601 vlc_object_lock( p_this );
602 p_this->b_die = true;
604 vlc_spin_lock (&priv->spin);
607 vlc_spin_unlock (&priv->spin);
611 msg_Dbg (p_this, "waitpipe: object killed");
615 vlc_object_signal_unlocked( p_this );
616 vlc_object_unlock( p_this );
621 * Find an object given its ID.
623 * This function looks for the object whose i_object_id field is i_id.
624 * This function is slow, and often used to hide bugs. Do not use it.
625 * If you need to retain reference to an object, yield the object pointer with
626 * vlc_object_yield(), use the pointer as your reference, and call
627 * vlc_object_release() when you're done.
629 void * vlc_object_get( int i_id )
631 libvlc_global_data_t *p_libvlc_global = vlc_global();
632 vlc_object_t *obj = NULL;
634 vlc_object_t *caller = vlc_threadobj ();
637 msg_Dbg (caller, "uses deprecated vlc_object_get(%d)", i_id);
639 fprintf (stderr, "main thread uses deprecated vlc_object_get(%d)\n",
642 vlc_mutex_lock( &structure_lock );
644 for( obj = vlc_internals (p_libvlc_global)->next;
645 obj != VLC_OBJECT (p_libvlc_global);
646 obj = vlc_internals (obj)->next )
648 if( obj->i_object_id == i_id )
650 vlc_object_yield( obj );
657 msg_Warn (caller, "wants non-existing object %d", i_id);
659 fprintf (stderr, "main thread wants non-existing object %d\n", i_id);
662 vlc_mutex_unlock( &structure_lock );
667 ****************************************************************************
668 * find a typed object and increment its refcount
669 *****************************************************************************
670 * This function recursively looks for a given object type. i_mode can be one
671 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
672 *****************************************************************************/
673 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
675 vlc_object_t *p_found;
677 /* If we are of the requested type ourselves, don't look further */
678 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
680 vlc_object_yield( p_this );
684 /* Otherwise, recursively look for the object */
685 if ((i_mode & 0x000f) == FIND_ANYWHERE)
688 if (i_type == VLC_OBJECT_PLAYLIST)
689 msg_Err (p_this, "using vlc_object_find(VLC_OBJECT_PLAYLIST) "
690 "instead of pl_Yield()");
692 return vlc_object_find (p_this->p_libvlc, i_type,
693 (i_mode & ~0x000f)|FIND_CHILD);
696 vlc_mutex_lock( &structure_lock );
697 p_found = FindObject( p_this, i_type, i_mode );
698 vlc_mutex_unlock( &structure_lock );
703 ****************************************************************************
704 * find a named object and increment its refcount
705 *****************************************************************************
706 * This function recursively looks for a given object name. i_mode can be one
707 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
708 *****************************************************************************/
709 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
712 vlc_object_t *p_found;
714 /* If have the requested name ourselves, don't look further */
715 if( !(i_mode & FIND_STRICT)
716 && p_this->psz_object_name
717 && !strcmp( p_this->psz_object_name, psz_name ) )
719 vlc_object_yield( p_this );
723 vlc_mutex_lock( &structure_lock );
725 /* Otherwise, recursively look for the object */
726 if( (i_mode & 0x000f) == FIND_ANYWHERE )
728 vlc_object_t *p_root = p_this;
731 while( p_root->p_parent != NULL &&
732 p_root != VLC_OBJECT( p_this->p_libvlc ) )
734 p_root = p_root->p_parent;
737 p_found = FindObjectName( p_root, psz_name,
738 (i_mode & ~0x000f)|FIND_CHILD );
739 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
741 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
742 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
747 p_found = FindObjectName( p_this, psz_name, i_mode );
750 vlc_mutex_unlock( &structure_lock );
756 * Increment an object reference counter.
758 void __vlc_object_yield( vlc_object_t *p_this )
760 vlc_object_internals_t *internals = vlc_internals( p_this );
762 vlc_spin_lock( &internals->ref_spin );
763 /* Avoid obvious freed object uses */
764 assert( internals->i_refcount > 0 );
765 /* Increment the counter */
766 internals->i_refcount++;
767 vlc_spin_unlock( &internals->ref_spin );
768 #ifdef LIBVLC_REFCHECK
769 /* Update the list of referenced objects */
770 /* Using TLS, so no need to lock */
771 /* The following line may leak memory if a thread leaks objects. */
772 held_list_t *newhead = malloc (sizeof (*newhead));
773 held_list_t *oldhead = vlc_threadvar_get (&held_objects);
774 newhead->next = oldhead;
775 newhead->obj = p_this;
776 vlc_threadvar_set (&held_objects, newhead);
780 /*****************************************************************************
781 * decrement an object refcount
782 * And destroy the object if its refcount reach zero.
783 *****************************************************************************/
784 void __vlc_object_release( vlc_object_t *p_this )
786 vlc_object_internals_t *internals = vlc_internals( p_this );
787 bool b_should_destroy;
789 #ifdef LIBVLC_REFCHECK
790 /* Update the list of referenced objects */
791 /* Using TLS, so no need to lock */
792 for (held_list_t *hlcur = vlc_threadvar_get (&held_objects),
795 hlprev = hlcur, hlcur = hlcur->next)
797 if (hlcur->obj == p_this)
800 vlc_threadvar_set (&held_objects, hlcur->next);
802 hlprev->next = hlcur->next;
807 /* TODO: what if releasing without references? */
810 vlc_spin_lock( &internals->ref_spin );
811 assert( internals->i_refcount > 0 );
813 if( internals->i_refcount > 1 )
816 /* There are still other references to the object */
817 internals->i_refcount--;
818 vlc_spin_unlock( &internals->ref_spin );
821 vlc_spin_unlock( &internals->ref_spin );
824 /* Remember that we cannot hold the spin while waiting on the mutex */
825 vlc_mutex_lock( &structure_lock );
826 /* Take the spin again. Note that another thread may have yielded the
827 * object in the (very short) mean time. */
828 vlc_spin_lock( &internals->ref_spin );
829 b_should_destroy = --internals->i_refcount == 0;
830 vlc_spin_unlock( &internals->ref_spin );
832 if( b_should_destroy )
834 /* Remove the object from object list
835 * so that it cannot be encountered by vlc_object_get() */
836 vlc_internals (internals->next)->prev = internals->prev;
837 vlc_internals (internals->prev)->next = internals->next;
839 /* Detach from parent to protect against FIND_CHILDREN */
840 vlc_object_detach_unlocked (p_this);
841 /* Detach from children to protect against FIND_PARENT */
842 for (int i = 0; i < internals->i_children; i++)
843 internals->pp_children[i]->p_parent = NULL;
846 vlc_mutex_unlock( &structure_lock );
848 if( b_should_destroy )
850 free( internals->pp_children );
851 internals->pp_children = NULL;
852 internals->i_children = 0;
853 vlc_object_destroy( p_this );
858 ****************************************************************************
859 * attach object to a parent object
860 *****************************************************************************
861 * This function sets p_this as a child of p_parent, and p_parent as a parent
862 * of p_this. This link can be undone using vlc_object_detach.
863 *****************************************************************************/
864 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
866 if( !p_this ) return;
868 vlc_mutex_lock( &structure_lock );
870 /* Attach the parent to its child */
871 assert (!p_this->p_parent);
872 p_this->p_parent = p_parent;
874 /* Attach the child to its parent */
875 vlc_object_internals_t *priv = vlc_internals( p_parent );
876 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
879 vlc_mutex_unlock( &structure_lock );
883 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
885 vlc_assert_locked (&structure_lock);
887 if (p_this->p_parent == NULL)
890 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
894 /* Remove p_this's parent */
895 p_this->p_parent = NULL;
897 /* Remove all of p_parent's children which are p_this */
898 for( i_index = priv->i_children ; i_index-- ; )
900 if( priv->pp_children[i_index] == p_this )
903 for( i = i_index ; i < priv->i_children ; i++ )
904 priv->pp_children[i] = priv->pp_children[i+1];
908 if( priv->i_children )
910 priv->pp_children = (vlc_object_t **)realloc( priv->pp_children,
911 priv->i_children * sizeof(vlc_object_t *) );
915 /* Special case - don't realloc() to zero to avoid leaking */
916 free( priv->pp_children );
917 priv->pp_children = NULL;
923 ****************************************************************************
924 * detach object from its parent
925 *****************************************************************************
926 * This function removes all links between an object and its parent.
927 *****************************************************************************/
928 void __vlc_object_detach( vlc_object_t *p_this )
930 if( !p_this ) return;
932 vlc_mutex_lock( &structure_lock );
933 if( !p_this->p_parent )
934 msg_Err( p_this, "object is not attached" );
936 vlc_object_detach_unlocked( p_this );
937 vlc_mutex_unlock( &structure_lock );
942 ****************************************************************************
943 * find a list typed objects and increment their refcount
944 *****************************************************************************
945 * This function recursively looks for a given object type. i_mode can be one
946 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
947 *****************************************************************************/
948 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
953 /* Look for the objects */
954 switch( i_mode & 0x000f )
957 /* Modules should probably not be object, and the module should perhaps
958 * not be shared across LibVLC instances. In the mean time, this ugly
959 * hack is brought to you by Courmisch. */
960 if (i_type == VLC_OBJECT_MODULE)
961 return vlc_list_find ((vlc_object_t *)vlc_global ()->p_module_bank,
963 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
966 vlc_mutex_lock( &structure_lock );
967 i_count = CountChildren( p_this, i_type );
968 p_list = NewList( i_count );
970 /* Check allocation was successful */
971 if( p_list->i_count != i_count )
973 vlc_mutex_unlock( &structure_lock );
974 msg_Err( p_this, "list allocation failed!" );
980 ListChildren( p_list, p_this, i_type );
981 vlc_mutex_unlock( &structure_lock );
985 msg_Err( p_this, "unimplemented!" );
986 p_list = NewList( 0 );
994 * Gets the list of children of an objects, and increment their reference
996 * @return a list (possibly empty) or NULL in case of error.
998 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
1001 vlc_object_internals_t *priv = vlc_internals( obj );
1003 vlc_mutex_lock( &structure_lock );
1004 l = NewList( priv->i_children );
1005 for (int i = 0; i < l->i_count; i++)
1007 vlc_object_yield( priv->pp_children[i] );
1008 l->p_values[i].p_object = priv->pp_children[i];
1010 vlc_mutex_unlock( &structure_lock );
1014 /*****************************************************************************
1015 * DumpCommand: print the current vlc structure
1016 *****************************************************************************
1017 * This function prints either an ASCII tree showing the connections between
1018 * vlc objects, and additional information such as their refcount, thread ID,
1019 * etc. (command "tree"), or the same data as a simple list (command "list").
1020 *****************************************************************************/
1021 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1022 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1024 (void)oldval; (void)p_data;
1025 if( *psz_cmd == 'l' )
1027 vlc_object_t *root = VLC_OBJECT (vlc_global ()), *cur = root;
1029 vlc_mutex_lock( &structure_lock );
1032 PrintObject (cur, "");
1033 cur = vlc_internals (cur)->next;
1035 while (cur != root);
1036 vlc_mutex_unlock( &structure_lock );
1040 vlc_object_t *p_object = NULL;
1042 if( *newval.psz_string )
1045 int i_id = strtol( newval.psz_string, &end, 0 );
1046 if( end != newval.psz_string )
1047 p_object = vlc_object_get( i_id );
1049 /* try using the object's name to find it */
1050 p_object = vlc_object_find_name( p_this, newval.psz_string,
1059 vlc_mutex_lock( &structure_lock );
1061 if( *psz_cmd == 't' )
1063 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1066 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1069 DumpStructure( p_object, 0, psz_foo );
1071 else if( *psz_cmd == 'v' )
1076 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1078 PrintObject( p_object, "" );
1080 if( !vlc_internals( p_object )->i_vars )
1081 printf( " `-o No variables\n" );
1082 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1084 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1086 const char *psz_type = "unknown";
1087 switch( p_var->i_type & VLC_VAR_TYPE )
1089 #define MYCASE( type, nice ) \
1090 case VLC_VAR_ ## type: \
1093 MYCASE( VOID, "void" );
1094 MYCASE( BOOL, "bool" );
1095 MYCASE( INTEGER, "integer" );
1096 MYCASE( HOTKEY, "hotkey" );
1097 MYCASE( STRING, "string" );
1098 MYCASE( MODULE, "module" );
1099 MYCASE( FILE, "file" );
1100 MYCASE( DIRECTORY, "directory" );
1101 MYCASE( VARIABLE, "variable" );
1102 MYCASE( FLOAT, "float" );
1103 MYCASE( TIME, "time" );
1104 MYCASE( ADDRESS, "address" );
1105 MYCASE( MUTEX, "mutex" );
1106 MYCASE( LIST, "list" );
1109 printf( " %c-o \"%s\" (%s",
1110 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1111 p_var->psz_name, psz_type );
1112 if( p_var->psz_text )
1113 printf( ", %s", p_var->psz_text );
1115 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1116 printf( ", command" );
1117 if( p_var->i_entries )
1118 printf( ", %d callbacks", p_var->i_entries );
1119 switch( p_var->i_type & 0x00f0 )
1125 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1127 case VLC_VAR_INTEGER:
1128 printf( ": %d", p_var->val.i_int );
1130 case VLC_VAR_STRING:
1131 printf( ": \"%s\"", p_var->val.psz_string );
1134 printf( ": %f", p_var->val.f_float );
1137 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1139 case VLC_VAR_ADDRESS:
1140 printf( ": %p", p_var->val.p_address );
1150 vlc_mutex_unlock( &structure_lock );
1152 if( *newval.psz_string )
1154 vlc_object_release( p_object );
1161 /*****************************************************************************
1162 * vlc_list_release: free a list previously allocated by vlc_list_find
1163 *****************************************************************************
1164 * This function decreases the refcount of all objects in the list and
1166 *****************************************************************************/
1167 void vlc_list_release( vlc_list_t *p_list )
1171 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1173 vlc_object_release( p_list->p_values[i_index].p_object );
1176 free( p_list->p_values );
1180 /*****************************************************************************
1181 * dump an object. (Debug function)
1182 *****************************************************************************/
1183 void __vlc_object_dump( vlc_object_t *p_this )
1185 vlc_mutex_lock( &structure_lock );
1186 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1188 DumpStructure( p_this, 0, psz_foo );
1189 vlc_mutex_unlock( &structure_lock );
1192 /* Following functions are local */
1194 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1197 vlc_object_t *p_tmp;
1199 switch( i_mode & 0x000f )
1202 p_tmp = p_this->p_parent;
1205 if( p_tmp->i_object_type == i_type )
1207 vlc_object_yield( p_tmp );
1212 return FindObject( p_tmp, i_type, i_mode );
1218 for( i = vlc_internals( p_this )->i_children; i--; )
1220 p_tmp = vlc_internals( p_this )->pp_children[i];
1221 if( p_tmp->i_object_type == i_type )
1223 vlc_object_yield( p_tmp );
1226 else if( vlc_internals( p_tmp )->i_children )
1228 p_tmp = FindObject( p_tmp, i_type, i_mode );
1238 /* Handled in vlc_object_find */
1245 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1246 const char *psz_name,
1250 vlc_object_t *p_tmp;
1252 switch( i_mode & 0x000f )
1255 p_tmp = p_this->p_parent;
1258 if( p_tmp->psz_object_name
1259 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1261 vlc_object_yield( p_tmp );
1266 return FindObjectName( p_tmp, psz_name, i_mode );
1272 for( i = vlc_internals( p_this )->i_children; i--; )
1274 p_tmp = vlc_internals( p_this )->pp_children[i];
1275 if( p_tmp->psz_object_name
1276 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1278 vlc_object_yield( p_tmp );
1281 else if( vlc_internals( p_tmp )->i_children )
1283 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1293 /* Handled in vlc_object_find */
1301 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1303 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1306 memset( &psz_name, 0, sizeof(psz_name) );
1307 if( p_this->psz_object_name )
1309 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1311 psz_name[48] = '\"';
1314 psz_children[0] = '\0';
1315 switch( vlc_internals( p_this )->i_children )
1320 strcpy( psz_children, ", 1 child" );
1323 snprintf( psz_children, 19, ", %i children",
1324 vlc_internals( p_this )->i_children );
1328 psz_refcount[0] = '\0';
1329 if( vlc_internals( p_this )->i_refcount > 0 )
1330 snprintf( psz_refcount, 19, ", refcount %u",
1331 vlc_internals( p_this )->i_refcount );
1333 psz_thread[0] = '\0';
1334 if( vlc_internals( p_this )->b_thread )
1335 snprintf( psz_thread, 29, " (thread %lu)",
1336 (unsigned long)vlc_internals( p_this )->thread_id );
1338 psz_parent[0] = '\0';
1339 if( p_this->p_parent )
1340 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1342 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1343 p_this->i_object_id, p_this->psz_object_type,
1344 psz_name, psz_thread, psz_refcount, psz_children,
1348 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1351 char i_back = psz_foo[i_level];
1352 psz_foo[i_level] = '\0';
1354 PrintObject( p_this, psz_foo );
1356 psz_foo[i_level] = i_back;
1358 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1360 msg_Warn( p_this, "structure tree is too deep" );
1364 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1368 psz_foo[i_level-1] = ' ';
1370 if( psz_foo[i_level-2] == '`' )
1372 psz_foo[i_level-2] = ' ';
1376 if( i == vlc_internals( p_this )->i_children - 1 )
1378 psz_foo[i_level] = '`';
1382 psz_foo[i_level] = '|';
1385 psz_foo[i_level+1] = '-';
1386 psz_foo[i_level+2] = '\0';
1388 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1393 static vlc_list_t * NewList( int i_count )
1395 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1396 if( p_list == NULL )
1401 p_list->i_count = i_count;
1405 p_list->p_values = NULL;
1409 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1410 if( p_list->p_values == NULL )
1412 p_list->i_count = 0;
1419 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1422 if( p_list == NULL || i_index >= p_list->i_count )
1427 vlc_object_yield( p_object );
1429 p_list->p_values[i_index].p_object = p_object;
1434 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1436 if( p_list == NULL )
1441 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1442 * sizeof( vlc_value_t ) );
1443 if( p_list->p_values == NULL )
1445 p_list->i_count = 0;
1449 vlc_object_yield( p_object );
1451 p_list->p_values[p_list->i_count].p_object = p_object;
1457 static int CountChildren( vlc_object_t *p_this, int i_type )
1459 vlc_object_t *p_tmp;
1462 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1464 p_tmp = vlc_internals( p_this )->pp_children[i];
1466 if( p_tmp->i_object_type == i_type )
1470 i_count += CountChildren( p_tmp, i_type );
1476 static void ListChildren( vlc_list_t *p_list, 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 )
1486 ListReplace( p_list, p_tmp, p_list->i_count++ );
1488 ListChildren( p_list, p_tmp, i_type );
1492 #ifdef LIBVLC_REFCHECK
1493 # if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE)
1494 # include <execinfo.h>
1497 void vlc_refcheck (vlc_object_t *obj)
1499 static unsigned errors = 0;
1503 /* Anyone can use the root object (though it should not exist) */
1504 if (obj == VLC_OBJECT (vlc_global ()))
1507 /* Anyone can use its libvlc instance object */
1508 if (obj == VLC_OBJECT (obj->p_libvlc))
1511 /* The thread that created the object holds the initial reference */
1512 vlc_object_internals_t *priv = vlc_internals (obj);
1513 #if defined (LIBVLC_USE_PTHREAD)
1514 if (pthread_equal (priv->creator_id, pthread_self ()))
1516 if (priv->creator_id == GetCurrentThreadId ())
1522 /* A thread can use its own object without references! */
1523 vlc_object_t *caller = vlc_threadobj ();
1527 /* The calling thread is younger than the object.
1528 * Access could be valid through cross-thread synchronization;
1529 * we would need better accounting. */
1530 if (caller && (caller->i_object_id > obj->i_object_id))
1534 vlc_spin_lock (&priv->ref_spin);
1535 refs = priv->i_refcount;
1536 vlc_spin_unlock (&priv->ref_spin);
1538 for (held_list_t *hlcur = vlc_threadvar_get (&held_objects);
1539 hlcur != NULL; hlcur = hlcur->next)
1540 if (hlcur->obj == obj)
1543 fprintf (stderr, "The %s %s thread object is accessing...\n"
1544 "the %s %s object without references.\n",
1545 caller && caller->psz_object_name
1546 ? caller->psz_object_name : "unnamed",
1547 caller ? caller->psz_object_type : "main",
1548 obj->psz_object_name ? obj->psz_object_name : "unnamed",
1549 obj->psz_object_type);
1552 #ifdef HAVE_BACKTRACE
1554 int stackdepth = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
1555 backtrace_symbols_fd (stack, stackdepth, 2);
1558 if (++errors == 100)
1559 fprintf (stderr, "Too many reference errors!\n");
1562 static void held_objects_destroy (void *data)
1565 held_list_t *hl = vlc_threadvar_get (&held_objects);
1566 vlc_object_t *caller = vlc_threadobj ();
1570 held_list_t *buf = hl->next;
1571 vlc_object_t *obj = hl->obj;
1573 fprintf (stderr, "The %s %s thread object leaked a reference to...\n"
1574 "the %s %s object.\n",
1575 caller && caller->psz_object_name
1576 ? caller->psz_object_name : "unnamed",
1577 caller ? caller->psz_object_type : "main",
1578 obj->psz_object_name ? obj->psz_object_name : "unnamed",
1579 obj->psz_object_type);