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