]> git.sesse.net Git - vlc/blob - src/misc/objects.c
Remove VLC_OBJECT_SOUT
[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
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37
38 #include "../libvlc.h"
39 #include <vlc_vout.h>
40 #include <vlc_aout.h>
41 #include "audio_output/aout_internal.h"
42
43 #include <vlc_access.h>
44 #include <vlc_demux.h>
45 #include <vlc_stream.h>
46
47 #include <vlc_sout.h>
48 #include "stream_output/stream_output.h"
49
50 #include "vlc_interface.h"
51 #include "vlc_codec.h"
52 #include "vlc_filter.h"
53
54 #include "variables.h"
55 #ifndef WIN32
56 # include <unistd.h>
57 #else
58 # include <io.h>
59 # include <fcntl.h>
60 # include <errno.h> /* ENOSYS */
61 #endif
62 #include <assert.h>
63
64 /*****************************************************************************
65  * Local prototypes
66  *****************************************************************************/
67 static int  DumpCommand( vlc_object_t *, char const *,
68                          vlc_value_t, vlc_value_t, void * );
69
70 static vlc_object_t * FindObject    ( vlc_object_t *, int, int );
71 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
72 static void           PrintObject   ( vlc_object_t *, const char * );
73 static void           DumpStructure ( vlc_object_t *, int, char * );
74
75 static vlc_list_t   * NewList       ( int );
76 static void           ListReplace   ( vlc_list_t *, vlc_object_t *, int );
77 /*static void           ListAppend    ( vlc_list_t *, vlc_object_t * );*/
78 static int            CountChildren ( vlc_object_t *, int );
79 static void           ListChildren  ( vlc_list_t *, vlc_object_t *, int );
80
81 static void vlc_object_destroy( vlc_object_t *p_this );
82 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
83
84 #ifdef LIBVLC_REFCHECK
85 static vlc_threadvar_t held_objects;
86 typedef struct held_list_t
87 {
88     struct held_list_t *next;
89     vlc_object_t *obj;
90 } held_list_t;
91 static void held_objects_destroy (void *);
92 #endif
93
94 /*****************************************************************************
95  * Local structure lock
96  *****************************************************************************/
97 static vlc_mutex_t     structure_lock;
98
99 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
100                          int i_type, const char *psz_type )
101 {
102     vlc_object_t *p_new;
103     vlc_object_internals_t *p_priv;
104
105     /* NOTE:
106      * VLC objects are laid out as follow:
107      * - first the LibVLC-private per-object data,
108      * - then VLC_COMMON members from vlc_object_t,
109      * - finally, the type-specific data (if any).
110      *
111      * This function initializes the LibVLC and common data,
112      * and zeroes the rest.
113      */
114     p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
115     if( p_priv == NULL )
116         return NULL;
117
118     assert (i_size >= sizeof (vlc_object_t));
119     p_new = (vlc_object_t *)(p_priv + 1);
120
121     p_new->i_object_type = i_type;
122     p_new->psz_object_type = psz_type;
123     p_new->psz_object_name = NULL;
124
125     p_new->b_die = false;
126     p_new->b_error = false;
127     p_new->b_dead = false;
128     p_new->b_force = false;
129
130     p_new->psz_header = NULL;
131
132     if (p_this)
133         p_new->i_flags = p_this->i_flags
134             & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
135
136     p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
137
138     if( !p_priv->p_vars )
139     {
140         free( p_priv );
141         return NULL;
142     }
143
144     libvlc_global_data_t *p_libvlc_global;
145     if( p_this == NULL )
146     {
147         /* Only the global root object is created out of the blue */
148         p_libvlc_global = (libvlc_global_data_t *)p_new;
149         p_new->p_libvlc = NULL;
150
151         p_libvlc_global->i_counter = 0;
152         p_priv->next = p_priv->prev = p_new;
153         vlc_mutex_init( &structure_lock );
154 #ifdef LIBVLC_REFCHECK
155         /* TODO: use the destruction callback to track ref leaks */
156         vlc_threadvar_create( &held_objects, held_objects_destroy );
157 #endif
158     }
159     else
160     {
161         p_libvlc_global = vlc_global();
162         if( i_type == VLC_OBJECT_LIBVLC )
163             p_new->p_libvlc = (libvlc_int_t*)p_new;
164         else
165             p_new->p_libvlc = p_this->p_libvlc;
166     }
167
168     vlc_spin_init( &p_priv->ref_spin );
169     p_priv->i_refcount = 1;
170     p_priv->pf_destructor = NULL;
171     p_priv->b_thread = false;
172     p_new->p_parent = NULL;
173     p_priv->pp_children = NULL;
174     p_priv->i_children = 0;
175
176     p_new->p_private = NULL;
177
178     /* Initialize mutexes and condvars */
179     vlc_mutex_init( &p_new->object_lock );
180     vlc_cond_init( p_new, &p_new->object_wait );
181     vlc_mutex_init( &p_priv->var_lock );
182     vlc_spin_init( &p_priv->spin );
183     p_priv->pipes[0] = p_priv->pipes[1] = -1;
184
185     p_priv->next = VLC_OBJECT (p_libvlc_global);
186 #if !defined (LIBVLC_REFCHECK)
187     /* ... */
188 #elif defined (LIBVLC_USE_PTHREAD)
189     p_priv->creator_id = pthread_self ();
190 #elif defined (WIN32)
191     p_priv->creator_id = GetCurrentThreadId ();
192 #endif
193     vlc_mutex_lock( &structure_lock );
194     p_priv->prev = vlc_internals (p_libvlc_global)->prev;
195     vlc_internals (p_libvlc_global)->prev = p_new;
196     vlc_internals (p_priv->prev)->next = p_new;
197     p_new->i_object_id = p_libvlc_global->i_counter++;
198     vlc_mutex_unlock( &structure_lock );
199
200     if( i_type == VLC_OBJECT_LIBVLC )
201     {
202         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
203         var_AddCallback( p_new, "list", DumpCommand, NULL );
204         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
205         var_AddCallback( p_new, "tree", DumpCommand, NULL );
206         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
207         var_AddCallback( p_new, "vars", DumpCommand, NULL );
208     }
209
210     return p_new;
211 }
212
213
214 /**
215  * Allocates and initializes a vlc object.
216  *
217  * @param i_type known object type (all of them are negative integer values),
218  *               or object byte size (always positive).
219  *
220  * @return the new object, or NULL on error.
221  */
222 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
223 {
224     const char   * psz_type;
225     size_t         i_size;
226
227     switch( i_type )
228     {
229         case VLC_OBJECT_LIBVLC:
230             i_size = sizeof(libvlc_int_t);
231             psz_type = "libvlc";
232             break;
233         case VLC_OBJECT_INTF:
234             i_size = sizeof(intf_thread_t);
235             psz_type = "interface";
236             break;
237         case VLC_OBJECT_DIALOGS:
238             i_size = sizeof(intf_thread_t);
239             psz_type = "dialogs";
240             break;
241         case VLC_OBJECT_DEMUX:
242             i_size = sizeof(demux_t);
243             psz_type = "demux";
244             break;
245         case VLC_OBJECT_ACCESS:
246             i_size = sizeof(access_t);
247             psz_type = "access";
248             break;
249         case VLC_OBJECT_DECODER:
250             i_size = sizeof(decoder_t);
251             psz_type = "decoder";
252             break;
253         case VLC_OBJECT_PACKETIZER:
254             i_size = sizeof(decoder_t);
255             psz_type = "packetizer";
256             break;
257         case VLC_OBJECT_ENCODER:
258             i_size = sizeof(encoder_t);
259             psz_type = "encoder";
260             break;
261         case VLC_OBJECT_FILTER:
262             i_size = sizeof(filter_t);
263             psz_type = "filter";
264             break;
265         case VLC_OBJECT_VOUT:
266             i_size = sizeof(vout_thread_t);
267             psz_type = "video output";
268             break;
269         case VLC_OBJECT_AOUT:
270             i_size = sizeof(aout_instance_t);
271             psz_type = "audio output";
272             break;
273         case VLC_OBJECT_OPENGL:
274             i_size = sizeof( vout_thread_t );
275             psz_type = "opengl";
276             break;
277         case VLC_OBJECT_ANNOUNCE:
278             i_size = sizeof( announce_handler_t );
279             psz_type = "announce";
280             break;
281         case VLC_OBJECT_INTERACTION:
282             i_size = sizeof( interaction_t );
283             psz_type = "interaction";
284             break;
285         default:
286             i_size = i_type > (int)sizeof(vlc_object_t)
287                          ? i_type : (int)sizeof(vlc_object_t);
288             i_type = VLC_OBJECT_GENERIC;
289             psz_type = "generic";
290             break;
291     }
292
293     return vlc_custom_create( p_this, i_size, i_type, psz_type );
294 }
295
296
297 /**
298  ****************************************************************************
299  * Set the destructor of a vlc object
300  *
301  * This function sets the destructor of the vlc object. It will be called
302  * when the object is destroyed when the its refcount reaches 0.
303  * (It is called by the internal function vlc_object_destroy())
304  *****************************************************************************/
305 void __vlc_object_set_destructor( vlc_object_t *p_this,
306                                   vlc_destructor_t pf_destructor )
307 {
308     vlc_object_internals_t *p_priv = vlc_internals(p_this );
309     p_priv->pf_destructor = pf_destructor;
310 }
311
312 /**
313  ****************************************************************************
314  * Destroy a vlc object (Internal)
315  *
316  * This function destroys an object that has been previously allocated with
317  * vlc_object_create. The object's refcount must be zero and it must not be
318  * attached to other objects in any way.
319  *****************************************************************************/
320 static void vlc_object_destroy( vlc_object_t *p_this )
321 {
322     vlc_object_internals_t *p_priv = vlc_internals( p_this );
323
324     /* Objects are always detached beforehand */
325     assert( !p_this->p_parent );
326
327     /* Send a kill to the object's thread if applicable */
328     vlc_object_kill( p_this );
329
330     /* If we are running on a thread, wait until it ends */
331     if( p_priv->b_thread )
332         vlc_thread_join( p_this );
333
334     /* Call the custom "subclass" destructor */
335     if( p_priv->pf_destructor )
336         p_priv->pf_destructor( p_this );
337
338     /* Destroy the associated variables, starting from the end so that
339      * no memmove calls have to be done. */
340     while( p_priv->i_vars )
341     {
342         var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
343     }
344
345     free( p_priv->p_vars );
346     vlc_mutex_destroy( &p_priv->var_lock );
347
348     free( p_this->psz_header );
349
350     if( p_this->p_libvlc == NULL )
351     {
352         libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
353
354 #ifndef NDEBUG
355         assert( p_global == vlc_global() );
356         /* Test for leaks */
357         if (p_priv->next != p_this)
358         {
359             vlc_object_t *leaked = p_priv->next, *first = leaked;
360             do
361             {
362                 /* We are leaking this object */
363                 fprintf( stderr,
364                          "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
365                          leaked->i_object_id, leaked->psz_object_type,
366                          leaked->psz_object_name );
367                 /* Dump libvlc object to ease debugging */
368                 vlc_object_dump( leaked );
369                 fflush(stderr);
370                 leaked = vlc_internals (leaked)->next;
371             }
372             while (leaked != first);
373
374             /* Dump global object to ease debugging */
375             vlc_object_dump( p_this );
376             /* Strongly abort, cause we want these to be fixed */
377             abort();
378         }
379 #endif
380
381         /* We are the global object ... no need to lock. */
382         vlc_mutex_destroy( &structure_lock );
383 #ifdef LIBVLC_REFCHECK
384         held_objects_destroy( vlc_threadvar_get( &held_objects ) );
385         vlc_threadvar_delete( &held_objects );
386 #endif
387     }
388
389     FREENULL( p_this->psz_object_name );
390
391 #if defined(WIN32) || defined(UNDER_CE)
392     /* if object has an associated thread, close it now */
393     if( p_priv->thread_id )
394        CloseHandle(p_priv->thread_id);
395 #endif
396
397     vlc_spin_destroy( &p_priv->ref_spin );
398     vlc_mutex_destroy( &p_this->object_lock );
399     vlc_cond_destroy( &p_this->object_wait );
400     vlc_spin_destroy( &p_priv->spin );
401     if( p_priv->pipes[1] != -1 )
402         close( p_priv->pipes[1] );
403     if( p_priv->pipes[0] != -1 )
404         close( p_priv->pipes[0] );
405
406     free( p_priv );
407 }
408
409
410 /** Inter-object signaling */
411
412 void __vlc_object_lock( vlc_object_t *obj )
413 {
414     vlc_mutex_lock( &obj->object_lock );
415 }
416
417 void __vlc_object_unlock( vlc_object_t *obj )
418 {
419     vlc_assert_locked( &obj->object_lock );
420     vlc_mutex_unlock( &obj->object_lock );
421 }
422
423 #ifdef WIN32
424 # include <winsock2.h>
425 # include <ws2tcpip.h>
426
427 /**
428  * select()-able pipes emulated using Winsock
429  */
430 static int pipe (int fd[2])
431 {
432     SOCKADDR_IN addr;
433     int addrlen = sizeof (addr);
434
435     SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
436            c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
437     if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
438         goto error;
439
440     memset (&addr, 0, sizeof (addr));
441     addr.sin_family = AF_INET;
442     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
443     if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
444      || getsockname (l, (PSOCKADDR)&addr, &addrlen)
445      || listen (l, 1)
446      || connect (c, (PSOCKADDR)&addr, addrlen))
447         goto error;
448
449     a = accept (l, NULL, NULL);
450     if (a == INVALID_SOCKET)
451         goto error;
452
453     closesocket (l);
454     //shutdown (a, 0);
455     //shutdown (c, 1);
456     fd[0] = c;
457     fd[1] = a;
458     return 0;
459
460 error:
461     if (l != INVALID_SOCKET)
462         closesocket (l);
463     if (c != INVALID_SOCKET)
464         closesocket (c);
465     return -1;
466 }
467
468 #undef  read
469 #define read( a, b, c )  recv (a, b, c, 0)
470 #undef  write
471 #define write( a, b, c ) send (a, b, c, 0)
472 #undef  close
473 #define close( a )       closesocket (a)
474 #endif /* WIN32 */
475
476 /**
477  * Returns the readable end of a pipe that becomes readable once termination
478  * of the object is requested (vlc_object_kill()).
479  * This can be used to wake-up out of a select() or poll() event loop, such
480  * typically when doing network I/O.
481  *
482  * Note that the pipe will remain the same for the lifetime of the object.
483  * DO NOT read the pipe nor close it yourself. Ever.
484  *
485  * @param obj object that would be "killed"
486  * @return a readable pipe descriptor, or -1 on error.
487  */
488 int __vlc_object_waitpipe( vlc_object_t *obj )
489 {
490     int pfd[2] = { -1, -1 };
491     vlc_object_internals_t *internals = vlc_internals( obj );
492     bool killed = false;
493
494     vlc_spin_lock (&internals->spin);
495     if (internals->pipes[0] == -1)
496     {
497         /* This can only ever happen if someone killed us without locking: */
498         assert (internals->pipes[1] == -1);
499         vlc_spin_unlock (&internals->spin);
500
501         if (pipe (pfd))
502             return -1;
503
504         vlc_spin_lock (&internals->spin);
505         if (internals->pipes[0] == -1)
506         {
507             internals->pipes[0] = pfd[0];
508             internals->pipes[1] = pfd[1];
509             pfd[0] = pfd[1] = -1;
510         }
511         killed = obj->b_die;
512     }
513     vlc_spin_unlock (&internals->spin);
514
515     if (killed)
516     {
517         /* Race condition: vlc_object_kill() already invoked! */
518         int fd;
519
520         vlc_spin_lock (&internals->spin);
521         fd = internals->pipes[1];
522         internals->pipes[1] = -1;
523         vlc_spin_unlock (&internals->spin);
524
525         msg_Dbg (obj, "waitpipe: object already dying");
526         if (fd != -1)
527             close (fd);
528     }
529
530     /* Race condition: two threads call pipe() - unlikely */
531     if (pfd[0] != -1)
532         close (pfd[0]);
533     if (pfd[1] != -1)
534         close (pfd[1]);
535
536     return internals->pipes[0];
537 }
538
539
540 /**
541  * Waits for the object to be signaled (using vlc_object_signal()).
542  * It is assumed that the caller has locked the object. This function will
543  * unlock the object, and lock it again before returning.
544  * If the object was signaled before the caller locked the object, it is
545  * undefined whether the signal will be lost or will wake the process.
546  *
547  * @return true if the object is dying and should terminate.
548  */
549 void __vlc_object_wait( vlc_object_t *obj )
550 {
551     vlc_assert_locked( &obj->object_lock );
552     vlc_cond_wait( &obj->object_wait, &obj->object_lock );
553 }
554
555
556 /**
557  * Waits for the object to be signaled (using vlc_object_signal()), or for
558  * a timer to expire. It is asserted that the caller holds the object lock.
559  *
560  * @return 0 if the object was signaled before the timer expiration, or
561  * ETIMEDOUT if the timer expired without any signal.
562  */
563 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
564 {
565     vlc_assert_locked( &obj->object_lock );
566     return vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
567 }
568
569
570 /**
571  * Checks whether an object has been "killed".
572  * The object lock must be held.
573  *
574  * Typical code for an object thread could be:
575  *
576    vlc_object_lock (self);
577    ...initialization...
578    while (vlc_object_alive (self))
579    {
580        ...preprocessing...
581
582        vlc_object_wait (self);
583
584        ...postprocessing...
585    }
586    ...deinitialization...
587    vlc_object_unlock (self);
588  *
589  *
590  * @return true iff the object has not been killed yet
591  */
592 bool __vlc_object_alive( vlc_object_t *obj )
593 {
594     vlc_assert_locked( &obj->object_lock );
595     return !obj->b_die;
596 }
597
598
599 /**
600  * Signals an object for which the lock is held.
601  * At least one thread currently sleeping in vlc_object_wait() or
602  * vlc_object_timedwait() will wake up, assuming that there is at least one
603  * such thread in the first place. Otherwise, it is undefined whether the
604  * signal will be lost or will wake up one or more thread later.
605  */
606 void __vlc_object_signal_unlocked( vlc_object_t *obj )
607 {
608     vlc_assert_locked (&obj->object_lock);
609     vlc_cond_signal( &obj->object_wait );
610 }
611
612
613 /**
614  * Requests termination of an object.
615  * If the object is LibVLC, also request to terminate all its children.
616  */
617 void __vlc_object_kill( vlc_object_t *p_this )
618 {
619     vlc_object_internals_t *internals = vlc_internals( p_this );
620     int fd;
621
622     vlc_mutex_lock( &p_this->object_lock );
623     p_this->b_die = true;
624
625     vlc_spin_lock (&internals->spin);
626     fd = internals->pipes[1];
627     internals->pipes[1] = -1;
628     vlc_spin_unlock (&internals->spin);
629
630     if( fd != -1 )
631     {
632         msg_Dbg (p_this, "waitpipe: object killed");
633         close (fd);
634     }
635
636     vlc_object_signal_unlocked( p_this );
637     vlc_mutex_unlock( &p_this->object_lock );
638
639     if (p_this->i_object_type == VLC_OBJECT_LIBVLC)
640     {
641         vlc_list_t *children = vlc_list_children (p_this);
642         for (int i = 0; i < children->i_count; i++)
643             vlc_object_kill (children->p_values[i].p_object);
644         vlc_list_release (children);
645     }
646 }
647
648
649 /**
650  * Find an object given its ID.
651  *
652  * This function looks for the object whose i_object_id field is i_id.
653  * This function is slow, and often used to hide bugs. Do not use it.
654  * If you need to retain reference to an object, yield the object pointer with
655  * vlc_object_yield(), use the pointer as your reference, and call
656  * vlc_object_release() when you're done.
657  */
658 void * vlc_object_get( int i_id )
659 {
660     libvlc_global_data_t *p_libvlc_global = vlc_global();
661     vlc_object_t *obj = NULL;
662 #ifndef NDEBUG
663     vlc_object_t *caller = vlc_threadobj ();
664
665     if (caller)
666         msg_Dbg (caller, "uses deprecated vlc_object_get(%d)", i_id);
667     else
668         fprintf (stderr, "main thread uses deprecated vlc_object_get(%d)\n",
669                  i_id);
670 #endif
671     vlc_mutex_lock( &structure_lock );
672
673     for( obj = vlc_internals (p_libvlc_global)->next;
674          obj != VLC_OBJECT (p_libvlc_global);
675          obj = vlc_internals (obj)->next )
676     {
677         if( obj->i_object_id == i_id )
678         {
679             vlc_object_yield( obj );
680             goto out;
681         }
682     }
683     obj = NULL;
684 #ifndef NDEBUG
685     if (caller)
686         msg_Warn (caller, "wants non-existing object %d", i_id);
687     else
688         fprintf (stderr, "main thread wants non-existing object %d\n", i_id);
689 #endif
690 out:
691     vlc_mutex_unlock( &structure_lock );
692     return obj;
693 }
694
695 /**
696  ****************************************************************************
697  * find a typed object and increment its refcount
698  *****************************************************************************
699  * This function recursively looks for a given object type. i_mode can be one
700  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
701  *****************************************************************************/
702 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
703 {
704     vlc_object_t *p_found;
705
706     /* If we are of the requested type ourselves, don't look further */
707     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
708     {
709         vlc_object_yield( p_this );
710         return p_this;
711     }
712
713     /* Otherwise, recursively look for the object */
714     if ((i_mode & 0x000f) == FIND_ANYWHERE)
715     {
716 #ifndef NDEBUG
717         if (i_type == VLC_OBJECT_PLAYLIST)
718             msg_Warn (p_this, "using vlc_object_find(VLC_OBJECT_PLAYLIST) "
719                       "instead of pl_Yield()");
720 #endif
721         return vlc_object_find (p_this->p_libvlc, i_type,
722                                 (i_mode & ~0x000f)|FIND_CHILD);
723     }
724
725     vlc_mutex_lock( &structure_lock );
726     p_found = FindObject( p_this, i_type, i_mode );
727     vlc_mutex_unlock( &structure_lock );
728     return p_found;
729 }
730
731 /**
732  ****************************************************************************
733  * find a named object and increment its refcount
734  *****************************************************************************
735  * This function recursively looks for a given object name. i_mode can be one
736  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
737  *****************************************************************************/
738 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
739                                int i_mode )
740 {
741     vlc_object_t *p_found;
742
743     /* If have the requested name ourselves, don't look further */
744     if( !(i_mode & FIND_STRICT)
745         && p_this->psz_object_name
746         && !strcmp( p_this->psz_object_name, psz_name ) )
747     {
748         vlc_object_yield( p_this );
749         return p_this;
750     }
751
752     vlc_mutex_lock( &structure_lock );
753
754     /* Otherwise, recursively look for the object */
755     if( (i_mode & 0x000f) == FIND_ANYWHERE )
756     {
757         vlc_object_t *p_root = p_this;
758
759         /* Find the root */
760         while( p_root->p_parent != NULL &&
761                p_root != VLC_OBJECT( p_this->p_libvlc ) )
762         {
763             p_root = p_root->p_parent;
764         }
765
766         p_found = FindObjectName( p_root, psz_name,
767                                  (i_mode & ~0x000f)|FIND_CHILD );
768         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
769         {
770             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
771                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
772         }
773     }
774     else
775     {
776         p_found = FindObjectName( p_this, psz_name, i_mode );
777     }
778
779     vlc_mutex_unlock( &structure_lock );
780
781     return p_found;
782 }
783
784 /**
785  * Increment an object reference counter.
786  */
787 void __vlc_object_yield( vlc_object_t *p_this )
788 {
789     vlc_object_internals_t *internals = vlc_internals( p_this );
790
791     vlc_spin_lock( &internals->ref_spin );
792     /* Avoid obvious freed object uses */
793     assert( internals->i_refcount > 0 );
794     /* Increment the counter */
795     internals->i_refcount++;
796     vlc_spin_unlock( &internals->ref_spin );
797 #ifdef LIBVLC_REFCHECK
798     /* Update the list of referenced objects */
799     /* Using TLS, so no need to lock */
800     /* The following line may leak memory if a thread leaks objects. */
801     held_list_t *newhead = malloc (sizeof (*newhead));
802     held_list_t *oldhead = vlc_threadvar_get (&held_objects);
803     newhead->next = oldhead;
804     newhead->obj = p_this;
805     vlc_threadvar_set (&held_objects, newhead);
806 #endif
807 }
808
809 /*****************************************************************************
810  * decrement an object refcount
811  * And destroy the object if its refcount reach zero.
812  *****************************************************************************/
813 void __vlc_object_release( vlc_object_t *p_this )
814 {
815     vlc_object_internals_t *internals = vlc_internals( p_this );
816     bool b_should_destroy;
817
818 #ifdef LIBVLC_REFCHECK
819     /* Update the list of referenced objects */
820     /* Using TLS, so no need to lock */
821     for (held_list_t *hlcur = vlc_threadvar_get (&held_objects),
822                      *hlprev = NULL;
823          hlcur != NULL;
824          hlprev = hlcur, hlcur = hlcur->next)
825     {
826         if (hlcur->obj == p_this)
827         {
828             if (hlprev == NULL)
829                 vlc_threadvar_set (&held_objects, hlcur->next);
830             else
831                 hlprev->next = hlcur->next;
832             free (hlcur);
833             break;
834         }
835     }
836     /* TODO: what if releasing without references? */
837 #endif
838
839     vlc_spin_lock( &internals->ref_spin );
840     assert( internals->i_refcount > 0 );
841
842     if( internals->i_refcount > 1 )
843     {
844         /* Fast path */
845         /* There are still other references to the object */
846         internals->i_refcount--;
847         vlc_spin_unlock( &internals->ref_spin );
848         return;
849     }
850     vlc_spin_unlock( &internals->ref_spin );
851
852     /* Slow path */
853     /* Remember that we cannot hold the spin while waiting on the mutex */
854     vlc_mutex_lock( &structure_lock );
855     /* Take the spin again. Note that another thread may have yielded the
856      * object in the (very short) mean time. */
857     vlc_spin_lock( &internals->ref_spin );
858     b_should_destroy = --internals->i_refcount == 0;
859     vlc_spin_unlock( &internals->ref_spin );
860
861     if( b_should_destroy )
862     {
863         /* Remove the object from object list
864          * so that it cannot be encountered by vlc_object_get() */
865         vlc_internals (internals->next)->prev = internals->prev;
866         vlc_internals (internals->prev)->next = internals->next;
867
868         /* Detach from parent to protect against FIND_CHILDREN */
869         vlc_object_detach_unlocked (p_this);
870         /* Detach from children to protect against FIND_PARENT */
871         for (int i = 0; i < internals->i_children; i++)
872             internals->pp_children[i]->p_parent = NULL;
873     }
874
875     vlc_mutex_unlock( &structure_lock );
876
877     if( b_should_destroy )
878     {
879         free( internals->pp_children );
880         internals->pp_children = NULL;
881         internals->i_children = 0;
882         vlc_object_destroy( p_this );
883     }
884 }
885
886 /**
887  ****************************************************************************
888  * attach object to a parent object
889  *****************************************************************************
890  * This function sets p_this as a child of p_parent, and p_parent as a parent
891  * of p_this. This link can be undone using vlc_object_detach.
892  *****************************************************************************/
893 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
894 {
895     if( !p_this ) return;
896
897     vlc_mutex_lock( &structure_lock );
898
899     /* Attach the parent to its child */
900     assert (!p_this->p_parent);
901     p_this->p_parent = p_parent;
902
903     /* Attach the child to its parent */
904     vlc_object_internals_t *priv = vlc_internals( p_parent );
905     INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
906                  p_this );
907
908     /* Kill the object if parent is already dead.
909      * Note: We should surely lock parent here, but that would
910      * create quite a few dead lock case. Hopefully, it
911      * is perfectly safe to do it that way. We only risk
912      * receiving kill event twice. But given current API
913      * it is ok. */
914     if( p_this->p_parent->b_die )
915         vlc_object_kill( p_this );
916
917     vlc_mutex_unlock( &structure_lock );
918 }
919
920
921 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
922 {
923     vlc_assert_locked (&structure_lock);
924
925     if (p_this->p_parent == NULL)
926         return;
927
928     vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
929
930     int i_index, i;
931
932     /* Remove p_this's parent */
933     p_this->p_parent = NULL;
934
935     /* Remove all of p_parent's children which are p_this */
936     for( i_index = priv->i_children ; i_index-- ; )
937     {
938         if( priv->pp_children[i_index] == p_this )
939         {
940             priv->i_children--;
941             for( i = i_index ; i < priv->i_children ; i++ )
942                 priv->pp_children[i] = priv->pp_children[i+1];
943         }
944     }
945
946     if( priv->i_children )
947     {
948         priv->pp_children = (vlc_object_t **)realloc( priv->pp_children,
949                                priv->i_children * sizeof(vlc_object_t *) );
950     }
951     else
952     {
953         /* Special case - don't realloc() to zero to avoid leaking */
954         free( priv->pp_children );
955         priv->pp_children = NULL;
956     }
957 }
958
959
960 /**
961  ****************************************************************************
962  * detach object from its parent
963  *****************************************************************************
964  * This function removes all links between an object and its parent.
965  *****************************************************************************/
966 void __vlc_object_detach( vlc_object_t *p_this )
967 {
968     if( !p_this ) return;
969
970     vlc_mutex_lock( &structure_lock );
971     if( !p_this->p_parent )
972         msg_Err( p_this, "object is not attached" );
973     else
974         vlc_object_detach_unlocked( p_this );
975     vlc_mutex_unlock( &structure_lock );
976 }
977
978
979 /**
980  ****************************************************************************
981  * find a list typed objects and increment their refcount
982  *****************************************************************************
983  * This function recursively looks for a given object type. i_mode can be one
984  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
985  *****************************************************************************/
986 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
987 {
988     vlc_list_t *p_list;
989     int i_count = 0;
990
991     /* Look for the objects */
992     switch( i_mode & 0x000f )
993     {
994     case FIND_ANYWHERE:
995         /* Modules should probably not be object, and the module should perhaps
996          * not be shared across LibVLC instances. In the mean time, this ugly
997          * hack is brought to you by Courmisch. */
998         if (i_type == VLC_OBJECT_MODULE)
999             return vlc_list_find ((vlc_object_t *)vlc_global ()->p_module_bank,
1000                                   i_type, FIND_CHILD);
1001         return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
1002
1003     case FIND_CHILD:
1004         vlc_mutex_lock( &structure_lock );
1005         i_count = CountChildren( p_this, i_type );
1006         p_list = NewList( i_count );
1007
1008         /* Check allocation was successful */
1009         if( p_list->i_count != i_count )
1010         {
1011             vlc_mutex_unlock( &structure_lock );
1012             msg_Err( p_this, "list allocation failed!" );
1013             p_list->i_count = 0;
1014             break;
1015         }
1016
1017         p_list->i_count = 0;
1018         ListChildren( p_list, p_this, i_type );
1019         vlc_mutex_unlock( &structure_lock );
1020         break;
1021
1022     default:
1023         msg_Err( p_this, "unimplemented!" );
1024         p_list = NewList( 0 );
1025         break;
1026     }
1027
1028     return p_list;
1029 }
1030
1031 /**
1032  * Gets the list of children of an objects, and increment their reference
1033  * count.
1034  * @return a list (possibly empty) or NULL in case of error.
1035  */
1036 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
1037 {
1038     vlc_list_t *l;
1039     vlc_object_internals_t *priv = vlc_internals( obj );
1040
1041     vlc_mutex_lock( &structure_lock );
1042     l = NewList( priv->i_children );
1043     for (int i = 0; i < l->i_count; i++)
1044     {
1045         vlc_object_yield( priv->pp_children[i] );
1046         l->p_values[i].p_object = priv->pp_children[i];
1047     }
1048     vlc_mutex_unlock( &structure_lock );
1049     return l;
1050 }
1051
1052 /*****************************************************************************
1053  * DumpCommand: print the current vlc structure
1054  *****************************************************************************
1055  * This function prints either an ASCII tree showing the connections between
1056  * vlc objects, and additional information such as their refcount, thread ID,
1057  * etc. (command "tree"), or the same data as a simple list (command "list").
1058  *****************************************************************************/
1059 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1060                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1061 {
1062     (void)oldval; (void)p_data;
1063     if( *psz_cmd == 'l' )
1064     {
1065         vlc_object_t *root = VLC_OBJECT (vlc_global ()), *cur = root; 
1066
1067         vlc_mutex_lock( &structure_lock );
1068         do
1069         {
1070             PrintObject (cur, "");
1071             cur = vlc_internals (cur)->next;
1072         }
1073         while (cur != root);
1074         vlc_mutex_unlock( &structure_lock );
1075     }
1076     else
1077     {
1078         vlc_object_t *p_object = NULL;
1079
1080         if( *newval.psz_string )
1081         {
1082             char *end;
1083             int i_id = strtol( newval.psz_string, &end, 0 );
1084             if( end != newval.psz_string )
1085                 p_object = vlc_object_get( i_id );
1086             else
1087                 /* try using the object's name to find it */
1088                 p_object = vlc_object_find_name( p_this, newval.psz_string,
1089                                                  FIND_ANYWHERE );
1090
1091             if( !p_object )
1092             {
1093                 return VLC_ENOOBJ;
1094             }
1095         }
1096
1097         vlc_mutex_lock( &structure_lock );
1098
1099         if( *psz_cmd == 't' )
1100         {
1101             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1102
1103             if( !p_object )
1104                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1105
1106             psz_foo[0] = '|';
1107             DumpStructure( p_object, 0, psz_foo );
1108         }
1109         else if( *psz_cmd == 'v' )
1110         {
1111             int i;
1112
1113             if( !p_object )
1114                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1115
1116             PrintObject( p_object, "" );
1117
1118             if( !vlc_internals( p_object )->i_vars )
1119                 printf( " `-o No variables\n" );
1120             for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1121             {
1122                 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1123
1124                 const char *psz_type = "unknown";
1125                 switch( p_var->i_type & VLC_VAR_TYPE )
1126                 {
1127 #define MYCASE( type, nice )                \
1128                     case VLC_VAR_ ## type:  \
1129                         psz_type = nice;    \
1130                         break;
1131                     MYCASE( VOID, "void" );
1132                     MYCASE( BOOL, "bool" );
1133                     MYCASE( INTEGER, "integer" );
1134                     MYCASE( HOTKEY, "hotkey" );
1135                     MYCASE( STRING, "string" );
1136                     MYCASE( MODULE, "module" );
1137                     MYCASE( FILE, "file" );
1138                     MYCASE( DIRECTORY, "directory" );
1139                     MYCASE( VARIABLE, "variable" );
1140                     MYCASE( FLOAT, "float" );
1141                     MYCASE( TIME, "time" );
1142                     MYCASE( ADDRESS, "address" );
1143                     MYCASE( MUTEX, "mutex" );
1144                     MYCASE( LIST, "list" );
1145 #undef MYCASE
1146                 }
1147                 printf( " %c-o \"%s\" (%s",
1148                         i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1149                         p_var->psz_name, psz_type );
1150                 if( p_var->psz_text )
1151                     printf( ", %s", p_var->psz_text );
1152                 printf( ")" );
1153                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1154                     printf( ", command" );
1155                 if( p_var->i_entries )
1156                     printf( ", %d callbacks", p_var->i_entries );
1157                 switch( p_var->i_type & 0x00f0 )
1158                 {
1159                     case VLC_VAR_VOID:
1160                     case VLC_VAR_MUTEX:
1161                         break;
1162                     case VLC_VAR_BOOL:
1163                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1164                         break;
1165                     case VLC_VAR_INTEGER:
1166                         printf( ": %d", p_var->val.i_int );
1167                         break;
1168                     case VLC_VAR_STRING:
1169                         printf( ": \"%s\"", p_var->val.psz_string );
1170                         break;
1171                     case VLC_VAR_FLOAT:
1172                         printf( ": %f", p_var->val.f_float );
1173                         break;
1174                     case VLC_VAR_TIME:
1175                         printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1176                         break;
1177                     case VLC_VAR_ADDRESS:
1178                         printf( ": %p", p_var->val.p_address );
1179                         break;
1180                     case VLC_VAR_LIST:
1181                         printf( ": TODO" );
1182                         break;
1183                 }
1184                 printf( "\n" );
1185             }
1186         }
1187
1188         vlc_mutex_unlock( &structure_lock );
1189
1190         if( *newval.psz_string )
1191         {
1192             vlc_object_release( p_object );
1193         }
1194     }
1195
1196     return VLC_SUCCESS;
1197 }
1198
1199 /*****************************************************************************
1200  * vlc_list_release: free a list previously allocated by vlc_list_find
1201  *****************************************************************************
1202  * This function decreases the refcount of all objects in the list and
1203  * frees the list.
1204  *****************************************************************************/
1205 void vlc_list_release( vlc_list_t *p_list )
1206 {
1207     int i_index;
1208
1209     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1210     {
1211         vlc_object_release( p_list->p_values[i_index].p_object );
1212     }
1213
1214     free( p_list->p_values );
1215     free( p_list );
1216 }
1217
1218 /*****************************************************************************
1219  * dump an object. (Debug function)
1220  *****************************************************************************/
1221 void __vlc_object_dump( vlc_object_t *p_this )
1222 {
1223     vlc_mutex_lock( &structure_lock );
1224     char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1225     psz_foo[0] = '|';
1226     DumpStructure( p_this, 0, psz_foo );
1227     vlc_mutex_unlock( &structure_lock );
1228 }
1229
1230 /* Following functions are local */
1231
1232 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1233 {
1234     int i;
1235     vlc_object_t *p_tmp;
1236
1237     switch( i_mode & 0x000f )
1238     {
1239     case FIND_PARENT:
1240         p_tmp = p_this->p_parent;
1241         if( p_tmp )
1242         {
1243             if( p_tmp->i_object_type == i_type )
1244             {
1245                 vlc_object_yield( p_tmp );
1246                 return p_tmp;
1247             }
1248             else
1249             {
1250                 return FindObject( p_tmp, i_type, i_mode );
1251             }
1252         }
1253         break;
1254
1255     case FIND_CHILD:
1256         for( i = vlc_internals( p_this )->i_children; i--; )
1257         {
1258             p_tmp = vlc_internals( p_this )->pp_children[i];
1259             if( p_tmp->i_object_type == i_type )
1260             {
1261                 vlc_object_yield( p_tmp );
1262                 return p_tmp;
1263             }
1264             else if( vlc_internals( p_tmp )->i_children )
1265             {
1266                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1267                 if( p_tmp )
1268                 {
1269                     return p_tmp;
1270                 }
1271             }
1272         }
1273         break;
1274
1275     case FIND_ANYWHERE:
1276         /* Handled in vlc_object_find */
1277         break;
1278     }
1279
1280     return NULL;
1281 }
1282
1283 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1284                                       const char *psz_name,
1285                                       int i_mode )
1286 {
1287     int i;
1288     vlc_object_t *p_tmp;
1289
1290     switch( i_mode & 0x000f )
1291     {
1292     case FIND_PARENT:
1293         p_tmp = p_this->p_parent;
1294         if( p_tmp )
1295         {
1296             if( p_tmp->psz_object_name
1297                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1298             {
1299                 vlc_object_yield( p_tmp );
1300                 return p_tmp;
1301             }
1302             else
1303             {
1304                 return FindObjectName( p_tmp, psz_name, i_mode );
1305             }
1306         }
1307         break;
1308
1309     case FIND_CHILD:
1310         for( i = vlc_internals( p_this )->i_children; i--; )
1311         {
1312             p_tmp = vlc_internals( p_this )->pp_children[i];
1313             if( p_tmp->psz_object_name
1314                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1315             {
1316                 vlc_object_yield( p_tmp );
1317                 return p_tmp;
1318             }
1319             else if( vlc_internals( p_tmp )->i_children )
1320             {
1321                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1322                 if( p_tmp )
1323                 {
1324                     return p_tmp;
1325                 }
1326             }
1327         }
1328         break;
1329
1330     case FIND_ANYWHERE:
1331         /* Handled in vlc_object_find */
1332         break;
1333     }
1334
1335     return NULL;
1336 }
1337
1338
1339 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1340 {
1341     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1342          psz_parent[20];
1343
1344     memset( &psz_name, 0, sizeof(psz_name) );
1345     if( p_this->psz_object_name )
1346     {
1347         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1348         if( psz_name[48] )
1349             psz_name[48] = '\"';
1350     }
1351
1352     psz_children[0] = '\0';
1353     switch( vlc_internals( p_this )->i_children )
1354     {
1355         case 0:
1356             break;
1357         case 1:
1358             strcpy( psz_children, ", 1 child" );
1359             break;
1360         default:
1361             snprintf( psz_children, 19, ", %i children",
1362                       vlc_internals( p_this )->i_children );
1363             break;
1364     }
1365
1366     psz_refcount[0] = '\0';
1367     if( vlc_internals( p_this )->i_refcount > 0 )
1368         snprintf( psz_refcount, 19, ", refcount %u",
1369                   vlc_internals( p_this )->i_refcount );
1370
1371     psz_thread[0] = '\0';
1372     if( vlc_internals( p_this )->b_thread )
1373         snprintf( psz_thread, 29, " (thread %lu)",
1374                   (unsigned long)vlc_internals( p_this )->thread_id );
1375
1376     psz_parent[0] = '\0';
1377     if( p_this->p_parent )
1378         snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1379
1380     printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1381             p_this->i_object_id, p_this->psz_object_type,
1382             psz_name, psz_thread, psz_refcount, psz_children,
1383             psz_parent );
1384 }
1385
1386 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1387 {
1388     int i;
1389     char i_back = psz_foo[i_level];
1390     psz_foo[i_level] = '\0';
1391
1392     PrintObject( p_this, psz_foo );
1393
1394     psz_foo[i_level] = i_back;
1395
1396     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1397     {
1398         msg_Warn( p_this, "structure tree is too deep" );
1399         return;
1400     }
1401
1402     for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1403     {
1404         if( i_level )
1405         {
1406             psz_foo[i_level-1] = ' ';
1407
1408             if( psz_foo[i_level-2] == '`' )
1409             {
1410                 psz_foo[i_level-2] = ' ';
1411             }
1412         }
1413
1414         if( i == vlc_internals( p_this )->i_children - 1 )
1415         {
1416             psz_foo[i_level] = '`';
1417         }
1418         else
1419         {
1420             psz_foo[i_level] = '|';
1421         }
1422
1423         psz_foo[i_level+1] = '-';
1424         psz_foo[i_level+2] = '\0';
1425
1426         DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1427                        psz_foo );
1428     }
1429 }
1430
1431 static vlc_list_t * NewList( int i_count )
1432 {
1433     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1434     if( p_list == NULL )
1435     {
1436         return NULL;
1437     }
1438
1439     p_list->i_count = i_count;
1440
1441     if( i_count == 0 )
1442     {
1443         p_list->p_values = NULL;
1444         return p_list;
1445     }
1446
1447     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1448     if( p_list->p_values == NULL )
1449     {
1450         p_list->i_count = 0;
1451         return p_list;
1452     }
1453
1454     return p_list;
1455 }
1456
1457 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1458                          int i_index )
1459 {
1460     if( p_list == NULL || i_index >= p_list->i_count )
1461     {
1462         return;
1463     }
1464
1465     vlc_object_yield( p_object );
1466
1467     p_list->p_values[i_index].p_object = p_object;
1468
1469     return;
1470 }
1471
1472 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1473 {
1474     if( p_list == NULL )
1475     {
1476         return;
1477     }
1478
1479     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1480                                 * sizeof( vlc_value_t ) );
1481     if( p_list->p_values == NULL )
1482     {
1483         p_list->i_count = 0;
1484         return;
1485     }
1486
1487     vlc_object_yield( p_object );
1488
1489     p_list->p_values[p_list->i_count].p_object = p_object;
1490     p_list->i_count++;
1491
1492     return;
1493 }*/
1494
1495 static int CountChildren( vlc_object_t *p_this, int i_type )
1496 {
1497     vlc_object_t *p_tmp;
1498     int i, i_count = 0;
1499
1500     for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1501     {
1502         p_tmp = vlc_internals( p_this )->pp_children[i];
1503
1504         if( p_tmp->i_object_type == i_type )
1505         {
1506             i_count++;
1507         }
1508         i_count += CountChildren( p_tmp, i_type );
1509     }
1510
1511     return i_count;
1512 }
1513
1514 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1515 {
1516     vlc_object_t *p_tmp;
1517     int i;
1518
1519     for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1520     {
1521         p_tmp = vlc_internals( p_this )->pp_children[i];
1522
1523         if( p_tmp->i_object_type == i_type )
1524             ListReplace( p_list, p_tmp, p_list->i_count++ );
1525
1526         ListChildren( p_list, p_tmp, i_type );
1527     }
1528 }
1529
1530 #ifdef LIBVLC_REFCHECK
1531 # if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE)
1532 #  include <execinfo.h>
1533 # endif
1534
1535 void vlc_refcheck (vlc_object_t *obj)
1536 {
1537     static unsigned errors = 0;
1538     if (errors > 100)
1539         return;
1540
1541     /* Anyone can use the root object (though it should not exist) */
1542     if (obj == VLC_OBJECT (vlc_global ()))
1543         return;
1544
1545     /* Anyone can use its libvlc instance object */
1546     if (obj == VLC_OBJECT (obj->p_libvlc))
1547         return;
1548
1549     /* The thread that created the object holds the initial reference */
1550     vlc_object_internals_t *priv = vlc_internals (obj);
1551 #if defined (LIBVLC_USE_PTHREAD)
1552     if (pthread_equal (priv->creator_id, pthread_self ()))
1553 #elif defined WIN32
1554     if (priv->creator_id == GetCurrentThreadId ())
1555 #else
1556     if (0)
1557 #endif
1558         return;
1559
1560     /* A thread can use its own object without references! */
1561     vlc_object_t *caller = vlc_threadobj ();
1562     if (caller == obj)
1563         return;
1564 #if 0
1565     /* The calling thread is younger than the object.
1566      * Access could be valid through cross-thread synchronization;
1567      * we would need better accounting. */
1568     if (caller && (caller->i_object_id > obj->i_object_id))
1569         return;
1570 #endif
1571     int refs;
1572     vlc_spin_lock (&priv->ref_spin);
1573     refs = priv->i_refcount;
1574     vlc_spin_unlock (&priv->ref_spin);
1575
1576     for (held_list_t *hlcur = vlc_threadvar_get (&held_objects);
1577          hlcur != NULL; hlcur = hlcur->next)
1578         if (hlcur->obj == obj)
1579             return;
1580
1581     fprintf (stderr, "The %s %s thread object is accessing...\n"
1582              "the %s %s object without references.\n",
1583              caller && caller->psz_object_name
1584                      ? caller->psz_object_name : "unnamed",
1585              caller ? caller->psz_object_type : "main",
1586              obj->psz_object_name ? obj->psz_object_name : "unnamed",
1587              obj->psz_object_type);
1588     fflush (stderr);
1589
1590 #ifdef HAVE_BACKTRACE
1591     void *stack[20];
1592     int stackdepth = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
1593     backtrace_symbols_fd (stack, stackdepth, 2);
1594 #endif
1595
1596     if (++errors == 100)
1597         fprintf (stderr, "Too many reference errors!\n");
1598 }
1599
1600 static void held_objects_destroy (void *data)
1601 {
1602     VLC_UNUSED( data );
1603     held_list_t *hl = vlc_threadvar_get (&held_objects);
1604     vlc_object_t *caller = vlc_threadobj ();
1605
1606     while (hl != NULL)
1607     {
1608         held_list_t *buf = hl->next;
1609         vlc_object_t *obj = hl->obj;
1610
1611         fprintf (stderr, "The %s %s thread object leaked a reference to...\n"
1612                          "the %s %s object.\n",
1613                  caller && caller->psz_object_name
1614                      ? caller->psz_object_name : "unnamed",
1615                  caller ? caller->psz_object_type : "main",
1616                  obj->psz_object_name ? obj->psz_object_name : "unnamed",
1617                  obj->psz_object_type);
1618         free (hl);
1619         hl = buf;
1620     }
1621 }
1622 #endif