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