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