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