]> git.sesse.net Git - vlc/blob - src/misc/objects.c
Remove vlc_object_detach()
[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 # ifndef EFD_CLOEXEC
71 #  define EFD_CLOEXEC 0
72 #  warning EFD_CLOEXEC missing. Consider updating libc.
73 # endif
74 #endif
75
76
77 /*****************************************************************************
78  * Local prototypes
79  *****************************************************************************/
80 static int  DumpCommand( vlc_object_t *, char const *,
81                          vlc_value_t, vlc_value_t, void * );
82
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 * );
89
90 static vlc_list_t   * NewList       ( int );
91
92 static void vlc_object_destroy( vlc_object_t *p_this );
93 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
94
95 /*****************************************************************************
96  * Local structure lock
97  *****************************************************************************/
98 static void libvlc_lock (libvlc_int_t *p_libvlc)
99 {
100     vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
101 }
102
103 static void libvlc_unlock (libvlc_int_t *p_libvlc)
104 {
105     vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
106 }
107
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 )
111 {
112     vlc_object_t *p_new;
113     vlc_object_internals_t *p_priv;
114
115     /* NOTE:
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).
120      *
121      * This function initializes the LibVLC and common data,
122      * and zeroes the rest.
123      */
124     p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
125     if( p_priv == NULL )
126         return NULL;
127
128     assert (i_size >= sizeof (vlc_object_t));
129     p_new = (vlc_object_t *)(p_priv + 1);
130
131     p_priv->i_object_type = i_type;
132     p_new->psz_object_type = psz_type;
133     p_priv->psz_name = NULL;
134
135     p_new->b_die = false;
136     p_new->b_force = false;
137
138     p_new->psz_header = NULL;
139
140     if (p_this)
141         p_new->i_flags = p_this->i_flags
142             & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
143
144     p_priv->var_root = NULL;
145
146     if( p_this == NULL )
147     {
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));
151         p_this = p_new;
152     }
153     else
154         p_new->p_libvlc = p_this->p_libvlc;
155
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;
162 #ifndef NDEBUG
163     p_priv->old_parent = NULL;
164 #endif
165
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;
170
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);
179     }
180
181     return p_new;
182 }
183
184 #undef vlc_object_create
185 /**
186  * Allocates and initializes a vlc object.
187  *
188  * @param i_size object byte size
189  *
190  * @return the new object, or NULL on error.
191  */
192 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
193 {
194     return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
195 }
196
197 #undef vlc_object_set_destructor
198 /**
199  ****************************************************************************
200  * Set the destructor of a vlc object
201  *
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 )
208 {
209     vlc_object_internals_t *p_priv = vlc_internals(p_this );
210
211     vlc_spin_lock( &p_priv->ref_spin );
212     p_priv->pf_destructor = pf_destructor;
213     vlc_spin_unlock( &p_priv->ref_spin );
214 }
215
216 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
217
218 #undef vlc_object_set_name
219 int vlc_object_set_name(vlc_object_t *obj, const char *name)
220 {
221     vlc_object_internals_t *priv = vlc_internals(obj);
222     char *newname = name ? strdup (name) : NULL;
223     char *oldname;
224
225     vlc_mutex_lock (&name_lock);
226     oldname = priv->psz_name;
227     priv->psz_name = newname;
228     vlc_mutex_unlock (&name_lock);
229
230     free (oldname);
231     return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
232 }
233
234 #undef vlc_object_get_name
235 char *vlc_object_get_name(const vlc_object_t *obj)
236 {
237     vlc_object_internals_t *priv = vlc_internals(obj);
238     char *name;
239
240     vlc_mutex_lock (&name_lock);
241     name = priv->psz_name ? strdup (priv->psz_name) : NULL;
242     vlc_mutex_unlock (&name_lock);
243
244     return name;
245 }
246
247 /**
248  ****************************************************************************
249  * Destroy a vlc object (Internal)
250  *
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.
254  *
255  * This function must be called with cancellation disabled (currently).
256  *****************************************************************************/
257 static void vlc_object_destroy( vlc_object_t *p_this )
258 {
259     vlc_object_internals_t *p_priv = vlc_internals( p_this );
260
261     /* Objects are always detached beforehand */
262     assert( !p_this->p_parent );
263
264     /* Send a kill to the object's thread if applicable */
265     vlc_object_kill( p_this );
266
267     /* Call the custom "subclass" destructor */
268     if( p_priv->pf_destructor )
269         p_priv->pf_destructor( p_this );
270
271     /* Any thread must have been cleaned up at this point. */
272     assert( !p_priv->b_thread );
273
274     /* Destroy the associated variables. */
275     var_DestroyAll( p_this );
276
277     vlc_cond_destroy( &p_priv->var_wait );
278     vlc_mutex_destroy( &p_priv->var_lock );
279
280     free( p_this->psz_header );
281
282     free( p_priv->psz_name );
283
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));
291
292     free( p_priv );
293 }
294
295
296 #ifdef WIN32
297 /**
298  * select()-able pipes emulated using Winsock
299  */
300 static int pipe (int fd[2])
301 {
302     SOCKADDR_IN addr;
303     int addrlen = sizeof (addr);
304
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))
308         goto error;
309
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)
315      || listen (l, 1)
316      || connect (c, (PSOCKADDR)&addr, addrlen))
317         goto error;
318
319     a = accept (l, NULL, NULL);
320     if (a == INVALID_SOCKET)
321         goto error;
322
323     closesocket (l);
324     //shutdown (a, 0);
325     //shutdown (c, 1);
326     fd[0] = c;
327     fd[1] = a;
328     return 0;
329
330 error:
331     if (l != INVALID_SOCKET)
332         closesocket (l);
333     if (c != INVALID_SOCKET)
334         closesocket (c);
335     return -1;
336 }
337 #endif /* WIN32 */
338
339 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
340
341 /**
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.
346  *
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.
349  *
350  * @param obj object that would be "killed"
351  * @return a readable pipe descriptor, or -1 on error.
352  */
353 int vlc_object_waitpipe( vlc_object_t *obj )
354 {
355     vlc_object_internals_t *internals = vlc_internals( obj );
356
357     vlc_mutex_lock (&pipe_lock);
358     if (internals->pipes[0] == -1)
359     {
360         /* This can only ever happen if someone killed us without locking: */
361         assert (internals->pipes[1] == -1);
362
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)
369 #endif
370         {
371             if (pipe (internals->pipes))
372                 internals->pipes[0] = internals->pipes[1] = -1;
373         }
374
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));
379         }
380         vlc_restorecancel (canc);
381     }
382     vlc_mutex_unlock (&pipe_lock);
383     return internals->pipes[0];
384 }
385
386 #undef vlc_object_kill
387 /**
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.
390  */
391 void vlc_object_kill( vlc_object_t *p_this )
392 {
393     vlc_object_internals_t *priv = vlc_internals( p_this );
394     int fd = -1;
395
396     vlc_thread_cancel( p_this );
397     vlc_mutex_lock( &pipe_lock );
398     if( !p_this->b_die )
399     {
400         fd = priv->pipes[1];
401         p_this->b_die = true;
402     }
403
404     /* This also serves as a memory barrier toward vlc_object_alive(): */
405     vlc_mutex_unlock( &pipe_lock );
406
407     if (fd != -1)
408     {
409         int canc = vlc_savecancel ();
410
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);
415     }
416 }
417
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 )
426 {
427     vlc_object_t *p_found;
428
429     /* If we are of the requested type ourselves, don't look further */
430     if( vlc_internals (p_this)->i_object_type == i_type )
431     {
432         vlc_object_hold( p_this );
433         return p_this;
434     }
435
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);
439
440     switch (i_type)
441     {
442         case VLC_OBJECT_VOUT:
443         case VLC_OBJECT_AOUT:
444             break;
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)
449                 return NULL;
450             break;
451         default:
452             return NULL;
453     }
454
455     libvlc_lock (p_this->p_libvlc);
456     switch (i_mode)
457     {
458         case FIND_PARENT:
459             p_found = FindParent (p_this, i_type);
460             break;
461         case FIND_CHILD:
462             p_found = FindChild (vlc_internals (p_this), i_type);
463             break;
464         default:
465             assert (0);
466     }
467     libvlc_unlock (p_this->p_libvlc);
468     return p_found;
469 }
470
471
472 static int objnamecmp(const vlc_object_t *obj, const char *name)
473 {
474     char *objname = vlc_object_get_name(obj);
475     if (objname == NULL)
476         return INT_MIN;
477
478     int ret = strcmp (objname, name);
479     free (objname);
480     return ret;
481 }
482
483 #undef vlc_object_find_name
484 /**
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.
492  *
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.
496  *
497  * @return a matching object (must be released by the caller),
498  * or NULL on error.
499  */
500 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
501                                     const char *psz_name, int i_mode )
502 {
503     vlc_object_t *p_found;
504
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) )
510     {
511         vlc_object_hold( p_this );
512         return p_this;
513     }
514
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,
518                                      FIND_CHILD);
519
520     libvlc_lock (p_this->p_libvlc);
521     vlc_mutex_lock (&name_lock);
522     switch (i_mode)
523     {
524         case FIND_PARENT:
525             p_found = FindParentName (p_this, psz_name);
526             break;
527         case FIND_CHILD:
528             p_found = FindChildName (vlc_internals (p_this), psz_name);
529             break;
530         default:
531             assert (0);
532     }
533     vlc_mutex_unlock (&name_lock);
534     libvlc_unlock (p_this->p_libvlc);
535     return p_found;
536 }
537
538 #undef vlc_object_hold
539 /**
540  * Increment an object reference counter.
541  */
542 void * vlc_object_hold( vlc_object_t *p_this )
543 {
544     vlc_object_internals_t *internals = vlc_internals( p_this );
545
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 );
552     return p_this;
553 }
554
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 )
561 {
562     vlc_object_internals_t *internals = vlc_internals( p_this );
563     vlc_object_t *parent = NULL;
564     bool b_should_destroy;
565
566     vlc_spin_lock( &internals->ref_spin );
567     assert( internals->i_refcount > 0 );
568
569     if( internals->i_refcount > 1 )
570     {
571         /* Fast path */
572         /* There are still other references to the object */
573         internals->i_refcount--;
574         vlc_spin_unlock( &internals->ref_spin );
575         return;
576     }
577     vlc_spin_unlock( &internals->ref_spin );
578
579     /* Slow path */
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 );
587
588     if( b_should_destroy )
589     {
590         parent = p_this->p_parent;
591         if (parent)
592             /* Detach from parent to protect against FIND_CHILDREN */
593             vlc_object_detach_unlocked (p_this);
594
595         /* We have no children */
596         assert (internals->first == NULL);
597     }
598     libvlc_unlock (p_this->p_libvlc);
599
600     if( b_should_destroy )
601     {
602         int canc;
603
604         canc = vlc_savecancel ();
605         vlc_object_destroy( p_this );
606         vlc_restorecancel (canc);
607         if (parent)
608             vlc_object_release (parent);
609     }
610 }
611
612 #undef vlc_object_attach
613 /**
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.
619  *****************************************************************************/
620 void vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
621 {
622     if( !p_this ) return;
623
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;
627
628     priv->prev = NULL;
629     vlc_object_hold (p_parent);
630     libvlc_lock (p_this->p_libvlc);
631 #ifndef NDEBUG
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.
640      */
641     if (priv->old_parent)
642         msg_Info (p_this, "Reparenting an object is dangerous (%p -> %p)!",
643                   priv->old_parent, p_parent);
644 #endif
645
646     p_old_parent = p_this->p_parent;
647     if (p_old_parent)
648         vlc_object_detach_unlocked (p_this);
649
650     /* Attach the parent to its child */
651     p_this->p_parent = p_parent;
652
653     /* Attach the child to its parent */
654     priv->next = pap->first;
655     if (priv->next != NULL)
656         priv->next->prev = priv;
657     pap->first = priv;
658     libvlc_unlock (p_this->p_libvlc);
659
660     if (p_old_parent)
661         vlc_object_release (p_old_parent);
662 }
663
664
665 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
666 {
667     assert (p_this->p_parent != NULL);
668
669     vlc_object_internals_t *pap = vlc_internals (p_this->p_parent);
670     vlc_object_internals_t *priv = vlc_internals (p_this);
671
672     /* Unlink */
673     if (priv->prev != NULL)
674         priv->prev->next = priv->next;
675     else
676         pap->first = priv->next;
677     if (priv->next != NULL)
678         priv->next->prev = priv->prev;
679
680     /* Remove p_this's parent */
681 #ifndef NDEBUG
682     priv->old_parent = p_this->p_parent;
683 #endif
684     p_this->p_parent = NULL;
685 }
686
687 #undef vlc_list_children
688 /**
689  * Gets the list of children of an objects, and increment their reference
690  * count.
691  * @return a list (possibly empty) or NULL in case of error.
692  */
693 vlc_list_t *vlc_list_children( vlc_object_t *obj )
694 {
695     vlc_list_t *l;
696     vlc_object_internals_t *priv;
697     unsigned count = 0;
698
699     libvlc_lock (obj->p_libvlc);
700     for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
701          count++;
702     l = NewList (count);
703     if (likely(l != NULL))
704     {
705         unsigned i = 0;
706
707         for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
708             l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
709     }
710     libvlc_unlock (obj->p_libvlc);
711     return l;
712 }
713
714 static void DumpVariable (const void *data, const VISIT which, const int depth)
715 {
716     if (which != postorder && which != leaf)
717         return;
718     (void) depth;
719
720     const variable_t *p_var = *(const variable_t **)data;
721     const char *psz_type = "unknown";
722
723     switch( p_var->i_type & VLC_VAR_TYPE )
724     {
725 #define MYCASE( type, nice )    \
726         case VLC_VAR_ ## type:  \
727             psz_type = nice;    \
728             break;
729         MYCASE( VOID, "void" );
730         MYCASE( BOOL, "bool" );
731         MYCASE( INTEGER, "integer" );
732         MYCASE( HOTKEY, "hotkey" );
733         MYCASE( STRING, "string" );
734         MYCASE( MODULE, "module" );
735         MYCASE( FILE, "file" );
736         MYCASE( DIRECTORY, "directory" );
737         MYCASE( VARIABLE, "variable" );
738         MYCASE( FLOAT, "float" );
739         MYCASE( TIME, "time" );
740         MYCASE( COORDS, "coords" );
741         MYCASE( ADDRESS, "address" );
742         MYCASE( MUTEX, "mutex" );
743         MYCASE( LIST, "list" );
744 #undef MYCASE
745     }
746     printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
747     if( p_var->psz_text )
748         printf( ", %s", p_var->psz_text );
749     fputc( ')', stdout );
750     if( p_var->i_type & VLC_VAR_HASCHOICE )
751         fputs( ", has choices", stdout );
752     if( p_var->i_type & VLC_VAR_ISCOMMAND )
753         fputs( ", command", stdout );
754     if( p_var->i_entries )
755         printf( ", %d callbacks", p_var->i_entries );
756     switch( p_var->i_type & VLC_VAR_CLASS )
757     {
758         case VLC_VAR_VOID:
759         case VLC_VAR_MUTEX:
760             break;
761         case VLC_VAR_BOOL:
762             printf( ": %s", p_var->val.b_bool ? "true" : "false" );
763             break;
764         case VLC_VAR_INTEGER:
765             printf( ": %d", p_var->val.i_int );
766             break;
767         case VLC_VAR_STRING:
768             printf( ": \"%s\"", p_var->val.psz_string );
769             break;
770         case VLC_VAR_FLOAT:
771             printf( ": %f", p_var->val.f_float );
772             break;
773         case VLC_VAR_TIME:
774             printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
775             break;
776         case VLC_VAR_COORDS:
777             printf( ": %"PRId32"x%"PRId32,
778                     p_var->val.coords.x, p_var->val.coords.y );
779             break;
780         case VLC_VAR_ADDRESS:
781             printf( ": %p", p_var->val.p_address );
782             break;
783         case VLC_VAR_LIST:
784             fputs( ": TODO", stdout );
785             break;
786     }
787     fputc( '\n', stdout );
788 }
789
790 /*****************************************************************************
791  * DumpCommand: print the current vlc structure
792  *****************************************************************************
793  * This function prints either an ASCII tree showing the connections between
794  * vlc objects, and additional information such as their refcount, thread ID,
795  * etc. (command "tree"), or the same data as a simple list (command "list").
796  *****************************************************************************/
797 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
798                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
799 {
800     (void)oldval; (void)p_data;
801     vlc_object_t *p_object = NULL;
802
803     if( *newval.psz_string )
804     {
805         /* try using the object's name to find it */
806         p_object = vlc_object_find_name( p_this, newval.psz_string,
807                                          FIND_ANYWHERE );
808         if( !p_object )
809         {
810             return VLC_ENOOBJ;
811         }
812     }
813
814     libvlc_lock (p_this->p_libvlc);
815     if( *psz_cmd == 't' )
816     {
817         char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
818
819         if( !p_object )
820             p_object = VLC_OBJECT(p_this->p_libvlc);
821
822         psz_foo[0] = '|';
823         DumpStructure( vlc_internals(p_object), 0, psz_foo );
824     }
825     else if( *psz_cmd == 'v' )
826     {
827         if( !p_object )
828             p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
829
830         PrintObject( vlc_internals(p_object), "" );
831         vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
832         if( vlc_internals( p_object )->var_root == NULL )
833             puts( " `-o No variables" );
834         else
835             twalk( vlc_internals( p_object )->var_root, DumpVariable );
836         vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
837     }
838     libvlc_unlock (p_this->p_libvlc);
839
840     if( *newval.psz_string )
841     {
842         vlc_object_release( p_object );
843     }
844     return VLC_SUCCESS;
845 }
846
847 /*****************************************************************************
848  * vlc_list_release: free a list previously allocated by vlc_list_find
849  *****************************************************************************
850  * This function decreases the refcount of all objects in the list and
851  * frees the list.
852  *****************************************************************************/
853 void vlc_list_release( vlc_list_t *p_list )
854 {
855     int i_index;
856
857     for( i_index = 0; i_index < p_list->i_count; i_index++ )
858     {
859         vlc_object_release( p_list->p_values[i_index].p_object );
860     }
861
862     free( p_list->p_values );
863     free( p_list );
864 }
865
866 /* Following functions are local */
867
868 static vlc_object_t *FindParent (vlc_object_t *p_this, int i_type)
869 {
870     for (vlc_object_t *parent = p_this->p_parent;
871          parent != NULL;
872          parent = parent->p_parent)
873     {
874         if (vlc_internals (parent)->i_object_type == i_type)
875             return vlc_object_hold (parent);
876     }
877     return NULL;
878 }
879
880 static vlc_object_t *FindParentName (vlc_object_t *p_this, const char *name)
881 {
882     for (vlc_object_t *parent = p_this->p_parent;
883          parent != NULL;
884          parent = parent->p_parent)
885     {
886         const char *objname = vlc_internals (parent)->psz_name;
887         if (objname && !strcmp (objname, name))
888             return vlc_object_hold (parent);
889     }
890     return NULL;
891 }
892
893 static vlc_object_t *FindChild (vlc_object_internals_t *priv, int i_type)
894 {
895     for (priv = priv->first; priv != NULL; priv = priv->next)
896     {
897         if (priv->i_object_type == i_type)
898             return vlc_object_hold (vlc_externals (priv));
899
900         vlc_object_t *found = FindChild (priv, i_type);
901         if (found != NULL)
902             return found;
903     }
904     return NULL;
905 }
906
907 static vlc_object_t *FindChildName (vlc_object_internals_t *priv,
908                                     const char *name)
909 {
910     for (priv = priv->first; priv != NULL; priv = priv->next)
911     {
912         if (priv->psz_name && !strcmp (priv->psz_name, name))
913             return vlc_object_hold (vlc_externals (priv));
914
915         vlc_object_t *found = FindChildName (priv, name);
916         if (found != NULL)
917             return found;
918     }
919     return NULL;
920 }
921
922 static void PrintObject( vlc_object_internals_t *priv,
923                          const char *psz_prefix )
924 {
925     char psz_refcount[20], psz_thread[30], psz_name[50], psz_parent[20];
926
927     int canc = vlc_savecancel ();
928     memset( &psz_name, 0, sizeof(psz_name) );
929
930     vlc_mutex_lock (&name_lock);
931     if (priv->psz_name != NULL)
932     {
933         snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
934         if( psz_name[48] )
935             psz_name[48] = '\"';
936     }
937     vlc_mutex_unlock (&name_lock);
938
939     psz_refcount[0] = '\0';
940     if( priv->i_refcount > 0 )
941         snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
942
943     psz_thread[0] = '\0';
944     if( priv->b_thread )
945         snprintf( psz_thread, 29, " (thread %lu)",
946                   (unsigned long)priv->thread_id );
947
948     psz_parent[0] = '\0';
949     /* FIXME: need structure lock!!! */
950     if( vlc_externals(priv)->p_parent )
951         snprintf( psz_parent, 19, ", parent %p",
952                   vlc_externals(priv)->p_parent );
953
954     printf( " %so %p %s%s%s%s%s\n", psz_prefix,
955             vlc_externals(priv), vlc_externals(priv)->psz_object_type,
956             psz_name, psz_thread, psz_refcount, psz_parent );
957     vlc_restorecancel (canc);
958 }
959
960 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
961                            char *psz_foo)
962 {
963     char i_back = psz_foo[i_level];
964     psz_foo[i_level] = '\0';
965
966     PrintObject (priv, psz_foo);
967
968     psz_foo[i_level] = i_back;
969
970     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
971     {
972         msg_Warn( vlc_externals(priv), "structure tree is too deep" );
973         return;
974     }
975
976     for (priv = priv->first; priv != NULL; priv = priv->next)
977     {
978         if( i_level )
979         {
980             psz_foo[i_level-1] = ' ';
981
982             if( psz_foo[i_level-2] == '`' )
983             {
984                 psz_foo[i_level-2] = ' ';
985             }
986         }
987
988         psz_foo[i_level] = priv->next ? '|' : '`';
989         psz_foo[i_level+1] = '-';
990         psz_foo[i_level+2] = '\0';
991
992         DumpStructure (priv, i_level + 2, psz_foo);
993     }
994 }
995
996 static vlc_list_t * NewList( int i_count )
997 {
998     vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
999     if( p_list == NULL )
1000         return NULL;
1001
1002     p_list->i_count = i_count;
1003
1004     if( i_count == 0 )
1005     {
1006         p_list->p_values = NULL;
1007         return p_list;
1008     }
1009
1010     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1011     if( p_list->p_values == NULL )
1012     {
1013         p_list->i_count = 0;
1014         return p_list;
1015     }
1016
1017     return p_list;
1018 }