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"
43 #include "audio_output/aout_internal.h"
45 #include "vlc_interface.h"
46 #include "vlc_codec.h"
48 #include "variables.h"
53 # include <winsock2.h>
54 # include <ws2tcpip.h>
56 # define read( a, b, c ) recv (a, b, c, 0)
58 # define write( a, b, c ) send (a, b, c, 0)
60 # define close( a ) closesocket (a)
67 #if defined (HAVE_SYS_EVENTFD_H)
68 # include <sys/eventfd.h>
72 /*****************************************************************************
74 *****************************************************************************/
75 static int DumpCommand( vlc_object_t *, char const *,
76 vlc_value_t, vlc_value_t, void * );
78 static vlc_object_t * FindParent ( vlc_object_t *, int );
79 static vlc_object_t * FindChild ( vlc_object_t *, int );
80 static vlc_object_t * FindParentName( vlc_object_t *, const char * );
81 static vlc_object_t * FindChildName ( vlc_object_t *, const char * );
82 static void PrintObject ( vlc_object_t *, const char * );
83 static void DumpStructure ( vlc_object_t *, int, char * );
85 static vlc_list_t * NewList ( int );
87 static void vlc_object_destroy( vlc_object_t *p_this );
88 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
90 /*****************************************************************************
91 * Local structure lock
92 *****************************************************************************/
93 static void libvlc_lock (libvlc_int_t *p_libvlc)
95 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
98 static void libvlc_unlock (libvlc_int_t *p_libvlc)
100 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
103 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
104 int i_type, const char *psz_type )
107 vlc_object_internals_t *p_priv;
110 * VLC objects are laid out as follow:
111 * - first the LibVLC-private per-object data,
112 * - then VLC_COMMON members from vlc_object_t,
113 * - finally, the type-specific data (if any).
115 * This function initializes the LibVLC and common data,
116 * and zeroes the rest.
118 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
122 assert (i_size >= sizeof (vlc_object_t));
123 p_new = (vlc_object_t *)(p_priv + 1);
125 p_priv->i_object_type = i_type;
126 p_new->psz_object_type = psz_type;
127 p_priv->psz_name = NULL;
129 p_new->b_die = false;
130 p_new->b_error = 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->pp_children = NULL;
157 p_priv->i_children = 0;
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 (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);
535 p_found = FindParentName (p_this, psz_name);
538 p_found = FindChildName (p_this, psz_name);
543 libvlc_unlock (p_this->p_libvlc);
548 * Increment an object reference counter.
550 void * __vlc_object_hold( vlc_object_t *p_this )
552 vlc_object_internals_t *internals = vlc_internals( p_this );
554 vlc_spin_lock( &internals->ref_spin );
555 /* Avoid obvious freed object uses */
556 assert( internals->i_refcount > 0 );
557 /* Increment the counter */
558 internals->i_refcount++;
559 vlc_spin_unlock( &internals->ref_spin );
563 /*****************************************************************************
564 * Decrement an object refcount
565 * And destroy the object if its refcount reach zero.
566 *****************************************************************************/
567 void __vlc_object_release( vlc_object_t *p_this )
569 vlc_object_internals_t *internals = vlc_internals( p_this );
570 vlc_object_t *parent = NULL;
571 bool b_should_destroy;
573 vlc_spin_lock( &internals->ref_spin );
574 assert( internals->i_refcount > 0 );
576 if( internals->i_refcount > 1 )
579 /* There are still other references to the object */
580 internals->i_refcount--;
581 vlc_spin_unlock( &internals->ref_spin );
584 vlc_spin_unlock( &internals->ref_spin );
587 /* Remember that we cannot hold the spin while waiting on the mutex */
588 libvlc_lock (p_this->p_libvlc);
589 /* Take the spin again. Note that another thread may have held the
590 * object in the (very short) mean time. */
591 vlc_spin_lock( &internals->ref_spin );
592 b_should_destroy = --internals->i_refcount == 0;
593 vlc_spin_unlock( &internals->ref_spin );
595 if( b_should_destroy )
597 parent = p_this->p_parent;
599 /* Detach from parent to protect against FIND_CHILDREN */
600 vlc_object_detach_unlocked (p_this);
602 /* We have no children */
603 assert (internals->i_children == 0);
605 libvlc_unlock (p_this->p_libvlc);
607 if( b_should_destroy )
611 canc = vlc_savecancel ();
612 vlc_object_destroy( p_this );
613 vlc_restorecancel (canc);
615 vlc_object_release (parent);
620 ****************************************************************************
621 * attach object to a parent object
622 *****************************************************************************
623 * This function sets p_this as a child of p_parent, and p_parent as a parent
624 * of p_this. This link can be undone using vlc_object_detach.
625 *****************************************************************************/
626 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
628 if( !p_this ) return;
630 vlc_object_hold (p_parent);
631 libvlc_lock (p_this->p_libvlc);
633 /* Attach the parent to its child */
634 assert (!p_this->p_parent);
635 p_this->p_parent = p_parent;
637 /* Attach the child to its parent */
638 vlc_object_internals_t *priv = vlc_internals( p_parent );
639 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
641 libvlc_unlock (p_this->p_libvlc);
645 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
647 if (p_this->p_parent == NULL)
650 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
654 /* Remove p_this's parent */
655 p_this->p_parent = NULL;
657 /* Remove all of p_parent's children which are p_this */
658 for( i_index = priv->i_children ; i_index-- ; )
660 if( priv->pp_children[i_index] == p_this )
663 for( i = i_index ; i < priv->i_children ; i++ )
664 priv->pp_children[i] = priv->pp_children[i+1];
668 if( priv->i_children )
670 vlc_object_t **pp_children = (vlc_object_t **)
671 realloc( priv->pp_children,
672 priv->i_children * sizeof(vlc_object_t *) );
674 priv->pp_children = pp_children;
678 /* Special case - don't realloc() to zero to avoid leaking */
679 free( priv->pp_children );
680 priv->pp_children = NULL;
686 ****************************************************************************
687 * detach object from its parent
688 *****************************************************************************
689 * This function removes all links between an object and its parent.
690 *****************************************************************************/
691 void __vlc_object_detach( vlc_object_t *p_this )
693 vlc_object_t *p_parent;
694 if( !p_this ) return;
696 libvlc_lock (p_this->p_libvlc);
697 p_parent = p_this->p_parent;
699 vlc_object_detach_unlocked( p_this );
700 libvlc_unlock (p_this->p_libvlc);
703 vlc_object_release (p_parent);
707 * Gets the list of children of an objects, and increment their reference
709 * @return a list (possibly empty) or NULL in case of error.
711 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
714 vlc_object_internals_t *priv = vlc_internals( obj );
716 libvlc_lock (obj->p_libvlc);
717 l = NewList( priv->i_children );
718 for (int i = 0; i < l->i_count; i++)
720 vlc_object_hold( priv->pp_children[i] );
721 l->p_values[i].p_object = priv->pp_children[i];
723 libvlc_unlock (obj->p_libvlc);
727 static void DumpVariable (const void *data, const VISIT which, const int depth)
729 if (which != postorder && which != leaf)
733 const variable_t *p_var = *(const variable_t **)data;
734 const char *psz_type = "unknown";
736 switch( p_var->i_type & VLC_VAR_TYPE )
738 #define MYCASE( type, nice ) \
739 case VLC_VAR_ ## type: \
742 MYCASE( VOID, "void" );
743 MYCASE( BOOL, "bool" );
744 MYCASE( INTEGER, "integer" );
745 MYCASE( HOTKEY, "hotkey" );
746 MYCASE( STRING, "string" );
747 MYCASE( MODULE, "module" );
748 MYCASE( FILE, "file" );
749 MYCASE( DIRECTORY, "directory" );
750 MYCASE( VARIABLE, "variable" );
751 MYCASE( FLOAT, "float" );
752 MYCASE( TIME, "time" );
753 MYCASE( ADDRESS, "address" );
754 MYCASE( MUTEX, "mutex" );
755 MYCASE( LIST, "list" );
758 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
759 if( p_var->psz_text )
760 printf( ", %s", p_var->psz_text );
761 fputc( ')', stdout );
762 if( p_var->i_type & VLC_VAR_HASCHOICE )
763 fputs( ", has choices", stdout );
764 if( p_var->i_type & VLC_VAR_ISCOMMAND )
765 fputs( ", command", stdout );
766 if( p_var->i_entries )
767 printf( ", %d callbacks", p_var->i_entries );
768 switch( p_var->i_type & VLC_VAR_CLASS )
774 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
776 case VLC_VAR_INTEGER:
777 printf( ": %d", p_var->val.i_int );
780 printf( ": \"%s\"", p_var->val.psz_string );
783 printf( ": %f", p_var->val.f_float );
786 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
788 case VLC_VAR_ADDRESS:
789 printf( ": %p", p_var->val.p_address );
792 fputs( ": TODO", stdout );
795 fputc( '\n', stdout );
798 /*****************************************************************************
799 * DumpCommand: print the current vlc structure
800 *****************************************************************************
801 * This function prints either an ASCII tree showing the connections between
802 * vlc objects, and additional information such as their refcount, thread ID,
803 * etc. (command "tree"), or the same data as a simple list (command "list").
804 *****************************************************************************/
805 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
806 vlc_value_t oldval, vlc_value_t newval, void *p_data )
808 (void)oldval; (void)p_data;
809 vlc_object_t *p_object = NULL;
811 if( *newval.psz_string )
813 /* try using the object's name to find it */
814 p_object = vlc_object_find_name( p_this, newval.psz_string,
822 libvlc_lock (p_this->p_libvlc);
823 if( *psz_cmd == 't' )
825 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
828 p_object = VLC_OBJECT(p_this->p_libvlc);
831 DumpStructure( p_object, 0, psz_foo );
833 else if( *psz_cmd == 'v' )
836 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
838 PrintObject( p_object, "" );
839 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
840 if( vlc_internals( p_object )->var_root == NULL )
841 puts( " `-o No variables" );
843 twalk( vlc_internals( p_object )->var_root, DumpVariable );
844 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
846 libvlc_unlock (p_this->p_libvlc);
848 if( *newval.psz_string )
850 vlc_object_release( p_object );
855 /*****************************************************************************
856 * vlc_list_release: free a list previously allocated by vlc_list_find
857 *****************************************************************************
858 * This function decreases the refcount of all objects in the list and
860 *****************************************************************************/
861 void vlc_list_release( vlc_list_t *p_list )
865 for( i_index = 0; i_index < p_list->i_count; i_index++ )
867 vlc_object_release( p_list->p_values[i_index].p_object );
870 free( p_list->p_values );
874 /* Following functions are local */
876 static vlc_object_t *FindParent (vlc_object_t *p_this, int i_type)
878 for (vlc_object_t *parent = p_this->p_parent;
880 parent = parent->p_parent)
882 if (vlc_internals (parent)->i_object_type == i_type)
883 return vlc_object_hold (parent);
888 static vlc_object_t *FindParentName (vlc_object_t *p_this, const char *name)
890 for (vlc_object_t *parent = p_this->p_parent;
892 parent = parent->p_parent)
894 if (!objnamecmp (parent, name))
895 return vlc_object_hold (parent);
900 static vlc_object_t *FindChild (vlc_object_t *p_this, int i_type)
902 for (int i = vlc_internals( p_this )->i_children; i--; )
904 vlc_object_t *child = vlc_internals (p_this)->pp_children[i];
905 if (vlc_internals (child)->i_object_type == i_type)
906 return vlc_object_hold (child);
908 child = FindChild (child, i_type);
915 static vlc_object_t *FindChildName (vlc_object_t *p_this, const char *name)
917 for (int i = vlc_internals( p_this )->i_children; i--; )
919 vlc_object_t *child = vlc_internals (p_this)->pp_children[i];
920 if (!objnamecmp (child, name))
921 return vlc_object_hold (child);
923 child = FindChildName (child, name);
930 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
932 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
935 int canc = vlc_savecancel ();
936 memset( &psz_name, 0, sizeof(psz_name) );
937 char *name = vlc_object_get_name(p_this);
940 snprintf( psz_name, 49, " \"%s\"", name );
946 psz_children[0] = '\0';
947 switch( vlc_internals( p_this )->i_children )
952 strcpy( psz_children, ", 1 child" );
955 snprintf( psz_children, 19, ", %i children",
956 vlc_internals( p_this )->i_children );
960 psz_refcount[0] = '\0';
961 if( vlc_internals( p_this )->i_refcount > 0 )
962 snprintf( psz_refcount, 19, ", refcount %u",
963 vlc_internals( p_this )->i_refcount );
965 psz_thread[0] = '\0';
966 if( vlc_internals( p_this )->b_thread )
967 snprintf( psz_thread, 29, " (thread %lu)",
968 (unsigned long)vlc_internals( p_this )->thread_id );
970 psz_parent[0] = '\0';
971 if( p_this->p_parent )
972 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
974 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
975 p_this, p_this->psz_object_type,
976 psz_name, psz_thread, psz_refcount, psz_children,
978 vlc_restorecancel (canc);
981 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
984 char i_back = psz_foo[i_level];
985 psz_foo[i_level] = '\0';
987 PrintObject( p_this, psz_foo );
989 psz_foo[i_level] = i_back;
991 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
993 msg_Warn( p_this, "structure tree is too deep" );
997 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1001 psz_foo[i_level-1] = ' ';
1003 if( psz_foo[i_level-2] == '`' )
1005 psz_foo[i_level-2] = ' ';
1009 if( i == vlc_internals( p_this )->i_children - 1 )
1011 psz_foo[i_level] = '`';
1015 psz_foo[i_level] = '|';
1018 psz_foo[i_level+1] = '-';
1019 psz_foo[i_level+2] = '\0';
1021 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1026 static vlc_list_t * NewList( int i_count )
1028 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
1029 if( p_list == NULL )
1032 p_list->i_count = i_count;
1036 p_list->p_values = NULL;
1040 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1041 if( p_list->p_values == NULL )
1043 p_list->i_count = 0;