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