]> git.sesse.net Git - vlc/blob - src/misc/objects.c
An object must not have a thread when it is destroyed
[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 static unsigned    object_counter = 0;
99
100 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
101                            int i_type, const char *psz_type )
102 {
103     vlc_object_t *p_new;
104     vlc_object_internals_t *p_priv;
105
106     /* NOTE:
107      * VLC objects are laid out as follow:
108      * - first the LibVLC-private per-object data,
109      * - then VLC_COMMON members from vlc_object_t,
110      * - finally, the type-specific data (if any).
111      *
112      * This function initializes the LibVLC and common data,
113      * and zeroes the rest.
114      */
115     p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
116     if( p_priv == NULL )
117         return NULL;
118
119     assert (i_size >= sizeof (vlc_object_t));
120     p_new = (vlc_object_t *)(p_priv + 1);
121
122     p_new->i_object_type = i_type;
123     p_new->psz_object_type = psz_type;
124     p_new->psz_object_name = NULL;
125
126     p_new->b_die = false;
127     p_new->b_error = false;
128     p_new->b_dead = false;
129     p_new->b_force = false;
130
131     p_new->psz_header = NULL;
132
133     if (p_this)
134         p_new->i_flags = p_this->i_flags
135             & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
136
137     p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
138
139     if( !p_priv->p_vars )
140     {
141         free( p_priv );
142         return NULL;
143     }
144
145     libvlc_global_data_t *p_libvlc_global;
146     if( p_this == NULL )
147     {
148         /* Only the global root object is created out of the blue */
149         p_libvlc_global = (libvlc_global_data_t *)p_new;
150         p_new->p_libvlc = NULL;
151
152         object_counter = 0; /* reset */
153         p_priv->next = p_priv->prev = p_new;
154         vlc_mutex_init( &structure_lock );
155 #ifdef LIBVLC_REFCHECK
156         /* TODO: use the destruction callback to track ref leaks */
157         vlc_threadvar_create( &held_objects, held_objects_destroy );
158 #endif
159     }
160     else
161     {
162         p_libvlc_global = vlc_global();
163         if( i_type == VLC_OBJECT_LIBVLC )
164             p_new->p_libvlc = (libvlc_int_t*)p_new;
165         else
166             p_new->p_libvlc = p_this->p_libvlc;
167     }
168
169     vlc_spin_init( &p_priv->ref_spin );
170     p_priv->i_refcount = 1;
171     p_priv->pf_destructor = NULL;
172     p_priv->b_thread = false;
173     p_new->p_parent = NULL;
174     p_priv->pp_children = NULL;
175     p_priv->i_children = 0;
176
177     p_new->p_private = NULL;
178
179     /* Initialize mutexes and condvars */
180     vlc_mutex_init( &p_priv->lock );
181     vlc_cond_init( p_new, &p_priv->wait );
182     vlc_mutex_init( &p_priv->var_lock );
183     vlc_spin_init( &p_priv->spin );
184     p_priv->pipes[0] = p_priv->pipes[1] = -1;
185
186     p_priv->next = VLC_OBJECT (p_libvlc_global);
187 #if !defined (LIBVLC_REFCHECK)
188     /* ... */
189 #elif defined (LIBVLC_USE_PTHREAD)
190     p_priv->creator_id = pthread_self ();
191 #elif defined (WIN32)
192     p_priv->creator_id = GetCurrentThreadId ();
193 #endif
194     vlc_mutex_lock( &structure_lock );
195     p_priv->prev = vlc_internals (p_libvlc_global)->prev;
196     vlc_internals (p_libvlc_global)->prev = p_new;
197     vlc_internals (p_priv->prev)->next = p_new;
198     p_new->i_object_id = object_counter++; /* fetch THEN increment */
199     vlc_mutex_unlock( &structure_lock );
200
201     if( i_type == VLC_OBJECT_LIBVLC )
202     {
203         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
204         var_AddCallback( p_new, "list", DumpCommand, NULL );
205         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
206         var_AddCallback( p_new, "tree", DumpCommand, NULL );
207         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
208         var_AddCallback( p_new, "vars", DumpCommand, NULL );
209     }
210
211     return p_new;
212 }
213
214
215 /**
216  * Allocates and initializes a vlc object.
217  *
218  * @param i_type known object type (all of them are negative integer values),
219  *               or object byte size (always positive).
220  *
221  * @return the new object, or NULL on error.
222  */
223 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
224 {
225     const char   * psz_type;
226     size_t         i_size;
227
228     switch( i_type )
229     {
230         case VLC_OBJECT_INTF:
231             i_size = sizeof(intf_thread_t);
232             psz_type = "interface";
233             break;
234         case VLC_OBJECT_DECODER:
235             i_size = sizeof(decoder_t);
236             psz_type = "decoder";
237             break;
238         case VLC_OBJECT_PACKETIZER:
239             i_size = sizeof(decoder_t);
240             psz_type = "packetizer";
241             break;
242         case VLC_OBJECT_ENCODER:
243             i_size = sizeof(encoder_t);
244             psz_type = "encoder";
245             break;
246         case VLC_OBJECT_AOUT:
247             i_size = sizeof(aout_instance_t);
248             psz_type = "audio output";
249             break;
250         case VLC_OBJECT_OPENGL:
251             i_size = sizeof( vout_thread_t );
252             psz_type = "opengl";
253             break;
254         case VLC_OBJECT_ANNOUNCE:
255             i_size = sizeof( announce_handler_t );
256             psz_type = "announce";
257             break;
258         default:
259             assert( i_type > 0 ); /* unknown type?! */
260             i_size = i_type;
261             i_type = VLC_OBJECT_GENERIC;
262             psz_type = "generic";
263             break;
264     }
265
266     return vlc_custom_create( p_this, i_size, i_type, psz_type );
267 }
268
269
270 /**
271  ****************************************************************************
272  * Set the destructor of a vlc object
273  *
274  * This function sets the destructor of the vlc object. It will be called
275  * when the object is destroyed when the its refcount reaches 0.
276  * (It is called by the internal function vlc_object_destroy())
277  *****************************************************************************/
278 void __vlc_object_set_destructor( vlc_object_t *p_this,
279                                   vlc_destructor_t pf_destructor )
280 {
281     vlc_object_internals_t *p_priv = vlc_internals(p_this );
282     p_priv->pf_destructor = pf_destructor;
283 }
284
285 /**
286  ****************************************************************************
287  * Destroy a vlc object (Internal)
288  *
289  * This function destroys an object that has been previously allocated with
290  * vlc_object_create. The object's refcount must be zero and it must not be
291  * attached to other objects in any way.
292  *****************************************************************************/
293 static void vlc_object_destroy( vlc_object_t *p_this )
294 {
295     vlc_object_internals_t *p_priv = vlc_internals( p_this );
296
297     /* Objects are always detached beforehand */
298     assert( !p_this->p_parent );
299
300     /* Send a kill to the object's thread if applicable */
301     vlc_object_kill( p_this );
302
303     /* Call the custom "subclass" destructor */
304     if( p_priv->pf_destructor )
305         p_priv->pf_destructor( p_this );
306
307     /* Any thread must have been cleaned up at this point. */
308     assert( !p_priv->b_thread );
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 #ifndef NDEBUG
325         libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
326
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  * Signals an object for which the lock is held.
546  * At least one thread currently sleeping in vlc_object_wait() or
547  * vlc_object_timedwait() will wake up, assuming that there is at least one
548  * such thread in the first place. Otherwise, it is undefined whether the
549  * signal will be lost or will wake up one or more thread later.
550  */
551 void __vlc_object_signal_unlocked( vlc_object_t *obj )
552 {
553     vlc_assert_locked (&(vlc_internals(obj)->lock));
554     vlc_cond_signal( &(vlc_internals(obj)->wait) );
555 }
556
557
558 /**
559  * Requests termination of an object.
560  * If the object is LibVLC, also request to terminate all its children.
561  */
562 void __vlc_object_kill( vlc_object_t *p_this )
563 {
564     vlc_object_internals_t *priv = vlc_internals( p_this );
565     int fd;
566
567     vlc_object_lock( p_this );
568     p_this->b_die = true;
569
570     vlc_spin_lock (&priv->spin);
571     fd = priv->pipes[1];
572     priv->pipes[1] = -1;
573     vlc_spin_unlock (&priv->spin);
574
575     if( fd != -1 )
576     {
577         msg_Dbg (p_this, "waitpipe: object killed");
578         close (fd);
579     }
580
581     vlc_object_signal_unlocked( p_this );
582     /* This also serves as a memory barrier toward vlc_object_alive(): */
583     vlc_object_unlock( p_this );
584 }
585
586
587 /**
588  * Find an object given its ID.
589  *
590  * This function looks for the object whose i_object_id field is i_id.
591  * This function is slow, and often used to hide bugs. Do not use it.
592  * If you need to retain reference to an object, yield the object pointer with
593  * vlc_object_yield(), use the pointer as your reference, and call
594  * vlc_object_release() when you're done.
595  */
596 void * vlc_object_get( int i_id )
597 {
598     libvlc_global_data_t *p_libvlc_global = vlc_global();
599     vlc_object_t *obj = NULL;
600 #ifndef NDEBUG
601     vlc_object_t *caller = vlc_threadobj ();
602
603     if (caller)
604         msg_Dbg (caller, "uses deprecated vlc_object_get(%d)", i_id);
605     else
606         fprintf (stderr, "main thread uses deprecated vlc_object_get(%d)\n",
607                  i_id);
608 #endif
609     vlc_mutex_lock( &structure_lock );
610
611     for( obj = vlc_internals (p_libvlc_global)->next;
612          obj != VLC_OBJECT (p_libvlc_global);
613          obj = vlc_internals (obj)->next )
614     {
615         if( obj->i_object_id == i_id )
616         {
617             vlc_object_yield( obj );
618             goto out;
619         }
620     }
621     obj = NULL;
622 #ifndef NDEBUG
623     if (caller)
624         msg_Warn (caller, "wants non-existing object %d", i_id);
625     else
626         fprintf (stderr, "main thread wants non-existing object %d\n", i_id);
627 #endif
628 out:
629     vlc_mutex_unlock( &structure_lock );
630     return obj;
631 }
632
633 /**
634  ****************************************************************************
635  * find a typed object and increment its refcount
636  *****************************************************************************
637  * This function recursively looks for a given object type. i_mode can be one
638  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
639  *****************************************************************************/
640 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
641 {
642     vlc_object_t *p_found;
643
644     /* If we are of the requested type ourselves, don't look further */
645     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
646     {
647         vlc_object_yield( p_this );
648         return p_this;
649     }
650
651     /* Otherwise, recursively look for the object */
652     if ((i_mode & 0x000f) == FIND_ANYWHERE)
653     {
654 #ifndef NDEBUG
655         if (i_type == VLC_OBJECT_PLAYLIST)
656             msg_Err (p_this, "using vlc_object_find(VLC_OBJECT_PLAYLIST) "
657                      "instead of pl_Yield()");
658 #endif
659         return vlc_object_find (p_this->p_libvlc, i_type,
660                                 (i_mode & ~0x000f)|FIND_CHILD);
661     }
662
663     vlc_mutex_lock( &structure_lock );
664     p_found = FindObject( p_this, i_type, i_mode );
665     vlc_mutex_unlock( &structure_lock );
666     return p_found;
667 }
668
669 /**
670  ****************************************************************************
671  * find a named object and increment its refcount
672  *****************************************************************************
673  * This function recursively looks for a given object name. i_mode can be one
674  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
675  *****************************************************************************/
676 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
677                                int i_mode )
678 {
679     vlc_object_t *p_found;
680
681     /* If have the requested name ourselves, don't look further */
682     if( !(i_mode & FIND_STRICT)
683         && p_this->psz_object_name
684         && !strcmp( p_this->psz_object_name, psz_name ) )
685     {
686         vlc_object_yield( p_this );
687         return p_this;
688     }
689
690     vlc_mutex_lock( &structure_lock );
691
692     /* Otherwise, recursively look for the object */
693     if( (i_mode & 0x000f) == FIND_ANYWHERE )
694     {
695         vlc_object_t *p_root = p_this;
696
697         /* Find the root */
698         while( p_root->p_parent != NULL &&
699                p_root != VLC_OBJECT( p_this->p_libvlc ) )
700         {
701             p_root = p_root->p_parent;
702         }
703
704         p_found = FindObjectName( p_root, psz_name,
705                                  (i_mode & ~0x000f)|FIND_CHILD );
706         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
707         {
708             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
709                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
710         }
711     }
712     else
713     {
714         p_found = FindObjectName( p_this, psz_name, i_mode );
715     }
716
717     vlc_mutex_unlock( &structure_lock );
718
719     return p_found;
720 }
721
722 /**
723  * Increment an object reference counter.
724  */
725 void __vlc_object_yield( vlc_object_t *p_this )
726 {
727     vlc_object_internals_t *internals = vlc_internals( p_this );
728
729     vlc_spin_lock( &internals->ref_spin );
730     /* Avoid obvious freed object uses */
731     assert( internals->i_refcount > 0 );
732     /* Increment the counter */
733     internals->i_refcount++;
734     vlc_spin_unlock( &internals->ref_spin );
735 #ifdef LIBVLC_REFCHECK
736     /* Update the list of referenced objects */
737     /* Using TLS, so no need to lock */
738     /* The following line may leak memory if a thread leaks objects. */
739     held_list_t *newhead = malloc (sizeof (*newhead));
740     held_list_t *oldhead = vlc_threadvar_get (&held_objects);
741     newhead->next = oldhead;
742     newhead->obj = p_this;
743     vlc_threadvar_set (&held_objects, newhead);
744 #endif
745 }
746
747 /*****************************************************************************
748  * decrement an object refcount
749  * And destroy the object if its refcount reach zero.
750  *****************************************************************************/
751 void __vlc_object_release( vlc_object_t *p_this )
752 {
753     vlc_object_internals_t *internals = vlc_internals( p_this );
754     bool b_should_destroy;
755
756 #ifdef LIBVLC_REFCHECK
757     /* Update the list of referenced objects */
758     /* Using TLS, so no need to lock */
759     for (held_list_t *hlcur = vlc_threadvar_get (&held_objects),
760                      *hlprev = NULL;
761          hlcur != NULL;
762          hlprev = hlcur, hlcur = hlcur->next)
763     {
764         if (hlcur->obj == p_this)
765         {
766             if (hlprev == NULL)
767                 vlc_threadvar_set (&held_objects, hlcur->next);
768             else
769                 hlprev->next = hlcur->next;
770             free (hlcur);
771             break;
772         }
773     }
774     /* TODO: what if releasing without references? */
775 #endif
776
777     vlc_spin_lock( &internals->ref_spin );
778     assert( internals->i_refcount > 0 );
779
780     if( internals->i_refcount > 1 )
781     {
782         /* Fast path */
783         /* There are still other references to the object */
784         internals->i_refcount--;
785         vlc_spin_unlock( &internals->ref_spin );
786         return;
787     }
788     vlc_spin_unlock( &internals->ref_spin );
789
790     /* Slow path */
791     /* Remember that we cannot hold the spin while waiting on the mutex */
792     vlc_mutex_lock( &structure_lock );
793     /* Take the spin again. Note that another thread may have yielded the
794      * object in the (very short) mean time. */
795     vlc_spin_lock( &internals->ref_spin );
796     b_should_destroy = --internals->i_refcount == 0;
797     vlc_spin_unlock( &internals->ref_spin );
798
799     if( b_should_destroy )
800     {
801         /* Remove the object from object list
802          * so that it cannot be encountered by vlc_object_get() */
803         vlc_internals (internals->next)->prev = internals->prev;
804         vlc_internals (internals->prev)->next = internals->next;
805
806         /* Detach from parent to protect against FIND_CHILDREN */
807         vlc_object_detach_unlocked (p_this);
808         /* Detach from children to protect against FIND_PARENT */
809         for (int i = 0; i < internals->i_children; i++)
810             internals->pp_children[i]->p_parent = NULL;
811     }
812
813     vlc_mutex_unlock( &structure_lock );
814
815     if( b_should_destroy )
816     {
817         free( internals->pp_children );
818         internals->pp_children = NULL;
819         internals->i_children = 0;
820         vlc_object_destroy( p_this );
821     }
822 }
823
824 /**
825  ****************************************************************************
826  * attach object to a parent object
827  *****************************************************************************
828  * This function sets p_this as a child of p_parent, and p_parent as a parent
829  * of p_this. This link can be undone using vlc_object_detach.
830  *****************************************************************************/
831 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
832 {
833     if( !p_this ) return;
834
835     vlc_mutex_lock( &structure_lock );
836
837     /* Attach the parent to its child */
838     assert (!p_this->p_parent);
839     p_this->p_parent = p_parent;
840
841     /* Attach the child to its parent */
842     vlc_object_internals_t *priv = vlc_internals( p_parent );
843     INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
844                  p_this );
845
846     vlc_mutex_unlock( &structure_lock );
847 }
848
849
850 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
851 {
852     vlc_assert_locked (&structure_lock);
853
854     if (p_this->p_parent == NULL)
855         return;
856
857     vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
858
859     int i_index, i;
860
861     /* Remove p_this's parent */
862     p_this->p_parent = NULL;
863
864     /* Remove all of p_parent's children which are p_this */
865     for( i_index = priv->i_children ; i_index-- ; )
866     {
867         if( priv->pp_children[i_index] == p_this )
868         {
869             priv->i_children--;
870             for( i = i_index ; i < priv->i_children ; i++ )
871                 priv->pp_children[i] = priv->pp_children[i+1];
872         }
873     }
874
875     if( priv->i_children )
876     {
877         priv->pp_children = (vlc_object_t **)realloc( priv->pp_children,
878                                priv->i_children * sizeof(vlc_object_t *) );
879     }
880     else
881     {
882         /* Special case - don't realloc() to zero to avoid leaking */
883         free( priv->pp_children );
884         priv->pp_children = NULL;
885     }
886 }
887
888
889 /**
890  ****************************************************************************
891  * detach object from its parent
892  *****************************************************************************
893  * This function removes all links between an object and its parent.
894  *****************************************************************************/
895 void __vlc_object_detach( vlc_object_t *p_this )
896 {
897     if( !p_this ) return;
898
899     vlc_mutex_lock( &structure_lock );
900     if( !p_this->p_parent )
901         msg_Err( p_this, "object is not attached" );
902     else
903         vlc_object_detach_unlocked( p_this );
904     vlc_mutex_unlock( &structure_lock );
905 }
906
907
908 /**
909  ****************************************************************************
910  * find a list typed objects and increment their refcount
911  *****************************************************************************
912  * This function recursively looks for a given object type. i_mode can be one
913  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
914  *****************************************************************************/
915 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
916 {
917     vlc_list_t *p_list;
918     int i_count = 0;
919
920     /* Look for the objects */
921     switch( i_mode & 0x000f )
922     {
923     case FIND_ANYWHERE:
924         /* Modules should probably not be object, and the module should perhaps
925          * not be shared across LibVLC instances. In the mean time, this ugly
926          * hack is brought to you by Courmisch. */
927         if (i_type == VLC_OBJECT_MODULE)
928             return vlc_list_find ((vlc_object_t *)vlc_global ()->p_module_bank,
929                                   i_type, FIND_CHILD);
930         return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
931
932     case FIND_CHILD:
933         vlc_mutex_lock( &structure_lock );
934         i_count = CountChildren( p_this, i_type );
935         p_list = NewList( i_count );
936
937         /* Check allocation was successful */
938         if( p_list->i_count != i_count )
939         {
940             vlc_mutex_unlock( &structure_lock );
941             msg_Err( p_this, "list allocation failed!" );
942             p_list->i_count = 0;
943             break;
944         }
945
946         p_list->i_count = 0;
947         ListChildren( p_list, p_this, i_type );
948         vlc_mutex_unlock( &structure_lock );
949         break;
950
951     default:
952         msg_Err( p_this, "unimplemented!" );
953         p_list = NewList( 0 );
954         break;
955     }
956
957     return p_list;
958 }
959
960 /**
961  * Gets the list of children of an objects, and increment their reference
962  * count.
963  * @return a list (possibly empty) or NULL in case of error.
964  */
965 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
966 {
967     vlc_list_t *l;
968     vlc_object_internals_t *priv = vlc_internals( obj );
969
970     vlc_mutex_lock( &structure_lock );
971     l = NewList( priv->i_children );
972     for (int i = 0; i < l->i_count; i++)
973     {
974         vlc_object_yield( priv->pp_children[i] );
975         l->p_values[i].p_object = priv->pp_children[i];
976     }
977     vlc_mutex_unlock( &structure_lock );
978     return l;
979 }
980
981 /*****************************************************************************
982  * DumpCommand: print the current vlc structure
983  *****************************************************************************
984  * This function prints either an ASCII tree showing the connections between
985  * vlc objects, and additional information such as their refcount, thread ID,
986  * etc. (command "tree"), or the same data as a simple list (command "list").
987  *****************************************************************************/
988 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
989                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
990 {
991     (void)oldval; (void)p_data;
992     if( *psz_cmd == 'l' )
993     {
994         vlc_object_t *root = VLC_OBJECT (vlc_global ()), *cur = root; 
995
996         vlc_mutex_lock( &structure_lock );
997         do
998         {
999             PrintObject (cur, "");
1000             cur = vlc_internals (cur)->next;
1001         }
1002         while (cur != root);
1003         vlc_mutex_unlock( &structure_lock );
1004     }
1005     else
1006     {
1007         vlc_object_t *p_object = NULL;
1008
1009         if( *newval.psz_string )
1010         {
1011             char *end;
1012             int i_id = strtol( newval.psz_string, &end, 0 );
1013             if( end != newval.psz_string )
1014                 p_object = vlc_object_get( i_id );
1015             else
1016                 /* try using the object's name to find it */
1017                 p_object = vlc_object_find_name( p_this, newval.psz_string,
1018                                                  FIND_ANYWHERE );
1019
1020             if( !p_object )
1021             {
1022                 return VLC_ENOOBJ;
1023             }
1024         }
1025
1026         vlc_mutex_lock( &structure_lock );
1027
1028         if( *psz_cmd == 't' )
1029         {
1030             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1031
1032             if( !p_object )
1033                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1034
1035             psz_foo[0] = '|';
1036             DumpStructure( p_object, 0, psz_foo );
1037         }
1038         else if( *psz_cmd == 'v' )
1039         {
1040             int i;
1041
1042             if( !p_object )
1043                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1044
1045             PrintObject( p_object, "" );
1046
1047             if( !vlc_internals( p_object )->i_vars )
1048                 printf( " `-o No variables\n" );
1049             for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1050             {
1051                 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1052
1053                 const char *psz_type = "unknown";
1054                 switch( p_var->i_type & VLC_VAR_TYPE )
1055                 {
1056 #define MYCASE( type, nice )                \
1057                     case VLC_VAR_ ## type:  \
1058                         psz_type = nice;    \
1059                         break;
1060                     MYCASE( VOID, "void" );
1061                     MYCASE( BOOL, "bool" );
1062                     MYCASE( INTEGER, "integer" );
1063                     MYCASE( HOTKEY, "hotkey" );
1064                     MYCASE( STRING, "string" );
1065                     MYCASE( MODULE, "module" );
1066                     MYCASE( FILE, "file" );
1067                     MYCASE( DIRECTORY, "directory" );
1068                     MYCASE( VARIABLE, "variable" );
1069                     MYCASE( FLOAT, "float" );
1070                     MYCASE( TIME, "time" );
1071                     MYCASE( ADDRESS, "address" );
1072                     MYCASE( MUTEX, "mutex" );
1073                     MYCASE( LIST, "list" );
1074 #undef MYCASE
1075                 }
1076                 printf( " %c-o \"%s\" (%s",
1077                         i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1078                         p_var->psz_name, psz_type );
1079                 if( p_var->psz_text )
1080                     printf( ", %s", p_var->psz_text );
1081                 printf( ")" );
1082                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1083                     printf( ", command" );
1084                 if( p_var->i_entries )
1085                     printf( ", %d callbacks", p_var->i_entries );
1086                 switch( p_var->i_type & 0x00f0 )
1087                 {
1088                     case VLC_VAR_VOID:
1089                     case VLC_VAR_MUTEX:
1090                         break;
1091                     case VLC_VAR_BOOL:
1092                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1093                         break;
1094                     case VLC_VAR_INTEGER:
1095                         printf( ": %d", p_var->val.i_int );
1096                         break;
1097                     case VLC_VAR_STRING:
1098                         printf( ": \"%s\"", p_var->val.psz_string );
1099                         break;
1100                     case VLC_VAR_FLOAT:
1101                         printf( ": %f", p_var->val.f_float );
1102                         break;
1103                     case VLC_VAR_TIME:
1104                         printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1105                         break;
1106                     case VLC_VAR_ADDRESS:
1107                         printf( ": %p", p_var->val.p_address );
1108                         break;
1109                     case VLC_VAR_LIST:
1110                         printf( ": TODO" );
1111                         break;
1112                 }
1113                 printf( "\n" );
1114             }
1115         }
1116
1117         vlc_mutex_unlock( &structure_lock );
1118
1119         if( *newval.psz_string )
1120         {
1121             vlc_object_release( p_object );
1122         }
1123     }
1124
1125     return VLC_SUCCESS;
1126 }
1127
1128 /*****************************************************************************
1129  * vlc_list_release: free a list previously allocated by vlc_list_find
1130  *****************************************************************************
1131  * This function decreases the refcount of all objects in the list and
1132  * frees the list.
1133  *****************************************************************************/
1134 void vlc_list_release( vlc_list_t *p_list )
1135 {
1136     int i_index;
1137
1138     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1139     {
1140         vlc_object_release( p_list->p_values[i_index].p_object );
1141     }
1142
1143     free( p_list->p_values );
1144     free( p_list );
1145 }
1146
1147 /*****************************************************************************
1148  * dump an object. (Debug function)
1149  *****************************************************************************/
1150 void __vlc_object_dump( vlc_object_t *p_this )
1151 {
1152     vlc_mutex_lock( &structure_lock );
1153     char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1154     psz_foo[0] = '|';
1155     DumpStructure( p_this, 0, psz_foo );
1156     vlc_mutex_unlock( &structure_lock );
1157 }
1158
1159 /* Following functions are local */
1160
1161 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1162 {
1163     int i;
1164     vlc_object_t *p_tmp;
1165
1166     switch( i_mode & 0x000f )
1167     {
1168     case FIND_PARENT:
1169         p_tmp = p_this->p_parent;
1170         if( p_tmp )
1171         {
1172             if( p_tmp->i_object_type == i_type )
1173             {
1174                 vlc_object_yield( p_tmp );
1175                 return p_tmp;
1176             }
1177             else
1178             {
1179                 return FindObject( p_tmp, i_type, i_mode );
1180             }
1181         }
1182         break;
1183
1184     case FIND_CHILD:
1185         for( i = vlc_internals( p_this )->i_children; i--; )
1186         {
1187             p_tmp = vlc_internals( p_this )->pp_children[i];
1188             if( p_tmp->i_object_type == i_type )
1189             {
1190                 vlc_object_yield( p_tmp );
1191                 return p_tmp;
1192             }
1193             else if( vlc_internals( p_tmp )->i_children )
1194             {
1195                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1196                 if( p_tmp )
1197                 {
1198                     return p_tmp;
1199                 }
1200             }
1201         }
1202         break;
1203
1204     case FIND_ANYWHERE:
1205         /* Handled in vlc_object_find */
1206         break;
1207     }
1208
1209     return NULL;
1210 }
1211
1212 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1213                                       const char *psz_name,
1214                                       int i_mode )
1215 {
1216     int i;
1217     vlc_object_t *p_tmp;
1218
1219     switch( i_mode & 0x000f )
1220     {
1221     case FIND_PARENT:
1222         p_tmp = p_this->p_parent;
1223         if( p_tmp )
1224         {
1225             if( p_tmp->psz_object_name
1226                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1227             {
1228                 vlc_object_yield( p_tmp );
1229                 return p_tmp;
1230             }
1231             else
1232             {
1233                 return FindObjectName( p_tmp, psz_name, i_mode );
1234             }
1235         }
1236         break;
1237
1238     case FIND_CHILD:
1239         for( i = vlc_internals( p_this )->i_children; i--; )
1240         {
1241             p_tmp = vlc_internals( p_this )->pp_children[i];
1242             if( p_tmp->psz_object_name
1243                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1244             {
1245                 vlc_object_yield( p_tmp );
1246                 return p_tmp;
1247             }
1248             else if( vlc_internals( p_tmp )->i_children )
1249             {
1250                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1251                 if( p_tmp )
1252                 {
1253                     return p_tmp;
1254                 }
1255             }
1256         }
1257         break;
1258
1259     case FIND_ANYWHERE:
1260         /* Handled in vlc_object_find */
1261         break;
1262     }
1263
1264     return NULL;
1265 }
1266
1267
1268 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1269 {
1270     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1271          psz_parent[20];
1272
1273     memset( &psz_name, 0, sizeof(psz_name) );
1274     if( p_this->psz_object_name )
1275     {
1276         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1277         if( psz_name[48] )
1278             psz_name[48] = '\"';
1279     }
1280
1281     psz_children[0] = '\0';
1282     switch( vlc_internals( p_this )->i_children )
1283     {
1284         case 0:
1285             break;
1286         case 1:
1287             strcpy( psz_children, ", 1 child" );
1288             break;
1289         default:
1290             snprintf( psz_children, 19, ", %i children",
1291                       vlc_internals( p_this )->i_children );
1292             break;
1293     }
1294
1295     psz_refcount[0] = '\0';
1296     if( vlc_internals( p_this )->i_refcount > 0 )
1297         snprintf( psz_refcount, 19, ", refcount %u",
1298                   vlc_internals( p_this )->i_refcount );
1299
1300     psz_thread[0] = '\0';
1301     if( vlc_internals( p_this )->b_thread )
1302         snprintf( psz_thread, 29, " (thread %lu)",
1303                   (unsigned long)vlc_internals( p_this )->thread_id );
1304
1305     psz_parent[0] = '\0';
1306     if( p_this->p_parent )
1307         snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1308
1309     printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1310             p_this->i_object_id, p_this->psz_object_type,
1311             psz_name, psz_thread, psz_refcount, psz_children,
1312             psz_parent );
1313 }
1314
1315 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1316 {
1317     int i;
1318     char i_back = psz_foo[i_level];
1319     psz_foo[i_level] = '\0';
1320
1321     PrintObject( p_this, psz_foo );
1322
1323     psz_foo[i_level] = i_back;
1324
1325     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1326     {
1327         msg_Warn( p_this, "structure tree is too deep" );
1328         return;
1329     }
1330
1331     for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1332     {
1333         if( i_level )
1334         {
1335             psz_foo[i_level-1] = ' ';
1336
1337             if( psz_foo[i_level-2] == '`' )
1338             {
1339                 psz_foo[i_level-2] = ' ';
1340             }
1341         }
1342
1343         if( i == vlc_internals( p_this )->i_children - 1 )
1344         {
1345             psz_foo[i_level] = '`';
1346         }
1347         else
1348         {
1349             psz_foo[i_level] = '|';
1350         }
1351
1352         psz_foo[i_level+1] = '-';
1353         psz_foo[i_level+2] = '\0';
1354
1355         DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1356                        psz_foo );
1357     }
1358 }
1359
1360 static vlc_list_t * NewList( int i_count )
1361 {
1362     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1363     if( p_list == NULL )
1364     {
1365         return NULL;
1366     }
1367
1368     p_list->i_count = i_count;
1369
1370     if( i_count == 0 )
1371     {
1372         p_list->p_values = NULL;
1373         return p_list;
1374     }
1375
1376     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1377     if( p_list->p_values == NULL )
1378     {
1379         p_list->i_count = 0;
1380         return p_list;
1381     }
1382
1383     return p_list;
1384 }
1385
1386 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1387                          int i_index )
1388 {
1389     if( p_list == NULL || i_index >= p_list->i_count )
1390     {
1391         return;
1392     }
1393
1394     vlc_object_yield( p_object );
1395
1396     p_list->p_values[i_index].p_object = p_object;
1397
1398     return;
1399 }
1400
1401 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1402 {
1403     if( p_list == NULL )
1404     {
1405         return;
1406     }
1407
1408     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1409                                 * sizeof( vlc_value_t ) );
1410     if( p_list->p_values == NULL )
1411     {
1412         p_list->i_count = 0;
1413         return;
1414     }
1415
1416     vlc_object_yield( p_object );
1417
1418     p_list->p_values[p_list->i_count].p_object = p_object;
1419     p_list->i_count++;
1420
1421     return;
1422 }*/
1423
1424 static int CountChildren( vlc_object_t *p_this, int i_type )
1425 {
1426     vlc_object_t *p_tmp;
1427     int i, i_count = 0;
1428
1429     for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1430     {
1431         p_tmp = vlc_internals( p_this )->pp_children[i];
1432
1433         if( p_tmp->i_object_type == i_type )
1434         {
1435             i_count++;
1436         }
1437         i_count += CountChildren( p_tmp, i_type );
1438     }
1439
1440     return i_count;
1441 }
1442
1443 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1444 {
1445     vlc_object_t *p_tmp;
1446     int i;
1447
1448     for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1449     {
1450         p_tmp = vlc_internals( p_this )->pp_children[i];
1451
1452         if( p_tmp->i_object_type == i_type )
1453             ListReplace( p_list, p_tmp, p_list->i_count++ );
1454
1455         ListChildren( p_list, p_tmp, i_type );
1456     }
1457 }
1458
1459 #ifdef LIBVLC_REFCHECK
1460 # if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE)
1461 #  include <execinfo.h>
1462 # endif
1463
1464 void vlc_refcheck (vlc_object_t *obj)
1465 {
1466     static unsigned errors = 0;
1467     if (errors > 100)
1468         return;
1469
1470     /* Anyone can use the root object (though it should not exist) */
1471     if (obj == VLC_OBJECT (vlc_global ()))
1472         return;
1473
1474     /* Anyone can use its libvlc instance object */
1475     if (obj == VLC_OBJECT (obj->p_libvlc))
1476         return;
1477
1478     /* The thread that created the object holds the initial reference */
1479     vlc_object_internals_t *priv = vlc_internals (obj);
1480 #if defined (LIBVLC_USE_PTHREAD)
1481     if (pthread_equal (priv->creator_id, pthread_self ()))
1482 #elif defined WIN32
1483     if (priv->creator_id == GetCurrentThreadId ())
1484 #else
1485     if (0)
1486 #endif
1487         return;
1488
1489     /* A thread can use its own object without references! */
1490     vlc_object_t *caller = vlc_threadobj ();
1491     if (caller == obj)
1492         return;
1493 #if 0
1494     /* The calling thread is younger than the object.
1495      * Access could be valid through cross-thread synchronization;
1496      * we would need better accounting. */
1497     if (caller && (caller->i_object_id > obj->i_object_id))
1498         return;
1499 #endif
1500     int refs;
1501     vlc_spin_lock (&priv->ref_spin);
1502     refs = priv->i_refcount;
1503     vlc_spin_unlock (&priv->ref_spin);
1504
1505     for (held_list_t *hlcur = vlc_threadvar_get (&held_objects);
1506          hlcur != NULL; hlcur = hlcur->next)
1507         if (hlcur->obj == obj)
1508             return;
1509
1510     fprintf (stderr, "The %s %s thread object is accessing...\n"
1511              "the %s %s object without references.\n",
1512              caller && caller->psz_object_name
1513                      ? caller->psz_object_name : "unnamed",
1514              caller ? caller->psz_object_type : "main",
1515              obj->psz_object_name ? obj->psz_object_name : "unnamed",
1516              obj->psz_object_type);
1517     fflush (stderr);
1518
1519 #ifdef HAVE_BACKTRACE
1520     void *stack[20];
1521     int stackdepth = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
1522     backtrace_symbols_fd (stack, stackdepth, 2);
1523 #endif
1524
1525     if (++errors == 100)
1526         fprintf (stderr, "Too many reference errors!\n");
1527 }
1528
1529 static void held_objects_destroy (void *data)
1530 {
1531     VLC_UNUSED( data );
1532     held_list_t *hl = vlc_threadvar_get (&held_objects);
1533     vlc_object_t *caller = vlc_threadobj ();
1534
1535     while (hl != NULL)
1536     {
1537         held_list_t *buf = hl->next;
1538         vlc_object_t *obj = hl->obj;
1539
1540         fprintf (stderr, "The %s %s thread object leaked a reference to...\n"
1541                          "the %s %s object.\n",
1542                  caller && caller->psz_object_name
1543                      ? caller->psz_object_name : "unnamed",
1544                  caller ? caller->psz_object_type : "main",
1545                  obj->psz_object_name ? obj->psz_object_name : "unnamed",
1546                  obj->psz_object_type);
1547         free (hl);
1548         hl = buf;
1549     }
1550 }
1551 #endif