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