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