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