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