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