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