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 /* Initialize mutexes and condvars */
164 vlc_mutex_init( &p_priv
->var_lock
);
165 vlc_cond_init( &p_priv
->var_wait
);
166 p_priv
->pipes
[0] = p_priv
->pipes
[1] = -1;
168 if (p_new
== VLC_OBJECT(p_new
->p_libvlc
))
169 { /* TODO: should be in src/libvlc.c */
170 int canc
= vlc_savecancel ();
171 var_Create( p_new
, "tree", VLC_VAR_STRING
| VLC_VAR_ISCOMMAND
);
172 var_AddCallback( p_new
, "tree", DumpCommand
, NULL
);
173 var_Create( p_new
, "vars", VLC_VAR_STRING
| VLC_VAR_ISCOMMAND
);
174 var_AddCallback( p_new
, "vars", DumpCommand
, NULL
);
175 vlc_restorecancel (canc
);
181 #undef vlc_object_create
183 * Allocates and initializes a vlc object.
185 * @param i_size object byte size
187 * @return the new object, or NULL on error.
189 void *vlc_object_create( vlc_object_t
*p_this
, size_t i_size
)
191 return vlc_custom_create( p_this
, i_size
, VLC_OBJECT_GENERIC
, "generic" );
194 #undef vlc_object_set_destructor
196 ****************************************************************************
197 * Set the destructor of a vlc object
199 * This function sets the destructor of the vlc object. It will be called
200 * when the object is destroyed when the its refcount reaches 0.
201 * (It is called by the internal function vlc_object_destroy())
202 *****************************************************************************/
203 void vlc_object_set_destructor( vlc_object_t
*p_this
,
204 vlc_destructor_t pf_destructor
)
206 vlc_object_internals_t
*p_priv
= vlc_internals(p_this
);
208 vlc_spin_lock( &p_priv
->ref_spin
);
209 p_priv
->pf_destructor
= pf_destructor
;
210 vlc_spin_unlock( &p_priv
->ref_spin
);
213 static vlc_mutex_t name_lock
= VLC_STATIC_MUTEX
;
215 #undef vlc_object_set_name
216 int vlc_object_set_name(vlc_object_t
*obj
, const char *name
)
218 vlc_object_internals_t
*priv
= vlc_internals(obj
);
219 char *newname
= name
? strdup (name
) : NULL
;
222 vlc_mutex_lock (&name_lock
);
223 oldname
= priv
->psz_name
;
224 priv
->psz_name
= newname
;
225 vlc_mutex_unlock (&name_lock
);
228 return (priv
->psz_name
|| !name
) ? VLC_SUCCESS
: VLC_ENOMEM
;
231 #undef vlc_object_get_name
232 char *vlc_object_get_name(const vlc_object_t
*obj
)
234 vlc_object_internals_t
*priv
= vlc_internals(obj
);
237 vlc_mutex_lock (&name_lock
);
238 name
= priv
->psz_name
? strdup (priv
->psz_name
) : NULL
;
239 vlc_mutex_unlock (&name_lock
);
245 ****************************************************************************
246 * Destroy a vlc object (Internal)
248 * This function destroys an object that has been previously allocated with
249 * vlc_object_create. The object's refcount must be zero and it must not be
250 * attached to other objects in any way.
252 * This function must be called with cancellation disabled (currently).
253 *****************************************************************************/
254 static void vlc_object_destroy( vlc_object_t
*p_this
)
256 vlc_object_internals_t
*p_priv
= vlc_internals( p_this
);
258 /* Objects are always detached beforehand */
259 assert( !p_this
->p_parent
);
261 /* Send a kill to the object's thread if applicable */
262 vlc_object_kill( p_this
);
264 /* Call the custom "subclass" destructor */
265 if( p_priv
->pf_destructor
)
266 p_priv
->pf_destructor( p_this
);
268 /* Any thread must have been cleaned up at this point. */
269 assert( !p_priv
->b_thread
);
271 /* Destroy the associated variables. */
272 var_DestroyAll( p_this
);
274 vlc_cond_destroy( &p_priv
->var_wait
);
275 vlc_mutex_destroy( &p_priv
->var_lock
);
277 free( p_this
->psz_header
);
279 free( p_priv
->psz_name
);
281 vlc_spin_destroy( &p_priv
->ref_spin
);
282 if( p_priv
->pipes
[1] != -1 && p_priv
->pipes
[1] != p_priv
->pipes
[0] )
283 close( p_priv
->pipes
[1] );
284 if( p_priv
->pipes
[0] != -1 )
285 close( p_priv
->pipes
[0] );
286 if( VLC_OBJECT(p_this
->p_libvlc
) == p_this
)
287 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t
*)p_this
)->structure_lock
));
295 * select()-able pipes emulated using Winsock
297 static int pipe (int fd
[2])
300 int addrlen
= sizeof (addr
);
302 SOCKET l
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
), a
,
303 c
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
304 if ((l
== INVALID_SOCKET
) || (c
== INVALID_SOCKET
))
307 memset (&addr
, 0, sizeof (addr
));
308 addr
.sin_family
= AF_INET
;
309 addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
310 if (bind (l
, (PSOCKADDR
)&addr
, sizeof (addr
))
311 || getsockname (l
, (PSOCKADDR
)&addr
, &addrlen
)
313 || connect (c
, (PSOCKADDR
)&addr
, addrlen
))
316 a
= accept (l
, NULL
, NULL
);
317 if (a
== INVALID_SOCKET
)
328 if (l
!= INVALID_SOCKET
)
330 if (c
!= INVALID_SOCKET
)
336 static vlc_mutex_t pipe_lock
= VLC_STATIC_MUTEX
;
339 * Returns the readable end of a pipe that becomes readable once termination
340 * of the object is requested (vlc_object_kill()).
341 * This can be used to wake-up out of a select() or poll() event loop, such
342 * typically when doing network I/O.
344 * Note that the pipe will remain the same for the lifetime of the object.
345 * DO NOT read the pipe nor close it yourself. Ever.
347 * @param obj object that would be "killed"
348 * @return a readable pipe descriptor, or -1 on error.
350 int vlc_object_waitpipe( vlc_object_t
*obj
)
352 vlc_object_internals_t
*internals
= vlc_internals( obj
);
354 vlc_mutex_lock (&pipe_lock
);
355 if (internals
->pipes
[0] == -1)
357 /* This can only ever happen if someone killed us without locking: */
358 assert (internals
->pipes
[1] == -1);
360 /* pipe() is not a cancellation point, but write() is and eventfd() is
361 * unspecified (not in POSIX). */
362 int canc
= vlc_savecancel ();
363 #if defined (HAVE_SYS_EVENTFD_H)
364 internals
->pipes
[0] = internals
->pipes
[1] = eventfd (0, EFD_CLOEXEC
);
365 if (internals
->pipes
[0] == -1)
368 if (pipe (internals
->pipes
))
369 internals
->pipes
[0] = internals
->pipes
[1] = -1;
372 if (internals
->pipes
[0] != -1 && obj
->b_die
)
373 { /* Race condition: vlc_object_kill() already invoked! */
374 msg_Dbg (obj
, "waitpipe: object already dying");
375 write (internals
->pipes
[1], &(uint64_t){ 1 }, sizeof (uint64_t));
377 vlc_restorecancel (canc
);
379 vlc_mutex_unlock (&pipe_lock
);
380 return internals
->pipes
[0];
383 #undef vlc_object_kill
385 * Requests termination of an object, cancels the object thread, and make the
386 * object wait pipe (if it exists) readable. Not a cancellation point.
388 void vlc_object_kill( vlc_object_t
*p_this
)
390 vlc_object_internals_t
*priv
= vlc_internals( p_this
);
393 vlc_thread_cancel( p_this
);
394 vlc_mutex_lock( &pipe_lock
);
398 p_this
->b_die
= true;
401 /* This also serves as a memory barrier toward vlc_object_alive(): */
402 vlc_mutex_unlock( &pipe_lock
);
406 int canc
= vlc_savecancel ();
408 /* write _after_ setting b_die, so vlc_object_alive() returns false */
409 write (fd
, &(uint64_t){ 1 }, sizeof (uint64_t));
410 msg_Dbg (p_this
, "waitpipe: object killed");
411 vlc_restorecancel (canc
);
415 #undef vlc_object_find
416 /*****************************************************************************
417 * find a typed object and increment its refcount
418 *****************************************************************************
419 * This function recursively looks for a given object type. i_mode can be one
420 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
421 *****************************************************************************/
422 void * vlc_object_find( vlc_object_t
*p_this
, int i_type
, int i_mode
)
424 vlc_object_t
*p_found
;
426 /* If we are of the requested type ourselves, don't look further */
427 if( vlc_internals (p_this
)->i_object_type
== i_type
)
429 vlc_object_hold( p_this
);
433 /* Otherwise, recursively look for the object */
434 if (i_mode
== FIND_ANYWHERE
)
435 return vlc_object_find (VLC_OBJECT(p_this
->p_libvlc
), i_type
, FIND_CHILD
);
439 case VLC_OBJECT_VOUT
:
440 case VLC_OBJECT_AOUT
:
442 case VLC_OBJECT_INPUT
:
443 /* input can only be accessed like this from children,
444 * otherwise we could not promise that it is initialized */
445 if (i_mode
!= FIND_PARENT
)
452 libvlc_lock (p_this
->p_libvlc
);
456 p_found
= FindParent (p_this
, i_type
);
459 p_found
= FindChild (vlc_internals (p_this
), i_type
);
464 libvlc_unlock (p_this
->p_libvlc
);
469 static int objnamecmp(const vlc_object_t
*obj
, const char *name
)
471 char *objname
= vlc_object_get_name(obj
);
475 int ret
= strcmp (objname
, name
);
480 #undef vlc_object_find_name
482 * Finds a named object and increment its reference count.
483 * Beware that objects found in this manner can be "owned" by another thread,
484 * be of _any_ type, and be attached to any module (if any). With such an
485 * object reference, you can set or get object variables, emit log messages,
486 * and read write-once object parameters (psz_object_type, etc).
487 * You CANNOT cast the object to a more specific object type, and you
488 * definitely cannot invoke object type-specific callbacks with this.
490 * @param p_this object to search from
491 * @param psz_name name of the object to search for
492 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
494 * @return a matching object (must be released by the caller),
497 vlc_object_t
*vlc_object_find_name( vlc_object_t
*p_this
,
498 const char *psz_name
, int i_mode
)
500 vlc_object_t
*p_found
;
502 /* Reading psz_object_name from a separate inhibits thread-safety.
503 * Use a libvlc address variable instead for that sort of things! */
504 msg_Warn( p_this
, "%s(%s) is not safe!", __func__
, psz_name
);
505 /* If have the requested name ourselves, don't look further */
506 if( !objnamecmp(p_this
, psz_name
) )
508 vlc_object_hold( p_this
);
512 /* Otherwise, recursively look for the object */
513 if (i_mode
== FIND_ANYWHERE
)
514 return vlc_object_find_name (VLC_OBJECT(p_this
->p_libvlc
), psz_name
,
517 libvlc_lock (p_this
->p_libvlc
);
518 vlc_mutex_lock (&name_lock
);
522 p_found
= FindParentName (p_this
, psz_name
);
525 p_found
= FindChildName (vlc_internals (p_this
), psz_name
);
530 vlc_mutex_unlock (&name_lock
);
531 libvlc_unlock (p_this
->p_libvlc
);
535 #undef vlc_object_hold
537 * Increment an object reference counter.
539 void * vlc_object_hold( vlc_object_t
*p_this
)
541 vlc_object_internals_t
*internals
= vlc_internals( p_this
);
543 vlc_spin_lock( &internals
->ref_spin
);
544 /* Avoid obvious freed object uses */
545 assert( internals
->i_refcount
> 0 );
546 /* Increment the counter */
547 internals
->i_refcount
++;
548 vlc_spin_unlock( &internals
->ref_spin
);
552 #undef vlc_object_release
553 /*****************************************************************************
554 * Decrement an object refcount
555 * And destroy the object if its refcount reach zero.
556 *****************************************************************************/
557 void vlc_object_release( vlc_object_t
*p_this
)
559 vlc_object_internals_t
*internals
= vlc_internals( p_this
);
560 vlc_object_t
*parent
= NULL
;
561 bool b_should_destroy
;
563 vlc_spin_lock( &internals
->ref_spin
);
564 assert( internals
->i_refcount
> 0 );
566 if( internals
->i_refcount
> 1 )
569 /* There are still other references to the object */
570 internals
->i_refcount
--;
571 vlc_spin_unlock( &internals
->ref_spin
);
574 vlc_spin_unlock( &internals
->ref_spin
);
577 /* Remember that we cannot hold the spin while waiting on the mutex */
578 libvlc_lock (p_this
->p_libvlc
);
579 /* Take the spin again. Note that another thread may have held the
580 * object in the (very short) mean time. */
581 vlc_spin_lock( &internals
->ref_spin
);
582 b_should_destroy
= --internals
->i_refcount
== 0;
583 vlc_spin_unlock( &internals
->ref_spin
);
585 if( b_should_destroy
)
587 parent
= p_this
->p_parent
;
589 /* Detach from parent to protect against FIND_CHILDREN */
590 vlc_object_detach_unlocked (p_this
);
592 /* We have no children */
593 assert (internals
->first
== NULL
);
595 libvlc_unlock (p_this
->p_libvlc
);
597 if( b_should_destroy
)
601 canc
= vlc_savecancel ();
602 vlc_object_destroy( p_this
);
603 vlc_restorecancel (canc
);
605 vlc_object_release (parent
);
609 #undef vlc_object_attach
611 ****************************************************************************
612 * attach object to a parent object
613 *****************************************************************************
614 * This function sets p_this as a child of p_parent, and p_parent as a parent
616 *****************************************************************************/
617 void vlc_object_attach( vlc_object_t
*p_this
, vlc_object_t
*p_parent
)
619 if( !p_this
) return;
621 vlc_object_internals_t
*pap
= vlc_internals (p_parent
);
622 vlc_object_internals_t
*priv
= vlc_internals (p_this
);
625 vlc_object_hold (p_parent
);
626 libvlc_lock (p_this
->p_libvlc
);
628 /* Attach the parent to its child */
629 assert (p_this
->p_parent
== NULL
);
630 p_this
->p_parent
= p_parent
;
632 /* Attach the child to its parent */
633 priv
->next
= pap
->first
;
634 if (priv
->next
!= NULL
)
635 priv
->next
->prev
= priv
;
637 libvlc_unlock (p_this
->p_libvlc
);
641 static void vlc_object_detach_unlocked (vlc_object_t
*p_this
)
643 assert (p_this
->p_parent
!= NULL
);
645 vlc_object_internals_t
*pap
= vlc_internals (p_this
->p_parent
);
646 vlc_object_internals_t
*priv
= vlc_internals (p_this
);
649 if (priv
->prev
!= NULL
)
650 priv
->prev
->next
= priv
->next
;
652 pap
->first
= priv
->next
;
653 if (priv
->next
!= NULL
)
654 priv
->next
->prev
= priv
->prev
;
656 /* Remove p_this's parent */
657 p_this
->p_parent
= NULL
;
660 #undef vlc_list_children
662 * Gets the list of children of an objects, and increment their reference
664 * @return a list (possibly empty) or NULL in case of error.
666 vlc_list_t
*vlc_list_children( vlc_object_t
*obj
)
669 vlc_object_internals_t
*priv
;
672 libvlc_lock (obj
->p_libvlc
);
673 for (priv
= vlc_internals (obj
)->first
; priv
; priv
= priv
->next
)
676 if (likely(l
!= NULL
))
680 for (priv
= vlc_internals (obj
)->first
; priv
; priv
= priv
->next
)
681 l
->p_values
[i
++].p_object
= vlc_object_hold (vlc_externals (priv
));
683 libvlc_unlock (obj
->p_libvlc
);
687 static void DumpVariable (const void *data
, const VISIT which
, const int depth
)
689 if (which
!= postorder
&& which
!= leaf
)
693 const variable_t
*p_var
= *(const variable_t
**)data
;
694 const char *psz_type
= "unknown";
696 switch( p_var
->i_type
& VLC_VAR_TYPE
)
698 #define MYCASE( type, nice ) \
699 case VLC_VAR_ ## type: \
702 MYCASE( VOID
, "void" );
703 MYCASE( BOOL
, "bool" );
704 MYCASE( INTEGER
, "integer" );
705 MYCASE( HOTKEY
, "hotkey" );
706 MYCASE( STRING
, "string" );
707 MYCASE( MODULE
, "module" );
708 MYCASE( FILE, "file" );
709 MYCASE( DIRECTORY
, "directory" );
710 MYCASE( VARIABLE
, "variable" );
711 MYCASE( FLOAT
, "float" );
712 MYCASE( TIME
, "time" );
713 MYCASE( COORDS
, "coords" );
714 MYCASE( ADDRESS
, "address" );
715 MYCASE( MUTEX
, "mutex" );
716 MYCASE( LIST
, "list" );
719 printf( " *-o \"%s\" (%s", p_var
->psz_name
, psz_type
);
720 if( p_var
->psz_text
)
721 printf( ", %s", p_var
->psz_text
);
722 fputc( ')', stdout
);
723 if( p_var
->i_type
& VLC_VAR_HASCHOICE
)
724 fputs( ", has choices", stdout
);
725 if( p_var
->i_type
& VLC_VAR_ISCOMMAND
)
726 fputs( ", command", stdout
);
727 if( p_var
->i_entries
)
728 printf( ", %d callbacks", p_var
->i_entries
);
729 switch( p_var
->i_type
& VLC_VAR_CLASS
)
735 printf( ": %s", p_var
->val
.b_bool
? "true" : "false" );
737 case VLC_VAR_INTEGER
:
738 printf( ": %d", p_var
->val
.i_int
);
741 printf( ": \"%s\"", p_var
->val
.psz_string
);
744 printf( ": %f", p_var
->val
.f_float
);
747 printf( ": %"PRIi64
, (int64_t)p_var
->val
.i_time
);
750 printf( ": %"PRId32
"x%"PRId32
,
751 p_var
->val
.coords
.x
, p_var
->val
.coords
.y
);
753 case VLC_VAR_ADDRESS
:
754 printf( ": %p", p_var
->val
.p_address
);
757 fputs( ": TODO", stdout
);
760 fputc( '\n', stdout
);
763 /*****************************************************************************
764 * DumpCommand: print the current vlc structure
765 *****************************************************************************
766 * This function prints either an ASCII tree showing the connections between
767 * vlc objects, and additional information such as their refcount, thread ID,
768 * etc. (command "tree"), or the same data as a simple list (command "list").
769 *****************************************************************************/
770 static int DumpCommand( vlc_object_t
*p_this
, char const *psz_cmd
,
771 vlc_value_t oldval
, vlc_value_t newval
, void *p_data
)
773 (void)oldval
; (void)p_data
;
774 vlc_object_t
*p_object
= NULL
;
776 if( *newval
.psz_string
)
778 /* try using the object's name to find it */
779 p_object
= vlc_object_find_name( p_this
, newval
.psz_string
,
787 libvlc_lock (p_this
->p_libvlc
);
788 if( *psz_cmd
== 't' )
790 char psz_foo
[2 * MAX_DUMPSTRUCTURE_DEPTH
+ 1];
793 p_object
= VLC_OBJECT(p_this
->p_libvlc
);
796 DumpStructure( vlc_internals(p_object
), 0, psz_foo
);
798 else if( *psz_cmd
== 'v' )
801 p_object
= p_this
->p_libvlc
? VLC_OBJECT(p_this
->p_libvlc
) : p_this
;
803 PrintObject( vlc_internals(p_object
), "" );
804 vlc_mutex_lock( &vlc_internals( p_object
)->var_lock
);
805 if( vlc_internals( p_object
)->var_root
== NULL
)
806 puts( " `-o No variables" );
808 twalk( vlc_internals( p_object
)->var_root
, DumpVariable
);
809 vlc_mutex_unlock( &vlc_internals( p_object
)->var_lock
);
811 libvlc_unlock (p_this
->p_libvlc
);
813 if( *newval
.psz_string
)
815 vlc_object_release( p_object
);
820 /*****************************************************************************
821 * vlc_list_release: free a list previously allocated by vlc_list_find
822 *****************************************************************************
823 * This function decreases the refcount of all objects in the list and
825 *****************************************************************************/
826 void vlc_list_release( vlc_list_t
*p_list
)
830 for( i_index
= 0; i_index
< p_list
->i_count
; i_index
++ )
832 vlc_object_release( p_list
->p_values
[i_index
].p_object
);
835 free( p_list
->p_values
);
839 /* Following functions are local */
841 static vlc_object_t
*FindParent (vlc_object_t
*p_this
, int i_type
)
843 for (vlc_object_t
*parent
= p_this
->p_parent
;
845 parent
= parent
->p_parent
)
847 if (vlc_internals (parent
)->i_object_type
== i_type
)
848 return vlc_object_hold (parent
);
853 static vlc_object_t
*FindParentName (vlc_object_t
*p_this
, const char *name
)
855 for (vlc_object_t
*parent
= p_this
->p_parent
;
857 parent
= parent
->p_parent
)
859 const char *objname
= vlc_internals (parent
)->psz_name
;
860 if (objname
&& !strcmp (objname
, name
))
861 return vlc_object_hold (parent
);
866 static vlc_object_t
*FindChild (vlc_object_internals_t
*priv
, int i_type
)
868 for (priv
= priv
->first
; priv
!= NULL
; priv
= priv
->next
)
870 if (priv
->i_object_type
== i_type
)
871 return vlc_object_hold (vlc_externals (priv
));
873 vlc_object_t
*found
= FindChild (priv
, i_type
);
880 static vlc_object_t
*FindChildName (vlc_object_internals_t
*priv
,
883 for (priv
= priv
->first
; priv
!= NULL
; priv
= priv
->next
)
885 if (priv
->psz_name
&& !strcmp (priv
->psz_name
, name
))
886 return vlc_object_hold (vlc_externals (priv
));
888 vlc_object_t
*found
= FindChildName (priv
, name
);
895 static void PrintObject( vlc_object_internals_t
*priv
,
896 const char *psz_prefix
)
898 char psz_refcount
[20], psz_thread
[30], psz_name
[50], psz_parent
[20];
900 int canc
= vlc_savecancel ();
901 memset( &psz_name
, 0, sizeof(psz_name
) );
903 vlc_mutex_lock (&name_lock
);
904 if (priv
->psz_name
!= NULL
)
906 snprintf( psz_name
, 49, " \"%s\"", priv
->psz_name
);
910 vlc_mutex_unlock (&name_lock
);
912 psz_refcount
[0] = '\0';
913 if( priv
->i_refcount
> 0 )
914 snprintf( psz_refcount
, 19, ", %u refs", priv
->i_refcount
);
916 psz_thread
[0] = '\0';
918 snprintf( psz_thread
, 29, " (thread %lu)",
919 (unsigned long)priv
->thread_id
);
921 psz_parent
[0] = '\0';
922 /* FIXME: need structure lock!!! */
923 if( vlc_externals(priv
)->p_parent
)
924 snprintf( psz_parent
, 19, ", parent %p",
925 vlc_externals(priv
)->p_parent
);
927 printf( " %so %p %s%s%s%s%s\n", psz_prefix
,
928 vlc_externals(priv
), vlc_externals(priv
)->psz_object_type
,
929 psz_name
, psz_thread
, psz_refcount
, psz_parent
);
930 vlc_restorecancel (canc
);
933 static void DumpStructure (vlc_object_internals_t
*priv
, unsigned i_level
,
936 char i_back
= psz_foo
[i_level
];
937 psz_foo
[i_level
] = '\0';
939 PrintObject (priv
, psz_foo
);
941 psz_foo
[i_level
] = i_back
;
943 if( i_level
/ 2 >= MAX_DUMPSTRUCTURE_DEPTH
)
945 msg_Warn( vlc_externals(priv
), "structure tree is too deep" );
949 for (priv
= priv
->first
; priv
!= NULL
; priv
= priv
->next
)
953 psz_foo
[i_level
-1] = ' ';
955 if( psz_foo
[i_level
-2] == '`' )
957 psz_foo
[i_level
-2] = ' ';
961 psz_foo
[i_level
] = priv
->next
? '|' : '`';
962 psz_foo
[i_level
+1] = '-';
963 psz_foo
[i_level
+2] = '\0';
965 DumpStructure (priv
, i_level
+ 2, psz_foo
);
969 static vlc_list_t
* NewList( int i_count
)
971 vlc_list_t
* p_list
= malloc( sizeof( vlc_list_t
) );
975 p_list
->i_count
= i_count
;
979 p_list
->p_values
= NULL
;
983 p_list
->p_values
= malloc( i_count
* sizeof( vlc_value_t
) );
984 if( p_list
->p_values
== NULL
)