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