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