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