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_force = false;
133 p_new->psz_header = NULL;
136 p_new->i_flags = p_this->i_flags
137 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
139 p_priv->var_root = NULL;
143 libvlc_int_t *self = (libvlc_int_t*)p_new;
144 p_new->p_libvlc = self;
145 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
149 p_new->p_libvlc = p_this->p_libvlc;
151 vlc_spin_init( &p_priv->ref_spin );
152 p_priv->i_refcount = 1;
153 p_priv->pf_destructor = NULL;
154 p_priv->b_thread = false;
155 p_new->p_parent = NULL;
156 p_priv->first = NULL;
158 p_priv->old_parent = NULL;
161 /* Initialize mutexes and condvars */
162 vlc_mutex_init( &p_priv->var_lock );
163 vlc_cond_init( &p_priv->var_wait );
164 p_priv->pipes[0] = p_priv->pipes[1] = -1;
166 if (p_new == VLC_OBJECT(p_new->p_libvlc))
167 { /* TODO: should be in src/libvlc.c */
168 int canc = vlc_savecancel ();
169 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
170 var_AddCallback( p_new, "tree", DumpCommand, NULL );
171 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
172 var_AddCallback( p_new, "vars", DumpCommand, NULL );
173 vlc_restorecancel (canc);
179 #undef vlc_object_create
181 * Allocates and initializes a vlc object.
183 * @param i_size object byte size
185 * @return the new object, or NULL on error.
187 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
189 return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
194 ****************************************************************************
195 * Set the destructor of a vlc object
197 * This function sets the destructor of the vlc object. It will be called
198 * when the object is destroyed when the its refcount reaches 0.
199 * (It is called by the internal function vlc_object_destroy())
200 *****************************************************************************/
201 void __vlc_object_set_destructor( vlc_object_t *p_this,
202 vlc_destructor_t pf_destructor )
204 vlc_object_internals_t *p_priv = vlc_internals(p_this );
206 vlc_spin_lock( &p_priv->ref_spin );
207 p_priv->pf_destructor = pf_destructor;
208 vlc_spin_unlock( &p_priv->ref_spin );
211 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
213 #undef vlc_object_set_name
214 int vlc_object_set_name(vlc_object_t *obj, const char *name)
216 vlc_object_internals_t *priv = vlc_internals(obj);
217 char *newname = name ? strdup (name) : NULL;
220 vlc_mutex_lock (&name_lock);
221 oldname = priv->psz_name;
222 priv->psz_name = newname;
223 vlc_mutex_unlock (&name_lock);
226 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
229 #undef vlc_object_get_name
230 char *vlc_object_get_name(const vlc_object_t *obj)
232 vlc_object_internals_t *priv = vlc_internals(obj);
235 vlc_mutex_lock (&name_lock);
236 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
237 vlc_mutex_unlock (&name_lock);
243 ****************************************************************************
244 * Destroy a vlc object (Internal)
246 * This function destroys an object that has been previously allocated with
247 * vlc_object_create. The object's refcount must be zero and it must not be
248 * attached to other objects in any way.
250 * This function must be called with cancellation disabled (currently).
251 *****************************************************************************/
252 static void vlc_object_destroy( vlc_object_t *p_this )
254 vlc_object_internals_t *p_priv = vlc_internals( p_this );
256 /* Objects are always detached beforehand */
257 assert( !p_this->p_parent );
259 /* Send a kill to the object's thread if applicable */
260 vlc_object_kill( p_this );
262 /* Call the custom "subclass" destructor */
263 if( p_priv->pf_destructor )
264 p_priv->pf_destructor( p_this );
266 /* Any thread must have been cleaned up at this point. */
267 assert( !p_priv->b_thread );
269 /* Destroy the associated variables. */
270 var_DestroyAll( p_this );
272 vlc_cond_destroy( &p_priv->var_wait );
273 vlc_mutex_destroy( &p_priv->var_lock );
275 free( p_this->psz_header );
277 free( p_priv->psz_name );
279 vlc_spin_destroy( &p_priv->ref_spin );
280 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
281 close( p_priv->pipes[1] );
282 if( p_priv->pipes[0] != -1 )
283 close( p_priv->pipes[0] );
284 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
285 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
293 * select()-able pipes emulated using Winsock
295 static int pipe (int fd[2])
298 int addrlen = sizeof (addr);
300 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
301 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
302 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
305 memset (&addr, 0, sizeof (addr));
306 addr.sin_family = AF_INET;
307 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
308 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
309 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
311 || connect (c, (PSOCKADDR)&addr, addrlen))
314 a = accept (l, NULL, NULL);
315 if (a == INVALID_SOCKET)
326 if (l != INVALID_SOCKET)
328 if (c != INVALID_SOCKET)
334 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
337 * Returns the readable end of a pipe that becomes readable once termination
338 * of the object is requested (vlc_object_kill()).
339 * This can be used to wake-up out of a select() or poll() event loop, such
340 * typically when doing network I/O.
342 * Note that the pipe will remain the same for the lifetime of the object.
343 * DO NOT read the pipe nor close it yourself. Ever.
345 * @param obj object that would be "killed"
346 * @return a readable pipe descriptor, or -1 on error.
348 int vlc_object_waitpipe( vlc_object_t *obj )
350 vlc_object_internals_t *internals = vlc_internals( obj );
352 vlc_mutex_lock (&pipe_lock);
353 if (internals->pipes[0] == -1)
355 /* This can only ever happen if someone killed us without locking: */
356 assert (internals->pipes[1] == -1);
358 #if defined (HAVE_SYS_EVENTFD_H)
359 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
360 if (internals->pipes[0] == -1)
363 if (pipe (internals->pipes))
364 internals->pipes[0] = internals->pipes[1] = -1;
367 if (internals->pipes[0] != -1 && obj->b_die)
368 { /* Race condition: vlc_object_kill() already invoked! */
369 msg_Dbg (obj, "waitpipe: object already dying");
370 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
373 vlc_mutex_unlock (&pipe_lock);
374 return internals->pipes[0];
379 * Requests termination of an object, cancels the object thread, and make the
380 * object wait pipe (if it exists) readable. Not a cancellation point.
382 void __vlc_object_kill( vlc_object_t *p_this )
384 vlc_object_internals_t *priv = vlc_internals( p_this );
387 vlc_thread_cancel( p_this );
388 vlc_mutex_lock( &pipe_lock );
392 p_this->b_die = true;
395 /* This also serves as a memory barrier toward vlc_object_alive(): */
396 vlc_mutex_unlock( &pipe_lock );
400 int canc = vlc_savecancel ();
402 /* write _after_ setting b_die, so vlc_object_alive() returns false */
403 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
404 msg_Dbg (p_this, "waitpipe: object killed");
405 vlc_restorecancel (canc);
410 /*****************************************************************************
411 * find a typed object and increment its refcount
412 *****************************************************************************
413 * This function recursively looks for a given object type. i_mode can be one
414 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
415 *****************************************************************************/
416 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
418 vlc_object_t *p_found;
420 /* If we are of the requested type ourselves, don't look further */
421 if( vlc_internals (p_this)->i_object_type == i_type )
423 vlc_object_hold( p_this );
427 /* Otherwise, recursively look for the object */
428 if (i_mode == FIND_ANYWHERE)
429 return vlc_object_find (p_this->p_libvlc, i_type, FIND_CHILD);
433 case VLC_OBJECT_VOUT:
434 case VLC_OBJECT_AOUT:
436 case VLC_OBJECT_INPUT:
437 /* input can only be accessed like this from children,
438 * otherwise we could not promise that it is initialized */
439 if (i_mode != FIND_PARENT)
446 libvlc_lock (p_this->p_libvlc);
450 p_found = FindParent (p_this, i_type);
453 p_found = FindChild (vlc_internals (p_this), i_type);
458 libvlc_unlock (p_this->p_libvlc);
463 static int objnamecmp(const vlc_object_t *obj, const char *name)
465 char *objname = vlc_object_get_name(obj);
469 int ret = strcmp (objname, name);
474 #undef vlc_object_find_name
476 * Finds a named object and increment its reference count.
477 * Beware that objects found in this manner can be "owned" by another thread,
478 * be of _any_ type, and be attached to any module (if any). With such an
479 * object reference, you can set or get object variables, emit log messages,
480 * and read write-once object parameters (psz_object_type, etc).
481 * You CANNOT cast the object to a more specific object type, and you
482 * definitely cannot invoke object type-specific callbacks with this.
484 * @param p_this object to search from
485 * @param psz_name name of the object to search for
486 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
488 * @return a matching object (must be released by the caller),
491 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
492 const char *psz_name, int i_mode )
494 vlc_object_t *p_found;
496 /* Reading psz_object_name from a separate inhibits thread-safety.
497 * Use a libvlc address variable instead for that sort of things! */
498 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
499 /* If have the requested name ourselves, don't look further */
500 if( !objnamecmp(p_this, psz_name) )
502 vlc_object_hold( p_this );
506 /* Otherwise, recursively look for the object */
507 if (i_mode == FIND_ANYWHERE)
508 return vlc_object_find_name (VLC_OBJECT(p_this->p_libvlc), psz_name,
511 libvlc_lock (p_this->p_libvlc);
512 vlc_mutex_lock (&name_lock);
516 p_found = FindParentName (p_this, psz_name);
519 p_found = FindChildName (vlc_internals (p_this), psz_name);
524 vlc_mutex_unlock (&name_lock);
525 libvlc_unlock (p_this->p_libvlc);
530 * Increment an object reference counter.
532 void * __vlc_object_hold( vlc_object_t *p_this )
534 vlc_object_internals_t *internals = vlc_internals( p_this );
536 vlc_spin_lock( &internals->ref_spin );
537 /* Avoid obvious freed object uses */
538 assert( internals->i_refcount > 0 );
539 /* Increment the counter */
540 internals->i_refcount++;
541 vlc_spin_unlock( &internals->ref_spin );
545 /*****************************************************************************
546 * Decrement an object refcount
547 * And destroy the object if its refcount reach zero.
548 *****************************************************************************/
549 void __vlc_object_release( vlc_object_t *p_this )
551 vlc_object_internals_t *internals = vlc_internals( p_this );
552 vlc_object_t *parent = NULL;
553 bool b_should_destroy;
555 vlc_spin_lock( &internals->ref_spin );
556 assert( internals->i_refcount > 0 );
558 if( internals->i_refcount > 1 )
561 /* There are still other references to the object */
562 internals->i_refcount--;
563 vlc_spin_unlock( &internals->ref_spin );
566 vlc_spin_unlock( &internals->ref_spin );
569 /* Remember that we cannot hold the spin while waiting on the mutex */
570 libvlc_lock (p_this->p_libvlc);
571 /* Take the spin again. Note that another thread may have held the
572 * object in the (very short) mean time. */
573 vlc_spin_lock( &internals->ref_spin );
574 b_should_destroy = --internals->i_refcount == 0;
575 vlc_spin_unlock( &internals->ref_spin );
577 if( b_should_destroy )
579 parent = p_this->p_parent;
581 /* Detach from parent to protect against FIND_CHILDREN */
582 vlc_object_detach_unlocked (p_this);
584 /* We have no children */
585 assert (internals->first == NULL);
587 libvlc_unlock (p_this->p_libvlc);
589 if( b_should_destroy )
593 canc = vlc_savecancel ();
594 vlc_object_destroy( p_this );
595 vlc_restorecancel (canc);
597 vlc_object_release (parent);
602 ****************************************************************************
603 * attach object to a parent object
604 *****************************************************************************
605 * This function sets p_this as a child of p_parent, and p_parent as a parent
606 * of p_this. This link can be undone using vlc_object_detach.
607 *****************************************************************************/
608 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
610 if( !p_this ) return;
612 vlc_object_internals_t *pap = vlc_internals (p_parent);
613 vlc_object_internals_t *priv = vlc_internals (p_this);
614 vlc_object_t *p_old_parent;
617 vlc_object_hold (p_parent);
618 libvlc_lock (p_this->p_libvlc);
620 /* Reparenting an object carries a risk of invalid access to the parent,
621 * from another thread. This can happen when inheriting a variable, or
622 * through any direct access to vlc_object_t.p_parent. Also, reparenting
623 * brings a functional bug, whereby the reparented object uses incorrect
624 * old values for inherited variables (as the new parent may have different
625 * variable values, especially if it is an input).
626 * Note that the old parent may be already destroyed.
627 * So its pointer must not be dereferenced.
629 if (priv->old_parent)
630 msg_Info (p_this, "Reparenting an object is dangerous (%p -> %p)!",
631 priv->old_parent, p_parent);
634 p_old_parent = p_this->p_parent;
636 vlc_object_detach_unlocked (p_this);
638 /* Attach the parent to its child */
639 p_this->p_parent = p_parent;
641 /* Attach the child to its parent */
642 priv->next = pap->first;
643 if (priv->next != NULL)
644 priv->next->prev = priv;
646 libvlc_unlock (p_this->p_libvlc);
649 vlc_object_release (p_old_parent);
653 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
655 assert (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 */
670 priv->old_parent = p_this->p_parent;
672 p_this->p_parent = NULL;
677 ****************************************************************************
678 * detach object from its parent
679 *****************************************************************************
680 * This function removes all links between an object and its parent.
681 *****************************************************************************/
682 void __vlc_object_detach( vlc_object_t *p_this )
684 vlc_object_t *p_parent;
685 if( !p_this ) return;
687 libvlc_lock (p_this->p_libvlc);
688 p_parent = p_this->p_parent;
690 vlc_object_detach_unlocked( p_this );
691 libvlc_unlock (p_this->p_libvlc);
694 vlc_object_release (p_parent);
698 * Gets the list of children of an objects, and increment their reference
700 * @return a list (possibly empty) or NULL in case of error.
702 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
705 vlc_object_internals_t *priv;
708 libvlc_lock (obj->p_libvlc);
709 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
712 if (likely(l != NULL))
716 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
717 l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
719 libvlc_unlock (obj->p_libvlc);
723 static void DumpVariable (const void *data, const VISIT which, const int depth)
725 if (which != postorder && which != leaf)
729 const variable_t *p_var = *(const variable_t **)data;
730 const char *psz_type = "unknown";
732 switch( p_var->i_type & VLC_VAR_TYPE )
734 #define MYCASE( type, nice ) \
735 case VLC_VAR_ ## type: \
738 MYCASE( VOID, "void" );
739 MYCASE( BOOL, "bool" );
740 MYCASE( INTEGER, "integer" );
741 MYCASE( HOTKEY, "hotkey" );
742 MYCASE( STRING, "string" );
743 MYCASE( MODULE, "module" );
744 MYCASE( FILE, "file" );
745 MYCASE( DIRECTORY, "directory" );
746 MYCASE( VARIABLE, "variable" );
747 MYCASE( FLOAT, "float" );
748 MYCASE( TIME, "time" );
749 MYCASE( ADDRESS, "address" );
750 MYCASE( MUTEX, "mutex" );
751 MYCASE( LIST, "list" );
754 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
755 if( p_var->psz_text )
756 printf( ", %s", p_var->psz_text );
757 fputc( ')', stdout );
758 if( p_var->i_type & VLC_VAR_HASCHOICE )
759 fputs( ", has choices", stdout );
760 if( p_var->i_type & VLC_VAR_ISCOMMAND )
761 fputs( ", command", stdout );
762 if( p_var->i_entries )
763 printf( ", %d callbacks", p_var->i_entries );
764 switch( p_var->i_type & VLC_VAR_CLASS )
770 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
772 case VLC_VAR_INTEGER:
773 printf( ": %d", p_var->val.i_int );
776 printf( ": \"%s\"", p_var->val.psz_string );
779 printf( ": %f", p_var->val.f_float );
782 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
784 case VLC_VAR_ADDRESS:
785 printf( ": %p", p_var->val.p_address );
788 fputs( ": TODO", stdout );
791 fputc( '\n', stdout );
794 /*****************************************************************************
795 * DumpCommand: print the current vlc structure
796 *****************************************************************************
797 * This function prints either an ASCII tree showing the connections between
798 * vlc objects, and additional information such as their refcount, thread ID,
799 * etc. (command "tree"), or the same data as a simple list (command "list").
800 *****************************************************************************/
801 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
802 vlc_value_t oldval, vlc_value_t newval, void *p_data )
804 (void)oldval; (void)p_data;
805 vlc_object_t *p_object = NULL;
807 if( *newval.psz_string )
809 /* try using the object's name to find it */
810 p_object = vlc_object_find_name( p_this, newval.psz_string,
818 libvlc_lock (p_this->p_libvlc);
819 if( *psz_cmd == 't' )
821 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
824 p_object = VLC_OBJECT(p_this->p_libvlc);
827 DumpStructure( vlc_internals(p_object), 0, psz_foo );
829 else if( *psz_cmd == 'v' )
832 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
834 PrintObject( vlc_internals(p_object), "" );
835 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
836 if( vlc_internals( p_object )->var_root == NULL )
837 puts( " `-o No variables" );
839 twalk( vlc_internals( p_object )->var_root, DumpVariable );
840 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
842 libvlc_unlock (p_this->p_libvlc);
844 if( *newval.psz_string )
846 vlc_object_release( p_object );
851 /*****************************************************************************
852 * vlc_list_release: free a list previously allocated by vlc_list_find
853 *****************************************************************************
854 * This function decreases the refcount of all objects in the list and
856 *****************************************************************************/
857 void vlc_list_release( vlc_list_t *p_list )
861 for( i_index = 0; i_index < p_list->i_count; i_index++ )
863 vlc_object_release( p_list->p_values[i_index].p_object );
866 free( p_list->p_values );
870 /* Following functions are local */
872 static vlc_object_t *FindParent (vlc_object_t *p_this, int i_type)
874 for (vlc_object_t *parent = p_this->p_parent;
876 parent = parent->p_parent)
878 if (vlc_internals (parent)->i_object_type == i_type)
879 return vlc_object_hold (parent);
884 static vlc_object_t *FindParentName (vlc_object_t *p_this, const char *name)
886 for (vlc_object_t *parent = p_this->p_parent;
888 parent = parent->p_parent)
890 const char *objname = vlc_internals (parent)->psz_name;
891 if (objname && !strcmp (objname, name))
892 return vlc_object_hold (parent);
897 static vlc_object_t *FindChild (vlc_object_internals_t *priv, int i_type)
899 for (priv = priv->first; priv != NULL; priv = priv->next)
901 if (priv->i_object_type == i_type)
902 return vlc_object_hold (vlc_externals (priv));
904 vlc_object_t *found = FindChild (priv, i_type);
911 static vlc_object_t *FindChildName (vlc_object_internals_t *priv,
914 for (priv = priv->first; priv != NULL; priv = priv->next)
916 if (priv->psz_name && !strcmp (priv->psz_name, name))
917 return vlc_object_hold (vlc_externals (priv));
919 vlc_object_t *found = FindChildName (priv, name);
926 static void PrintObject( vlc_object_internals_t *priv,
927 const char *psz_prefix )
929 char psz_refcount[20], psz_thread[30], psz_name[50], psz_parent[20];
931 int canc = vlc_savecancel ();
932 memset( &psz_name, 0, sizeof(psz_name) );
934 vlc_mutex_lock (&name_lock);
935 if (priv->psz_name != NULL)
937 snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
941 vlc_mutex_unlock (&name_lock);
943 psz_refcount[0] = '\0';
944 if( priv->i_refcount > 0 )
945 snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
947 psz_thread[0] = '\0';
949 snprintf( psz_thread, 29, " (thread %lu)",
950 (unsigned long)priv->thread_id );
952 psz_parent[0] = '\0';
953 /* FIXME: need structure lock!!! */
954 if( vlc_externals(priv)->p_parent )
955 snprintf( psz_parent, 19, ", parent %p",
956 vlc_externals(priv)->p_parent );
958 printf( " %so %p %s%s%s%s%s\n", psz_prefix,
959 vlc_externals(priv), vlc_externals(priv)->psz_object_type,
960 psz_name, psz_thread, psz_refcount, psz_parent );
961 vlc_restorecancel (canc);
964 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
967 char i_back = psz_foo[i_level];
968 psz_foo[i_level] = '\0';
970 PrintObject (priv, psz_foo);
972 psz_foo[i_level] = i_back;
974 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
976 msg_Warn( vlc_externals(priv), "structure tree is too deep" );
980 for (priv = priv->first; priv != NULL; priv = priv->next)
984 psz_foo[i_level-1] = ' ';
986 if( psz_foo[i_level-2] == '`' )
988 psz_foo[i_level-2] = ' ';
992 psz_foo[i_level] = priv->next ? '|' : '`';
993 psz_foo[i_level+1] = '-';
994 psz_foo[i_level+2] = '\0';
996 DumpStructure (priv, i_level + 2, psz_foo);
1000 static vlc_list_t * NewList( int i_count )
1002 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
1003 if( p_list == NULL )
1006 p_list->i_count = i_count;
1010 p_list->p_values = NULL;
1014 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1015 if( p_list->p_values == NULL )
1017 p_list->i_count = 0;