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