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