]> git.sesse.net Git - vlc/blob - src/misc/objects.c
Don't overwrite the LibVLC structure lock with memset()
[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
51 #ifdef HAVE_SEARCH_H
52 # include <search.h>
53 #endif
54
55 #ifndef WIN32
56 # include <vlc_fs.h>
57 # include <unistd.h>
58 #else
59 # include <io.h>
60 # include <winsock2.h>
61 # include <ws2tcpip.h>
62 # undef  read
63 # define read( a, b, c )  recv (a, b, c, 0)
64 # undef  write
65 # define write( a, b, c ) send (a, b, c, 0)
66 # undef  close
67 # define close( a )       closesocket (a)
68 #endif
69
70 #include <limits.h>
71 #include <assert.h>
72
73 #if defined (HAVE_SYS_EVENTFD_H)
74 # include <sys/eventfd.h>
75 # ifndef EFD_CLOEXEC
76 #  define EFD_CLOEXEC 0
77 #  warning EFD_CLOEXEC missing. Consider updating libc.
78 # endif
79 #endif
80
81
82 /*****************************************************************************
83  * Local prototypes
84  *****************************************************************************/
85 static int  DumpCommand( vlc_object_t *, char const *,
86                          vlc_value_t, vlc_value_t, void * );
87
88 static vlc_object_t * FindName ( vlc_object_internals_t *, const char * );
89 static void PrintObject( vlc_object_internals_t *, const char * );
90 static void DumpStructure( vlc_object_internals_t *, unsigned, char * );
91
92 static vlc_list_t   * NewList       ( int );
93
94 static void vlc_object_destroy( vlc_object_t *p_this );
95
96 /*****************************************************************************
97  * Local structure lock
98  *****************************************************************************/
99 static void libvlc_lock (libvlc_int_t *p_libvlc)
100 {
101     vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
102 }
103
104 static void libvlc_unlock (libvlc_int_t *p_libvlc)
105 {
106     vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
107 }
108
109 #undef vlc_custom_create
110 void *vlc_custom_create (vlc_object_t *parent, size_t length,
111                          int type, const char *typename)
112 {
113     /* NOTE:
114      * VLC objects are laid out as follow:
115      * - first the LibVLC-private per-object data,
116      * - then VLC_COMMON members from vlc_object_t,
117      * - finally, the type-specific data (if any).
118      *
119      * This function initializes the LibVLC and common data,
120      * and zeroes the rest.
121      */
122     assert (length >= sizeof (vlc_object_t));
123
124     vlc_object_internals_t *priv = malloc (sizeof (*priv) + length);
125     if (unlikely(priv == NULL))
126         return NULL;
127     priv->i_object_type = type;
128     priv->psz_name = NULL;
129     priv->var_root = NULL;
130     vlc_mutex_init (&priv->var_lock);
131     vlc_cond_init (&priv->var_wait);
132     priv->pipes[0] = priv->pipes[1] = -1;
133     vlc_spin_init (&priv->ref_spin);
134     priv->i_refcount = 1;
135     priv->pf_destructor = NULL;
136     priv->prev = NULL;
137     priv->first = NULL;
138
139     vlc_object_t *obj = (vlc_object_t *)(priv + 1);
140     obj->psz_object_type = typename;
141     obj->psz_header = NULL;
142     obj->b_die = false;
143     obj->b_force = false;
144     memset (obj + 1, 0, length - sizeof (*obj)); /* type-specific stuff */
145
146     if (likely(parent != NULL))
147     {
148         vlc_object_internals_t *papriv = vlc_internals (parent);
149
150         obj->i_flags = parent->i_flags;
151         obj->p_libvlc = parent->p_libvlc;
152
153         /* Attach the child to its parent (no lock needed) */
154         obj->p_parent = vlc_object_hold (parent);
155
156         /* Attach the parent to its child (structure lock needed) */
157         libvlc_lock (obj->p_libvlc);
158         priv->next = papriv->first;
159         if (priv->next != NULL)
160             priv->next->prev = priv;
161         papriv->first = priv;
162         libvlc_unlock (obj->p_libvlc);
163     }
164     else
165     {
166         libvlc_int_t *self = (libvlc_int_t *)obj;
167
168         obj->i_flags = 0;
169         obj->p_libvlc = self;
170         obj->p_parent = NULL;
171         priv->next = NULL;
172         vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
173
174         /* TODO: should be in src/libvlc.c */
175         int canc = vlc_savecancel ();
176         var_Create (obj, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND);
177         var_AddCallback (obj, "tree", DumpCommand, obj);
178         var_Create (obj, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND);
179         var_AddCallback (obj, "vars", DumpCommand, obj);
180         vlc_restorecancel (canc);
181     }
182
183     return obj;
184 }
185
186 #undef vlc_object_create
187 /**
188  * Allocates and initializes a vlc object.
189  *
190  * @param i_size object byte size
191  *
192  * @return the new object, or NULL on error.
193  */
194 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
195 {
196     return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
197 }
198
199 #undef vlc_object_set_destructor
200 /**
201  ****************************************************************************
202  * Set the destructor of a vlc object
203  *
204  * This function sets the destructor of the vlc object. It will be called
205  * when the object is destroyed when the its refcount reaches 0.
206  * (It is called by the internal function vlc_object_destroy())
207  *****************************************************************************/
208 void vlc_object_set_destructor( vlc_object_t *p_this,
209                                 vlc_destructor_t pf_destructor )
210 {
211     vlc_object_internals_t *p_priv = vlc_internals(p_this );
212
213     vlc_spin_lock( &p_priv->ref_spin );
214     p_priv->pf_destructor = pf_destructor;
215     vlc_spin_unlock( &p_priv->ref_spin );
216 }
217
218 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
219
220 #undef vlc_object_set_name
221 int vlc_object_set_name(vlc_object_t *obj, const char *name)
222 {
223     vlc_object_internals_t *priv = vlc_internals(obj);
224     char *newname = name ? strdup (name) : NULL;
225     char *oldname;
226
227     vlc_mutex_lock (&name_lock);
228     oldname = priv->psz_name;
229     priv->psz_name = newname;
230     vlc_mutex_unlock (&name_lock);
231
232     free (oldname);
233     return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
234 }
235
236 #undef vlc_object_get_name
237 char *vlc_object_get_name(const vlc_object_t *obj)
238 {
239     vlc_object_internals_t *priv = vlc_internals(obj);
240     char *name;
241
242     vlc_mutex_lock (&name_lock);
243     name = priv->psz_name ? strdup (priv->psz_name) : NULL;
244     vlc_mutex_unlock (&name_lock);
245
246     return name;
247 }
248
249 /**
250  * Destroys a VLC object once it has no more references.
251  *
252  * This function must be called with cancellation disabled (currently).
253  */
254 static void vlc_object_destroy( vlc_object_t *p_this )
255 {
256     vlc_object_internals_t *p_priv = vlc_internals( p_this );
257
258     /* Call the custom "subclass" destructor */
259     if( p_priv->pf_destructor )
260         p_priv->pf_destructor( p_this );
261
262     /* Destroy the associated variables. */
263     var_DestroyAll( p_this );
264
265     vlc_cond_destroy( &p_priv->var_wait );
266     vlc_mutex_destroy( &p_priv->var_lock );
267
268     free( p_this->psz_header );
269
270     free( p_priv->psz_name );
271
272     vlc_spin_destroy( &p_priv->ref_spin );
273     if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
274         close( p_priv->pipes[1] );
275     if( p_priv->pipes[0] != -1 )
276         close( p_priv->pipes[0] );
277     if( VLC_OBJECT(p_this->p_libvlc) == p_this )
278         vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
279
280     free( p_priv );
281 }
282
283
284 #ifdef WIN32
285 /**
286  * select()-able pipes emulated using Winsock
287  */
288 # define vlc_pipe selectable_pipe
289 static int selectable_pipe (int fd[2])
290 {
291     SOCKADDR_IN addr;
292     int addrlen = sizeof (addr);
293
294     SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
295            c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
296     if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
297         goto error;
298
299     memset (&addr, 0, sizeof (addr));
300     addr.sin_family = AF_INET;
301     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
302     if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
303      || getsockname (l, (PSOCKADDR)&addr, &addrlen)
304      || listen (l, 1)
305      || connect (c, (PSOCKADDR)&addr, addrlen))
306         goto error;
307
308     a = accept (l, NULL, NULL);
309     if (a == INVALID_SOCKET)
310         goto error;
311
312     closesocket (l);
313     //shutdown (a, 0);
314     //shutdown (c, 1);
315     fd[0] = c;
316     fd[1] = a;
317     return 0;
318
319 error:
320     if (l != INVALID_SOCKET)
321         closesocket (l);
322     if (c != INVALID_SOCKET)
323         closesocket (c);
324     return -1;
325 }
326 #endif /* WIN32 */
327
328 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
329
330 /**
331  * Returns the readable end of a pipe that becomes readable once termination
332  * of the object is requested (vlc_object_kill()).
333  * This can be used to wake-up out of a select() or poll() event loop, such
334  * typically when doing network I/O.
335  *
336  * Note that the pipe will remain the same for the lifetime of the object.
337  * DO NOT read the pipe nor close it yourself. Ever.
338  *
339  * @param obj object that would be "killed"
340  * @return a readable pipe descriptor, or -1 on error.
341  */
342 int vlc_object_waitpipe( vlc_object_t *obj )
343 {
344     vlc_object_internals_t *internals = vlc_internals( obj );
345
346     vlc_mutex_lock (&pipe_lock);
347     if (internals->pipes[0] == -1)
348     {
349         /* This can only ever happen if someone killed us without locking: */
350         assert (internals->pipes[1] == -1);
351
352         /* pipe() is not a cancellation point, but write() is and eventfd() is
353          * unspecified (not in POSIX). */
354         int canc = vlc_savecancel ();
355 #if defined (HAVE_SYS_EVENTFD_H)
356         internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC);
357         if (internals->pipes[0] == -1)
358 #endif
359         {
360             if (vlc_pipe (internals->pipes))
361                 internals->pipes[0] = internals->pipes[1] = -1;
362         }
363
364         if (internals->pipes[0] != -1 && obj->b_die)
365         {   /* Race condition: vlc_object_kill() already invoked! */
366             msg_Dbg (obj, "waitpipe: object already dying");
367             write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
368         }
369         vlc_restorecancel (canc);
370     }
371     vlc_mutex_unlock (&pipe_lock);
372     return internals->pipes[0];
373 }
374
375 #undef vlc_object_kill
376 /**
377  * Requests termination of an object, cancels the object thread, and make the
378  * object wait pipe (if it exists) readable. Not a cancellation point.
379  */
380 void vlc_object_kill( vlc_object_t *p_this )
381 {
382     vlc_object_internals_t *priv = vlc_internals( p_this );
383     int fd = -1;
384
385     vlc_mutex_lock( &pipe_lock );
386     if( !p_this->b_die )
387     {
388         fd = priv->pipes[1];
389         p_this->b_die = true;
390     }
391
392     /* This also serves as a memory barrier toward vlc_object_alive(): */
393     vlc_mutex_unlock( &pipe_lock );
394
395     if (fd != -1)
396     {
397         int canc = vlc_savecancel ();
398
399         /* write _after_ setting b_die, so vlc_object_alive() returns false */
400         write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
401         msg_Dbg (p_this, "waitpipe: object killed");
402         vlc_restorecancel (canc);
403     }
404 }
405
406 static int objnamecmp(const vlc_object_t *obj, const char *name)
407 {
408     char *objname = vlc_object_get_name(obj);
409     if (objname == NULL)
410         return INT_MIN;
411
412     int ret = strcmp (objname, name);
413     free (objname);
414     return ret;
415 }
416
417 #undef vlc_object_find_name
418 /**
419  * Finds a named object and increment its reference count.
420  * Beware that objects found in this manner can be "owned" by another thread,
421  * be of _any_ type, and be attached to any module (if any). With such an
422  * object reference, you can set or get object variables, emit log messages,
423  * and read write-once object parameters (psz_object_type, etc).
424  * You CANNOT cast the object to a more specific object type, and you
425  * definitely cannot invoke object type-specific callbacks with this.
426  *
427  * @param p_this object to search from
428  * @param psz_name name of the object to search for
429  *
430  * @return a matching object (must be released by the caller),
431  * or NULL on error.
432  */
433 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this, const char *psz_name )
434 {
435     vlc_object_t *p_found;
436
437     /* Reading psz_object_name from a separate inhibits thread-safety.
438      * Use a libvlc address variable instead for that sort of things! */
439     msg_Err( p_this, "%s(\"%s\") is not safe!", __func__, psz_name );
440
441     libvlc_lock (p_this->p_libvlc);
442     vlc_mutex_lock (&name_lock);
443     p_found = FindName (vlc_internals (p_this), psz_name);
444     vlc_mutex_unlock (&name_lock);
445     libvlc_unlock (p_this->p_libvlc);
446     return p_found;
447 }
448
449 #undef vlc_object_hold
450 /**
451  * Increment an object reference counter.
452  */
453 void * vlc_object_hold( vlc_object_t *p_this )
454 {
455     vlc_object_internals_t *internals = vlc_internals( p_this );
456
457     vlc_spin_lock( &internals->ref_spin );
458     /* Avoid obvious freed object uses */
459     assert( internals->i_refcount > 0 );
460     /* Increment the counter */
461     internals->i_refcount++;
462     vlc_spin_unlock( &internals->ref_spin );
463     return p_this;
464 }
465
466 #undef vlc_object_release
467 /**
468  * Drops a reference to an object (decrements the reference count).
469  * If the count reaches zero, the object is destroyed.
470  */
471 void vlc_object_release( vlc_object_t *p_this )
472 {
473     vlc_object_internals_t *internals = vlc_internals( p_this );
474     vlc_object_t *parent = NULL;
475     bool b_should_destroy;
476
477     vlc_spin_lock( &internals->ref_spin );
478     assert( internals->i_refcount > 0 );
479
480     if( internals->i_refcount > 1 )
481     {
482         /* Fast path */
483         /* There are still other references to the object */
484         internals->i_refcount--;
485         vlc_spin_unlock( &internals->ref_spin );
486         return;
487     }
488     vlc_spin_unlock( &internals->ref_spin );
489
490     /* Slow path */
491     /* Remember that we cannot hold the spin while waiting on the mutex */
492     libvlc_lock (p_this->p_libvlc);
493     /* Take the spin again. Note that another thread may have held the
494      * object in the (very short) mean time. */
495     vlc_spin_lock( &internals->ref_spin );
496     b_should_destroy = --internals->i_refcount == 0;
497     vlc_spin_unlock( &internals->ref_spin );
498
499     if( b_should_destroy )
500     {
501         /* Detach from parent to protect against vlc_object_find_name() */
502         parent = p_this->p_parent;
503         if (likely(parent))
504         {
505            /* Unlink */
506            if (internals->prev != NULL)
507                internals->prev->next = internals->next;
508            else
509                vlc_internals(parent)->first = internals->next;
510            if (internals->next != NULL)
511                internals->next->prev = internals->prev;
512         }
513
514         /* We have no children */
515         assert (internals->first == NULL);
516     }
517     libvlc_unlock (p_this->p_libvlc);
518
519     if( b_should_destroy )
520     {
521         int canc;
522
523         canc = vlc_savecancel ();
524         vlc_object_destroy( p_this );
525         vlc_restorecancel (canc);
526         if (parent)
527             vlc_object_release (parent);
528     }
529 }
530
531 #undef vlc_list_children
532 /**
533  * Gets the list of children of an objects, and increment their reference
534  * count.
535  * @return a list (possibly empty) or NULL in case of error.
536  */
537 vlc_list_t *vlc_list_children( vlc_object_t *obj )
538 {
539     vlc_list_t *l;
540     vlc_object_internals_t *priv;
541     unsigned count = 0;
542
543     libvlc_lock (obj->p_libvlc);
544     for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
545          count++;
546     l = NewList (count);
547     if (likely(l != NULL))
548     {
549         unsigned i = 0;
550
551         for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
552             l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
553     }
554     libvlc_unlock (obj->p_libvlc);
555     return l;
556 }
557
558 static void DumpVariable (const void *data, const VISIT which, const int depth)
559 {
560     if (which != postorder && which != leaf)
561         return;
562     (void) depth;
563
564     const variable_t *p_var = *(const variable_t **)data;
565     const char *psz_type = "unknown";
566
567     switch( p_var->i_type & VLC_VAR_TYPE )
568     {
569 #define MYCASE( type, nice )    \
570         case VLC_VAR_ ## type:  \
571             psz_type = nice;    \
572             break;
573         MYCASE( VOID, "void" );
574         MYCASE( BOOL, "bool" );
575         MYCASE( INTEGER, "integer" );
576         MYCASE( HOTKEY, "hotkey" );
577         MYCASE( STRING, "string" );
578         MYCASE( VARIABLE, "variable" );
579         MYCASE( FLOAT, "float" );
580         MYCASE( TIME, "time" );
581         MYCASE( COORDS, "coords" );
582         MYCASE( ADDRESS, "address" );
583         MYCASE( MUTEX, "mutex" );
584 #undef MYCASE
585     }
586     printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
587     if( p_var->psz_text )
588         printf( ", %s", p_var->psz_text );
589     fputc( ')', stdout );
590     if( p_var->i_type & VLC_VAR_HASCHOICE )
591         fputs( ", has choices", stdout );
592     if( p_var->i_type & VLC_VAR_ISCOMMAND )
593         fputs( ", command", stdout );
594     if( p_var->i_entries )
595         printf( ", %d callbacks", p_var->i_entries );
596     switch( p_var->i_type & VLC_VAR_CLASS )
597     {
598         case VLC_VAR_VOID:
599         case VLC_VAR_MUTEX:
600             break;
601         case VLC_VAR_BOOL:
602             printf( ": %s", p_var->val.b_bool ? "true" : "false" );
603             break;
604         case VLC_VAR_INTEGER:
605             printf( ": %"PRId64, p_var->val.i_int );
606             break;
607         case VLC_VAR_STRING:
608             printf( ": \"%s\"", p_var->val.psz_string );
609             break;
610         case VLC_VAR_FLOAT:
611             printf( ": %f", p_var->val.f_float );
612             break;
613         case VLC_VAR_TIME:
614             printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
615             break;
616         case VLC_VAR_COORDS:
617             printf( ": %"PRId32"x%"PRId32,
618                     p_var->val.coords.x, p_var->val.coords.y );
619             break;
620         case VLC_VAR_ADDRESS:
621             printf( ": %p", p_var->val.p_address );
622             break;
623     }
624     fputc( '\n', stdout );
625 }
626
627 /*****************************************************************************
628  * DumpCommand: print the current vlc structure
629  *****************************************************************************
630  * This function prints either an ASCII tree showing the connections between
631  * vlc objects, and additional information such as their refcount, thread ID,
632  * etc. (command "tree"), or the same data as a simple list (command "list").
633  *****************************************************************************/
634 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
635                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
636 {
637     (void)oldval;
638     vlc_object_t *p_object = NULL;
639
640     if( *newval.psz_string )
641     {
642         /* try using the object's name to find it */
643         p_object = vlc_object_find_name( p_data, newval.psz_string );
644         if( !p_object )
645             return VLC_ENOOBJ;
646     }
647
648     libvlc_lock (p_this->p_libvlc);
649     if( *psz_cmd == 't' )
650     {
651         char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
652
653         if( !p_object )
654             p_object = VLC_OBJECT(p_this->p_libvlc);
655
656         psz_foo[0] = '|';
657         DumpStructure( vlc_internals(p_object), 0, psz_foo );
658     }
659     else if( *psz_cmd == 'v' )
660     {
661         if( !p_object )
662             p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
663
664         PrintObject( vlc_internals(p_object), "" );
665         vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
666         if( vlc_internals( p_object )->var_root == NULL )
667             puts( " `-o No variables" );
668         else
669             twalk( vlc_internals( p_object )->var_root, DumpVariable );
670         vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
671     }
672     libvlc_unlock (p_this->p_libvlc);
673
674     if( *newval.psz_string )
675     {
676         vlc_object_release( p_object );
677     }
678     return VLC_SUCCESS;
679 }
680
681 /*****************************************************************************
682  * vlc_list_release: free a list previously allocated by vlc_list_find
683  *****************************************************************************
684  * This function decreases the refcount of all objects in the list and
685  * frees the list.
686  *****************************************************************************/
687 void vlc_list_release( vlc_list_t *p_list )
688 {
689     int i_index;
690
691     for( i_index = 0; i_index < p_list->i_count; i_index++ )
692     {
693         vlc_object_release( p_list->p_values[i_index].p_object );
694     }
695
696     free( p_list->p_values );
697     free( p_list );
698 }
699
700 /* Following functions are local */
701
702 static vlc_object_t *FindName (vlc_object_internals_t *priv, const char *name)
703 {
704     if (priv->psz_name != NULL && !strcmp (priv->psz_name, name))
705         return vlc_object_hold (vlc_externals (priv));
706
707     for (priv = priv->first; priv != NULL; priv = priv->next)
708     {
709         vlc_object_t *found = FindName (priv, name);
710         if (found != NULL)
711             return found;
712     }
713     return NULL;
714 }
715
716 static void PrintObject( vlc_object_internals_t *priv,
717                          const char *psz_prefix )
718 {
719     char psz_refcount[20], psz_name[50], psz_parent[20];
720
721     int canc = vlc_savecancel ();
722     memset( &psz_name, 0, sizeof(psz_name) );
723
724     vlc_mutex_lock (&name_lock);
725     if (priv->psz_name != NULL)
726     {
727         snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
728         if( psz_name[48] )
729             psz_name[48] = '\"';
730     }
731     vlc_mutex_unlock (&name_lock);
732
733     psz_refcount[0] = '\0';
734     if( priv->i_refcount > 0 )
735         snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
736
737     psz_parent[0] = '\0';
738     /* FIXME: need structure lock!!! */
739     if( vlc_externals(priv)->p_parent )
740         snprintf( psz_parent, 19, ", parent %p",
741                   vlc_externals(priv)->p_parent );
742
743     printf( " %so %p %s%s%s%s\n", psz_prefix,
744             vlc_externals(priv), vlc_externals(priv)->psz_object_type,
745             psz_name, psz_refcount, psz_parent );
746     vlc_restorecancel (canc);
747 }
748
749 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
750                            char *psz_foo)
751 {
752     char i_back = psz_foo[i_level];
753     psz_foo[i_level] = '\0';
754
755     PrintObject (priv, psz_foo);
756
757     psz_foo[i_level] = i_back;
758
759     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
760     {
761         msg_Warn( vlc_externals(priv), "structure tree is too deep" );
762         return;
763     }
764
765     for (priv = priv->first; priv != NULL; priv = priv->next)
766     {
767         if( i_level )
768         {
769             psz_foo[i_level-1] = ' ';
770
771             if( psz_foo[i_level-2] == '`' )
772             {
773                 psz_foo[i_level-2] = ' ';
774             }
775         }
776
777         psz_foo[i_level] = priv->next ? '|' : '`';
778         psz_foo[i_level+1] = '-';
779         psz_foo[i_level+2] = '\0';
780
781         DumpStructure (priv, i_level + 2, psz_foo);
782     }
783 }
784
785 static vlc_list_t * NewList( int i_count )
786 {
787     vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
788     if( p_list == NULL )
789         return NULL;
790
791     p_list->i_count = i_count;
792
793     if( i_count == 0 )
794     {
795         p_list->p_values = NULL;
796         return p_list;
797     }
798
799     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
800     if( p_list->p_values == NULL )
801     {
802         p_list->i_count = 0;
803         return p_list;
804     }
805
806     return p_list;
807 }