]> git.sesse.net Git - vlc/blob - src/misc/objects.c
Remove unused vlc_object_wait()
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /**
24  * \file
25  * This file contains the functions to handle the vlc_object_t type
26  *
27  * Unless otherwise stated, functions in this file are not cancellation point.
28  * All functions in this file are safe w.r.t. deferred cancellation.
29  */
30
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc_common.h>
40
41 #include "../libvlc.h"
42 #include <vlc_vout.h>
43 #include <vlc_aout.h>
44 #include "audio_output/aout_internal.h"
45
46 #include <vlc_access.h>
47 #include <vlc_demux.h>
48 #include <vlc_stream.h>
49
50 #include <vlc_sout.h>
51 #include "stream_output/stream_output.h"
52
53 #include "vlc_interface.h"
54 #include "vlc_codec.h"
55 #include "vlc_filter.h"
56
57 #include "variables.h"
58 #ifndef WIN32
59 # include <unistd.h>
60 #else
61 # include <io.h>
62 # include <fcntl.h>
63 # include <errno.h> /* ENOSYS */
64 #endif
65 #ifdef HAVE_SYS_EVENTFD_H
66 # include <sys/eventfd.h>
67 #endif
68 #include <assert.h>
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 static int  DumpCommand( vlc_object_t *, char const *,
74                          vlc_value_t, vlc_value_t, void * );
75
76 static vlc_object_t * FindObject    ( vlc_object_t *, int, int );
77 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
78 static void           PrintObject   ( vlc_object_t *, const char * );
79 static void           DumpStructure ( vlc_object_t *, int, char * );
80
81 static vlc_list_t   * NewList       ( int );
82 static void           ListReplace   ( vlc_list_t *, vlc_object_t *, int );
83 /*static void           ListAppend    ( vlc_list_t *, vlc_object_t * );*/
84 static int            CountChildren ( vlc_object_t *, int );
85 static void           ListChildren  ( vlc_list_t *, vlc_object_t *, int );
86
87 static void vlc_object_destroy( vlc_object_t *p_this );
88 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
89 #ifndef NDEBUG
90 static void vlc_object_dump( vlc_object_t *p_this );
91 #endif
92
93 /*****************************************************************************
94  * Local structure lock
95  *****************************************************************************/
96 static void libvlc_lock (libvlc_int_t *p_libvlc)
97 {
98     vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
99 }
100
101 static void libvlc_unlock (libvlc_int_t *p_libvlc)
102 {
103     vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
104 }
105
106 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
107                            int i_type, const char *psz_type )
108 {
109     vlc_object_t *p_new;
110     vlc_object_internals_t *p_priv;
111
112     /* NOTE:
113      * VLC objects are laid out as follow:
114      * - first the LibVLC-private per-object data,
115      * - then VLC_COMMON members from vlc_object_t,
116      * - finally, the type-specific data (if any).
117      *
118      * This function initializes the LibVLC and common data,
119      * and zeroes the rest.
120      */
121     p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
122     if( p_priv == NULL )
123         return NULL;
124
125     assert (i_size >= sizeof (vlc_object_t));
126     p_new = (vlc_object_t *)(p_priv + 1);
127
128     p_priv->i_object_type = i_type;
129     p_new->psz_object_type = psz_type;
130     p_new->psz_object_name = NULL;
131
132     p_new->b_die = false;
133     p_new->b_error = false;
134     p_new->b_force = false;
135
136     p_new->psz_header = NULL;
137
138     if (p_this)
139         p_new->i_flags = p_this->i_flags
140             & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
141
142     p_priv->p_vars = calloc( 16, sizeof( variable_t ) );
143
144     if( !p_priv->p_vars )
145     {
146         free( p_priv );
147         return NULL;
148     }
149
150     if( p_this == NULL )
151     {
152         libvlc_int_t *self = (libvlc_int_t*)p_new;
153         p_new->p_libvlc = self;
154         vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
155         p_this = p_priv->next = p_priv->prev = p_new;
156     }
157     else
158         p_new->p_libvlc = p_this->p_libvlc;
159
160     vlc_spin_init( &p_priv->ref_spin );
161     p_priv->i_refcount = 1;
162     p_priv->pf_destructor = NULL;
163     p_priv->b_thread = false;
164     p_new->p_parent = NULL;
165     p_priv->pp_children = NULL;
166     p_priv->i_children = 0;
167
168     p_new->p_private = NULL;
169
170     /* Initialize mutexes and condvars */
171     vlc_mutex_init( &p_priv->lock );
172     vlc_mutex_init( &p_priv->var_lock );
173     vlc_cond_init( &p_priv->var_wait );
174     p_priv->pipes[0] = p_priv->pipes[1] = -1;
175
176     p_priv->next = p_this;
177     libvlc_lock (p_new->p_libvlc);
178     p_priv->prev = vlc_internals (p_this)->prev;
179     vlc_internals (p_this)->prev = p_new;
180     vlc_internals (p_priv->prev)->next = p_new;
181     libvlc_unlock (p_new->p_libvlc);
182
183     if (p_new == VLC_OBJECT(p_new->p_libvlc))
184     {   /* TODO: should be in src/libvlc.c */
185         int canc = vlc_savecancel ();
186         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
187         var_AddCallback( p_new, "list", DumpCommand, NULL );
188         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
189         var_AddCallback( p_new, "tree", DumpCommand, NULL );
190         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
191         var_AddCallback( p_new, "vars", DumpCommand, NULL );
192         vlc_restorecancel (canc);
193     }
194
195     return p_new;
196 }
197
198
199 /**
200  * Allocates and initializes a vlc object.
201  *
202  * @param i_type known object type (all of them are negative integer values),
203  *               or object byte size (always positive).
204  *
205  * @return the new object, or NULL on error.
206  */
207 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
208 {
209     const char   * psz_type;
210     size_t         i_size;
211
212     switch( i_type )
213     {
214         case VLC_OBJECT_INTF:
215             i_size = sizeof(intf_thread_t);
216             psz_type = "interface";
217             break;
218         case VLC_OBJECT_DECODER:
219             i_size = sizeof(decoder_t);
220             psz_type = "decoder";
221             break;
222         case VLC_OBJECT_PACKETIZER:
223             i_size = sizeof(decoder_t);
224             psz_type = "packetizer";
225             break;
226         case VLC_OBJECT_AOUT:
227             i_size = sizeof(aout_instance_t);
228             psz_type = "audio output";
229             break;
230         default:
231             assert( i_type > 0 ); /* unknown type?! */
232             i_size = i_type;
233             i_type = VLC_OBJECT_GENERIC;
234             psz_type = "generic";
235             break;
236     }
237
238     return vlc_custom_create( p_this, i_size, i_type, psz_type );
239 }
240
241
242 /**
243  ****************************************************************************
244  * Set the destructor of a vlc object
245  *
246  * This function sets the destructor of the vlc object. It will be called
247  * when the object is destroyed when the its refcount reaches 0.
248  * (It is called by the internal function vlc_object_destroy())
249  *****************************************************************************/
250 void __vlc_object_set_destructor( vlc_object_t *p_this,
251                                   vlc_destructor_t pf_destructor )
252 {
253     vlc_object_internals_t *p_priv = vlc_internals(p_this );
254     p_priv->pf_destructor = pf_destructor;
255 }
256
257 /**
258  ****************************************************************************
259  * Destroy a vlc object (Internal)
260  *
261  * This function destroys an object that has been previously allocated with
262  * vlc_object_create. The object's refcount must be zero and it must not be
263  * attached to other objects in any way.
264  *
265  * This function must be called with cancellation disabled (currently).
266  *****************************************************************************/
267 static void vlc_object_destroy( vlc_object_t *p_this )
268 {
269     vlc_object_internals_t *p_priv = vlc_internals( p_this );
270
271     /* Objects are always detached beforehand */
272     assert( !p_this->p_parent );
273
274     /* Send a kill to the object's thread if applicable */
275     vlc_object_kill( p_this );
276
277     /* Call the custom "subclass" destructor */
278     if( p_priv->pf_destructor )
279         p_priv->pf_destructor( p_this );
280
281     /* Any thread must have been cleaned up at this point. */
282     assert( !p_priv->b_thread );
283
284     /* Destroy the associated variables, starting from the end so that
285      * no memmove calls have to be done. */
286     while( p_priv->i_vars )
287     {
288         var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
289     }
290
291     free( p_priv->p_vars );
292     vlc_cond_destroy( &p_priv->var_wait );
293     vlc_mutex_destroy( &p_priv->var_lock );
294
295     free( p_this->psz_header );
296
297     FREENULL( p_this->psz_object_name );
298
299     vlc_spin_destroy( &p_priv->ref_spin );
300     vlc_mutex_destroy( &p_priv->lock );
301     if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
302         close( p_priv->pipes[1] );
303     if( p_priv->pipes[0] != -1 )
304         close( p_priv->pipes[0] );
305     if( VLC_OBJECT(p_this->p_libvlc) == p_this )
306         vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
307
308     free( p_priv );
309 }
310
311
312 /** Inter-object signaling */
313
314 void __vlc_object_lock( vlc_object_t *obj )
315 {
316     vlc_mutex_lock( &(vlc_internals(obj)->lock) );
317 }
318
319 void __vlc_object_unlock( vlc_object_t *obj )
320 {
321     vlc_assert_locked( &(vlc_internals(obj)->lock) );
322     vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
323 }
324 void __vlc_object_assert_locked( vlc_object_t *obj )
325 {
326     vlc_assert_locked( &(vlc_internals(obj)->lock) );
327 }
328
329 #ifdef WIN32
330 # include <winsock2.h>
331 # include <ws2tcpip.h>
332
333 /**
334  * select()-able pipes emulated using Winsock
335  */
336 static int pipe (int fd[2])
337 {
338     SOCKADDR_IN addr;
339     int addrlen = sizeof (addr);
340
341     SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
342            c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
343     if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
344         goto error;
345
346     memset (&addr, 0, sizeof (addr));
347     addr.sin_family = AF_INET;
348     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
349     if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
350      || getsockname (l, (PSOCKADDR)&addr, &addrlen)
351      || listen (l, 1)
352      || connect (c, (PSOCKADDR)&addr, addrlen))
353         goto error;
354
355     a = accept (l, NULL, NULL);
356     if (a == INVALID_SOCKET)
357         goto error;
358
359     closesocket (l);
360     //shutdown (a, 0);
361     //shutdown (c, 1);
362     fd[0] = c;
363     fd[1] = a;
364     return 0;
365
366 error:
367     if (l != INVALID_SOCKET)
368         closesocket (l);
369     if (c != INVALID_SOCKET)
370         closesocket (c);
371     return -1;
372 }
373
374 #undef  read
375 #define read( a, b, c )  recv (a, b, c, 0)
376 #undef  write
377 #define write( a, b, c ) send (a, b, c, 0)
378 #undef  close
379 #define close( a )       closesocket (a)
380 #endif /* WIN32 */
381
382 /**
383  * Returns the readable end of a pipe that becomes readable once termination
384  * of the object is requested (vlc_object_kill()).
385  * This can be used to wake-up out of a select() or poll() event loop, such
386  * typically when doing network I/O.
387  *
388  * Note that the pipe will remain the same for the lifetime of the object.
389  * DO NOT read the pipe nor close it yourself. Ever.
390  *
391  * @param obj object that would be "killed"
392  * @return a readable pipe descriptor, or -1 on error.
393  */
394 int vlc_object_waitpipe( vlc_object_t *obj )
395 {
396     vlc_object_internals_t *internals = vlc_internals( obj );
397
398     vlc_object_lock (obj);
399     if (internals->pipes[0] == -1)
400     {
401         /* This can only ever happen if someone killed us without locking: */
402         assert (internals->pipes[1] == -1);
403
404 #ifdef HAVE_EVENTFD
405         internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
406         if (internals->pipes[0] == -1)
407 #endif
408         {
409             if (pipe (internals->pipes))
410                 internals->pipes[0] = internals->pipes[1] = -1;
411         }
412
413         if (internals->pipes[0] != -1 && obj->b_die)
414         {   /* Race condition: vlc_object_kill() already invoked! */
415             msg_Dbg (obj, "waitpipe: object already dying");
416             write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
417         }
418     }
419     vlc_object_unlock (obj);
420     return internals->pipes[0];
421 }
422
423
424 /**
425  * Requests termination of an object, cancels the object thread, and make the
426  * object wait pipe (if it exists) readable. Not a cancellation point.
427  */
428 void __vlc_object_kill( vlc_object_t *p_this )
429 {
430     vlc_object_internals_t *priv = vlc_internals( p_this );
431     int fd = -1;
432
433     vlc_thread_cancel( p_this );
434     vlc_object_lock( p_this );
435     if( !p_this->b_die )
436     {
437         fd = priv->pipes[1];
438         p_this->b_die = true;
439     }
440
441     /* This also serves as a memory barrier toward vlc_object_alive(): */
442     vlc_object_unlock( p_this );
443
444     if (fd != -1)
445     {
446         int canc = vlc_savecancel ();
447
448         /* write _after_ setting b_die, so vlc_object_alive() returns false */
449         write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
450         msg_Dbg (p_this, "waitpipe: object killed");
451         vlc_restorecancel (canc);
452     }
453 }
454
455
456 /*****************************************************************************
457  * find a typed object and increment its refcount
458  *****************************************************************************
459  * This function recursively looks for a given object type. i_mode can be one
460  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
461  *****************************************************************************/
462 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
463 {
464     vlc_object_t *p_found;
465
466     /* If we are of the requested type ourselves, don't look further */
467     if( !(i_mode & FIND_STRICT)
468      && vlc_internals (p_this)->i_object_type == i_type )
469     {
470         vlc_object_hold( p_this );
471         return p_this;
472     }
473
474     /* Otherwise, recursively look for the object */
475     if ((i_mode & 0x000f) == FIND_ANYWHERE)
476         return vlc_object_find (p_this->p_libvlc, i_type,
477                                 (i_mode & ~0x000f)|FIND_CHILD);
478
479     libvlc_lock (p_this->p_libvlc);
480     p_found = FindObject( p_this, i_type, i_mode );
481     libvlc_unlock (p_this->p_libvlc);
482     return p_found;
483 }
484
485 #undef vlc_object_find_name
486 /**
487  * Finds a named object and increment its reference count.
488  * Beware that objects found in this manner can be "owned" by another thread,
489  * be of _any_ type, and be attached to any module (if any). With such an
490  * object reference, you can set or get object variables, emit log messages,
491  * and read write-once object parameters (psz_object_type, etc).
492  * You CANNOT cast the object to a more specific object type, and you
493  * definitely cannot invoke object type-specific callbacks with this.
494  *
495  * @param p_this object to search from
496  * @param psz_name name of the object to search for
497  * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
498  *
499  * @return a matching object (must be released by the caller),
500  * or NULL on error.
501  */
502 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
503                                     const char *psz_name, int i_mode )
504 {
505     vlc_object_t *p_found;
506
507     /* If have the requested name ourselves, don't look further */
508     if( !(i_mode & FIND_STRICT)
509         && p_this->psz_object_name
510         && !strcmp( p_this->psz_object_name, psz_name ) )
511     {
512         vlc_object_hold( p_this );
513         return p_this;
514     }
515
516     libvlc_lock (p_this->p_libvlc);
517
518     /* Otherwise, recursively look for the object */
519     if( (i_mode & 0x000f) == FIND_ANYWHERE )
520     {
521         vlc_object_t *p_root = p_this;
522
523         /* Find the root */
524         while( p_root->p_parent != NULL &&
525                p_root != VLC_OBJECT( p_this->p_libvlc ) )
526         {
527             p_root = p_root->p_parent;
528         }
529
530         p_found = FindObjectName( p_root, psz_name,
531                                  (i_mode & ~0x000f)|FIND_CHILD );
532         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
533         {
534             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
535                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
536         }
537     }
538     else
539     {
540         p_found = FindObjectName( p_this, psz_name, i_mode );
541     }
542
543     libvlc_unlock (p_this->p_libvlc);
544     return p_found;
545 }
546
547 /**
548  * Increment an object reference counter.
549  */
550 void * __vlc_object_hold( vlc_object_t *p_this )
551 {
552     vlc_object_internals_t *internals = vlc_internals( p_this );
553
554     vlc_spin_lock( &internals->ref_spin );
555     /* Avoid obvious freed object uses */
556     assert( internals->i_refcount > 0 );
557     /* Increment the counter */
558     internals->i_refcount++;
559     vlc_spin_unlock( &internals->ref_spin );
560     return p_this;
561 }
562
563 /*****************************************************************************
564  * Decrement an object refcount
565  * And destroy the object if its refcount reach zero.
566  *****************************************************************************/
567 void __vlc_object_release( vlc_object_t *p_this )
568 {
569     vlc_object_internals_t *internals = vlc_internals( p_this );
570     vlc_object_t *parent = NULL;
571     bool b_should_destroy;
572
573     vlc_spin_lock( &internals->ref_spin );
574     assert( internals->i_refcount > 0 );
575
576     if( internals->i_refcount > 1 )
577     {
578         /* Fast path */
579         /* There are still other references to the object */
580         internals->i_refcount--;
581         vlc_spin_unlock( &internals->ref_spin );
582         return;
583     }
584     vlc_spin_unlock( &internals->ref_spin );
585
586     /* Slow path */
587     /* Remember that we cannot hold the spin while waiting on the mutex */
588     libvlc_lock (p_this->p_libvlc);
589     /* Take the spin again. Note that another thread may have held the
590      * object in the (very short) mean time. */
591     vlc_spin_lock( &internals->ref_spin );
592     b_should_destroy = --internals->i_refcount == 0;
593     vlc_spin_unlock( &internals->ref_spin );
594
595     if( b_should_destroy )
596     {
597         /* We have no children */
598         assert (internals->i_children == 0);
599         parent = p_this->p_parent;
600
601 #ifndef NDEBUG
602         if( VLC_OBJECT(p_this->p_libvlc) == p_this )
603         {
604             /* Test for leaks */
605             vlc_object_t *leaked = internals->next;
606             while( leaked != p_this )
607             {
608                 /* We are leaking this object */
609                 fprintf( stderr,
610                          "ERROR: leaking object (%p, type:%s, name:%s)\n",
611                          leaked, leaked->psz_object_type,
612                          leaked->psz_object_name );
613                 /* Dump object to ease debugging */
614                 vlc_object_dump( leaked );
615                 fflush(stderr);
616                 leaked = vlc_internals (leaked)->next;
617             }
618
619             if( internals->next != p_this )
620                 /* Dump libvlc object to ease debugging */
621                 vlc_object_dump( p_this );
622         }
623 #endif
624         /* Remove the object from object list
625          * so that it cannot be encountered by vlc_object_get() */
626         vlc_internals (internals->next)->prev = internals->prev;
627         vlc_internals (internals->prev)->next = internals->next;
628
629         if (parent)
630             /* Detach from parent to protect against FIND_CHILDREN */
631             vlc_object_detach_unlocked (p_this);
632     }
633     libvlc_unlock (p_this->p_libvlc);
634
635     if( b_should_destroy )
636     {
637         int canc;
638
639         canc = vlc_savecancel ();
640         vlc_object_destroy( p_this );
641         vlc_restorecancel (canc);
642         if (parent)
643             vlc_object_release (parent);
644     }
645 }
646
647 /**
648  ****************************************************************************
649  * attach object to a parent object
650  *****************************************************************************
651  * This function sets p_this as a child of p_parent, and p_parent as a parent
652  * of p_this. This link can be undone using vlc_object_detach.
653  *****************************************************************************/
654 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
655 {
656     if( !p_this ) return;
657
658     vlc_object_hold (p_parent);
659     libvlc_lock (p_this->p_libvlc);
660
661     /* Attach the parent to its child */
662     assert (!p_this->p_parent);
663     p_this->p_parent = p_parent;
664
665     /* Attach the child to its parent */
666     vlc_object_internals_t *priv = vlc_internals( p_parent );
667     INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
668                  p_this );
669     libvlc_unlock (p_this->p_libvlc);
670 }
671
672
673 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
674 {
675     if (p_this->p_parent == NULL)
676         return;
677
678     vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
679
680     int i_index, i;
681
682     /* Remove p_this's parent */
683     p_this->p_parent = NULL;
684
685     /* Remove all of p_parent's children which are p_this */
686     for( i_index = priv->i_children ; i_index-- ; )
687     {
688         if( priv->pp_children[i_index] == p_this )
689         {
690             priv->i_children--;
691             for( i = i_index ; i < priv->i_children ; i++ )
692                 priv->pp_children[i] = priv->pp_children[i+1];
693         }
694     }
695
696     if( priv->i_children )
697     {
698         vlc_object_t **pp_children = (vlc_object_t **)
699             realloc( priv->pp_children,
700                      priv->i_children * sizeof(vlc_object_t *) );
701         if( pp_children )
702             priv->pp_children = pp_children;
703     }
704     else
705     {
706         /* Special case - don't realloc() to zero to avoid leaking */
707         free( priv->pp_children );
708         priv->pp_children = NULL;
709     }
710 }
711
712
713 /**
714  ****************************************************************************
715  * detach object from its parent
716  *****************************************************************************
717  * This function removes all links between an object and its parent.
718  *****************************************************************************/
719 void __vlc_object_detach( vlc_object_t *p_this )
720 {
721     vlc_object_t *p_parent;
722     if( !p_this ) return;
723
724     libvlc_lock (p_this->p_libvlc);
725     p_parent = p_this->p_parent;
726     if (p_parent)
727         vlc_object_detach_unlocked( p_this );
728     libvlc_unlock (p_this->p_libvlc);
729
730     if (p_parent)
731         vlc_object_release (p_parent);
732 }
733
734
735 /**
736  ****************************************************************************
737  * find a list typed objects and increment their refcount
738  *****************************************************************************
739  * This function recursively looks for a given object type. i_mode can be one
740  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
741  *****************************************************************************/
742 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
743 {
744     vlc_list_t *p_list;
745     int i_count = 0;
746
747     /* Look for the objects */
748     switch( i_mode & 0x000f )
749     {
750     case FIND_ANYWHERE:
751         return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
752
753     case FIND_CHILD:
754         libvlc_lock (p_this->p_libvlc);
755         i_count = CountChildren( p_this, i_type );
756         p_list = NewList( i_count );
757
758         /* Check allocation was successful */
759         if( p_list->i_count != i_count )
760         {
761             libvlc_unlock (p_this->p_libvlc);
762             p_list->i_count = 0;
763             break;
764         }
765
766         p_list->i_count = 0;
767         ListChildren( p_list, p_this, i_type );
768         libvlc_unlock (p_this->p_libvlc);
769         break;
770
771     default:
772         msg_Err( p_this, "unimplemented!" );
773         p_list = NewList( 0 );
774         break;
775     }
776
777     return p_list;
778 }
779
780 /**
781  * Gets the list of children of an objects, and increment their reference
782  * count.
783  * @return a list (possibly empty) or NULL in case of error.
784  */
785 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
786 {
787     vlc_list_t *l;
788     vlc_object_internals_t *priv = vlc_internals( obj );
789
790     libvlc_lock (obj->p_libvlc);
791     l = NewList( priv->i_children );
792     for (int i = 0; i < l->i_count; i++)
793     {
794         vlc_object_hold( priv->pp_children[i] );
795         l->p_values[i].p_object = priv->pp_children[i];
796     }
797     libvlc_unlock (obj->p_libvlc);
798     return l;
799 }
800
801 /*****************************************************************************
802  * DumpCommand: print the current vlc structure
803  *****************************************************************************
804  * This function prints either an ASCII tree showing the connections between
805  * vlc objects, and additional information such as their refcount, thread ID,
806  * etc. (command "tree"), or the same data as a simple list (command "list").
807  *****************************************************************************/
808 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
809                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
810 {
811     libvlc_int_t *p_libvlc = p_this->p_libvlc;
812
813     (void)oldval; (void)p_data;
814     if( *psz_cmd == 'l' )
815     {
816         vlc_object_t *cur = VLC_OBJECT (p_libvlc);
817
818         libvlc_lock (p_this->p_libvlc);
819         do
820         {
821             PrintObject (cur, "");
822             cur = vlc_internals (cur)->next;
823         }
824         while (cur != VLC_OBJECT(p_libvlc));
825         libvlc_unlock (p_this->p_libvlc);
826     }
827     else
828     {
829         vlc_object_t *p_object = NULL;
830
831         if( *newval.psz_string )
832         {
833             /* try using the object's name to find it */
834             p_object = vlc_object_find_name( p_this, newval.psz_string,
835                                              FIND_ANYWHERE );
836             if( !p_object )
837             {
838                 return VLC_ENOOBJ;
839             }
840         }
841
842         libvlc_lock (p_this->p_libvlc);
843         if( *psz_cmd == 't' )
844         {
845             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
846
847             if( !p_object )
848                 p_object = VLC_OBJECT(p_this->p_libvlc);
849
850             psz_foo[0] = '|';
851             DumpStructure( p_object, 0, psz_foo );
852         }
853         else if( *psz_cmd == 'v' )
854         {
855             int i;
856
857             if( !p_object )
858                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
859
860             PrintObject( p_object, "" );
861
862             if( !vlc_internals( p_object )->i_vars )
863                 printf( " `-o No variables\n" );
864             for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
865             {
866                 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
867
868                 const char *psz_type = "unknown";
869                 switch( p_var->i_type & VLC_VAR_TYPE )
870                 {
871 #define MYCASE( type, nice )                \
872                     case VLC_VAR_ ## type:  \
873                         psz_type = nice;    \
874                         break;
875                     MYCASE( VOID, "void" );
876                     MYCASE( BOOL, "bool" );
877                     MYCASE( INTEGER, "integer" );
878                     MYCASE( HOTKEY, "hotkey" );
879                     MYCASE( STRING, "string" );
880                     MYCASE( MODULE, "module" );
881                     MYCASE( FILE, "file" );
882                     MYCASE( DIRECTORY, "directory" );
883                     MYCASE( VARIABLE, "variable" );
884                     MYCASE( FLOAT, "float" );
885                     MYCASE( TIME, "time" );
886                     MYCASE( ADDRESS, "address" );
887                     MYCASE( MUTEX, "mutex" );
888                     MYCASE( LIST, "list" );
889 #undef MYCASE
890                 }
891                 printf( " %c-o \"%s\" (%s",
892                         i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
893                         p_var->psz_name, psz_type );
894                 if( p_var->psz_text )
895                     printf( ", %s", p_var->psz_text );
896                 printf( ")" );
897                 if( p_var->i_type & VLC_VAR_HASCHOICE )
898                     printf( ", has choices" );
899                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
900                     printf( ", command" );
901                 if( p_var->i_entries )
902                     printf( ", %d callbacks", p_var->i_entries );
903                 switch( p_var->i_type & VLC_VAR_CLASS )
904                 {
905                     case VLC_VAR_VOID:
906                     case VLC_VAR_MUTEX:
907                         break;
908                     case VLC_VAR_BOOL:
909                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
910                         break;
911                     case VLC_VAR_INTEGER:
912                         printf( ": %d", p_var->val.i_int );
913                         break;
914                     case VLC_VAR_STRING:
915                         printf( ": \"%s\"", p_var->val.psz_string );
916                         break;
917                     case VLC_VAR_FLOAT:
918                         printf( ": %f", p_var->val.f_float );
919                         break;
920                     case VLC_VAR_TIME:
921                         printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
922                         break;
923                     case VLC_VAR_ADDRESS:
924                         printf( ": %p", p_var->val.p_address );
925                         break;
926                     case VLC_VAR_LIST:
927                         printf( ": TODO" );
928                         break;
929                 }
930                 printf( "\n" );
931             }
932         }
933         libvlc_unlock (p_this->p_libvlc);
934
935         if( *newval.psz_string )
936         {
937             vlc_object_release( p_object );
938         }
939     }
940
941     return VLC_SUCCESS;
942 }
943
944 /*****************************************************************************
945  * vlc_list_release: free a list previously allocated by vlc_list_find
946  *****************************************************************************
947  * This function decreases the refcount of all objects in the list and
948  * frees the list.
949  *****************************************************************************/
950 void vlc_list_release( vlc_list_t *p_list )
951 {
952     int i_index;
953
954     for( i_index = 0; i_index < p_list->i_count; i_index++ )
955     {
956         vlc_object_release( p_list->p_values[i_index].p_object );
957     }
958
959     free( p_list->p_values );
960     free( p_list );
961 }
962
963 /*****************************************************************************
964  * dump an object. (Debug function)
965  *****************************************************************************/
966 #ifndef NDEBUG
967 static void vlc_object_dump( vlc_object_t *p_this )
968 {
969     char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
970     psz_foo[0] = '|';
971
972     DumpStructure( p_this, 0, psz_foo );
973 }
974 #endif
975
976 /* Following functions are local */
977
978 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
979 {
980     int i;
981     vlc_object_t *p_tmp;
982
983     switch( i_mode & 0x000f )
984     {
985     case FIND_PARENT:
986         p_tmp = p_this->p_parent;
987         if( p_tmp )
988         {
989             if( vlc_internals( p_tmp )->i_object_type == i_type )
990             {
991                 vlc_object_hold( p_tmp );
992                 return p_tmp;
993             }
994             else
995             {
996                 return FindObject( p_tmp, i_type, i_mode );
997             }
998         }
999         break;
1000
1001     case FIND_CHILD:
1002         for( i = vlc_internals( p_this )->i_children; i--; )
1003         {
1004             p_tmp = vlc_internals( p_this )->pp_children[i];
1005             if( vlc_internals( p_tmp )->i_object_type == i_type )
1006             {
1007                 vlc_object_hold( p_tmp );
1008                 return p_tmp;
1009             }
1010             else if( vlc_internals( p_tmp )->i_children )
1011             {
1012                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1013                 if( p_tmp )
1014                 {
1015                     return p_tmp;
1016                 }
1017             }
1018         }
1019         break;
1020
1021     case FIND_ANYWHERE:
1022         /* Handled in vlc_object_find */
1023         break;
1024     }
1025
1026     return NULL;
1027 }
1028
1029 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1030                                       const char *psz_name,
1031                                       int i_mode )
1032 {
1033     int i;
1034     vlc_object_t *p_tmp;
1035
1036     switch( i_mode & 0x000f )
1037     {
1038     case FIND_PARENT:
1039         p_tmp = p_this->p_parent;
1040         if( p_tmp )
1041         {
1042             if( p_tmp->psz_object_name
1043                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1044             {
1045                 vlc_object_hold( p_tmp );
1046                 return p_tmp;
1047             }
1048             else
1049             {
1050                 return FindObjectName( p_tmp, psz_name, i_mode );
1051             }
1052         }
1053         break;
1054
1055     case FIND_CHILD:
1056         for( i = vlc_internals( p_this )->i_children; i--; )
1057         {
1058             p_tmp = vlc_internals( p_this )->pp_children[i];
1059             if( p_tmp->psz_object_name
1060                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1061             {
1062                 vlc_object_hold( p_tmp );
1063                 return p_tmp;
1064             }
1065             else if( vlc_internals( p_tmp )->i_children )
1066             {
1067                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1068                 if( p_tmp )
1069                 {
1070                     return p_tmp;
1071                 }
1072             }
1073         }
1074         break;
1075
1076     case FIND_ANYWHERE:
1077         /* Handled in vlc_object_find */
1078         break;
1079     }
1080
1081     return NULL;
1082 }
1083
1084
1085 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1086 {
1087     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1088          psz_parent[20];
1089
1090     int canc = vlc_savecancel ();
1091     memset( &psz_name, 0, sizeof(psz_name) );
1092     if( p_this->psz_object_name )
1093     {
1094         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1095         if( psz_name[48] )
1096             psz_name[48] = '\"';
1097     }
1098
1099     psz_children[0] = '\0';
1100     switch( vlc_internals( p_this )->i_children )
1101     {
1102         case 0:
1103             break;
1104         case 1:
1105             strcpy( psz_children, ", 1 child" );
1106             break;
1107         default:
1108             snprintf( psz_children, 19, ", %i children",
1109                       vlc_internals( p_this )->i_children );
1110             break;
1111     }
1112
1113     psz_refcount[0] = '\0';
1114     if( vlc_internals( p_this )->i_refcount > 0 )
1115         snprintf( psz_refcount, 19, ", refcount %u",
1116                   vlc_internals( p_this )->i_refcount );
1117
1118     psz_thread[0] = '\0';
1119     if( vlc_internals( p_this )->b_thread )
1120         snprintf( psz_thread, 29, " (thread %lu)",
1121                   (unsigned long)vlc_internals( p_this )->thread_id );
1122
1123     psz_parent[0] = '\0';
1124     if( p_this->p_parent )
1125         snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1126
1127     printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1128             p_this, p_this->psz_object_type,
1129             psz_name, psz_thread, psz_refcount, psz_children,
1130             psz_parent );
1131     vlc_restorecancel (canc);
1132 }
1133
1134 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1135 {
1136     int i;
1137     char i_back = psz_foo[i_level];
1138     psz_foo[i_level] = '\0';
1139
1140     PrintObject( p_this, psz_foo );
1141
1142     psz_foo[i_level] = i_back;
1143
1144     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1145     {
1146         msg_Warn( p_this, "structure tree is too deep" );
1147         return;
1148     }
1149
1150     for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1151     {
1152         if( i_level )
1153         {
1154             psz_foo[i_level-1] = ' ';
1155
1156             if( psz_foo[i_level-2] == '`' )
1157             {
1158                 psz_foo[i_level-2] = ' ';
1159             }
1160         }
1161
1162         if( i == vlc_internals( p_this )->i_children - 1 )
1163         {
1164             psz_foo[i_level] = '`';
1165         }
1166         else
1167         {
1168             psz_foo[i_level] = '|';
1169         }
1170
1171         psz_foo[i_level+1] = '-';
1172         psz_foo[i_level+2] = '\0';
1173
1174         DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1175                        psz_foo );
1176     }
1177 }
1178
1179 static vlc_list_t * NewList( int i_count )
1180 {
1181     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1182     if( p_list == NULL )
1183     {
1184         return NULL;
1185     }
1186
1187     p_list->i_count = i_count;
1188
1189     if( i_count == 0 )
1190     {
1191         p_list->p_values = NULL;
1192         return p_list;
1193     }
1194
1195     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1196     if( p_list->p_values == NULL )
1197     {
1198         p_list->i_count = 0;
1199         return p_list;
1200     }
1201
1202     return p_list;
1203 }
1204
1205 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1206                          int i_index )
1207 {
1208     if( p_list == NULL || i_index >= p_list->i_count )
1209     {
1210         return;
1211     }
1212
1213     vlc_object_hold( p_object );
1214
1215     p_list->p_values[i_index].p_object = p_object;
1216
1217     return;
1218 }
1219
1220 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1221 {
1222     if( p_list == NULL )
1223     {
1224         return;
1225     }
1226
1227     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1228                                 * sizeof( vlc_value_t ) );
1229     if( p_list->p_values == NULL )
1230     {
1231         p_list->i_count = 0;
1232         return;
1233     }
1234
1235     vlc_object_hold( p_object );
1236
1237     p_list->p_values[p_list->i_count].p_object = p_object;
1238     p_list->i_count++;
1239
1240     return;
1241 }*/
1242
1243 static int CountChildren( vlc_object_t *p_this, int i_type )
1244 {
1245     vlc_object_t *p_tmp;
1246     int i, i_count = 0;
1247
1248     for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1249     {
1250         p_tmp = vlc_internals( p_this )->pp_children[i];
1251
1252         if( vlc_internals( p_tmp )->i_object_type == i_type )
1253         {
1254             i_count++;
1255         }
1256         i_count += CountChildren( p_tmp, i_type );
1257     }
1258
1259     return i_count;
1260 }
1261
1262 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1263 {
1264     vlc_object_t *p_tmp;
1265     int i;
1266
1267     for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1268     {
1269         p_tmp = vlc_internals( p_this )->pp_children[i];
1270
1271         if( vlc_internals( p_tmp )->i_object_type == i_type )
1272             ListReplace( p_list, p_tmp, p_list->i_count++ );
1273
1274         ListChildren( p_list, p_tmp, i_type );
1275     }
1276 }