]> git.sesse.net Git - vlc/blob - src/misc/objects.c
vlc_object_waitpipe: cancellation safety
[vlc] / src / misc / objects.c
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
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /**
25  * \file
26  * This file contains the functions to handle the vlc_object_t type
27  *
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.
30  */
31
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <vlc_common.h>
41
42 #include "../libvlc.h"
43 #include <vlc_aout.h>
44 #include "audio_output/aout_internal.h"
45
46 #include "vlc_interface.h"
47 #include "vlc_codec.h"
48
49 #include "variables.h"
50 #ifndef WIN32
51 # include <unistd.h>
52 #else
53 # include <io.h>
54 # include <winsock2.h>
55 # include <ws2tcpip.h>
56 # undef  read
57 # define read( a, b, c )  recv (a, b, c, 0)
58 # undef  write
59 # define write( a, b, c ) send (a, b, c, 0)
60 # undef  close
61 # define close( a )       closesocket (a)
62 #endif
63
64 #include <search.h>
65 #include <limits.h>
66 #include <assert.h>
67
68 #if defined (HAVE_SYS_EVENTFD_H)
69 # include <sys/eventfd.h>
70 #endif
71
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76 static int  DumpCommand( vlc_object_t *, char const *,
77                          vlc_value_t, vlc_value_t, void * );
78
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 * );
85
86 static vlc_list_t   * NewList       ( int );
87
88 static void vlc_object_destroy( vlc_object_t *p_this );
89 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
90
91 /*****************************************************************************
92  * Local structure lock
93  *****************************************************************************/
94 static void libvlc_lock (libvlc_int_t *p_libvlc)
95 {
96     vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
97 }
98
99 static void libvlc_unlock (libvlc_int_t *p_libvlc)
100 {
101     vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
102 }
103
104 #undef vlc_custom_create
105 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
106                          int i_type, const char *psz_type )
107 {
108     vlc_object_t *p_new;
109     vlc_object_internals_t *p_priv;
110
111     /* NOTE:
112      * VLC objects are laid out as follow:
113      * - first the LibVLC-private per-object data,
114      * - then VLC_COMMON members from vlc_object_t,
115      * - finally, the type-specific data (if any).
116      *
117      * This function initializes the LibVLC and common data,
118      * and zeroes the rest.
119      */
120     p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
121     if( p_priv == NULL )
122         return NULL;
123
124     assert (i_size >= sizeof (vlc_object_t));
125     p_new = (vlc_object_t *)(p_priv + 1);
126
127     p_priv->i_object_type = i_type;
128     p_new->psz_object_type = psz_type;
129     p_priv->psz_name = NULL;
130
131     p_new->b_die = false;
132     p_new->b_force = false;
133
134     p_new->psz_header = NULL;
135
136     if (p_this)
137         p_new->i_flags = p_this->i_flags
138             & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
139
140     p_priv->var_root = NULL;
141
142     if( p_this == NULL )
143     {
144         libvlc_int_t *self = (libvlc_int_t*)p_new;
145         p_new->p_libvlc = self;
146         vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
147         p_this = p_new;
148     }
149     else
150         p_new->p_libvlc = p_this->p_libvlc;
151
152     vlc_spin_init( &p_priv->ref_spin );
153     p_priv->i_refcount = 1;
154     p_priv->pf_destructor = NULL;
155     p_priv->b_thread = false;
156     p_new->p_parent = NULL;
157     p_priv->first = NULL;
158 #ifndef NDEBUG
159     p_priv->old_parent = NULL;
160 #endif
161
162     /* Initialize mutexes and condvars */
163     vlc_mutex_init( &p_priv->var_lock );
164     vlc_cond_init( &p_priv->var_wait );
165     p_priv->pipes[0] = p_priv->pipes[1] = -1;
166
167     if (p_new == VLC_OBJECT(p_new->p_libvlc))
168     {   /* TODO: should be in src/libvlc.c */
169         int canc = vlc_savecancel ();
170         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
171         var_AddCallback( p_new, "tree", DumpCommand, NULL );
172         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
173         var_AddCallback( p_new, "vars", DumpCommand, NULL );
174         vlc_restorecancel (canc);
175     }
176
177     return p_new;
178 }
179
180 #undef vlc_object_create
181 /**
182  * Allocates and initializes a vlc object.
183  *
184  * @param i_size object byte size
185  *
186  * @return the new object, or NULL on error.
187  */
188 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
189 {
190     return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
191 }
192
193 #undef vlc_object_set_destructor
194 /**
195  ****************************************************************************
196  * Set the destructor of a vlc object
197  *
198  * This function sets the destructor of the vlc object. It will be called
199  * when the object is destroyed when the its refcount reaches 0.
200  * (It is called by the internal function vlc_object_destroy())
201  *****************************************************************************/
202 void vlc_object_set_destructor( vlc_object_t *p_this,
203                                 vlc_destructor_t pf_destructor )
204 {
205     vlc_object_internals_t *p_priv = vlc_internals(p_this );
206
207     vlc_spin_lock( &p_priv->ref_spin );
208     p_priv->pf_destructor = pf_destructor;
209     vlc_spin_unlock( &p_priv->ref_spin );
210 }
211
212 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
213
214 #undef vlc_object_set_name
215 int vlc_object_set_name(vlc_object_t *obj, const char *name)
216 {
217     vlc_object_internals_t *priv = vlc_internals(obj);
218     char *newname = name ? strdup (name) : NULL;
219     char *oldname;
220
221     vlc_mutex_lock (&name_lock);
222     oldname = priv->psz_name;
223     priv->psz_name = newname;
224     vlc_mutex_unlock (&name_lock);
225
226     free (oldname);
227     return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
228 }
229
230 #undef vlc_object_get_name
231 char *vlc_object_get_name(const vlc_object_t *obj)
232 {
233     vlc_object_internals_t *priv = vlc_internals(obj);
234     char *name;
235
236     vlc_mutex_lock (&name_lock);
237     name = priv->psz_name ? strdup (priv->psz_name) : NULL;
238     vlc_mutex_unlock (&name_lock);
239
240     return name;
241 }
242
243 /**
244  ****************************************************************************
245  * Destroy a vlc object (Internal)
246  *
247  * This function destroys an object that has been previously allocated with
248  * vlc_object_create. The object's refcount must be zero and it must not be
249  * attached to other objects in any way.
250  *
251  * This function must be called with cancellation disabled (currently).
252  *****************************************************************************/
253 static void vlc_object_destroy( vlc_object_t *p_this )
254 {
255     vlc_object_internals_t *p_priv = vlc_internals( p_this );
256
257     /* Objects are always detached beforehand */
258     assert( !p_this->p_parent );
259
260     /* Send a kill to the object's thread if applicable */
261     vlc_object_kill( p_this );
262
263     /* Call the custom "subclass" destructor */
264     if( p_priv->pf_destructor )
265         p_priv->pf_destructor( p_this );
266
267     /* Any thread must have been cleaned up at this point. */
268     assert( !p_priv->b_thread );
269
270     /* Destroy the associated variables. */
271     var_DestroyAll( p_this );
272
273     vlc_cond_destroy( &p_priv->var_wait );
274     vlc_mutex_destroy( &p_priv->var_lock );
275
276     free( p_this->psz_header );
277
278     free( p_priv->psz_name );
279
280     vlc_spin_destroy( &p_priv->ref_spin );
281     if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
282         close( p_priv->pipes[1] );
283     if( p_priv->pipes[0] != -1 )
284         close( p_priv->pipes[0] );
285     if( VLC_OBJECT(p_this->p_libvlc) == p_this )
286         vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
287
288     free( p_priv );
289 }
290
291
292 #ifdef WIN32
293 /**
294  * select()-able pipes emulated using Winsock
295  */
296 static int pipe (int fd[2])
297 {
298     SOCKADDR_IN addr;
299     int addrlen = sizeof (addr);
300
301     SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
302            c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
303     if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
304         goto error;
305
306     memset (&addr, 0, sizeof (addr));
307     addr.sin_family = AF_INET;
308     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
309     if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
310      || getsockname (l, (PSOCKADDR)&addr, &addrlen)
311      || listen (l, 1)
312      || connect (c, (PSOCKADDR)&addr, addrlen))
313         goto error;
314
315     a = accept (l, NULL, NULL);
316     if (a == INVALID_SOCKET)
317         goto error;
318
319     closesocket (l);
320     //shutdown (a, 0);
321     //shutdown (c, 1);
322     fd[0] = c;
323     fd[1] = a;
324     return 0;
325
326 error:
327     if (l != INVALID_SOCKET)
328         closesocket (l);
329     if (c != INVALID_SOCKET)
330         closesocket (c);
331     return -1;
332 }
333 #endif /* WIN32 */
334
335 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
336
337 /**
338  * Returns the readable end of a pipe that becomes readable once termination
339  * of the object is requested (vlc_object_kill()).
340  * This can be used to wake-up out of a select() or poll() event loop, such
341  * typically when doing network I/O.
342  *
343  * Note that the pipe will remain the same for the lifetime of the object.
344  * DO NOT read the pipe nor close it yourself. Ever.
345  *
346  * @param obj object that would be "killed"
347  * @return a readable pipe descriptor, or -1 on error.
348  */
349 int vlc_object_waitpipe( vlc_object_t *obj )
350 {
351     vlc_object_internals_t *internals = vlc_internals( obj );
352
353     vlc_mutex_lock (&pipe_lock);
354     if (internals->pipes[0] == -1)
355     {
356         /* This can only ever happen if someone killed us without locking: */
357         assert (internals->pipes[1] == -1);
358
359         /* pipe() is not a cancellation point, but write() is and eventfd() is
360          * unspecified (not in POSIX). */
361         int canc = vlc_savecancel ();
362 #if defined (HAVE_SYS_EVENTFD_H)
363         internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC);
364         if (internals->pipes[0] == -1)
365 #endif
366         {
367             if (pipe (internals->pipes))
368                 internals->pipes[0] = internals->pipes[1] = -1;
369         }
370
371         if (internals->pipes[0] != -1 && obj->b_die)
372         {   /* Race condition: vlc_object_kill() already invoked! */
373             msg_Dbg (obj, "waitpipe: object already dying");
374             write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
375         }
376         vlc_restorecancel (canc);
377     }
378     vlc_mutex_unlock (&pipe_lock);
379     return internals->pipes[0];
380 }
381
382 #undef vlc_object_kill
383 /**
384  * Requests termination of an object, cancels the object thread, and make the
385  * object wait pipe (if it exists) readable. Not a cancellation point.
386  */
387 void vlc_object_kill( vlc_object_t *p_this )
388 {
389     vlc_object_internals_t *priv = vlc_internals( p_this );
390     int fd = -1;
391
392     vlc_thread_cancel( p_this );
393     vlc_mutex_lock( &pipe_lock );
394     if( !p_this->b_die )
395     {
396         fd = priv->pipes[1];
397         p_this->b_die = true;
398     }
399
400     /* This also serves as a memory barrier toward vlc_object_alive(): */
401     vlc_mutex_unlock( &pipe_lock );
402
403     if (fd != -1)
404     {
405         int canc = vlc_savecancel ();
406
407         /* write _after_ setting b_die, so vlc_object_alive() returns false */
408         write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
409         msg_Dbg (p_this, "waitpipe: object killed");
410         vlc_restorecancel (canc);
411     }
412 }
413
414 #undef vlc_object_find
415 /*****************************************************************************
416  * find a typed object and increment its refcount
417  *****************************************************************************
418  * This function recursively looks for a given object type. i_mode can be one
419  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
420  *****************************************************************************/
421 void * vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
422 {
423     vlc_object_t *p_found;
424
425     /* If we are of the requested type ourselves, don't look further */
426     if( vlc_internals (p_this)->i_object_type == i_type )
427     {
428         vlc_object_hold( p_this );
429         return p_this;
430     }
431
432     /* Otherwise, recursively look for the object */
433     if (i_mode == FIND_ANYWHERE)
434         return vlc_object_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
435
436     switch (i_type)
437     {
438         case VLC_OBJECT_VOUT:
439         case VLC_OBJECT_AOUT:
440             break;
441         case VLC_OBJECT_INPUT:
442             /* input can only be accessed like this from children,
443              * otherwise we could not promise that it is initialized */
444             if (i_mode != FIND_PARENT)
445                 return NULL;
446             break;
447         default:
448             return NULL;
449     }
450
451     libvlc_lock (p_this->p_libvlc);
452     switch (i_mode)
453     {
454         case FIND_PARENT:
455             p_found = FindParent (p_this, i_type);
456             break;
457         case FIND_CHILD:
458             p_found = FindChild (vlc_internals (p_this), i_type);
459             break;
460         default:
461             assert (0);
462     }
463     libvlc_unlock (p_this->p_libvlc);
464     return p_found;
465 }
466
467
468 static int objnamecmp(const vlc_object_t *obj, const char *name)
469 {
470     char *objname = vlc_object_get_name(obj);
471     if (objname == NULL)
472         return INT_MIN;
473
474     int ret = strcmp (objname, name);
475     free (objname);
476     return ret;
477 }
478
479 #undef vlc_object_find_name
480 /**
481  * Finds a named object and increment its reference count.
482  * Beware that objects found in this manner can be "owned" by another thread,
483  * be of _any_ type, and be attached to any module (if any). With such an
484  * object reference, you can set or get object variables, emit log messages,
485  * and read write-once object parameters (psz_object_type, etc).
486  * You CANNOT cast the object to a more specific object type, and you
487  * definitely cannot invoke object type-specific callbacks with this.
488  *
489  * @param p_this object to search from
490  * @param psz_name name of the object to search for
491  * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
492  *
493  * @return a matching object (must be released by the caller),
494  * or NULL on error.
495  */
496 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
497                                     const char *psz_name, int i_mode )
498 {
499     vlc_object_t *p_found;
500
501     /* Reading psz_object_name from a separate inhibits thread-safety.
502      * Use a libvlc address variable instead for that sort of things! */
503     msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
504     /* If have the requested name ourselves, don't look further */
505     if( !objnamecmp(p_this, psz_name) )
506     {
507         vlc_object_hold( p_this );
508         return p_this;
509     }
510
511     /* Otherwise, recursively look for the object */
512     if (i_mode == FIND_ANYWHERE)
513         return vlc_object_find_name (VLC_OBJECT(p_this->p_libvlc), psz_name,
514                                      FIND_CHILD);
515
516     libvlc_lock (p_this->p_libvlc);
517     vlc_mutex_lock (&name_lock);
518     switch (i_mode)
519     {
520         case FIND_PARENT:
521             p_found = FindParentName (p_this, psz_name);
522             break;
523         case FIND_CHILD:
524             p_found = FindChildName (vlc_internals (p_this), psz_name);
525             break;
526         default:
527             assert (0);
528     }
529     vlc_mutex_unlock (&name_lock);
530     libvlc_unlock (p_this->p_libvlc);
531     return p_found;
532 }
533
534 #undef vlc_object_hold
535 /**
536  * Increment an object reference counter.
537  */
538 void * vlc_object_hold( vlc_object_t *p_this )
539 {
540     vlc_object_internals_t *internals = vlc_internals( p_this );
541
542     vlc_spin_lock( &internals->ref_spin );
543     /* Avoid obvious freed object uses */
544     assert( internals->i_refcount > 0 );
545     /* Increment the counter */
546     internals->i_refcount++;
547     vlc_spin_unlock( &internals->ref_spin );
548     return p_this;
549 }
550
551 #undef vlc_object_release
552 /*****************************************************************************
553  * Decrement an object refcount
554  * And destroy the object if its refcount reach zero.
555  *****************************************************************************/
556 void vlc_object_release( vlc_object_t *p_this )
557 {
558     vlc_object_internals_t *internals = vlc_internals( p_this );
559     vlc_object_t *parent = NULL;
560     bool b_should_destroy;
561
562     vlc_spin_lock( &internals->ref_spin );
563     assert( internals->i_refcount > 0 );
564
565     if( internals->i_refcount > 1 )
566     {
567         /* Fast path */
568         /* There are still other references to the object */
569         internals->i_refcount--;
570         vlc_spin_unlock( &internals->ref_spin );
571         return;
572     }
573     vlc_spin_unlock( &internals->ref_spin );
574
575     /* Slow path */
576     /* Remember that we cannot hold the spin while waiting on the mutex */
577     libvlc_lock (p_this->p_libvlc);
578     /* Take the spin again. Note that another thread may have held the
579      * object in the (very short) mean time. */
580     vlc_spin_lock( &internals->ref_spin );
581     b_should_destroy = --internals->i_refcount == 0;
582     vlc_spin_unlock( &internals->ref_spin );
583
584     if( b_should_destroy )
585     {
586         parent = p_this->p_parent;
587         if (parent)
588             /* Detach from parent to protect against FIND_CHILDREN */
589             vlc_object_detach_unlocked (p_this);
590
591         /* We have no children */
592         assert (internals->first == NULL);
593     }
594     libvlc_unlock (p_this->p_libvlc);
595
596     if( b_should_destroy )
597     {
598         int canc;
599
600         canc = vlc_savecancel ();
601         vlc_object_destroy( p_this );
602         vlc_restorecancel (canc);
603         if (parent)
604             vlc_object_release (parent);
605     }
606 }
607
608 #undef vlc_object_attach
609 /**
610  ****************************************************************************
611  * attach object to a parent object
612  *****************************************************************************
613  * This function sets p_this as a child of p_parent, and p_parent as a parent
614  * of p_this. This link can be undone using vlc_object_detach.
615  *****************************************************************************/
616 void vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
617 {
618     if( !p_this ) return;
619
620     vlc_object_internals_t *pap = vlc_internals (p_parent);
621     vlc_object_internals_t *priv = vlc_internals (p_this);
622     vlc_object_t *p_old_parent;
623
624     priv->prev = NULL;
625     vlc_object_hold (p_parent);
626     libvlc_lock (p_this->p_libvlc);
627 #ifndef NDEBUG
628     /* Reparenting an object carries a risk of invalid access to the parent,
629      * from another thread. This can happen when inheriting a variable, or
630      * through any direct access to vlc_object_t.p_parent. Also, reparenting
631      * brings a functional bug, whereby the reparented object uses incorrect
632      * old values for inherited variables (as the new parent may have different
633      * variable values, especially if it is an input).
634      * Note that the old parent may be already destroyed.
635      * So its pointer must not be dereferenced.
636      */
637     if (priv->old_parent)
638         msg_Info (p_this, "Reparenting an object is dangerous (%p -> %p)!",
639                   priv->old_parent, p_parent);
640 #endif
641
642     p_old_parent = p_this->p_parent;
643     if (p_old_parent)
644         vlc_object_detach_unlocked (p_this);
645
646     /* Attach the parent to its child */
647     p_this->p_parent = p_parent;
648
649     /* Attach the child to its parent */
650     priv->next = pap->first;
651     if (priv->next != NULL)
652         priv->next->prev = priv;
653     pap->first = priv;
654     libvlc_unlock (p_this->p_libvlc);
655
656     if (p_old_parent)
657         vlc_object_release (p_old_parent);
658 }
659
660
661 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
662 {
663     assert (p_this->p_parent != NULL);
664
665     vlc_object_internals_t *pap = vlc_internals (p_this->p_parent);
666     vlc_object_internals_t *priv = vlc_internals (p_this);
667
668     /* Unlink */
669     if (priv->prev != NULL)
670         priv->prev->next = priv->next;
671     else
672         pap->first = priv->next;
673     if (priv->next != NULL)
674         priv->next->prev = priv->prev;
675
676     /* Remove p_this's parent */
677 #ifndef NDEBUG
678     priv->old_parent = p_this->p_parent;
679 #endif
680     p_this->p_parent = NULL;
681 }
682
683 #undef vlc_object_detach
684 /**
685  ****************************************************************************
686  * detach object from its parent
687  *****************************************************************************
688  * This function removes all links between an object and its parent.
689  *****************************************************************************/
690 void vlc_object_detach( vlc_object_t *p_this )
691 {
692     vlc_object_t *p_parent;
693     if( !p_this ) return;
694
695     libvlc_lock (p_this->p_libvlc);
696     p_parent = p_this->p_parent;
697     if (p_parent)
698         vlc_object_detach_unlocked( p_this );
699     libvlc_unlock (p_this->p_libvlc);
700
701     if (p_parent)
702         vlc_object_release (p_parent);
703 }
704
705 #undef vlc_list_children
706 /**
707  * Gets the list of children of an objects, and increment their reference
708  * count.
709  * @return a list (possibly empty) or NULL in case of error.
710  */
711 vlc_list_t *vlc_list_children( vlc_object_t *obj )
712 {
713     vlc_list_t *l;
714     vlc_object_internals_t *priv;
715     unsigned count = 0;
716
717     libvlc_lock (obj->p_libvlc);
718     for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
719          count++;
720     l = NewList (count);
721     if (likely(l != NULL))
722     {
723         unsigned i = 0;
724
725         for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
726             l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
727     }
728     libvlc_unlock (obj->p_libvlc);
729     return l;
730 }
731
732 static void DumpVariable (const void *data, const VISIT which, const int depth)
733 {
734     if (which != postorder && which != leaf)
735         return;
736     (void) depth;
737
738     const variable_t *p_var = *(const variable_t **)data;
739     const char *psz_type = "unknown";
740
741     switch( p_var->i_type & VLC_VAR_TYPE )
742     {
743 #define MYCASE( type, nice )    \
744         case VLC_VAR_ ## type:  \
745             psz_type = nice;    \
746             break;
747         MYCASE( VOID, "void" );
748         MYCASE( BOOL, "bool" );
749         MYCASE( INTEGER, "integer" );
750         MYCASE( HOTKEY, "hotkey" );
751         MYCASE( STRING, "string" );
752         MYCASE( MODULE, "module" );
753         MYCASE( FILE, "file" );
754         MYCASE( DIRECTORY, "directory" );
755         MYCASE( VARIABLE, "variable" );
756         MYCASE( FLOAT, "float" );
757         MYCASE( TIME, "time" );
758         MYCASE( COORDS, "coords" );
759         MYCASE( ADDRESS, "address" );
760         MYCASE( MUTEX, "mutex" );
761         MYCASE( LIST, "list" );
762 #undef MYCASE
763     }
764     printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
765     if( p_var->psz_text )
766         printf( ", %s", p_var->psz_text );
767     fputc( ')', stdout );
768     if( p_var->i_type & VLC_VAR_HASCHOICE )
769         fputs( ", has choices", stdout );
770     if( p_var->i_type & VLC_VAR_ISCOMMAND )
771         fputs( ", command", stdout );
772     if( p_var->i_entries )
773         printf( ", %d callbacks", p_var->i_entries );
774     switch( p_var->i_type & VLC_VAR_CLASS )
775     {
776         case VLC_VAR_VOID:
777         case VLC_VAR_MUTEX:
778             break;
779         case VLC_VAR_BOOL:
780             printf( ": %s", p_var->val.b_bool ? "true" : "false" );
781             break;
782         case VLC_VAR_INTEGER:
783             printf( ": %d", p_var->val.i_int );
784             break;
785         case VLC_VAR_STRING:
786             printf( ": \"%s\"", p_var->val.psz_string );
787             break;
788         case VLC_VAR_FLOAT:
789             printf( ": %f", p_var->val.f_float );
790             break;
791         case VLC_VAR_TIME:
792             printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
793             break;
794         case VLC_VAR_COORDS:
795             printf( ": %"PRId32"x%"PRId32,
796                     p_var->val.coords.x, p_var->val.coords.y );
797             break;
798         case VLC_VAR_ADDRESS:
799             printf( ": %p", p_var->val.p_address );
800             break;
801         case VLC_VAR_LIST:
802             fputs( ": TODO", stdout );
803             break;
804     }
805     fputc( '\n', stdout );
806 }
807
808 /*****************************************************************************
809  * DumpCommand: print the current vlc structure
810  *****************************************************************************
811  * This function prints either an ASCII tree showing the connections between
812  * vlc objects, and additional information such as their refcount, thread ID,
813  * etc. (command "tree"), or the same data as a simple list (command "list").
814  *****************************************************************************/
815 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
816                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
817 {
818     (void)oldval; (void)p_data;
819     vlc_object_t *p_object = NULL;
820
821     if( *newval.psz_string )
822     {
823         /* try using the object's name to find it */
824         p_object = vlc_object_find_name( p_this, newval.psz_string,
825                                          FIND_ANYWHERE );
826         if( !p_object )
827         {
828             return VLC_ENOOBJ;
829         }
830     }
831
832     libvlc_lock (p_this->p_libvlc);
833     if( *psz_cmd == 't' )
834     {
835         char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
836
837         if( !p_object )
838             p_object = VLC_OBJECT(p_this->p_libvlc);
839
840         psz_foo[0] = '|';
841         DumpStructure( vlc_internals(p_object), 0, psz_foo );
842     }
843     else if( *psz_cmd == 'v' )
844     {
845         if( !p_object )
846             p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
847
848         PrintObject( vlc_internals(p_object), "" );
849         vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
850         if( vlc_internals( p_object )->var_root == NULL )
851             puts( " `-o No variables" );
852         else
853             twalk( vlc_internals( p_object )->var_root, DumpVariable );
854         vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
855     }
856     libvlc_unlock (p_this->p_libvlc);
857
858     if( *newval.psz_string )
859     {
860         vlc_object_release( p_object );
861     }
862     return VLC_SUCCESS;
863 }
864
865 /*****************************************************************************
866  * vlc_list_release: free a list previously allocated by vlc_list_find
867  *****************************************************************************
868  * This function decreases the refcount of all objects in the list and
869  * frees the list.
870  *****************************************************************************/
871 void vlc_list_release( vlc_list_t *p_list )
872 {
873     int i_index;
874
875     for( i_index = 0; i_index < p_list->i_count; i_index++ )
876     {
877         vlc_object_release( p_list->p_values[i_index].p_object );
878     }
879
880     free( p_list->p_values );
881     free( p_list );
882 }
883
884 /* Following functions are local */
885
886 static vlc_object_t *FindParent (vlc_object_t *p_this, int i_type)
887 {
888     for (vlc_object_t *parent = p_this->p_parent;
889          parent != NULL;
890          parent = parent->p_parent)
891     {
892         if (vlc_internals (parent)->i_object_type == i_type)
893             return vlc_object_hold (parent);
894     }
895     return NULL;
896 }
897
898 static vlc_object_t *FindParentName (vlc_object_t *p_this, const char *name)
899 {
900     for (vlc_object_t *parent = p_this->p_parent;
901          parent != NULL;
902          parent = parent->p_parent)
903     {
904         const char *objname = vlc_internals (parent)->psz_name;
905         if (objname && !strcmp (objname, name))
906             return vlc_object_hold (parent);
907     }
908     return NULL;
909 }
910
911 static vlc_object_t *FindChild (vlc_object_internals_t *priv, int i_type)
912 {
913     for (priv = priv->first; priv != NULL; priv = priv->next)
914     {
915         if (priv->i_object_type == i_type)
916             return vlc_object_hold (vlc_externals (priv));
917
918         vlc_object_t *found = FindChild (priv, i_type);
919         if (found != NULL)
920             return found;
921     }
922     return NULL;
923 }
924
925 static vlc_object_t *FindChildName (vlc_object_internals_t *priv,
926                                     const char *name)
927 {
928     for (priv = priv->first; priv != NULL; priv = priv->next)
929     {
930         if (priv->psz_name && !strcmp (priv->psz_name, name))
931             return vlc_object_hold (vlc_externals (priv));
932
933         vlc_object_t *found = FindChildName (priv, name);
934         if (found != NULL)
935             return found;
936     }
937     return NULL;
938 }
939
940 static void PrintObject( vlc_object_internals_t *priv,
941                          const char *psz_prefix )
942 {
943     char psz_refcount[20], psz_thread[30], psz_name[50], psz_parent[20];
944
945     int canc = vlc_savecancel ();
946     memset( &psz_name, 0, sizeof(psz_name) );
947
948     vlc_mutex_lock (&name_lock);
949     if (priv->psz_name != NULL)
950     {
951         snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
952         if( psz_name[48] )
953             psz_name[48] = '\"';
954     }
955     vlc_mutex_unlock (&name_lock);
956
957     psz_refcount[0] = '\0';
958     if( priv->i_refcount > 0 )
959         snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
960
961     psz_thread[0] = '\0';
962     if( priv->b_thread )
963         snprintf( psz_thread, 29, " (thread %lu)",
964                   (unsigned long)priv->thread_id );
965
966     psz_parent[0] = '\0';
967     /* FIXME: need structure lock!!! */
968     if( vlc_externals(priv)->p_parent )
969         snprintf( psz_parent, 19, ", parent %p",
970                   vlc_externals(priv)->p_parent );
971
972     printf( " %so %p %s%s%s%s%s\n", psz_prefix,
973             vlc_externals(priv), vlc_externals(priv)->psz_object_type,
974             psz_name, psz_thread, psz_refcount, psz_parent );
975     vlc_restorecancel (canc);
976 }
977
978 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
979                            char *psz_foo)
980 {
981     char i_back = psz_foo[i_level];
982     psz_foo[i_level] = '\0';
983
984     PrintObject (priv, psz_foo);
985
986     psz_foo[i_level] = i_back;
987
988     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
989     {
990         msg_Warn( vlc_externals(priv), "structure tree is too deep" );
991         return;
992     }
993
994     for (priv = priv->first; priv != NULL; priv = priv->next)
995     {
996         if( i_level )
997         {
998             psz_foo[i_level-1] = ' ';
999
1000             if( psz_foo[i_level-2] == '`' )
1001             {
1002                 psz_foo[i_level-2] = ' ';
1003             }
1004         }
1005
1006         psz_foo[i_level] = priv->next ? '|' : '`';
1007         psz_foo[i_level+1] = '-';
1008         psz_foo[i_level+2] = '\0';
1009
1010         DumpStructure (priv, i_level + 2, psz_foo);
1011     }
1012 }
1013
1014 static vlc_list_t * NewList( int i_count )
1015 {
1016     vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
1017     if( p_list == NULL )
1018         return NULL;
1019
1020     p_list->i_count = i_count;
1021
1022     if( i_count == 0 )
1023     {
1024         p_list->p_values = NULL;
1025         return p_list;
1026     }
1027
1028     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1029     if( p_list->p_values == NULL )
1030     {
1031         p_list->i_count = 0;
1032         return p_list;
1033     }
1034
1035     return p_list;
1036 }