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>
71 # define EFD_CLOEXEC 0
72 # warning EFD_CLOEXEC missing. Consider updating libc.
77 /*****************************************************************************
79 *****************************************************************************/
80 static int DumpCommand( vlc_object_t *, char const *,
81 vlc_value_t, vlc_value_t, void * );
83 static vlc_object_t * FindParent ( vlc_object_t *, int );
84 static vlc_object_t * FindChild ( vlc_object_internals_t *, int );
85 static vlc_object_t * FindParentName( vlc_object_t *, const char * );
86 static vlc_object_t * FindChildName ( vlc_object_internals_t *, const char * );
87 static void PrintObject( vlc_object_internals_t *, const char * );
88 static void DumpStructure( vlc_object_internals_t *, unsigned, char * );
90 static vlc_list_t * NewList ( int );
92 static void vlc_object_destroy( vlc_object_t *p_this );
93 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
95 /*****************************************************************************
96 * Local structure lock
97 *****************************************************************************/
98 static void libvlc_lock (libvlc_int_t *p_libvlc)
100 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
103 static void libvlc_unlock (libvlc_int_t *p_libvlc)
105 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
108 #undef vlc_custom_create
109 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
110 int i_type, const char *psz_type )
113 vlc_object_internals_t *p_priv;
116 * VLC objects are laid out as follow:
117 * - first the LibVLC-private per-object data,
118 * - then VLC_COMMON members from vlc_object_t,
119 * - finally, the type-specific data (if any).
121 * This function initializes the LibVLC and common data,
122 * and zeroes the rest.
124 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
128 assert (i_size >= sizeof (vlc_object_t));
129 p_new = (vlc_object_t *)(p_priv + 1);
131 p_priv->i_object_type = i_type;
132 p_new->psz_object_type = psz_type;
133 p_priv->psz_name = NULL;
135 p_new->b_die = false;
136 p_new->b_force = false;
138 p_new->psz_header = NULL;
141 p_new->i_flags = p_this->i_flags
142 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
144 p_priv->var_root = NULL;
148 libvlc_int_t *self = (libvlc_int_t*)p_new;
149 p_new->p_libvlc = self;
150 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
154 p_new->p_libvlc = p_this->p_libvlc;
156 vlc_spin_init( &p_priv->ref_spin );
157 p_priv->i_refcount = 1;
158 p_priv->pf_destructor = NULL;
159 p_priv->b_thread = false;
160 p_new->p_parent = NULL;
161 p_priv->first = NULL;
163 p_priv->old_parent = NULL;
166 /* Initialize mutexes and condvars */
167 vlc_mutex_init( &p_priv->var_lock );
168 vlc_cond_init( &p_priv->var_wait );
169 p_priv->pipes[0] = p_priv->pipes[1] = -1;
171 if (p_new == VLC_OBJECT(p_new->p_libvlc))
172 { /* TODO: should be in src/libvlc.c */
173 int canc = vlc_savecancel ();
174 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
175 var_AddCallback( p_new, "tree", DumpCommand, NULL );
176 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
177 var_AddCallback( p_new, "vars", DumpCommand, NULL );
178 vlc_restorecancel (canc);
184 #undef vlc_object_create
186 * Allocates and initializes a vlc object.
188 * @param i_size object byte size
190 * @return the new object, or NULL on error.
192 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
194 return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
197 #undef vlc_object_set_destructor
199 ****************************************************************************
200 * Set the destructor of a vlc object
202 * This function sets the destructor of the vlc object. It will be called
203 * when the object is destroyed when the its refcount reaches 0.
204 * (It is called by the internal function vlc_object_destroy())
205 *****************************************************************************/
206 void vlc_object_set_destructor( vlc_object_t *p_this,
207 vlc_destructor_t pf_destructor )
209 vlc_object_internals_t *p_priv = vlc_internals(p_this );
211 vlc_spin_lock( &p_priv->ref_spin );
212 p_priv->pf_destructor = pf_destructor;
213 vlc_spin_unlock( &p_priv->ref_spin );
216 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
218 #undef vlc_object_set_name
219 int vlc_object_set_name(vlc_object_t *obj, const char *name)
221 vlc_object_internals_t *priv = vlc_internals(obj);
222 char *newname = name ? strdup (name) : NULL;
225 vlc_mutex_lock (&name_lock);
226 oldname = priv->psz_name;
227 priv->psz_name = newname;
228 vlc_mutex_unlock (&name_lock);
231 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
234 #undef vlc_object_get_name
235 char *vlc_object_get_name(const vlc_object_t *obj)
237 vlc_object_internals_t *priv = vlc_internals(obj);
240 vlc_mutex_lock (&name_lock);
241 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
242 vlc_mutex_unlock (&name_lock);
248 ****************************************************************************
249 * Destroy a vlc object (Internal)
251 * This function destroys an object that has been previously allocated with
252 * vlc_object_create. The object's refcount must be zero and it must not be
253 * attached to other objects in any way.
255 * This function must be called with cancellation disabled (currently).
256 *****************************************************************************/
257 static void vlc_object_destroy( vlc_object_t *p_this )
259 vlc_object_internals_t *p_priv = vlc_internals( p_this );
261 /* Objects are always detached beforehand */
262 assert( !p_this->p_parent );
264 /* Send a kill to the object's thread if applicable */
265 vlc_object_kill( p_this );
267 /* Call the custom "subclass" destructor */
268 if( p_priv->pf_destructor )
269 p_priv->pf_destructor( p_this );
271 /* Any thread must have been cleaned up at this point. */
272 assert( !p_priv->b_thread );
274 /* Destroy the associated variables. */
275 var_DestroyAll( p_this );
277 vlc_cond_destroy( &p_priv->var_wait );
278 vlc_mutex_destroy( &p_priv->var_lock );
280 free( p_this->psz_header );
282 free( p_priv->psz_name );
284 vlc_spin_destroy( &p_priv->ref_spin );
285 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
286 close( p_priv->pipes[1] );
287 if( p_priv->pipes[0] != -1 )
288 close( p_priv->pipes[0] );
289 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
290 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
298 * select()-able pipes emulated using Winsock
300 static int pipe (int fd[2])
303 int addrlen = sizeof (addr);
305 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
306 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
307 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
310 memset (&addr, 0, sizeof (addr));
311 addr.sin_family = AF_INET;
312 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
313 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
314 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
316 || connect (c, (PSOCKADDR)&addr, addrlen))
319 a = accept (l, NULL, NULL);
320 if (a == INVALID_SOCKET)
331 if (l != INVALID_SOCKET)
333 if (c != INVALID_SOCKET)
339 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
342 * Returns the readable end of a pipe that becomes readable once termination
343 * of the object is requested (vlc_object_kill()).
344 * This can be used to wake-up out of a select() or poll() event loop, such
345 * typically when doing network I/O.
347 * Note that the pipe will remain the same for the lifetime of the object.
348 * DO NOT read the pipe nor close it yourself. Ever.
350 * @param obj object that would be "killed"
351 * @return a readable pipe descriptor, or -1 on error.
353 int vlc_object_waitpipe( vlc_object_t *obj )
355 vlc_object_internals_t *internals = vlc_internals( obj );
357 vlc_mutex_lock (&pipe_lock);
358 if (internals->pipes[0] == -1)
360 /* This can only ever happen if someone killed us without locking: */
361 assert (internals->pipes[1] == -1);
363 /* pipe() is not a cancellation point, but write() is and eventfd() is
364 * unspecified (not in POSIX). */
365 int canc = vlc_savecancel ();
366 #if defined (HAVE_SYS_EVENTFD_H)
367 internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC);
368 if (internals->pipes[0] == -1)
371 if (pipe (internals->pipes))
372 internals->pipes[0] = internals->pipes[1] = -1;
375 if (internals->pipes[0] != -1 && obj->b_die)
376 { /* Race condition: vlc_object_kill() already invoked! */
377 msg_Dbg (obj, "waitpipe: object already dying");
378 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
380 vlc_restorecancel (canc);
382 vlc_mutex_unlock (&pipe_lock);
383 return internals->pipes[0];
386 #undef vlc_object_kill
388 * Requests termination of an object, cancels the object thread, and make the
389 * object wait pipe (if it exists) readable. Not a cancellation point.
391 void vlc_object_kill( vlc_object_t *p_this )
393 vlc_object_internals_t *priv = vlc_internals( p_this );
396 vlc_thread_cancel( p_this );
397 vlc_mutex_lock( &pipe_lock );
401 p_this->b_die = true;
404 /* This also serves as a memory barrier toward vlc_object_alive(): */
405 vlc_mutex_unlock( &pipe_lock );
409 int canc = vlc_savecancel ();
411 /* write _after_ setting b_die, so vlc_object_alive() returns false */
412 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
413 msg_Dbg (p_this, "waitpipe: object killed");
414 vlc_restorecancel (canc);
418 #undef vlc_object_find
419 /*****************************************************************************
420 * find a typed object and increment its refcount
421 *****************************************************************************
422 * This function recursively looks for a given object type. i_mode can be one
423 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
424 *****************************************************************************/
425 void * vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
427 vlc_object_t *p_found;
429 /* If we are of the requested type ourselves, don't look further */
430 if( vlc_internals (p_this)->i_object_type == i_type )
432 vlc_object_hold( p_this );
436 /* Otherwise, recursively look for the object */
437 if (i_mode == FIND_ANYWHERE)
438 return vlc_object_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
442 case VLC_OBJECT_VOUT:
443 case VLC_OBJECT_AOUT:
445 case VLC_OBJECT_INPUT:
446 /* input can only be accessed like this from children,
447 * otherwise we could not promise that it is initialized */
448 if (i_mode != FIND_PARENT)
455 libvlc_lock (p_this->p_libvlc);
459 p_found = FindParent (p_this, i_type);
462 p_found = FindChild (vlc_internals (p_this), i_type);
467 libvlc_unlock (p_this->p_libvlc);
472 static int objnamecmp(const vlc_object_t *obj, const char *name)
474 char *objname = vlc_object_get_name(obj);
478 int ret = strcmp (objname, name);
483 #undef vlc_object_find_name
485 * Finds a named object and increment its reference count.
486 * Beware that objects found in this manner can be "owned" by another thread,
487 * be of _any_ type, and be attached to any module (if any). With such an
488 * object reference, you can set or get object variables, emit log messages,
489 * and read write-once object parameters (psz_object_type, etc).
490 * You CANNOT cast the object to a more specific object type, and you
491 * definitely cannot invoke object type-specific callbacks with this.
493 * @param p_this object to search from
494 * @param psz_name name of the object to search for
495 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
497 * @return a matching object (must be released by the caller),
500 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
501 const char *psz_name, int i_mode )
503 vlc_object_t *p_found;
505 /* Reading psz_object_name from a separate inhibits thread-safety.
506 * Use a libvlc address variable instead for that sort of things! */
507 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
508 /* If have the requested name ourselves, don't look further */
509 if( !objnamecmp(p_this, psz_name) )
511 vlc_object_hold( p_this );
515 /* Otherwise, recursively look for the object */
516 if (i_mode == FIND_ANYWHERE)
517 return vlc_object_find_name (VLC_OBJECT(p_this->p_libvlc), psz_name,
520 libvlc_lock (p_this->p_libvlc);
521 vlc_mutex_lock (&name_lock);
525 p_found = FindParentName (p_this, psz_name);
528 p_found = FindChildName (vlc_internals (p_this), psz_name);
533 vlc_mutex_unlock (&name_lock);
534 libvlc_unlock (p_this->p_libvlc);
538 #undef vlc_object_hold
540 * Increment an object reference counter.
542 void * vlc_object_hold( vlc_object_t *p_this )
544 vlc_object_internals_t *internals = vlc_internals( p_this );
546 vlc_spin_lock( &internals->ref_spin );
547 /* Avoid obvious freed object uses */
548 assert( internals->i_refcount > 0 );
549 /* Increment the counter */
550 internals->i_refcount++;
551 vlc_spin_unlock( &internals->ref_spin );
555 #undef vlc_object_release
556 /*****************************************************************************
557 * Decrement an object refcount
558 * And destroy the object if its refcount reach zero.
559 *****************************************************************************/
560 void vlc_object_release( vlc_object_t *p_this )
562 vlc_object_internals_t *internals = vlc_internals( p_this );
563 vlc_object_t *parent = NULL;
564 bool b_should_destroy;
566 vlc_spin_lock( &internals->ref_spin );
567 assert( internals->i_refcount > 0 );
569 if( internals->i_refcount > 1 )
572 /* There are still other references to the object */
573 internals->i_refcount--;
574 vlc_spin_unlock( &internals->ref_spin );
577 vlc_spin_unlock( &internals->ref_spin );
580 /* Remember that we cannot hold the spin while waiting on the mutex */
581 libvlc_lock (p_this->p_libvlc);
582 /* Take the spin again. Note that another thread may have held the
583 * object in the (very short) mean time. */
584 vlc_spin_lock( &internals->ref_spin );
585 b_should_destroy = --internals->i_refcount == 0;
586 vlc_spin_unlock( &internals->ref_spin );
588 if( b_should_destroy )
590 parent = p_this->p_parent;
592 /* Detach from parent to protect against FIND_CHILDREN */
593 vlc_object_detach_unlocked (p_this);
595 /* We have no children */
596 assert (internals->first == NULL);
598 libvlc_unlock (p_this->p_libvlc);
600 if( b_should_destroy )
604 canc = vlc_savecancel ();
605 vlc_object_destroy( p_this );
606 vlc_restorecancel (canc);
608 vlc_object_release (parent);
612 #undef vlc_object_attach
614 ****************************************************************************
615 * attach object to a parent object
616 *****************************************************************************
617 * This function sets p_this as a child of p_parent, and p_parent as a parent
618 * of p_this. This link can be undone using vlc_object_detach.
619 *****************************************************************************/
620 void vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
622 if( !p_this ) return;
624 vlc_object_internals_t *pap = vlc_internals (p_parent);
625 vlc_object_internals_t *priv = vlc_internals (p_this);
626 vlc_object_t *p_old_parent;
629 vlc_object_hold (p_parent);
630 libvlc_lock (p_this->p_libvlc);
632 /* Reparenting an object carries a risk of invalid access to the parent,
633 * from another thread. This can happen when inheriting a variable, or
634 * through any direct access to vlc_object_t.p_parent. Also, reparenting
635 * brings a functional bug, whereby the reparented object uses incorrect
636 * old values for inherited variables (as the new parent may have different
637 * variable values, especially if it is an input).
638 * Note that the old parent may be already destroyed.
639 * So its pointer must not be dereferenced.
641 if (priv->old_parent)
642 msg_Info (p_this, "Reparenting an object is dangerous (%p -> %p)!",
643 priv->old_parent, p_parent);
646 p_old_parent = p_this->p_parent;
648 vlc_object_detach_unlocked (p_this);
650 /* Attach the parent to its child */
651 p_this->p_parent = p_parent;
653 /* Attach the child to its parent */
654 priv->next = pap->first;
655 if (priv->next != NULL)
656 priv->next->prev = priv;
658 libvlc_unlock (p_this->p_libvlc);
661 vlc_object_release (p_old_parent);
665 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
667 assert (p_this->p_parent != NULL);
669 vlc_object_internals_t *pap = vlc_internals (p_this->p_parent);
670 vlc_object_internals_t *priv = vlc_internals (p_this);
673 if (priv->prev != NULL)
674 priv->prev->next = priv->next;
676 pap->first = priv->next;
677 if (priv->next != NULL)
678 priv->next->prev = priv->prev;
680 /* Remove p_this's parent */
682 priv->old_parent = p_this->p_parent;
684 p_this->p_parent = NULL;
687 #undef vlc_object_detach
689 ****************************************************************************
690 * detach object from its parent
691 *****************************************************************************
692 * This function removes all links between an object and its parent.
693 *****************************************************************************/
694 void vlc_object_detach( vlc_object_t *p_this )
696 vlc_object_t *p_parent;
697 if( !p_this ) return;
699 libvlc_lock (p_this->p_libvlc);
700 p_parent = p_this->p_parent;
702 vlc_object_detach_unlocked( p_this );
703 libvlc_unlock (p_this->p_libvlc);
706 vlc_object_release (p_parent);
709 #undef vlc_list_children
711 * Gets the list of children of an objects, and increment their reference
713 * @return a list (possibly empty) or NULL in case of error.
715 vlc_list_t *vlc_list_children( vlc_object_t *obj )
718 vlc_object_internals_t *priv;
721 libvlc_lock (obj->p_libvlc);
722 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
725 if (likely(l != NULL))
729 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
730 l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
732 libvlc_unlock (obj->p_libvlc);
736 static void DumpVariable (const void *data, const VISIT which, const int depth)
738 if (which != postorder && which != leaf)
742 const variable_t *p_var = *(const variable_t **)data;
743 const char *psz_type = "unknown";
745 switch( p_var->i_type & VLC_VAR_TYPE )
747 #define MYCASE( type, nice ) \
748 case VLC_VAR_ ## type: \
751 MYCASE( VOID, "void" );
752 MYCASE( BOOL, "bool" );
753 MYCASE( INTEGER, "integer" );
754 MYCASE( HOTKEY, "hotkey" );
755 MYCASE( STRING, "string" );
756 MYCASE( MODULE, "module" );
757 MYCASE( FILE, "file" );
758 MYCASE( DIRECTORY, "directory" );
759 MYCASE( VARIABLE, "variable" );
760 MYCASE( FLOAT, "float" );
761 MYCASE( TIME, "time" );
762 MYCASE( COORDS, "coords" );
763 MYCASE( ADDRESS, "address" );
764 MYCASE( MUTEX, "mutex" );
765 MYCASE( LIST, "list" );
768 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
769 if( p_var->psz_text )
770 printf( ", %s", p_var->psz_text );
771 fputc( ')', stdout );
772 if( p_var->i_type & VLC_VAR_HASCHOICE )
773 fputs( ", has choices", stdout );
774 if( p_var->i_type & VLC_VAR_ISCOMMAND )
775 fputs( ", command", stdout );
776 if( p_var->i_entries )
777 printf( ", %d callbacks", p_var->i_entries );
778 switch( p_var->i_type & VLC_VAR_CLASS )
784 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
786 case VLC_VAR_INTEGER:
787 printf( ": %d", p_var->val.i_int );
790 printf( ": \"%s\"", p_var->val.psz_string );
793 printf( ": %f", p_var->val.f_float );
796 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
799 printf( ": %"PRId32"x%"PRId32,
800 p_var->val.coords.x, p_var->val.coords.y );
802 case VLC_VAR_ADDRESS:
803 printf( ": %p", p_var->val.p_address );
806 fputs( ": TODO", stdout );
809 fputc( '\n', stdout );
812 /*****************************************************************************
813 * DumpCommand: print the current vlc structure
814 *****************************************************************************
815 * This function prints either an ASCII tree showing the connections between
816 * vlc objects, and additional information such as their refcount, thread ID,
817 * etc. (command "tree"), or the same data as a simple list (command "list").
818 *****************************************************************************/
819 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
820 vlc_value_t oldval, vlc_value_t newval, void *p_data )
822 (void)oldval; (void)p_data;
823 vlc_object_t *p_object = NULL;
825 if( *newval.psz_string )
827 /* try using the object's name to find it */
828 p_object = vlc_object_find_name( p_this, newval.psz_string,
836 libvlc_lock (p_this->p_libvlc);
837 if( *psz_cmd == 't' )
839 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
842 p_object = VLC_OBJECT(p_this->p_libvlc);
845 DumpStructure( vlc_internals(p_object), 0, psz_foo );
847 else if( *psz_cmd == 'v' )
850 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
852 PrintObject( vlc_internals(p_object), "" );
853 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
854 if( vlc_internals( p_object )->var_root == NULL )
855 puts( " `-o No variables" );
857 twalk( vlc_internals( p_object )->var_root, DumpVariable );
858 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
860 libvlc_unlock (p_this->p_libvlc);
862 if( *newval.psz_string )
864 vlc_object_release( p_object );
869 /*****************************************************************************
870 * vlc_list_release: free a list previously allocated by vlc_list_find
871 *****************************************************************************
872 * This function decreases the refcount of all objects in the list and
874 *****************************************************************************/
875 void vlc_list_release( vlc_list_t *p_list )
879 for( i_index = 0; i_index < p_list->i_count; i_index++ )
881 vlc_object_release( p_list->p_values[i_index].p_object );
884 free( p_list->p_values );
888 /* Following functions are local */
890 static vlc_object_t *FindParent (vlc_object_t *p_this, int i_type)
892 for (vlc_object_t *parent = p_this->p_parent;
894 parent = parent->p_parent)
896 if (vlc_internals (parent)->i_object_type == i_type)
897 return vlc_object_hold (parent);
902 static vlc_object_t *FindParentName (vlc_object_t *p_this, const char *name)
904 for (vlc_object_t *parent = p_this->p_parent;
906 parent = parent->p_parent)
908 const char *objname = vlc_internals (parent)->psz_name;
909 if (objname && !strcmp (objname, name))
910 return vlc_object_hold (parent);
915 static vlc_object_t *FindChild (vlc_object_internals_t *priv, int i_type)
917 for (priv = priv->first; priv != NULL; priv = priv->next)
919 if (priv->i_object_type == i_type)
920 return vlc_object_hold (vlc_externals (priv));
922 vlc_object_t *found = FindChild (priv, i_type);
929 static vlc_object_t *FindChildName (vlc_object_internals_t *priv,
932 for (priv = priv->first; priv != NULL; priv = priv->next)
934 if (priv->psz_name && !strcmp (priv->psz_name, name))
935 return vlc_object_hold (vlc_externals (priv));
937 vlc_object_t *found = FindChildName (priv, name);
944 static void PrintObject( vlc_object_internals_t *priv,
945 const char *psz_prefix )
947 char psz_refcount[20], psz_thread[30], psz_name[50], psz_parent[20];
949 int canc = vlc_savecancel ();
950 memset( &psz_name, 0, sizeof(psz_name) );
952 vlc_mutex_lock (&name_lock);
953 if (priv->psz_name != NULL)
955 snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
959 vlc_mutex_unlock (&name_lock);
961 psz_refcount[0] = '\0';
962 if( priv->i_refcount > 0 )
963 snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
965 psz_thread[0] = '\0';
967 snprintf( psz_thread, 29, " (thread %lu)",
968 (unsigned long)priv->thread_id );
970 psz_parent[0] = '\0';
971 /* FIXME: need structure lock!!! */
972 if( vlc_externals(priv)->p_parent )
973 snprintf( psz_parent, 19, ", parent %p",
974 vlc_externals(priv)->p_parent );
976 printf( " %so %p %s%s%s%s%s\n", psz_prefix,
977 vlc_externals(priv), vlc_externals(priv)->psz_object_type,
978 psz_name, psz_thread, psz_refcount, psz_parent );
979 vlc_restorecancel (canc);
982 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
985 char i_back = psz_foo[i_level];
986 psz_foo[i_level] = '\0';
988 PrintObject (priv, psz_foo);
990 psz_foo[i_level] = i_back;
992 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
994 msg_Warn( vlc_externals(priv), "structure tree is too deep" );
998 for (priv = priv->first; priv != NULL; priv = priv->next)
1002 psz_foo[i_level-1] = ' ';
1004 if( psz_foo[i_level-2] == '`' )
1006 psz_foo[i_level-2] = ' ';
1010 psz_foo[i_level] = priv->next ? '|' : '`';
1011 psz_foo[i_level+1] = '-';
1012 psz_foo[i_level+2] = '\0';
1014 DumpStructure (priv, i_level + 2, psz_foo);
1018 static vlc_list_t * NewList( int i_count )
1020 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
1021 if( p_list == NULL )
1024 p_list->i_count = i_count;
1028 p_list->p_values = NULL;
1032 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1033 if( p_list->p_values == NULL )
1035 p_list->i_count = 0;