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
27 * Unless otherwise stated, functions in this file are not cancellation point.
28 * All functions in this file are safe w.r.t. deferred cancellation.
32 /*****************************************************************************
34 *****************************************************************************/
39 #include <vlc_common.h>
41 #include "../libvlc.h"
44 #include "audio_output/aout_internal.h"
46 #include <vlc_access.h>
47 #include <vlc_demux.h>
48 #include <vlc_stream.h>
51 #include "stream_output/stream_output.h"
53 #include "vlc_interface.h"
54 #include "vlc_codec.h"
55 #include "vlc_filter.h"
57 #include "variables.h"
63 # include <errno.h> /* ENOSYS */
67 /*****************************************************************************
69 *****************************************************************************/
70 static int DumpCommand( vlc_object_t *, char const *,
71 vlc_value_t, vlc_value_t, void * );
73 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
74 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
75 static void PrintObject ( vlc_object_t *, const char * );
76 static void DumpStructure ( vlc_object_t *, int, char * );
78 static vlc_list_t * NewList ( int );
79 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
80 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
81 static int CountChildren ( vlc_object_t *, int );
82 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
84 static void vlc_object_destroy( vlc_object_t *p_this );
85 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
87 #ifdef LIBVLC_REFCHECK
88 static vlc_threadvar_t held_objects;
89 typedef struct held_list_t
91 struct held_list_t *next;
94 static void held_objects_destroy (void *);
97 /*****************************************************************************
98 * Local structure lock
99 *****************************************************************************/
100 static vlc_mutex_t structure_lock;
101 static unsigned object_counter = 0;
103 static void close_nocancel (int fd)
105 int canc = vlc_savecancel ();
107 vlc_restorecancel (canc);
110 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
111 int i_type, const char *psz_type )
114 vlc_object_internals_t *p_priv;
117 * VLC objects are laid out as follow:
118 * - first the LibVLC-private per-object data,
119 * - then VLC_COMMON members from vlc_object_t,
120 * - finally, the type-specific data (if any).
122 * This function initializes the LibVLC and common data,
123 * and zeroes the rest.
125 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
129 assert (i_size >= sizeof (vlc_object_t));
130 p_new = (vlc_object_t *)(p_priv + 1);
132 p_new->i_object_type = i_type;
133 p_new->psz_object_type = psz_type;
134 p_new->psz_object_name = NULL;
136 p_new->b_die = false;
137 p_new->b_error = false;
138 p_new->b_dead = false;
139 p_new->b_force = false;
141 p_new->psz_header = NULL;
144 p_new->i_flags = p_this->i_flags
145 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
147 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
149 if( !p_priv->p_vars )
155 libvlc_global_data_t *p_libvlc_global;
158 /* Only the global root object is created out of the blue */
159 p_libvlc_global = (libvlc_global_data_t *)p_new;
160 p_new->p_libvlc = NULL;
162 object_counter = 0; /* reset */
163 p_priv->next = p_priv->prev = p_new;
164 vlc_mutex_init( &structure_lock );
165 #ifdef LIBVLC_REFCHECK
166 /* TODO: use the destruction callback to track ref leaks */
167 vlc_threadvar_create( &held_objects, held_objects_destroy );
172 p_libvlc_global = vlc_global();
173 if( i_type == VLC_OBJECT_LIBVLC )
174 p_new->p_libvlc = (libvlc_int_t*)p_new;
176 p_new->p_libvlc = p_this->p_libvlc;
179 vlc_spin_init( &p_priv->ref_spin );
180 p_priv->i_refcount = 1;
181 p_priv->pf_destructor = NULL;
182 p_priv->b_thread = false;
183 p_new->p_parent = NULL;
184 p_priv->pp_children = NULL;
185 p_priv->i_children = 0;
187 p_new->p_private = NULL;
189 /* Initialize mutexes and condvars */
190 vlc_mutex_init( &p_priv->lock );
191 vlc_cond_init( p_new, &p_priv->wait );
192 vlc_mutex_init( &p_priv->var_lock );
193 vlc_spin_init( &p_priv->spin );
194 p_priv->pipes[0] = p_priv->pipes[1] = -1;
196 p_priv->next = VLC_OBJECT (p_libvlc_global);
197 #if !defined (LIBVLC_REFCHECK)
199 #elif defined (LIBVLC_USE_PTHREAD)
200 p_priv->creator_id = pthread_self ();
201 #elif defined (WIN32)
202 p_priv->creator_id = GetCurrentThreadId ();
204 vlc_mutex_lock( &structure_lock );
205 p_priv->prev = vlc_internals (p_libvlc_global)->prev;
206 vlc_internals (p_libvlc_global)->prev = p_new;
207 vlc_internals (p_priv->prev)->next = p_new;
208 p_new->i_object_id = object_counter++; /* fetch THEN increment */
209 vlc_mutex_unlock( &structure_lock );
211 if( i_type == VLC_OBJECT_LIBVLC )
213 int canc = vlc_savecancel ();
214 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
215 var_AddCallback( p_new, "list", DumpCommand, NULL );
216 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
217 var_AddCallback( p_new, "tree", DumpCommand, NULL );
218 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
219 var_AddCallback( p_new, "vars", DumpCommand, NULL );
220 vlc_restorecancel (canc);
228 * Allocates and initializes a vlc object.
230 * @param i_type known object type (all of them are negative integer values),
231 * or object byte size (always positive).
233 * @return the new object, or NULL on error.
235 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
237 const char * psz_type;
242 case VLC_OBJECT_INTF:
243 i_size = sizeof(intf_thread_t);
244 psz_type = "interface";
246 case VLC_OBJECT_DECODER:
247 i_size = sizeof(decoder_t);
248 psz_type = "decoder";
250 case VLC_OBJECT_PACKETIZER:
251 i_size = sizeof(decoder_t);
252 psz_type = "packetizer";
254 case VLC_OBJECT_ENCODER:
255 i_size = sizeof(encoder_t);
256 psz_type = "encoder";
258 case VLC_OBJECT_AOUT:
259 i_size = sizeof(aout_instance_t);
260 psz_type = "audio output";
262 case VLC_OBJECT_OPENGL:
263 i_size = sizeof( vout_thread_t );
266 case VLC_OBJECT_ANNOUNCE:
267 i_size = sizeof( announce_handler_t );
268 psz_type = "announce";
271 assert( i_type > 0 ); /* unknown type?! */
273 i_type = VLC_OBJECT_GENERIC;
274 psz_type = "generic";
278 return vlc_custom_create( p_this, i_size, i_type, psz_type );
283 ****************************************************************************
284 * Set the destructor of a vlc object
286 * This function sets the destructor of the vlc object. It will be called
287 * when the object is destroyed when the its refcount reaches 0.
288 * (It is called by the internal function vlc_object_destroy())
289 *****************************************************************************/
290 void __vlc_object_set_destructor( vlc_object_t *p_this,
291 vlc_destructor_t pf_destructor )
293 vlc_object_internals_t *p_priv = vlc_internals(p_this );
294 p_priv->pf_destructor = pf_destructor;
298 ****************************************************************************
299 * Destroy a vlc object (Internal)
301 * This function destroys an object that has been previously allocated with
302 * vlc_object_create. The object's refcount must be zero and it must not be
303 * attached to other objects in any way.
305 * This function must be called with cancellation disabled (currently).
306 *****************************************************************************/
307 static void vlc_object_destroy( vlc_object_t *p_this )
309 vlc_object_internals_t *p_priv = vlc_internals( p_this );
311 /* Objects are always detached beforehand */
312 assert( !p_this->p_parent );
314 /* Send a kill to the object's thread if applicable */
315 vlc_object_kill( p_this );
317 /* Call the custom "subclass" destructor */
318 if( p_priv->pf_destructor )
319 p_priv->pf_destructor( p_this );
321 /* Any thread must have been cleaned up at this point. */
322 assert( !p_priv->b_thread );
324 /* Destroy the associated variables, starting from the end so that
325 * no memmove calls have to be done. */
326 while( p_priv->i_vars )
328 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
331 free( p_priv->p_vars );
332 vlc_mutex_destroy( &p_priv->var_lock );
334 free( p_this->psz_header );
336 if( p_this->p_libvlc == NULL )
339 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
341 assert( p_global == vlc_global() );
343 if (p_priv->next != p_this)
345 vlc_object_t *leaked = p_priv->next, *first = leaked;
348 /* We are leaking this object */
350 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
351 leaked->i_object_id, leaked->psz_object_type,
352 leaked->psz_object_name );
353 /* Dump libvlc object to ease debugging */
354 vlc_object_dump( leaked );
356 leaked = vlc_internals (leaked)->next;
358 while (leaked != first);
360 /* Dump global object to ease debugging */
361 vlc_object_dump( p_this );
362 /* Strongly abort, cause we want these to be fixed */
367 /* We are the global object ... no need to lock. */
368 vlc_mutex_destroy( &structure_lock );
369 #ifdef LIBVLC_REFCHECK
370 held_objects_destroy( vlc_threadvar_get( &held_objects ) );
371 vlc_threadvar_delete( &held_objects );
375 FREENULL( p_this->psz_object_name );
377 #if defined(WIN32) || defined(UNDER_CE)
378 /* if object has an associated thread, close it now */
379 if( p_priv->thread_id )
380 CloseHandle(p_priv->thread_id);
383 vlc_spin_destroy( &p_priv->ref_spin );
384 vlc_mutex_destroy( &p_priv->lock );
385 vlc_cond_destroy( &p_priv->wait );
386 vlc_spin_destroy( &p_priv->spin );
387 if( p_priv->pipes[1] != -1 )
388 close( p_priv->pipes[1] );
389 if( p_priv->pipes[0] != -1 )
390 close( p_priv->pipes[0] );
396 /** Inter-object signaling */
398 void __vlc_object_lock( vlc_object_t *obj )
400 vlc_mutex_lock( &(vlc_internals(obj)->lock) );
403 void __vlc_object_unlock( vlc_object_t *obj )
405 vlc_assert_locked( &(vlc_internals(obj)->lock) );
406 vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
410 # include <winsock2.h>
411 # include <ws2tcpip.h>
414 * select()-able pipes emulated using Winsock
416 static int pipe (int fd[2])
419 int addrlen = sizeof (addr);
421 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
422 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
423 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
426 memset (&addr, 0, sizeof (addr));
427 addr.sin_family = AF_INET;
428 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
429 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
430 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
432 || connect (c, (PSOCKADDR)&addr, addrlen))
435 a = accept (l, NULL, NULL);
436 if (a == INVALID_SOCKET)
447 if (l != INVALID_SOCKET)
449 if (c != INVALID_SOCKET)
455 #define read( a, b, c ) recv (a, b, c, 0)
457 #define write( a, b, c ) send (a, b, c, 0)
459 #define close( a ) closesocket (a)
463 * Returns the readable end of a pipe that becomes readable once termination
464 * of the object is requested (vlc_object_kill()).
465 * This can be used to wake-up out of a select() or poll() event loop, such
466 * typically when doing network I/O.
468 * Note that the pipe will remain the same for the lifetime of the object.
469 * DO NOT read the pipe nor close it yourself. Ever.
471 * @param obj object that would be "killed"
472 * @return a readable pipe descriptor, or -1 on error.
474 int __vlc_object_waitpipe( vlc_object_t *obj )
476 int pfd[2] = { -1, -1 };
477 vlc_object_internals_t *internals = vlc_internals( obj );
480 vlc_spin_lock (&internals->spin);
481 if (internals->pipes[0] == -1)
483 /* This can only ever happen if someone killed us without locking: */
484 assert (internals->pipes[1] == -1);
485 vlc_spin_unlock (&internals->spin);
490 vlc_spin_lock (&internals->spin);
491 if (internals->pipes[0] == -1)
493 internals->pipes[0] = pfd[0];
494 internals->pipes[1] = pfd[1];
495 pfd[0] = pfd[1] = -1;
499 vlc_spin_unlock (&internals->spin);
503 /* Race condition: vlc_object_kill() already invoked! */
506 vlc_spin_lock (&internals->spin);
507 fd = internals->pipes[1];
508 internals->pipes[1] = -1;
509 vlc_spin_unlock (&internals->spin);
511 msg_Dbg (obj, "waitpipe: object already dying");
516 /* Race condition: two threads call pipe() - unlikely */
522 return internals->pipes[0];
527 * Waits for the object to be signaled (using vlc_object_signal()).
528 * It is assumed that the caller has locked the object. This function will
529 * unlock the object, and lock it again before returning.
530 * If the object was signaled before the caller locked the object, it is
531 * undefined whether the signal will be lost or will wake the process.
533 * This function is a cancellation point. In case of cancellation, the object
534 * will be in locked state.
536 * @return true if the object is dying and should terminate.
538 void __vlc_object_wait( vlc_object_t *obj )
540 vlc_object_internals_t *priv = vlc_internals( obj );
541 vlc_assert_locked( &priv->lock);
542 vlc_cond_wait( &priv->wait, &priv->lock );
547 * Waits for the object to be signaled (using vlc_object_signal()), or for
548 * a timer to expire. It is asserted that the caller holds the object lock.
550 * This function is a cancellation point. In case of cancellation, the object
551 * will be in locked state.
553 * @return 0 if the object was signaled before the timer expiration, or
554 * ETIMEDOUT if the timer expired without any signal.
556 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
558 vlc_object_internals_t *priv = vlc_internals( obj );
559 vlc_assert_locked( &priv->lock);
560 return vlc_cond_timedwait( &priv->wait, &priv->lock, deadline );
565 * Signals an object for which the lock is held.
566 * At least one thread currently sleeping in vlc_object_wait() or
567 * vlc_object_timedwait() will wake up, assuming that there is at least one
568 * such thread in the first place. Otherwise, it is undefined whether the
569 * signal will be lost or will wake up one or more thread later.
571 void __vlc_object_signal_unlocked( vlc_object_t *obj )
573 vlc_assert_locked (&(vlc_internals(obj)->lock));
574 vlc_cond_signal( &(vlc_internals(obj)->wait) );
579 * Requests termination of an object.
580 * If the object is LibVLC, also request to terminate all its children.
582 void __vlc_object_kill( vlc_object_t *p_this )
584 vlc_object_internals_t *priv = vlc_internals( p_this );
587 vlc_object_lock( p_this );
588 p_this->b_die = true;
590 vlc_spin_lock (&priv->spin);
593 vlc_spin_unlock (&priv->spin);
597 msg_Dbg (p_this, "waitpipe: object killed");
601 vlc_object_signal_unlocked( p_this );
602 /* This also serves as a memory barrier toward vlc_object_alive(): */
603 vlc_object_unlock( p_this );
608 * Find an object given its ID.
610 * This function looks for the object whose i_object_id field is i_id.
611 * This function is slow, and often used to hide bugs. Do not use it.
612 * If you need to retain reference to an object, yield the object pointer with
613 * vlc_object_yield(), use the pointer as your reference, and call
614 * vlc_object_release() when you're done.
616 void * vlc_object_get( int i_id )
618 libvlc_global_data_t *p_libvlc_global = vlc_global();
619 vlc_object_t *obj = NULL;
621 vlc_object_t *caller = vlc_threadobj ();
624 msg_Dbg (caller, "uses deprecated vlc_object_get(%d)", i_id);
627 int canc = vlc_savecancel ();
628 fprintf (stderr, "main thread uses deprecated vlc_object_get(%d)\n",
630 vlc_restorecancel (canc);
633 vlc_mutex_lock( &structure_lock );
635 for( obj = vlc_internals (p_libvlc_global)->next;
636 obj != VLC_OBJECT (p_libvlc_global);
637 obj = vlc_internals (obj)->next )
639 if( obj->i_object_id == i_id )
641 vlc_object_yield( obj );
648 msg_Warn (caller, "wants non-existing object %d", i_id);
650 fprintf (stderr, "main thread wants non-existing object %d\n", i_id);
653 vlc_mutex_unlock( &structure_lock );
658 ****************************************************************************
659 * find a typed object and increment its refcount
660 *****************************************************************************
661 * This function recursively looks for a given object type. i_mode can be one
662 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
663 *****************************************************************************/
664 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
666 vlc_object_t *p_found;
668 /* If we are of the requested type ourselves, don't look further */
669 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
671 vlc_object_yield( p_this );
675 /* Otherwise, recursively look for the object */
676 if ((i_mode & 0x000f) == FIND_ANYWHERE)
679 if (i_type == VLC_OBJECT_PLAYLIST)
680 msg_Err (p_this, "using vlc_object_find(VLC_OBJECT_PLAYLIST) "
681 "instead of pl_Yield()");
683 return vlc_object_find (p_this->p_libvlc, i_type,
684 (i_mode & ~0x000f)|FIND_CHILD);
687 vlc_mutex_lock( &structure_lock );
688 p_found = FindObject( p_this, i_type, i_mode );
689 vlc_mutex_unlock( &structure_lock );
694 ****************************************************************************
695 * find a named object and increment its refcount
696 *****************************************************************************
697 * This function recursively looks for a given object name. i_mode can be one
698 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
699 *****************************************************************************/
700 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
703 vlc_object_t *p_found;
705 /* If have the requested name ourselves, don't look further */
706 if( !(i_mode & FIND_STRICT)
707 && p_this->psz_object_name
708 && !strcmp( p_this->psz_object_name, psz_name ) )
710 vlc_object_yield( p_this );
714 vlc_mutex_lock( &structure_lock );
716 /* Otherwise, recursively look for the object */
717 if( (i_mode & 0x000f) == FIND_ANYWHERE )
719 vlc_object_t *p_root = p_this;
722 while( p_root->p_parent != NULL &&
723 p_root != VLC_OBJECT( p_this->p_libvlc ) )
725 p_root = p_root->p_parent;
728 p_found = FindObjectName( p_root, psz_name,
729 (i_mode & ~0x000f)|FIND_CHILD );
730 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
732 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
733 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
738 p_found = FindObjectName( p_this, psz_name, i_mode );
741 vlc_mutex_unlock( &structure_lock );
747 * Increment an object reference counter.
749 void __vlc_object_yield( vlc_object_t *p_this )
751 vlc_object_internals_t *internals = vlc_internals( p_this );
753 vlc_spin_lock( &internals->ref_spin );
754 /* Avoid obvious freed object uses */
755 assert( internals->i_refcount > 0 );
756 /* Increment the counter */
757 internals->i_refcount++;
758 vlc_spin_unlock( &internals->ref_spin );
759 #ifdef LIBVLC_REFCHECK
760 /* Update the list of referenced objects */
761 /* Using TLS, so no need to lock */
762 /* The following line may leak memory if a thread leaks objects. */
763 held_list_t *newhead = malloc (sizeof (*newhead));
764 held_list_t *oldhead = vlc_threadvar_get (&held_objects);
765 newhead->next = oldhead;
766 newhead->obj = p_this;
767 vlc_threadvar_set (&held_objects, newhead);
771 /*****************************************************************************
772 * Decrement an object refcount
773 * And destroy the object if its refcount reach zero.
774 *****************************************************************************/
775 void __vlc_object_release( vlc_object_t *p_this )
777 vlc_object_internals_t *internals = vlc_internals( p_this );
778 bool b_should_destroy;
780 #ifdef LIBVLC_REFCHECK
781 /* Update the list of referenced objects */
782 /* Using TLS, so no need to lock */
783 for (held_list_t *hlcur = vlc_threadvar_get (&held_objects),
786 hlprev = hlcur, hlcur = hlcur->next)
788 if (hlcur->obj == p_this)
791 vlc_threadvar_set (&held_objects, hlcur->next);
793 hlprev->next = hlcur->next;
798 /* TODO: what if releasing without references? */
801 vlc_spin_lock( &internals->ref_spin );
802 assert( internals->i_refcount > 0 );
804 if( internals->i_refcount > 1 )
807 /* There are still other references to the object */
808 internals->i_refcount--;
809 vlc_spin_unlock( &internals->ref_spin );
812 vlc_spin_unlock( &internals->ref_spin );
815 /* Remember that we cannot hold the spin while waiting on the mutex */
816 vlc_mutex_lock( &structure_lock );
817 /* Take the spin again. Note that another thread may have yielded the
818 * object in the (very short) mean time. */
819 vlc_spin_lock( &internals->ref_spin );
820 b_should_destroy = --internals->i_refcount == 0;
821 vlc_spin_unlock( &internals->ref_spin );
823 if( b_should_destroy )
825 /* Remove the object from object list
826 * so that it cannot be encountered by vlc_object_get() */
827 vlc_internals (internals->next)->prev = internals->prev;
828 vlc_internals (internals->prev)->next = internals->next;
830 /* Detach from parent to protect against FIND_CHILDREN */
831 vlc_object_detach_unlocked (p_this);
832 /* Detach from children to protect against FIND_PARENT */
833 for (int i = 0; i < internals->i_children; i++)
834 internals->pp_children[i]->p_parent = NULL;
837 vlc_mutex_unlock( &structure_lock );
839 if( b_should_destroy )
843 free( internals->pp_children );
844 internals->pp_children = NULL;
845 internals->i_children = 0;
846 canc = vlc_savecancel ();
847 vlc_object_destroy( p_this );
848 vlc_restorecancel (canc);
853 ****************************************************************************
854 * attach object to a parent object
855 *****************************************************************************
856 * This function sets p_this as a child of p_parent, and p_parent as a parent
857 * of p_this. This link can be undone using vlc_object_detach.
858 *****************************************************************************/
859 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
861 if( !p_this ) return;
863 vlc_mutex_lock( &structure_lock );
865 /* Attach the parent to its child */
866 assert (!p_this->p_parent);
867 p_this->p_parent = p_parent;
869 /* Attach the child to its parent */
870 vlc_object_internals_t *priv = vlc_internals( p_parent );
871 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
874 vlc_mutex_unlock( &structure_lock );
878 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
880 vlc_assert_locked (&structure_lock);
882 if (p_this->p_parent == NULL)
885 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
889 /* Remove p_this's parent */
890 p_this->p_parent = NULL;
892 /* Remove all of p_parent's children which are p_this */
893 for( i_index = priv->i_children ; i_index-- ; )
895 if( priv->pp_children[i_index] == p_this )
898 for( i = i_index ; i < priv->i_children ; i++ )
899 priv->pp_children[i] = priv->pp_children[i+1];
903 if( priv->i_children )
905 priv->pp_children = (vlc_object_t **)realloc( priv->pp_children,
906 priv->i_children * sizeof(vlc_object_t *) );
910 /* Special case - don't realloc() to zero to avoid leaking */
911 free( priv->pp_children );
912 priv->pp_children = NULL;
918 ****************************************************************************
919 * detach object from its parent
920 *****************************************************************************
921 * This function removes all links between an object and its parent.
922 *****************************************************************************/
923 void __vlc_object_detach( vlc_object_t *p_this )
925 if( !p_this ) return;
927 vlc_mutex_lock( &structure_lock );
928 if( !p_this->p_parent )
929 msg_Err( p_this, "object is not attached" );
931 vlc_object_detach_unlocked( p_this );
932 vlc_mutex_unlock( &structure_lock );
937 ****************************************************************************
938 * find a list typed objects and increment their refcount
939 *****************************************************************************
940 * This function recursively looks for a given object type. i_mode can be one
941 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
942 *****************************************************************************/
943 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
948 /* Look for the objects */
949 switch( i_mode & 0x000f )
952 /* Modules should probably not be object, and the module should perhaps
953 * not be shared across LibVLC instances. In the mean time, this ugly
954 * hack is brought to you by Courmisch. */
955 if (i_type == VLC_OBJECT_MODULE)
956 return vlc_list_find ((vlc_object_t *)vlc_global ()->p_module_bank,
958 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
961 vlc_mutex_lock( &structure_lock );
962 i_count = CountChildren( p_this, i_type );
963 p_list = NewList( i_count );
965 /* Check allocation was successful */
966 if( p_list->i_count != i_count )
968 vlc_mutex_unlock( &structure_lock );
969 msg_Err( p_this, "list allocation failed!" );
975 ListChildren( p_list, p_this, i_type );
976 vlc_mutex_unlock( &structure_lock );
980 msg_Err( p_this, "unimplemented!" );
981 p_list = NewList( 0 );
989 * Gets the list of children of an objects, and increment their reference
991 * @return a list (possibly empty) or NULL in case of error.
993 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
996 vlc_object_internals_t *priv = vlc_internals( obj );
998 vlc_mutex_lock( &structure_lock );
999 l = NewList( priv->i_children );
1000 for (int i = 0; i < l->i_count; i++)
1002 vlc_object_yield( priv->pp_children[i] );
1003 l->p_values[i].p_object = priv->pp_children[i];
1005 vlc_mutex_unlock( &structure_lock );
1009 /*****************************************************************************
1010 * DumpCommand: print the current vlc structure
1011 *****************************************************************************
1012 * This function prints either an ASCII tree showing the connections between
1013 * vlc objects, and additional information such as their refcount, thread ID,
1014 * etc. (command "tree"), or the same data as a simple list (command "list").
1015 *****************************************************************************/
1016 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1017 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1019 (void)oldval; (void)p_data;
1020 if( *psz_cmd == 'l' )
1022 vlc_object_t *root = VLC_OBJECT (vlc_global ()), *cur = root;
1024 vlc_mutex_lock( &structure_lock );
1027 PrintObject (cur, "");
1028 cur = vlc_internals (cur)->next;
1030 while (cur != root);
1031 vlc_mutex_unlock( &structure_lock );
1035 vlc_object_t *p_object = NULL;
1037 if( *newval.psz_string )
1040 int i_id = strtol( newval.psz_string, &end, 0 );
1041 if( end != newval.psz_string )
1042 p_object = vlc_object_get( i_id );
1044 /* try using the object's name to find it */
1045 p_object = vlc_object_find_name( p_this, newval.psz_string,
1054 vlc_mutex_lock( &structure_lock );
1056 if( *psz_cmd == 't' )
1058 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1061 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1064 DumpStructure( p_object, 0, psz_foo );
1066 else if( *psz_cmd == 'v' )
1071 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1073 PrintObject( p_object, "" );
1075 if( !vlc_internals( p_object )->i_vars )
1076 printf( " `-o No variables\n" );
1077 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1079 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1081 const char *psz_type = "unknown";
1082 switch( p_var->i_type & VLC_VAR_TYPE )
1084 #define MYCASE( type, nice ) \
1085 case VLC_VAR_ ## type: \
1088 MYCASE( VOID, "void" );
1089 MYCASE( BOOL, "bool" );
1090 MYCASE( INTEGER, "integer" );
1091 MYCASE( HOTKEY, "hotkey" );
1092 MYCASE( STRING, "string" );
1093 MYCASE( MODULE, "module" );
1094 MYCASE( FILE, "file" );
1095 MYCASE( DIRECTORY, "directory" );
1096 MYCASE( VARIABLE, "variable" );
1097 MYCASE( FLOAT, "float" );
1098 MYCASE( TIME, "time" );
1099 MYCASE( ADDRESS, "address" );
1100 MYCASE( MUTEX, "mutex" );
1101 MYCASE( LIST, "list" );
1104 printf( " %c-o \"%s\" (%s",
1105 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1106 p_var->psz_name, psz_type );
1107 if( p_var->psz_text )
1108 printf( ", %s", p_var->psz_text );
1110 if( p_var->i_type & VLC_VAR_HASCHOICE )
1111 printf( ", has choices" );
1112 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1113 printf( ", command" );
1114 if( p_var->i_entries )
1115 printf( ", %d callbacks", p_var->i_entries );
1116 switch( p_var->i_type & 0x00f0 )
1122 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1124 case VLC_VAR_INTEGER:
1125 printf( ": %d", p_var->val.i_int );
1127 case VLC_VAR_STRING:
1128 printf( ": \"%s\"", p_var->val.psz_string );
1131 printf( ": %f", p_var->val.f_float );
1134 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1136 case VLC_VAR_ADDRESS:
1137 printf( ": %p", p_var->val.p_address );
1147 vlc_mutex_unlock( &structure_lock );
1149 if( *newval.psz_string )
1151 vlc_object_release( p_object );
1158 /*****************************************************************************
1159 * vlc_list_release: free a list previously allocated by vlc_list_find
1160 *****************************************************************************
1161 * This function decreases the refcount of all objects in the list and
1163 *****************************************************************************/
1164 void vlc_list_release( vlc_list_t *p_list )
1168 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1170 vlc_object_release( p_list->p_values[i_index].p_object );
1173 free( p_list->p_values );
1177 /*****************************************************************************
1178 * dump an object. (Debug function)
1179 *****************************************************************************/
1180 void __vlc_object_dump( vlc_object_t *p_this )
1182 vlc_mutex_lock( &structure_lock );
1183 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1185 DumpStructure( p_this, 0, psz_foo );
1186 vlc_mutex_unlock( &structure_lock );
1189 /* Following functions are local */
1191 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1194 vlc_object_t *p_tmp;
1196 switch( i_mode & 0x000f )
1199 p_tmp = p_this->p_parent;
1202 if( p_tmp->i_object_type == i_type )
1204 vlc_object_yield( p_tmp );
1209 return FindObject( p_tmp, i_type, i_mode );
1215 for( i = vlc_internals( p_this )->i_children; i--; )
1217 p_tmp = vlc_internals( p_this )->pp_children[i];
1218 if( p_tmp->i_object_type == i_type )
1220 vlc_object_yield( p_tmp );
1223 else if( vlc_internals( p_tmp )->i_children )
1225 p_tmp = FindObject( p_tmp, i_type, i_mode );
1235 /* Handled in vlc_object_find */
1242 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1243 const char *psz_name,
1247 vlc_object_t *p_tmp;
1249 switch( i_mode & 0x000f )
1252 p_tmp = p_this->p_parent;
1255 if( p_tmp->psz_object_name
1256 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1258 vlc_object_yield( p_tmp );
1263 return FindObjectName( p_tmp, psz_name, i_mode );
1269 for( i = vlc_internals( p_this )->i_children; i--; )
1271 p_tmp = vlc_internals( p_this )->pp_children[i];
1272 if( p_tmp->psz_object_name
1273 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1275 vlc_object_yield( p_tmp );
1278 else if( vlc_internals( p_tmp )->i_children )
1280 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1290 /* Handled in vlc_object_find */
1298 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1300 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1303 int canc = vlc_savecancel ();
1304 memset( &psz_name, 0, sizeof(psz_name) );
1305 if( p_this->psz_object_name )
1307 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1309 psz_name[48] = '\"';
1312 psz_children[0] = '\0';
1313 switch( vlc_internals( p_this )->i_children )
1318 strcpy( psz_children, ", 1 child" );
1321 snprintf( psz_children, 19, ", %i children",
1322 vlc_internals( p_this )->i_children );
1326 psz_refcount[0] = '\0';
1327 if( vlc_internals( p_this )->i_refcount > 0 )
1328 snprintf( psz_refcount, 19, ", refcount %u",
1329 vlc_internals( p_this )->i_refcount );
1331 psz_thread[0] = '\0';
1332 if( vlc_internals( p_this )->b_thread )
1333 snprintf( psz_thread, 29, " (thread %lu)",
1334 (unsigned long)vlc_internals( p_this )->thread_id );
1336 psz_parent[0] = '\0';
1337 if( p_this->p_parent )
1338 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1340 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1341 p_this->i_object_id, p_this->psz_object_type,
1342 psz_name, psz_thread, psz_refcount, psz_children,
1344 vlc_restorecancel (canc);
1347 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1350 char i_back = psz_foo[i_level];
1351 psz_foo[i_level] = '\0';
1353 PrintObject( p_this, psz_foo );
1355 psz_foo[i_level] = i_back;
1357 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1359 msg_Warn( p_this, "structure tree is too deep" );
1363 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1367 psz_foo[i_level-1] = ' ';
1369 if( psz_foo[i_level-2] == '`' )
1371 psz_foo[i_level-2] = ' ';
1375 if( i == vlc_internals( p_this )->i_children - 1 )
1377 psz_foo[i_level] = '`';
1381 psz_foo[i_level] = '|';
1384 psz_foo[i_level+1] = '-';
1385 psz_foo[i_level+2] = '\0';
1387 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1392 static vlc_list_t * NewList( int i_count )
1394 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1395 if( p_list == NULL )
1400 p_list->i_count = i_count;
1404 p_list->p_values = NULL;
1408 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1409 if( p_list->p_values == NULL )
1411 p_list->i_count = 0;
1418 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1421 if( p_list == NULL || i_index >= p_list->i_count )
1426 vlc_object_yield( p_object );
1428 p_list->p_values[i_index].p_object = p_object;
1433 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1435 if( p_list == NULL )
1440 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1441 * sizeof( vlc_value_t ) );
1442 if( p_list->p_values == NULL )
1444 p_list->i_count = 0;
1448 vlc_object_yield( p_object );
1450 p_list->p_values[p_list->i_count].p_object = p_object;
1456 static int CountChildren( vlc_object_t *p_this, int i_type )
1458 vlc_object_t *p_tmp;
1461 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1463 p_tmp = vlc_internals( p_this )->pp_children[i];
1465 if( p_tmp->i_object_type == i_type )
1469 i_count += CountChildren( p_tmp, i_type );
1475 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1477 vlc_object_t *p_tmp;
1480 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1482 p_tmp = vlc_internals( p_this )->pp_children[i];
1484 if( p_tmp->i_object_type == i_type )
1485 ListReplace( p_list, p_tmp, p_list->i_count++ );
1487 ListChildren( p_list, p_tmp, i_type );
1491 #ifdef LIBVLC_REFCHECK
1492 # if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE)
1493 # include <execinfo.h>
1496 void vlc_refcheck (vlc_object_t *obj)
1498 static unsigned errors = 0;
1502 /* Anyone can use the root object (though it should not exist) */
1503 if (obj == VLC_OBJECT (vlc_global ()))
1506 /* Anyone can use its libvlc instance object */
1507 if (obj == VLC_OBJECT (obj->p_libvlc))
1510 /* The thread that created the object holds the initial reference */
1511 vlc_object_internals_t *priv = vlc_internals (obj);
1512 #if defined (LIBVLC_USE_PTHREAD)
1513 if (pthread_equal (priv->creator_id, pthread_self ()))
1515 if (priv->creator_id == GetCurrentThreadId ())
1521 /* A thread can use its own object without references! */
1522 vlc_object_t *caller = vlc_threadobj ();
1526 /* The calling thread is younger than the object.
1527 * Access could be valid through cross-thread synchronization;
1528 * we would need better accounting. */
1529 if (caller && (caller->i_object_id > obj->i_object_id))
1533 vlc_spin_lock (&priv->ref_spin);
1534 refs = priv->i_refcount;
1535 vlc_spin_unlock (&priv->ref_spin);
1537 for (held_list_t *hlcur = vlc_threadvar_get (&held_objects);
1538 hlcur != NULL; hlcur = hlcur->next)
1539 if (hlcur->obj == obj)
1542 int canc = vlc_savecancel ();
1544 fprintf (stderr, "The %s %s thread object is accessing...\n"
1545 "the %s %s object without references.\n",
1546 caller && caller->psz_object_name
1547 ? caller->psz_object_name : "unnamed",
1548 caller ? caller->psz_object_type : "main",
1549 obj->psz_object_name ? obj->psz_object_name : "unnamed",
1550 obj->psz_object_type);
1553 #ifdef HAVE_BACKTRACE
1555 int stackdepth = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
1556 backtrace_symbols_fd (stack, stackdepth, 2);
1559 if (++errors == 100)
1560 fprintf (stderr, "Too many reference errors!\n");
1561 vlc_restorecancel (canc);
1564 static void held_objects_destroy (void *data)
1567 held_list_t *hl = vlc_threadvar_get (&held_objects);
1568 vlc_object_t *caller = vlc_threadobj ();
1570 int canc = vlc_savecancel ();
1574 held_list_t *buf = hl->next;
1575 vlc_object_t *obj = hl->obj;
1577 fprintf (stderr, "The %s %s thread object leaked a reference to...\n"
1578 "the %s %s object.\n",
1579 caller && caller->psz_object_name
1580 ? caller->psz_object_name : "unnamed",
1581 caller ? caller->psz_object_type : "main",
1582 obj->psz_object_name ? obj->psz_object_name : "unnamed",
1583 obj->psz_object_type);
1587 vlc_restorecancel (canc);