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