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);
179 * Allocates and initializes a vlc object.
181 * @param i_type known object type (all of them are negative integer values),
182 * or object byte size (always positive).
184 * @return the new object, or NULL on error.
186 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
188 const char * psz_type;
193 case VLC_OBJECT_DECODER:
194 i_size = sizeof(decoder_t);
195 psz_type = "decoder";
197 case VLC_OBJECT_AOUT:
198 i_size = sizeof(aout_instance_t);
199 psz_type = "audio output";
202 assert( i_type > 0 ); /* unknown type?! */
204 i_type = VLC_OBJECT_GENERIC;
205 psz_type = "generic";
209 return vlc_custom_create( p_this, i_size, i_type, psz_type );
214 ****************************************************************************
215 * Set the destructor of a vlc object
217 * This function sets the destructor of the vlc object. It will be called
218 * when the object is destroyed when the its refcount reaches 0.
219 * (It is called by the internal function vlc_object_destroy())
220 *****************************************************************************/
221 void __vlc_object_set_destructor( vlc_object_t *p_this,
222 vlc_destructor_t pf_destructor )
224 vlc_object_internals_t *p_priv = vlc_internals(p_this );
226 vlc_spin_lock( &p_priv->ref_spin );
227 p_priv->pf_destructor = pf_destructor;
228 vlc_spin_unlock( &p_priv->ref_spin );
231 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
233 #undef vlc_object_set_name
234 int vlc_object_set_name(vlc_object_t *obj, const char *name)
236 vlc_object_internals_t *priv = vlc_internals(obj);
237 char *newname = name ? strdup (name) : NULL;
240 vlc_mutex_lock (&name_lock);
241 oldname = priv->psz_name;
242 priv->psz_name = newname;
243 vlc_mutex_unlock (&name_lock);
246 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
249 #undef vlc_object_get_name
250 char *vlc_object_get_name(const vlc_object_t *obj)
252 vlc_object_internals_t *priv = vlc_internals(obj);
255 vlc_mutex_lock (&name_lock);
256 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
257 vlc_mutex_unlock (&name_lock);
263 ****************************************************************************
264 * Destroy a vlc object (Internal)
266 * This function destroys an object that has been previously allocated with
267 * vlc_object_create. The object's refcount must be zero and it must not be
268 * attached to other objects in any way.
270 * This function must be called with cancellation disabled (currently).
271 *****************************************************************************/
272 static void vlc_object_destroy( vlc_object_t *p_this )
274 vlc_object_internals_t *p_priv = vlc_internals( p_this );
276 /* Objects are always detached beforehand */
277 assert( !p_this->p_parent );
279 /* Send a kill to the object's thread if applicable */
280 vlc_object_kill( p_this );
282 /* Call the custom "subclass" destructor */
283 if( p_priv->pf_destructor )
284 p_priv->pf_destructor( p_this );
286 /* Any thread must have been cleaned up at this point. */
287 assert( !p_priv->b_thread );
289 /* Destroy the associated variables. */
290 var_DestroyAll( p_this );
292 vlc_cond_destroy( &p_priv->var_wait );
293 vlc_mutex_destroy( &p_priv->var_lock );
295 free( p_this->psz_header );
297 free( p_priv->psz_name );
299 vlc_spin_destroy( &p_priv->ref_spin );
300 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
301 close( p_priv->pipes[1] );
302 if( p_priv->pipes[0] != -1 )
303 close( p_priv->pipes[0] );
304 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
305 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
313 * select()-able pipes emulated using Winsock
315 static int pipe (int fd[2])
318 int addrlen = sizeof (addr);
320 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
321 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
322 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
325 memset (&addr, 0, sizeof (addr));
326 addr.sin_family = AF_INET;
327 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
328 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
329 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
331 || connect (c, (PSOCKADDR)&addr, addrlen))
334 a = accept (l, NULL, NULL);
335 if (a == INVALID_SOCKET)
346 if (l != INVALID_SOCKET)
348 if (c != INVALID_SOCKET)
354 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
357 * Returns the readable end of a pipe that becomes readable once termination
358 * of the object is requested (vlc_object_kill()).
359 * This can be used to wake-up out of a select() or poll() event loop, such
360 * typically when doing network I/O.
362 * Note that the pipe will remain the same for the lifetime of the object.
363 * DO NOT read the pipe nor close it yourself. Ever.
365 * @param obj object that would be "killed"
366 * @return a readable pipe descriptor, or -1 on error.
368 int vlc_object_waitpipe( vlc_object_t *obj )
370 vlc_object_internals_t *internals = vlc_internals( obj );
372 vlc_mutex_lock (&pipe_lock);
373 if (internals->pipes[0] == -1)
375 /* This can only ever happen if someone killed us without locking: */
376 assert (internals->pipes[1] == -1);
378 #if defined (HAVE_SYS_EVENTFD_H)
379 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
380 if (internals->pipes[0] == -1)
383 if (pipe (internals->pipes))
384 internals->pipes[0] = internals->pipes[1] = -1;
387 if (internals->pipes[0] != -1 && obj->b_die)
388 { /* Race condition: vlc_object_kill() already invoked! */
389 msg_Dbg (obj, "waitpipe: object already dying");
390 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
393 vlc_mutex_unlock (&pipe_lock);
394 return internals->pipes[0];
399 * Requests termination of an object, cancels the object thread, and make the
400 * object wait pipe (if it exists) readable. Not a cancellation point.
402 void __vlc_object_kill( vlc_object_t *p_this )
404 vlc_object_internals_t *priv = vlc_internals( p_this );
407 vlc_thread_cancel( p_this );
408 vlc_mutex_lock( &pipe_lock );
412 p_this->b_die = true;
415 /* This also serves as a memory barrier toward vlc_object_alive(): */
416 vlc_mutex_unlock( &pipe_lock );
420 int canc = vlc_savecancel ();
422 /* write _after_ setting b_die, so vlc_object_alive() returns false */
423 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
424 msg_Dbg (p_this, "waitpipe: object killed");
425 vlc_restorecancel (canc);
430 /*****************************************************************************
431 * find a typed object and increment its refcount
432 *****************************************************************************
433 * This function recursively looks for a given object type. i_mode can be one
434 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
435 *****************************************************************************/
436 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
438 vlc_object_t *p_found;
440 /* If we are of the requested type ourselves, don't look further */
441 if( vlc_internals (p_this)->i_object_type == i_type )
443 vlc_object_hold( p_this );
447 /* Otherwise, recursively look for the object */
448 if (i_mode == FIND_ANYWHERE)
449 return vlc_object_find (p_this->p_libvlc, i_type, FIND_CHILD);
453 case VLC_OBJECT_VOUT:
454 case VLC_OBJECT_AOUT:
456 case VLC_OBJECT_INPUT:
457 /* input can only be accessed like this from children,
458 * otherwise we could not promise that it is initialized */
459 if (i_mode != FIND_PARENT)
466 libvlc_lock (p_this->p_libvlc);
470 p_found = FindParent (p_this, i_type);
473 p_found = FindChild (vlc_internals (p_this), i_type);
478 libvlc_unlock (p_this->p_libvlc);
483 static int objnamecmp(const vlc_object_t *obj, const char *name)
485 char *objname = vlc_object_get_name(obj);
489 int ret = strcmp (objname, name);
494 #undef vlc_object_find_name
496 * Finds a named object and increment its reference count.
497 * Beware that objects found in this manner can be "owned" by another thread,
498 * be of _any_ type, and be attached to any module (if any). With such an
499 * object reference, you can set or get object variables, emit log messages,
500 * and read write-once object parameters (psz_object_type, etc).
501 * You CANNOT cast the object to a more specific object type, and you
502 * definitely cannot invoke object type-specific callbacks with this.
504 * @param p_this object to search from
505 * @param psz_name name of the object to search for
506 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
508 * @return a matching object (must be released by the caller),
511 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
512 const char *psz_name, int i_mode )
514 vlc_object_t *p_found;
516 /* Reading psz_object_name from a separate inhibits thread-safety.
517 * Use a libvlc address variable instead for that sort of things! */
518 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
519 /* If have the requested name ourselves, don't look further */
520 if( !objnamecmp(p_this, psz_name) )
522 vlc_object_hold( p_this );
526 /* Otherwise, recursively look for the object */
527 if (i_mode == FIND_ANYWHERE)
528 return vlc_object_find_name (VLC_OBJECT(p_this->p_libvlc), psz_name,
531 libvlc_lock (p_this->p_libvlc);
532 vlc_mutex_lock (&name_lock);
536 p_found = FindParentName (p_this, psz_name);
539 p_found = FindChildName (vlc_internals (p_this), psz_name);
544 vlc_mutex_unlock (&name_lock);
545 libvlc_unlock (p_this->p_libvlc);
550 * Increment an object reference counter.
552 void * __vlc_object_hold( vlc_object_t *p_this )
554 vlc_object_internals_t *internals = vlc_internals( p_this );
556 vlc_spin_lock( &internals->ref_spin );
557 /* Avoid obvious freed object uses */
558 assert( internals->i_refcount > 0 );
559 /* Increment the counter */
560 internals->i_refcount++;
561 vlc_spin_unlock( &internals->ref_spin );
565 /*****************************************************************************
566 * Decrement an object refcount
567 * And destroy the object if its refcount reach zero.
568 *****************************************************************************/
569 void __vlc_object_release( vlc_object_t *p_this )
571 vlc_object_internals_t *internals = vlc_internals( p_this );
572 vlc_object_t *parent = NULL;
573 bool b_should_destroy;
575 vlc_spin_lock( &internals->ref_spin );
576 assert( internals->i_refcount > 0 );
578 if( internals->i_refcount > 1 )
581 /* There are still other references to the object */
582 internals->i_refcount--;
583 vlc_spin_unlock( &internals->ref_spin );
586 vlc_spin_unlock( &internals->ref_spin );
589 /* Remember that we cannot hold the spin while waiting on the mutex */
590 libvlc_lock (p_this->p_libvlc);
591 /* Take the spin again. Note that another thread may have held the
592 * object in the (very short) mean time. */
593 vlc_spin_lock( &internals->ref_spin );
594 b_should_destroy = --internals->i_refcount == 0;
595 vlc_spin_unlock( &internals->ref_spin );
597 if( b_should_destroy )
599 parent = p_this->p_parent;
601 /* Detach from parent to protect against FIND_CHILDREN */
602 vlc_object_detach_unlocked (p_this);
604 /* We have no children */
605 assert (internals->first == NULL);
607 libvlc_unlock (p_this->p_libvlc);
609 if( b_should_destroy )
613 canc = vlc_savecancel ();
614 vlc_object_destroy( p_this );
615 vlc_restorecancel (canc);
617 vlc_object_release (parent);
622 ****************************************************************************
623 * attach object to a parent object
624 *****************************************************************************
625 * This function sets p_this as a child of p_parent, and p_parent as a parent
626 * of p_this. This link can be undone using vlc_object_detach.
627 *****************************************************************************/
628 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
630 if( !p_this ) return;
632 vlc_object_internals_t *pap = vlc_internals (p_parent);
633 vlc_object_internals_t *priv = vlc_internals (p_this);
636 vlc_object_hold (p_parent);
637 libvlc_lock (p_this->p_libvlc);
639 /* Attach the parent to its child */
640 assert (!p_this->p_parent);
641 p_this->p_parent = p_parent;
643 /* Attach the child to its parent */
644 priv->next = pap->first;
645 if (priv->next != NULL)
646 priv->next->prev = priv;
648 libvlc_unlock (p_this->p_libvlc);
652 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
654 if (p_this->p_parent == NULL)
657 vlc_object_internals_t *pap = vlc_internals (p_this->p_parent);
658 vlc_object_internals_t *priv = vlc_internals (p_this);
661 if (priv->prev != NULL)
662 priv->prev->next = priv->next;
664 pap->first = priv->next;
665 if (priv->next != NULL)
666 priv->next->prev = priv->prev;
668 /* Remove p_this's parent */
669 p_this->p_parent = NULL;
674 ****************************************************************************
675 * detach object from its parent
676 *****************************************************************************
677 * This function removes all links between an object and its parent.
678 *****************************************************************************/
679 void __vlc_object_detach( vlc_object_t *p_this )
681 vlc_object_t *p_parent;
682 if( !p_this ) return;
684 libvlc_lock (p_this->p_libvlc);
685 p_parent = p_this->p_parent;
687 vlc_object_detach_unlocked( p_this );
688 libvlc_unlock (p_this->p_libvlc);
691 vlc_object_release (p_parent);
695 * Gets the list of children of an objects, and increment their reference
697 * @return a list (possibly empty) or NULL in case of error.
699 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
702 vlc_object_internals_t *priv;
705 libvlc_lock (obj->p_libvlc);
706 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
709 if (likely(l != NULL))
713 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
714 l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
716 libvlc_unlock (obj->p_libvlc);
720 static void DumpVariable (const void *data, const VISIT which, const int depth)
722 if (which != postorder && which != leaf)
726 const variable_t *p_var = *(const variable_t **)data;
727 const char *psz_type = "unknown";
729 switch( p_var->i_type & VLC_VAR_TYPE )
731 #define MYCASE( type, nice ) \
732 case VLC_VAR_ ## type: \
735 MYCASE( VOID, "void" );
736 MYCASE( BOOL, "bool" );
737 MYCASE( INTEGER, "integer" );
738 MYCASE( HOTKEY, "hotkey" );
739 MYCASE( STRING, "string" );
740 MYCASE( MODULE, "module" );
741 MYCASE( FILE, "file" );
742 MYCASE( DIRECTORY, "directory" );
743 MYCASE( VARIABLE, "variable" );
744 MYCASE( FLOAT, "float" );
745 MYCASE( TIME, "time" );
746 MYCASE( ADDRESS, "address" );
747 MYCASE( MUTEX, "mutex" );
748 MYCASE( LIST, "list" );
751 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
752 if( p_var->psz_text )
753 printf( ", %s", p_var->psz_text );
754 fputc( ')', stdout );
755 if( p_var->i_type & VLC_VAR_HASCHOICE )
756 fputs( ", has choices", stdout );
757 if( p_var->i_type & VLC_VAR_ISCOMMAND )
758 fputs( ", command", stdout );
759 if( p_var->i_entries )
760 printf( ", %d callbacks", p_var->i_entries );
761 switch( p_var->i_type & VLC_VAR_CLASS )
767 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
769 case VLC_VAR_INTEGER:
770 printf( ": %d", p_var->val.i_int );
773 printf( ": \"%s\"", p_var->val.psz_string );
776 printf( ": %f", p_var->val.f_float );
779 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
781 case VLC_VAR_ADDRESS:
782 printf( ": %p", p_var->val.p_address );
785 fputs( ": TODO", stdout );
788 fputc( '\n', stdout );
791 /*****************************************************************************
792 * DumpCommand: print the current vlc structure
793 *****************************************************************************
794 * This function prints either an ASCII tree showing the connections between
795 * vlc objects, and additional information such as their refcount, thread ID,
796 * etc. (command "tree"), or the same data as a simple list (command "list").
797 *****************************************************************************/
798 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
799 vlc_value_t oldval, vlc_value_t newval, void *p_data )
801 (void)oldval; (void)p_data;
802 vlc_object_t *p_object = NULL;
804 if( *newval.psz_string )
806 /* try using the object's name to find it */
807 p_object = vlc_object_find_name( p_this, newval.psz_string,
815 libvlc_lock (p_this->p_libvlc);
816 if( *psz_cmd == 't' )
818 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
821 p_object = VLC_OBJECT(p_this->p_libvlc);
824 DumpStructure( vlc_internals(p_object), 0, psz_foo );
826 else if( *psz_cmd == 'v' )
829 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
831 PrintObject( vlc_internals(p_object), "" );
832 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
833 if( vlc_internals( p_object )->var_root == NULL )
834 puts( " `-o No variables" );
836 twalk( vlc_internals( p_object )->var_root, DumpVariable );
837 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
839 libvlc_unlock (p_this->p_libvlc);
841 if( *newval.psz_string )
843 vlc_object_release( p_object );
848 /*****************************************************************************
849 * vlc_list_release: free a list previously allocated by vlc_list_find
850 *****************************************************************************
851 * This function decreases the refcount of all objects in the list and
853 *****************************************************************************/
854 void vlc_list_release( vlc_list_t *p_list )
858 for( i_index = 0; i_index < p_list->i_count; i_index++ )
860 vlc_object_release( p_list->p_values[i_index].p_object );
863 free( p_list->p_values );
867 /* Following functions are local */
869 static vlc_object_t *FindParent (vlc_object_t *p_this, int i_type)
871 for (vlc_object_t *parent = p_this->p_parent;
873 parent = parent->p_parent)
875 if (vlc_internals (parent)->i_object_type == i_type)
876 return vlc_object_hold (parent);
881 static vlc_object_t *FindParentName (vlc_object_t *p_this, const char *name)
883 for (vlc_object_t *parent = p_this->p_parent;
885 parent = parent->p_parent)
887 if (!strcmp (vlc_internals (parent)->psz_name, name))
888 return vlc_object_hold (parent);
893 static vlc_object_t *FindChild (vlc_object_internals_t *priv, int i_type)
895 for (priv = priv->first; priv != NULL; priv = priv->next)
897 if (priv->i_object_type == i_type)
898 return vlc_object_hold (vlc_externals (priv));
900 vlc_object_t *found = FindChild (priv, i_type);
907 static vlc_object_t *FindChildName (vlc_object_internals_t *priv,
910 for (priv = priv->first; priv != NULL; priv = priv->next)
912 if (!strcmp (priv->psz_name, name))
913 return vlc_object_hold (vlc_externals (priv));
915 vlc_object_t *found = FindChildName (priv, name);
922 static void PrintObject( vlc_object_internals_t *priv,
923 const char *psz_prefix )
925 char psz_refcount[20], psz_thread[30], psz_name[50], psz_parent[20];
927 int canc = vlc_savecancel ();
928 memset( &psz_name, 0, sizeof(psz_name) );
930 vlc_mutex_lock (&name_lock);
931 if (priv->psz_name != NULL)
933 snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
937 vlc_mutex_unlock (&name_lock);
939 psz_refcount[0] = '\0';
940 if( priv->i_refcount > 0 )
941 snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
943 psz_thread[0] = '\0';
945 snprintf( psz_thread, 29, " (thread %lu)",
946 (unsigned long)priv->thread_id );
948 psz_parent[0] = '\0';
949 /* FIXME: need structure lock!!! */
950 if( vlc_externals(priv)->p_parent )
951 snprintf( psz_parent, 19, ", parent %p",
952 vlc_externals(priv)->p_parent );
954 printf( " %so %p %s%s%s%s%s\n", psz_prefix,
955 vlc_externals(priv), vlc_externals(priv)->psz_object_type,
956 psz_name, psz_thread, psz_refcount, psz_parent );
957 vlc_restorecancel (canc);
960 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
963 char i_back = psz_foo[i_level];
964 psz_foo[i_level] = '\0';
966 PrintObject (priv, psz_foo);
968 psz_foo[i_level] = i_back;
970 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
972 msg_Warn( vlc_externals(priv), "structure tree is too deep" );
976 for (priv = priv->first; priv != NULL; priv = priv->next)
980 psz_foo[i_level-1] = ' ';
982 if( psz_foo[i_level-2] == '`' )
984 psz_foo[i_level-2] = ' ';
988 psz_foo[i_level] = priv->next ? '|' : '`';
989 psz_foo[i_level+1] = '-';
990 psz_foo[i_level+2] = '\0';
992 DumpStructure (priv, i_level + 2, psz_foo);
996 static vlc_list_t * NewList( int i_count )
998 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
1002 p_list->i_count = i_count;
1006 p_list->p_values = NULL;
1010 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1011 if( p_list->p_values == NULL )
1013 p_list->i_count = 0;