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