1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
5 * Copyright (C) 2006-2010 RĂ©mi Denis-Courmont
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
26 * This file contains the functions to handle the vlc_object_t type
28 * Unless otherwise stated, functions in this file are not cancellation point.
29 * All functions in this file are safe w.r.t. deferred cancellation.
33 /*****************************************************************************
35 *****************************************************************************/
40 #include <vlc_common.h>
42 #include "../libvlc.h"
44 #include "audio_output/aout_internal.h"
46 #include "vlc_interface.h"
47 #include "vlc_codec.h"
49 #include "variables.h"
54 # include <winsock2.h>
55 # include <ws2tcpip.h>
57 # define read( a, b, c ) recv (a, b, c, 0)
59 # define write( a, b, c ) send (a, b, c, 0)
61 # define close( a ) closesocket (a)
68 #if defined (HAVE_SYS_EVENTFD_H)
69 # include <sys/eventfd.h>
73 /*****************************************************************************
75 *****************************************************************************/
76 static int DumpCommand( vlc_object_t *, char const *,
77 vlc_value_t, vlc_value_t, void * );
79 static vlc_object_t * FindParent ( vlc_object_t *, int );
80 static vlc_object_t * FindChild ( vlc_object_internals_t *, int );
81 static vlc_object_t * FindParentName( vlc_object_t *, const char * );
82 static vlc_object_t * FindChildName ( vlc_object_internals_t *, const char * );
83 static void PrintObject( vlc_object_internals_t *, const char * );
84 static void DumpStructure( vlc_object_internals_t *, unsigned, char * );
86 static vlc_list_t * NewList ( int );
88 static void vlc_object_destroy( vlc_object_t *p_this );
89 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
91 /*****************************************************************************
92 * Local structure lock
93 *****************************************************************************/
94 static void libvlc_lock (libvlc_int_t *p_libvlc)
96 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
99 static void libvlc_unlock (libvlc_int_t *p_libvlc)
101 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
104 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
105 int i_type, const char *psz_type )
108 vlc_object_internals_t *p_priv;
111 * VLC objects are laid out as follow:
112 * - first the LibVLC-private per-object data,
113 * - then VLC_COMMON members from vlc_object_t,
114 * - finally, the type-specific data (if any).
116 * This function initializes the LibVLC and common data,
117 * and zeroes the rest.
119 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
123 assert (i_size >= sizeof (vlc_object_t));
124 p_new = (vlc_object_t *)(p_priv + 1);
126 p_priv->i_object_type = i_type;
127 p_new->psz_object_type = psz_type;
128 p_priv->psz_name = NULL;
130 p_new->b_die = false;
131 p_new->b_error = false;
132 p_new->b_force = false;
134 p_new->psz_header = NULL;
137 p_new->i_flags = p_this->i_flags
138 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
140 p_priv->var_root = NULL;
144 libvlc_int_t *self = (libvlc_int_t*)p_new;
145 p_new->p_libvlc = self;
146 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
150 p_new->p_libvlc = p_this->p_libvlc;
152 vlc_spin_init( &p_priv->ref_spin );
153 p_priv->i_refcount = 1;
154 p_priv->pf_destructor = NULL;
155 p_priv->b_thread = false;
156 p_new->p_parent = NULL;
157 p_priv->first = NULL;
159 /* Initialize mutexes and condvars */
160 vlc_mutex_init( &p_priv->var_lock );
161 vlc_cond_init( &p_priv->var_wait );
162 p_priv->pipes[0] = p_priv->pipes[1] = -1;
164 if (p_new == VLC_OBJECT(p_new->p_libvlc))
165 { /* TODO: should be in src/libvlc.c */
166 int canc = vlc_savecancel ();
167 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
168 var_AddCallback( p_new, "tree", DumpCommand, NULL );
169 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
170 var_AddCallback( p_new, "vars", DumpCommand, NULL );
171 vlc_restorecancel (canc);
177 #undef vlc_object_create
179 * Allocates and initializes a vlc object.
181 * @param i_size object byte size
183 * @return the new object, or NULL on error.
185 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
187 return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
192 ****************************************************************************
193 * Set the destructor of a vlc object
195 * This function sets the destructor of the vlc object. It will be called
196 * when the object is destroyed when the its refcount reaches 0.
197 * (It is called by the internal function vlc_object_destroy())
198 *****************************************************************************/
199 void __vlc_object_set_destructor( vlc_object_t *p_this,
200 vlc_destructor_t pf_destructor )
202 vlc_object_internals_t *p_priv = vlc_internals(p_this );
204 vlc_spin_lock( &p_priv->ref_spin );
205 p_priv->pf_destructor = pf_destructor;
206 vlc_spin_unlock( &p_priv->ref_spin );
209 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
211 #undef vlc_object_set_name
212 int vlc_object_set_name(vlc_object_t *obj, const char *name)
214 vlc_object_internals_t *priv = vlc_internals(obj);
215 char *newname = name ? strdup (name) : NULL;
218 vlc_mutex_lock (&name_lock);
219 oldname = priv->psz_name;
220 priv->psz_name = newname;
221 vlc_mutex_unlock (&name_lock);
224 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
227 #undef vlc_object_get_name
228 char *vlc_object_get_name(const vlc_object_t *obj)
230 vlc_object_internals_t *priv = vlc_internals(obj);
233 vlc_mutex_lock (&name_lock);
234 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
235 vlc_mutex_unlock (&name_lock);
241 ****************************************************************************
242 * Destroy a vlc object (Internal)
244 * This function destroys an object that has been previously allocated with
245 * vlc_object_create. The object's refcount must be zero and it must not be
246 * attached to other objects in any way.
248 * This function must be called with cancellation disabled (currently).
249 *****************************************************************************/
250 static void vlc_object_destroy( vlc_object_t *p_this )
252 vlc_object_internals_t *p_priv = vlc_internals( p_this );
254 /* Objects are always detached beforehand */
255 assert( !p_this->p_parent );
257 /* Send a kill to the object's thread if applicable */
258 vlc_object_kill( p_this );
260 /* Call the custom "subclass" destructor */
261 if( p_priv->pf_destructor )
262 p_priv->pf_destructor( p_this );
264 /* Any thread must have been cleaned up at this point. */
265 assert( !p_priv->b_thread );
267 /* Destroy the associated variables. */
268 var_DestroyAll( p_this );
270 vlc_cond_destroy( &p_priv->var_wait );
271 vlc_mutex_destroy( &p_priv->var_lock );
273 free( p_this->psz_header );
275 free( p_priv->psz_name );
277 vlc_spin_destroy( &p_priv->ref_spin );
278 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
279 close( p_priv->pipes[1] );
280 if( p_priv->pipes[0] != -1 )
281 close( p_priv->pipes[0] );
282 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
283 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
291 * select()-able pipes emulated using Winsock
293 static int pipe (int fd[2])
296 int addrlen = sizeof (addr);
298 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
299 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
300 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
303 memset (&addr, 0, sizeof (addr));
304 addr.sin_family = AF_INET;
305 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
306 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
307 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
309 || connect (c, (PSOCKADDR)&addr, addrlen))
312 a = accept (l, NULL, NULL);
313 if (a == INVALID_SOCKET)
324 if (l != INVALID_SOCKET)
326 if (c != INVALID_SOCKET)
332 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
335 * Returns the readable end of a pipe that becomes readable once termination
336 * of the object is requested (vlc_object_kill()).
337 * This can be used to wake-up out of a select() or poll() event loop, such
338 * typically when doing network I/O.
340 * Note that the pipe will remain the same for the lifetime of the object.
341 * DO NOT read the pipe nor close it yourself. Ever.
343 * @param obj object that would be "killed"
344 * @return a readable pipe descriptor, or -1 on error.
346 int vlc_object_waitpipe( vlc_object_t *obj )
348 vlc_object_internals_t *internals = vlc_internals( obj );
350 vlc_mutex_lock (&pipe_lock);
351 if (internals->pipes[0] == -1)
353 /* This can only ever happen if someone killed us without locking: */
354 assert (internals->pipes[1] == -1);
356 #if defined (HAVE_SYS_EVENTFD_H)
357 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
358 if (internals->pipes[0] == -1)
361 if (pipe (internals->pipes))
362 internals->pipes[0] = internals->pipes[1] = -1;
365 if (internals->pipes[0] != -1 && obj->b_die)
366 { /* Race condition: vlc_object_kill() already invoked! */
367 msg_Dbg (obj, "waitpipe: object already dying");
368 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
371 vlc_mutex_unlock (&pipe_lock);
372 return internals->pipes[0];
377 * Requests termination of an object, cancels the object thread, and make the
378 * object wait pipe (if it exists) readable. Not a cancellation point.
380 void __vlc_object_kill( vlc_object_t *p_this )
382 vlc_object_internals_t *priv = vlc_internals( p_this );
385 vlc_thread_cancel( p_this );
386 vlc_mutex_lock( &pipe_lock );
390 p_this->b_die = true;
393 /* This also serves as a memory barrier toward vlc_object_alive(): */
394 vlc_mutex_unlock( &pipe_lock );
398 int canc = vlc_savecancel ();
400 /* write _after_ setting b_die, so vlc_object_alive() returns false */
401 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
402 msg_Dbg (p_this, "waitpipe: object killed");
403 vlc_restorecancel (canc);
408 /*****************************************************************************
409 * find a typed object and increment its refcount
410 *****************************************************************************
411 * This function recursively looks for a given object type. i_mode can be one
412 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
413 *****************************************************************************/
414 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
416 vlc_object_t *p_found;
418 /* If we are of the requested type ourselves, don't look further */
419 if( vlc_internals (p_this)->i_object_type == i_type )
421 vlc_object_hold( p_this );
425 /* Otherwise, recursively look for the object */
426 if (i_mode == FIND_ANYWHERE)
427 return vlc_object_find (p_this->p_libvlc, i_type, FIND_CHILD);
431 case VLC_OBJECT_VOUT:
432 case VLC_OBJECT_AOUT:
434 case VLC_OBJECT_INPUT:
435 /* input can only be accessed like this from children,
436 * otherwise we could not promise that it is initialized */
437 if (i_mode != FIND_PARENT)
444 libvlc_lock (p_this->p_libvlc);
448 p_found = FindParent (p_this, i_type);
451 p_found = FindChild (vlc_internals (p_this), i_type);
456 libvlc_unlock (p_this->p_libvlc);
461 static int objnamecmp(const vlc_object_t *obj, const char *name)
463 char *objname = vlc_object_get_name(obj);
467 int ret = strcmp (objname, name);
472 #undef vlc_object_find_name
474 * Finds a named object and increment its reference count.
475 * Beware that objects found in this manner can be "owned" by another thread,
476 * be of _any_ type, and be attached to any module (if any). With such an
477 * object reference, you can set or get object variables, emit log messages,
478 * and read write-once object parameters (psz_object_type, etc).
479 * You CANNOT cast the object to a more specific object type, and you
480 * definitely cannot invoke object type-specific callbacks with this.
482 * @param p_this object to search from
483 * @param psz_name name of the object to search for
484 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
486 * @return a matching object (must be released by the caller),
489 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
490 const char *psz_name, int i_mode )
492 vlc_object_t *p_found;
494 /* Reading psz_object_name from a separate inhibits thread-safety.
495 * Use a libvlc address variable instead for that sort of things! */
496 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
497 /* If have the requested name ourselves, don't look further */
498 if( !objnamecmp(p_this, psz_name) )
500 vlc_object_hold( p_this );
504 /* Otherwise, recursively look for the object */
505 if (i_mode == FIND_ANYWHERE)
506 return vlc_object_find_name (VLC_OBJECT(p_this->p_libvlc), psz_name,
509 libvlc_lock (p_this->p_libvlc);
510 vlc_mutex_lock (&name_lock);
514 p_found = FindParentName (p_this, psz_name);
517 p_found = FindChildName (vlc_internals (p_this), psz_name);
522 vlc_mutex_unlock (&name_lock);
523 libvlc_unlock (p_this->p_libvlc);
528 * Increment an object reference counter.
530 void * __vlc_object_hold( vlc_object_t *p_this )
532 vlc_object_internals_t *internals = vlc_internals( p_this );
534 vlc_spin_lock( &internals->ref_spin );
535 /* Avoid obvious freed object uses */
536 assert( internals->i_refcount > 0 );
537 /* Increment the counter */
538 internals->i_refcount++;
539 vlc_spin_unlock( &internals->ref_spin );
543 /*****************************************************************************
544 * Decrement an object refcount
545 * And destroy the object if its refcount reach zero.
546 *****************************************************************************/
547 void __vlc_object_release( vlc_object_t *p_this )
549 vlc_object_internals_t *internals = vlc_internals( p_this );
550 vlc_object_t *parent = NULL;
551 bool b_should_destroy;
553 vlc_spin_lock( &internals->ref_spin );
554 assert( internals->i_refcount > 0 );
556 if( internals->i_refcount > 1 )
559 /* There are still other references to the object */
560 internals->i_refcount--;
561 vlc_spin_unlock( &internals->ref_spin );
564 vlc_spin_unlock( &internals->ref_spin );
567 /* Remember that we cannot hold the spin while waiting on the mutex */
568 libvlc_lock (p_this->p_libvlc);
569 /* Take the spin again. Note that another thread may have held the
570 * object in the (very short) mean time. */
571 vlc_spin_lock( &internals->ref_spin );
572 b_should_destroy = --internals->i_refcount == 0;
573 vlc_spin_unlock( &internals->ref_spin );
575 if( b_should_destroy )
577 parent = p_this->p_parent;
579 /* Detach from parent to protect against FIND_CHILDREN */
580 vlc_object_detach_unlocked (p_this);
582 /* We have no children */
583 assert (internals->first == NULL);
585 libvlc_unlock (p_this->p_libvlc);
587 if( b_should_destroy )
591 canc = vlc_savecancel ();
592 vlc_object_destroy( p_this );
593 vlc_restorecancel (canc);
595 vlc_object_release (parent);
600 ****************************************************************************
601 * attach object to a parent object
602 *****************************************************************************
603 * This function sets p_this as a child of p_parent, and p_parent as a parent
604 * of p_this. This link can be undone using vlc_object_detach.
605 *****************************************************************************/
606 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
608 if( !p_this ) return;
610 vlc_object_internals_t *pap = vlc_internals (p_parent);
611 vlc_object_internals_t *priv = vlc_internals (p_this);
614 vlc_object_hold (p_parent);
615 libvlc_lock (p_this->p_libvlc);
617 /* Attach the parent to its child */
618 assert (!p_this->p_parent);
619 p_this->p_parent = p_parent;
621 /* Attach the child to its parent */
622 priv->next = pap->first;
623 if (priv->next != NULL)
624 priv->next->prev = priv;
626 libvlc_unlock (p_this->p_libvlc);
630 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
632 if (p_this->p_parent == NULL)
635 vlc_object_internals_t *pap = vlc_internals (p_this->p_parent);
636 vlc_object_internals_t *priv = vlc_internals (p_this);
639 if (priv->prev != NULL)
640 priv->prev->next = priv->next;
642 pap->first = priv->next;
643 if (priv->next != NULL)
644 priv->next->prev = priv->prev;
646 /* Remove p_this's parent */
647 p_this->p_parent = NULL;
652 ****************************************************************************
653 * detach object from its parent
654 *****************************************************************************
655 * This function removes all links between an object and its parent.
656 *****************************************************************************/
657 void __vlc_object_detach( vlc_object_t *p_this )
659 vlc_object_t *p_parent;
660 if( !p_this ) return;
662 libvlc_lock (p_this->p_libvlc);
663 p_parent = p_this->p_parent;
665 vlc_object_detach_unlocked( p_this );
666 libvlc_unlock (p_this->p_libvlc);
669 vlc_object_release (p_parent);
673 * Gets the list of children of an objects, and increment their reference
675 * @return a list (possibly empty) or NULL in case of error.
677 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
680 vlc_object_internals_t *priv;
683 libvlc_lock (obj->p_libvlc);
684 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
687 if (likely(l != NULL))
691 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
692 l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
694 libvlc_unlock (obj->p_libvlc);
698 static void DumpVariable (const void *data, const VISIT which, const int depth)
700 if (which != postorder && which != leaf)
704 const variable_t *p_var = *(const variable_t **)data;
705 const char *psz_type = "unknown";
707 switch( p_var->i_type & VLC_VAR_TYPE )
709 #define MYCASE( type, nice ) \
710 case VLC_VAR_ ## type: \
713 MYCASE( VOID, "void" );
714 MYCASE( BOOL, "bool" );
715 MYCASE( INTEGER, "integer" );
716 MYCASE( HOTKEY, "hotkey" );
717 MYCASE( STRING, "string" );
718 MYCASE( MODULE, "module" );
719 MYCASE( FILE, "file" );
720 MYCASE( DIRECTORY, "directory" );
721 MYCASE( VARIABLE, "variable" );
722 MYCASE( FLOAT, "float" );
723 MYCASE( TIME, "time" );
724 MYCASE( ADDRESS, "address" );
725 MYCASE( MUTEX, "mutex" );
726 MYCASE( LIST, "list" );
729 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
730 if( p_var->psz_text )
731 printf( ", %s", p_var->psz_text );
732 fputc( ')', stdout );
733 if( p_var->i_type & VLC_VAR_HASCHOICE )
734 fputs( ", has choices", stdout );
735 if( p_var->i_type & VLC_VAR_ISCOMMAND )
736 fputs( ", command", stdout );
737 if( p_var->i_entries )
738 printf( ", %d callbacks", p_var->i_entries );
739 switch( p_var->i_type & VLC_VAR_CLASS )
745 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
747 case VLC_VAR_INTEGER:
748 printf( ": %d", p_var->val.i_int );
751 printf( ": \"%s\"", p_var->val.psz_string );
754 printf( ": %f", p_var->val.f_float );
757 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
759 case VLC_VAR_ADDRESS:
760 printf( ": %p", p_var->val.p_address );
763 fputs( ": TODO", stdout );
766 fputc( '\n', stdout );
769 /*****************************************************************************
770 * DumpCommand: print the current vlc structure
771 *****************************************************************************
772 * This function prints either an ASCII tree showing the connections between
773 * vlc objects, and additional information such as their refcount, thread ID,
774 * etc. (command "tree"), or the same data as a simple list (command "list").
775 *****************************************************************************/
776 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
777 vlc_value_t oldval, vlc_value_t newval, void *p_data )
779 (void)oldval; (void)p_data;
780 vlc_object_t *p_object = NULL;
782 if( *newval.psz_string )
784 /* try using the object's name to find it */
785 p_object = vlc_object_find_name( p_this, newval.psz_string,
793 libvlc_lock (p_this->p_libvlc);
794 if( *psz_cmd == 't' )
796 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
799 p_object = VLC_OBJECT(p_this->p_libvlc);
802 DumpStructure( vlc_internals(p_object), 0, psz_foo );
804 else if( *psz_cmd == 'v' )
807 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
809 PrintObject( vlc_internals(p_object), "" );
810 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
811 if( vlc_internals( p_object )->var_root == NULL )
812 puts( " `-o No variables" );
814 twalk( vlc_internals( p_object )->var_root, DumpVariable );
815 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
817 libvlc_unlock (p_this->p_libvlc);
819 if( *newval.psz_string )
821 vlc_object_release( p_object );
826 /*****************************************************************************
827 * vlc_list_release: free a list previously allocated by vlc_list_find
828 *****************************************************************************
829 * This function decreases the refcount of all objects in the list and
831 *****************************************************************************/
832 void vlc_list_release( vlc_list_t *p_list )
836 for( i_index = 0; i_index < p_list->i_count; i_index++ )
838 vlc_object_release( p_list->p_values[i_index].p_object );
841 free( p_list->p_values );
845 /* Following functions are local */
847 static vlc_object_t *FindParent (vlc_object_t *p_this, int i_type)
849 for (vlc_object_t *parent = p_this->p_parent;
851 parent = parent->p_parent)
853 if (vlc_internals (parent)->i_object_type == i_type)
854 return vlc_object_hold (parent);
859 static vlc_object_t *FindParentName (vlc_object_t *p_this, const char *name)
861 for (vlc_object_t *parent = p_this->p_parent;
863 parent = parent->p_parent)
865 const char *objname = vlc_internals (parent)->psz_name;
866 if (objname && !strcmp (objname, name))
867 return vlc_object_hold (parent);
872 static vlc_object_t *FindChild (vlc_object_internals_t *priv, int i_type)
874 for (priv = priv->first; priv != NULL; priv = priv->next)
876 if (priv->i_object_type == i_type)
877 return vlc_object_hold (vlc_externals (priv));
879 vlc_object_t *found = FindChild (priv, i_type);
886 static vlc_object_t *FindChildName (vlc_object_internals_t *priv,
889 for (priv = priv->first; priv != NULL; priv = priv->next)
891 if (priv->psz_name && !strcmp (priv->psz_name, name))
892 return vlc_object_hold (vlc_externals (priv));
894 vlc_object_t *found = FindChildName (priv, name);
901 static void PrintObject( vlc_object_internals_t *priv,
902 const char *psz_prefix )
904 char psz_refcount[20], psz_thread[30], psz_name[50], psz_parent[20];
906 int canc = vlc_savecancel ();
907 memset( &psz_name, 0, sizeof(psz_name) );
909 vlc_mutex_lock (&name_lock);
910 if (priv->psz_name != NULL)
912 snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
916 vlc_mutex_unlock (&name_lock);
918 psz_refcount[0] = '\0';
919 if( priv->i_refcount > 0 )
920 snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
922 psz_thread[0] = '\0';
924 snprintf( psz_thread, 29, " (thread %lu)",
925 (unsigned long)priv->thread_id );
927 psz_parent[0] = '\0';
928 /* FIXME: need structure lock!!! */
929 if( vlc_externals(priv)->p_parent )
930 snprintf( psz_parent, 19, ", parent %p",
931 vlc_externals(priv)->p_parent );
933 printf( " %so %p %s%s%s%s%s\n", psz_prefix,
934 vlc_externals(priv), vlc_externals(priv)->psz_object_type,
935 psz_name, psz_thread, psz_refcount, psz_parent );
936 vlc_restorecancel (canc);
939 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
942 char i_back = psz_foo[i_level];
943 psz_foo[i_level] = '\0';
945 PrintObject (priv, psz_foo);
947 psz_foo[i_level] = i_back;
949 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
951 msg_Warn( vlc_externals(priv), "structure tree is too deep" );
955 for (priv = priv->first; priv != NULL; priv = priv->next)
959 psz_foo[i_level-1] = ' ';
961 if( psz_foo[i_level-2] == '`' )
963 psz_foo[i_level-2] = ' ';
967 psz_foo[i_level] = priv->next ? '|' : '`';
968 psz_foo[i_level+1] = '-';
969 psz_foo[i_level+2] = '\0';
971 DumpStructure (priv, i_level + 2, psz_foo);
975 static vlc_list_t * NewList( int i_count )
977 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
981 p_list->i_count = i_count;
985 p_list->p_values = NULL;
989 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
990 if( p_list->p_values == NULL )