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