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