]> git.sesse.net Git - vlc/blob - src/misc/objects.c
Serialize vlc_object_set_destructor() against vlc_object_release()
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /**
24  * \file
25  * This file contains the functions to handle the vlc_object_t type
26  *
27  * Unless otherwise stated, functions in this file are not cancellation point.
28  * All functions in this file are safe w.r.t. deferred cancellation.
29  */
30
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc_common.h>
40
41 #include "../libvlc.h"
42 #include <vlc_aout.h>
43 #include "audio_output/aout_internal.h"
44
45 #include "vlc_interface.h"
46 #include "vlc_codec.h"
47
48 #include "variables.h"
49 #ifndef WIN32
50 # include <unistd.h>
51 #else
52 # include <io.h>
53 # include <fcntl.h>
54 # include <errno.h> /* ENOSYS */
55 #endif
56
57 #include <assert.h>
58
59 #if defined (HAVE_SYS_EVENTFD_H)
60 # include <sys/eventfd.h>
61 # define HAVE_EVENTFD 1
62 #endif
63
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 #ifndef NDEBUG
85 static void vlc_object_dump( vlc_object_t *p_this );
86 #endif
87
88 /*****************************************************************************
89  * Local structure lock
90  *****************************************************************************/
91 static void libvlc_lock (libvlc_int_t *p_libvlc)
92 {
93     vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
94 }
95
96 static void libvlc_unlock (libvlc_int_t *p_libvlc)
97 {
98     vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
99 }
100
101 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
102                            int i_type, const char *psz_type )
103 {
104     vlc_object_t *p_new;
105     vlc_object_internals_t *p_priv;
106
107     /* NOTE:
108      * VLC objects are laid out as follow:
109      * - first the LibVLC-private per-object data,
110      * - then VLC_COMMON members from vlc_object_t,
111      * - finally, the type-specific data (if any).
112      *
113      * This function initializes the LibVLC and common data,
114      * and zeroes the rest.
115      */
116     p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
117     if( p_priv == NULL )
118         return NULL;
119
120     assert (i_size >= sizeof (vlc_object_t));
121     p_new = (vlc_object_t *)(p_priv + 1);
122
123     p_priv->i_object_type = i_type;
124     p_new->psz_object_type = psz_type;
125     p_new->psz_object_name = NULL;
126
127     p_new->b_die = false;
128     p_new->b_error = false;
129     p_new->b_force = false;
130
131     p_new->psz_header = NULL;
132
133     if (p_this)
134         p_new->i_flags = p_this->i_flags
135             & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
136
137     p_priv->p_vars = calloc( 16, sizeof( variable_t ) );
138
139     if( !p_priv->p_vars )
140     {
141         free( p_priv );
142         return NULL;
143     }
144
145     if( p_this == NULL )
146     {
147         libvlc_int_t *self = (libvlc_int_t*)p_new;
148         p_new->p_libvlc = self;
149         vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
150         p_this = p_priv->next = p_priv->prev = p_new;
151     }
152     else
153         p_new->p_libvlc = p_this->p_libvlc;
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_priv->var_lock );
167     vlc_cond_init( &p_priv->var_wait );
168     p_priv->pipes[0] = p_priv->pipes[1] = -1;
169
170     p_priv->next = p_this;
171     libvlc_lock (p_new->p_libvlc);
172     p_priv->prev = vlc_internals (p_this)->prev;
173     vlc_internals (p_this)->prev = p_new;
174     vlc_internals (p_priv->prev)->next = p_new;
175     libvlc_unlock (p_new->p_libvlc);
176
177     if (p_new == VLC_OBJECT(p_new->p_libvlc))
178     {   /* TODO: should be in src/libvlc.c */
179         int canc = vlc_savecancel ();
180         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
181         var_AddCallback( p_new, "list", DumpCommand, NULL );
182         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
183         var_AddCallback( p_new, "tree", DumpCommand, NULL );
184         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
185         var_AddCallback( p_new, "vars", DumpCommand, NULL );
186         vlc_restorecancel (canc);
187     }
188
189     return p_new;
190 }
191
192
193 /**
194  * Allocates and initializes a vlc object.
195  *
196  * @param i_type known object type (all of them are negative integer values),
197  *               or object byte size (always positive).
198  *
199  * @return the new object, or NULL on error.
200  */
201 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
202 {
203     const char   * psz_type;
204     size_t         i_size;
205
206     switch( i_type )
207     {
208         case VLC_OBJECT_INTF:
209             i_size = sizeof(intf_thread_t);
210             psz_type = "interface";
211             break;
212         case VLC_OBJECT_DECODER:
213             i_size = sizeof(decoder_t);
214             psz_type = "decoder";
215             break;
216         case VLC_OBJECT_PACKETIZER:
217             i_size = sizeof(decoder_t);
218             psz_type = "packetizer";
219             break;
220         case VLC_OBJECT_AOUT:
221             i_size = sizeof(aout_instance_t);
222             psz_type = "audio output";
223             break;
224         default:
225             assert( i_type > 0 ); /* unknown type?! */
226             i_size = i_type;
227             i_type = VLC_OBJECT_GENERIC;
228             psz_type = "generic";
229             break;
230     }
231
232     return vlc_custom_create( p_this, i_size, i_type, psz_type );
233 }
234
235
236 /**
237  ****************************************************************************
238  * Set the destructor of a vlc object
239  *
240  * This function sets the destructor of the vlc object. It will be called
241  * when the object is destroyed when the its refcount reaches 0.
242  * (It is called by the internal function vlc_object_destroy())
243  *****************************************************************************/
244 void __vlc_object_set_destructor( vlc_object_t *p_this,
245                                   vlc_destructor_t pf_destructor )
246 {
247     vlc_object_internals_t *p_priv = vlc_internals(p_this );
248
249     vlc_spin_lock( &p_priv->ref_spin );
250     p_priv->pf_destructor = pf_destructor;
251     vlc_spin_unlock( &p_priv->ref_spin );
252 }
253
254 /**
255  ****************************************************************************
256  * Destroy a vlc object (Internal)
257  *
258  * This function destroys an object that has been previously allocated with
259  * vlc_object_create. The object's refcount must be zero and it must not be
260  * attached to other objects in any way.
261  *
262  * This function must be called with cancellation disabled (currently).
263  *****************************************************************************/
264 static void vlc_object_destroy( vlc_object_t *p_this )
265 {
266     vlc_object_internals_t *p_priv = vlc_internals( p_this );
267
268     /* Objects are always detached beforehand */
269     assert( !p_this->p_parent );
270
271     /* Send a kill to the object's thread if applicable */
272     vlc_object_kill( p_this );
273
274     /* Call the custom "subclass" destructor */
275     if( p_priv->pf_destructor )
276         p_priv->pf_destructor( p_this );
277
278     /* Any thread must have been cleaned up at this point. */
279     assert( !p_priv->b_thread );
280
281     /* Destroy the associated variables, starting from the end so that
282      * no memmove calls have to be done. */
283     while( p_priv->i_vars )
284     {
285         var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
286     }
287
288     free( p_priv->p_vars );
289     vlc_cond_destroy( &p_priv->var_wait );
290     vlc_mutex_destroy( &p_priv->var_lock );
291
292     free( p_this->psz_header );
293
294     FREENULL( p_this->psz_object_name );
295
296     vlc_spin_destroy( &p_priv->ref_spin );
297     if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
298         close( p_priv->pipes[1] );
299     if( p_priv->pipes[0] != -1 )
300         close( p_priv->pipes[0] );
301     if( VLC_OBJECT(p_this->p_libvlc) == p_this )
302         vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
303
304     free( p_priv );
305 }
306
307
308 #ifdef WIN32
309 # include <winsock2.h>
310 # include <ws2tcpip.h>
311
312 /**
313  * select()-able pipes emulated using Winsock
314  */
315 static int pipe (int fd[2])
316 {
317     SOCKADDR_IN addr;
318     int addrlen = sizeof (addr);
319
320     SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
321            c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
322     if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
323         goto error;
324
325     memset (&addr, 0, sizeof (addr));
326     addr.sin_family = AF_INET;
327     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
328     if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
329      || getsockname (l, (PSOCKADDR)&addr, &addrlen)
330      || listen (l, 1)
331      || connect (c, (PSOCKADDR)&addr, addrlen))
332         goto error;
333
334     a = accept (l, NULL, NULL);
335     if (a == INVALID_SOCKET)
336         goto error;
337
338     closesocket (l);
339     //shutdown (a, 0);
340     //shutdown (c, 1);
341     fd[0] = c;
342     fd[1] = a;
343     return 0;
344
345 error:
346     if (l != INVALID_SOCKET)
347         closesocket (l);
348     if (c != INVALID_SOCKET)
349         closesocket (c);
350     return -1;
351 }
352
353 #undef  read
354 #define read( a, b, c )  recv (a, b, c, 0)
355 #undef  write
356 #define write( a, b, c ) send (a, b, c, 0)
357 #undef  close
358 #define close( a )       closesocket (a)
359 #endif /* WIN32 */
360
361 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
362
363 /**
364  * Returns the readable end of a pipe that becomes readable once termination
365  * of the object is requested (vlc_object_kill()).
366  * This can be used to wake-up out of a select() or poll() event loop, such
367  * typically when doing network I/O.
368  *
369  * Note that the pipe will remain the same for the lifetime of the object.
370  * DO NOT read the pipe nor close it yourself. Ever.
371  *
372  * @param obj object that would be "killed"
373  * @return a readable pipe descriptor, or -1 on error.
374  */
375 int vlc_object_waitpipe( vlc_object_t *obj )
376 {
377     vlc_object_internals_t *internals = vlc_internals( obj );
378
379     vlc_mutex_lock (&pipe_lock);
380     if (internals->pipes[0] == -1)
381     {
382         /* This can only ever happen if someone killed us without locking: */
383         assert (internals->pipes[1] == -1);
384
385 #ifdef HAVE_EVENTFD
386         internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
387         if (internals->pipes[0] == -1)
388 #endif
389         {
390             if (pipe (internals->pipes))
391                 internals->pipes[0] = internals->pipes[1] = -1;
392         }
393
394         if (internals->pipes[0] != -1 && obj->b_die)
395         {   /* Race condition: vlc_object_kill() already invoked! */
396             msg_Dbg (obj, "waitpipe: object already dying");
397             write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
398         }
399     }
400     vlc_mutex_unlock (&pipe_lock);
401     return internals->pipes[0];
402 }
403
404
405 /**
406  * Requests termination of an object, cancels the object thread, and make the
407  * object wait pipe (if it exists) readable. Not a cancellation point.
408  */
409 void __vlc_object_kill( vlc_object_t *p_this )
410 {
411     vlc_object_internals_t *priv = vlc_internals( p_this );
412     int fd = -1;
413
414     vlc_thread_cancel( p_this );
415     vlc_mutex_lock( &pipe_lock );
416     if( !p_this->b_die )
417     {
418         fd = priv->pipes[1];
419         p_this->b_die = true;
420     }
421
422     /* This also serves as a memory barrier toward vlc_object_alive(): */
423     vlc_mutex_unlock( &pipe_lock );
424
425     if (fd != -1)
426     {
427         int canc = vlc_savecancel ();
428
429         /* write _after_ setting b_die, so vlc_object_alive() returns false */
430         write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
431         msg_Dbg (p_this, "waitpipe: object killed");
432         vlc_restorecancel (canc);
433     }
434 }
435
436
437 /*****************************************************************************
438  * find a typed object and increment its refcount
439  *****************************************************************************
440  * This function recursively looks for a given object type. i_mode can be one
441  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
442  *****************************************************************************/
443 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
444 {
445     vlc_object_t *p_found;
446
447     /* If we are of the requested type ourselves, don't look further */
448     if( !(i_mode & FIND_STRICT)
449      && vlc_internals (p_this)->i_object_type == i_type )
450     {
451         vlc_object_hold( p_this );
452         return p_this;
453     }
454
455     /* Otherwise, recursively look for the object */
456     if ((i_mode & 0x000f) == FIND_ANYWHERE)
457         return vlc_object_find (p_this->p_libvlc, i_type,
458                                 (i_mode & ~0x000f)|FIND_CHILD);
459
460     libvlc_lock (p_this->p_libvlc);
461     p_found = FindObject( p_this, i_type, i_mode );
462     libvlc_unlock (p_this->p_libvlc);
463     return p_found;
464 }
465
466 #undef vlc_object_find_name
467 /**
468  * Finds a named object and increment its reference count.
469  * Beware that objects found in this manner can be "owned" by another thread,
470  * be of _any_ type, and be attached to any module (if any). With such an
471  * object reference, you can set or get object variables, emit log messages,
472  * and read write-once object parameters (psz_object_type, etc).
473  * You CANNOT cast the object to a more specific object type, and you
474  * definitely cannot invoke object type-specific callbacks with this.
475  *
476  * @param p_this object to search from
477  * @param psz_name name of the object to search for
478  * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
479  *
480  * @return a matching object (must be released by the caller),
481  * or NULL on error.
482  */
483 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
484                                     const char *psz_name, int i_mode )
485 {
486     vlc_object_t *p_found;
487
488     /* If have the requested name ourselves, don't look further */
489     if( !(i_mode & FIND_STRICT)
490         && p_this->psz_object_name
491         && !strcmp( p_this->psz_object_name, psz_name ) )
492     {
493         vlc_object_hold( p_this );
494         return p_this;
495     }
496
497     libvlc_lock (p_this->p_libvlc);
498
499     /* Otherwise, recursively look for the object */
500     if( (i_mode & 0x000f) == FIND_ANYWHERE )
501     {
502         vlc_object_t *p_root = p_this;
503
504         /* Find the root */
505         while( p_root->p_parent != NULL &&
506                p_root != VLC_OBJECT( p_this->p_libvlc ) )
507         {
508             p_root = p_root->p_parent;
509         }
510
511         p_found = FindObjectName( p_root, psz_name,
512                                  (i_mode & ~0x000f)|FIND_CHILD );
513         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
514         {
515             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
516                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
517         }
518     }
519     else
520     {
521         p_found = FindObjectName( p_this, psz_name, i_mode );
522     }
523
524     libvlc_unlock (p_this->p_libvlc);
525     return p_found;
526 }
527
528 /**
529  * Increment an object reference counter.
530  */
531 void * __vlc_object_hold( vlc_object_t *p_this )
532 {
533     vlc_object_internals_t *internals = vlc_internals( p_this );
534
535     vlc_spin_lock( &internals->ref_spin );
536     /* Avoid obvious freed object uses */
537     assert( internals->i_refcount > 0 );
538     /* Increment the counter */
539     internals->i_refcount++;
540     vlc_spin_unlock( &internals->ref_spin );
541     return p_this;
542 }
543
544 /*****************************************************************************
545  * Decrement an object refcount
546  * And destroy the object if its refcount reach zero.
547  *****************************************************************************/
548 void __vlc_object_release( vlc_object_t *p_this )
549 {
550     vlc_object_internals_t *internals = vlc_internals( p_this );
551     vlc_object_t *parent = NULL;
552     bool b_should_destroy;
553
554     vlc_spin_lock( &internals->ref_spin );
555     assert( internals->i_refcount > 0 );
556
557     if( internals->i_refcount > 1 )
558     {
559         /* Fast path */
560         /* There are still other references to the object */
561         internals->i_refcount--;
562         vlc_spin_unlock( &internals->ref_spin );
563         return;
564     }
565     vlc_spin_unlock( &internals->ref_spin );
566
567     /* Slow path */
568     /* Remember that we cannot hold the spin while waiting on the mutex */
569     libvlc_lock (p_this->p_libvlc);
570     /* Take the spin again. Note that another thread may have held the
571      * object in the (very short) mean time. */
572     vlc_spin_lock( &internals->ref_spin );
573     b_should_destroy = --internals->i_refcount == 0;
574     vlc_spin_unlock( &internals->ref_spin );
575
576     if( b_should_destroy )
577     {
578         parent = p_this->p_parent;
579
580 #ifndef NDEBUG
581         if( VLC_OBJECT(p_this->p_libvlc) == p_this )
582         {
583             /* Test for leaks */
584             vlc_object_t *leaked = internals->next;
585             while( leaked != p_this )
586             {
587                 /* We are leaking this object */
588                 fprintf( stderr,
589                          "ERROR: leaking object (%p, type:%s, name:%s)\n",
590                          leaked, leaked->psz_object_type,
591                          leaked->psz_object_name );
592                 /* Dump object to ease debugging */
593                 vlc_object_dump( leaked );
594                 fflush(stderr);
595                 leaked = vlc_internals (leaked)->next;
596             }
597
598             if( internals->next != p_this )
599                 /* Dump libvlc object to ease debugging */
600                 vlc_object_dump( p_this );
601         }
602 #endif
603         /* Remove the object from object list
604          * so that it cannot be encountered by vlc_object_get() */
605         vlc_internals (internals->next)->prev = internals->prev;
606         vlc_internals (internals->prev)->next = internals->next;
607
608         if (parent)
609             /* Detach from parent to protect against FIND_CHILDREN */
610             vlc_object_detach_unlocked (p_this);
611
612         /* We have no children */
613         assert (internals->i_children == 0);
614     }
615     libvlc_unlock (p_this->p_libvlc);
616
617     if( b_should_destroy )
618     {
619         int canc;
620
621         canc = vlc_savecancel ();
622         vlc_object_destroy( p_this );
623         vlc_restorecancel (canc);
624         if (parent)
625             vlc_object_release (parent);
626     }
627 }
628
629 /**
630  ****************************************************************************
631  * attach object to a parent object
632  *****************************************************************************
633  * This function sets p_this as a child of p_parent, and p_parent as a parent
634  * of p_this. This link can be undone using vlc_object_detach.
635  *****************************************************************************/
636 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
637 {
638     if( !p_this ) return;
639
640     vlc_object_hold (p_parent);
641     libvlc_lock (p_this->p_libvlc);
642
643     /* Attach the parent to its child */
644     assert (!p_this->p_parent);
645     p_this->p_parent = p_parent;
646
647     /* Attach the child to its parent */
648     vlc_object_internals_t *priv = vlc_internals( p_parent );
649     INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
650                  p_this );
651     libvlc_unlock (p_this->p_libvlc);
652 }
653
654
655 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
656 {
657     if (p_this->p_parent == NULL)
658         return;
659
660     vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
661
662     int i_index, i;
663
664     /* Remove p_this's parent */
665     p_this->p_parent = NULL;
666
667     /* Remove all of p_parent's children which are p_this */
668     for( i_index = priv->i_children ; i_index-- ; )
669     {
670         if( priv->pp_children[i_index] == p_this )
671         {
672             priv->i_children--;
673             for( i = i_index ; i < priv->i_children ; i++ )
674                 priv->pp_children[i] = priv->pp_children[i+1];
675         }
676     }
677
678     if( priv->i_children )
679     {
680         vlc_object_t **pp_children = (vlc_object_t **)
681             realloc( priv->pp_children,
682                      priv->i_children * sizeof(vlc_object_t *) );
683         if( pp_children )
684             priv->pp_children = pp_children;
685     }
686     else
687     {
688         /* Special case - don't realloc() to zero to avoid leaking */
689         free( priv->pp_children );
690         priv->pp_children = NULL;
691     }
692 }
693
694
695 /**
696  ****************************************************************************
697  * detach object from its parent
698  *****************************************************************************
699  * This function removes all links between an object and its parent.
700  *****************************************************************************/
701 void __vlc_object_detach( vlc_object_t *p_this )
702 {
703     vlc_object_t *p_parent;
704     if( !p_this ) return;
705
706     libvlc_lock (p_this->p_libvlc);
707     p_parent = p_this->p_parent;
708     if (p_parent)
709         vlc_object_detach_unlocked( p_this );
710     libvlc_unlock (p_this->p_libvlc);
711
712     if (p_parent)
713         vlc_object_release (p_parent);
714 }
715
716
717 /**
718  ****************************************************************************
719  * find a list typed objects and increment their refcount
720  *****************************************************************************
721  * This function recursively looks for a given object type. i_mode can be one
722  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
723  *****************************************************************************/
724 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
725 {
726     vlc_list_t *p_list;
727     int i_count = 0;
728
729     /* Look for the objects */
730     switch( i_mode & 0x000f )
731     {
732     case FIND_ANYWHERE:
733         return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
734
735     case FIND_CHILD:
736         libvlc_lock (p_this->p_libvlc);
737         i_count = CountChildren( p_this, i_type );
738         p_list = NewList( i_count );
739
740         /* Check allocation was successful */
741         if( p_list->i_count != i_count )
742         {
743             libvlc_unlock (p_this->p_libvlc);
744             p_list->i_count = 0;
745             break;
746         }
747
748         p_list->i_count = 0;
749         ListChildren( p_list, p_this, i_type );
750         libvlc_unlock (p_this->p_libvlc);
751         break;
752
753     default:
754         msg_Err( p_this, "unimplemented!" );
755         p_list = NewList( 0 );
756         break;
757     }
758
759     return p_list;
760 }
761
762 /**
763  * Gets the list of children of an objects, and increment their reference
764  * count.
765  * @return a list (possibly empty) or NULL in case of error.
766  */
767 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
768 {
769     vlc_list_t *l;
770     vlc_object_internals_t *priv = vlc_internals( obj );
771
772     libvlc_lock (obj->p_libvlc);
773     l = NewList( priv->i_children );
774     for (int i = 0; i < l->i_count; i++)
775     {
776         vlc_object_hold( priv->pp_children[i] );
777         l->p_values[i].p_object = priv->pp_children[i];
778     }
779     libvlc_unlock (obj->p_libvlc);
780     return l;
781 }
782
783 /*****************************************************************************
784  * DumpCommand: print the current vlc structure
785  *****************************************************************************
786  * This function prints either an ASCII tree showing the connections between
787  * vlc objects, and additional information such as their refcount, thread ID,
788  * etc. (command "tree"), or the same data as a simple list (command "list").
789  *****************************************************************************/
790 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
791                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
792 {
793     libvlc_int_t *p_libvlc = p_this->p_libvlc;
794
795     (void)oldval; (void)p_data;
796     if( *psz_cmd == 'l' )
797     {
798         vlc_object_t *cur = VLC_OBJECT (p_libvlc);
799
800         libvlc_lock (p_this->p_libvlc);
801         do
802         {
803             PrintObject (cur, "");
804             cur = vlc_internals (cur)->next;
805         }
806         while (cur != VLC_OBJECT(p_libvlc));
807         libvlc_unlock (p_this->p_libvlc);
808     }
809     else
810     {
811         vlc_object_t *p_object = NULL;
812
813         if( *newval.psz_string )
814         {
815             /* try using the object's name to find it */
816             p_object = vlc_object_find_name( p_this, newval.psz_string,
817                                              FIND_ANYWHERE );
818             if( !p_object )
819             {
820                 return VLC_ENOOBJ;
821             }
822         }
823
824         libvlc_lock (p_this->p_libvlc);
825         if( *psz_cmd == 't' )
826         {
827             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
828
829             if( !p_object )
830                 p_object = VLC_OBJECT(p_this->p_libvlc);
831
832             psz_foo[0] = '|';
833             DumpStructure( p_object, 0, psz_foo );
834         }
835         else if( *psz_cmd == 'v' )
836         {
837             int i;
838
839             if( !p_object )
840                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
841
842             PrintObject( p_object, "" );
843
844             if( !vlc_internals( p_object )->i_vars )
845                 printf( " `-o No variables\n" );
846             for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
847             {
848                 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
849
850                 const char *psz_type = "unknown";
851                 switch( p_var->i_type & VLC_VAR_TYPE )
852                 {
853 #define MYCASE( type, nice )                \
854                     case VLC_VAR_ ## type:  \
855                         psz_type = nice;    \
856                         break;
857                     MYCASE( VOID, "void" );
858                     MYCASE( BOOL, "bool" );
859                     MYCASE( INTEGER, "integer" );
860                     MYCASE( HOTKEY, "hotkey" );
861                     MYCASE( STRING, "string" );
862                     MYCASE( MODULE, "module" );
863                     MYCASE( FILE, "file" );
864                     MYCASE( DIRECTORY, "directory" );
865                     MYCASE( VARIABLE, "variable" );
866                     MYCASE( FLOAT, "float" );
867                     MYCASE( TIME, "time" );
868                     MYCASE( ADDRESS, "address" );
869                     MYCASE( MUTEX, "mutex" );
870                     MYCASE( LIST, "list" );
871 #undef MYCASE
872                 }
873                 printf( " %c-o \"%s\" (%s",
874                         i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
875                         p_var->psz_name, psz_type );
876                 if( p_var->psz_text )
877                     printf( ", %s", p_var->psz_text );
878                 printf( ")" );
879                 if( p_var->i_type & VLC_VAR_HASCHOICE )
880                     printf( ", has choices" );
881                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
882                     printf( ", command" );
883                 if( p_var->i_entries )
884                     printf( ", %d callbacks", p_var->i_entries );
885                 switch( p_var->i_type & VLC_VAR_CLASS )
886                 {
887                     case VLC_VAR_VOID:
888                     case VLC_VAR_MUTEX:
889                         break;
890                     case VLC_VAR_BOOL:
891                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
892                         break;
893                     case VLC_VAR_INTEGER:
894                         printf( ": %d", p_var->val.i_int );
895                         break;
896                     case VLC_VAR_STRING:
897                         printf( ": \"%s\"", p_var->val.psz_string );
898                         break;
899                     case VLC_VAR_FLOAT:
900                         printf( ": %f", p_var->val.f_float );
901                         break;
902                     case VLC_VAR_TIME:
903                         printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
904                         break;
905                     case VLC_VAR_ADDRESS:
906                         printf( ": %p", p_var->val.p_address );
907                         break;
908                     case VLC_VAR_LIST:
909                         printf( ": TODO" );
910                         break;
911                 }
912                 printf( "\n" );
913             }
914         }
915         libvlc_unlock (p_this->p_libvlc);
916
917         if( *newval.psz_string )
918         {
919             vlc_object_release( p_object );
920         }
921     }
922
923     return VLC_SUCCESS;
924 }
925
926 /*****************************************************************************
927  * vlc_list_release: free a list previously allocated by vlc_list_find
928  *****************************************************************************
929  * This function decreases the refcount of all objects in the list and
930  * frees the list.
931  *****************************************************************************/
932 void vlc_list_release( vlc_list_t *p_list )
933 {
934     int i_index;
935
936     for( i_index = 0; i_index < p_list->i_count; i_index++ )
937     {
938         vlc_object_release( p_list->p_values[i_index].p_object );
939     }
940
941     free( p_list->p_values );
942     free( p_list );
943 }
944
945 /*****************************************************************************
946  * dump an object. (Debug function)
947  *****************************************************************************/
948 #ifndef NDEBUG
949 static void vlc_object_dump( vlc_object_t *p_this )
950 {
951     char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
952     psz_foo[0] = '|';
953
954     DumpStructure( p_this, 0, psz_foo );
955 }
956 #endif
957
958 /* Following functions are local */
959
960 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
961 {
962     int i;
963     vlc_object_t *p_tmp;
964
965     switch( i_mode & 0x000f )
966     {
967     case FIND_PARENT:
968         p_tmp = p_this->p_parent;
969         if( p_tmp )
970         {
971             if( vlc_internals( p_tmp )->i_object_type == i_type )
972             {
973                 vlc_object_hold( p_tmp );
974                 return p_tmp;
975             }
976             else
977             {
978                 return FindObject( p_tmp, i_type, i_mode );
979             }
980         }
981         break;
982
983     case FIND_CHILD:
984         for( i = vlc_internals( p_this )->i_children; i--; )
985         {
986             p_tmp = vlc_internals( p_this )->pp_children[i];
987             if( vlc_internals( p_tmp )->i_object_type == i_type )
988             {
989                 vlc_object_hold( p_tmp );
990                 return p_tmp;
991             }
992             else if( vlc_internals( p_tmp )->i_children )
993             {
994                 p_tmp = FindObject( p_tmp, i_type, i_mode );
995                 if( p_tmp )
996                 {
997                     return p_tmp;
998                 }
999             }
1000         }
1001         break;
1002
1003     case FIND_ANYWHERE:
1004         /* Handled in vlc_object_find */
1005         break;
1006     }
1007
1008     return NULL;
1009 }
1010
1011 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1012                                       const char *psz_name,
1013                                       int i_mode )
1014 {
1015     int i;
1016     vlc_object_t *p_tmp;
1017
1018     switch( i_mode & 0x000f )
1019     {
1020     case FIND_PARENT:
1021         p_tmp = p_this->p_parent;
1022         if( p_tmp )
1023         {
1024             if( p_tmp->psz_object_name
1025                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1026             {
1027                 vlc_object_hold( p_tmp );
1028                 return p_tmp;
1029             }
1030             else
1031             {
1032                 return FindObjectName( p_tmp, psz_name, i_mode );
1033             }
1034         }
1035         break;
1036
1037     case FIND_CHILD:
1038         for( i = vlc_internals( p_this )->i_children; i--; )
1039         {
1040             p_tmp = vlc_internals( p_this )->pp_children[i];
1041             if( p_tmp->psz_object_name
1042                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1043             {
1044                 vlc_object_hold( p_tmp );
1045                 return p_tmp;
1046             }
1047             else if( vlc_internals( p_tmp )->i_children )
1048             {
1049                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1050                 if( p_tmp )
1051                 {
1052                     return p_tmp;
1053                 }
1054             }
1055         }
1056         break;
1057
1058     case FIND_ANYWHERE:
1059         /* Handled in vlc_object_find */
1060         break;
1061     }
1062
1063     return NULL;
1064 }
1065
1066
1067 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1068 {
1069     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1070          psz_parent[20];
1071
1072     int canc = vlc_savecancel ();
1073     memset( &psz_name, 0, sizeof(psz_name) );
1074     if( p_this->psz_object_name )
1075     {
1076         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1077         if( psz_name[48] )
1078             psz_name[48] = '\"';
1079     }
1080
1081     psz_children[0] = '\0';
1082     switch( vlc_internals( p_this )->i_children )
1083     {
1084         case 0:
1085             break;
1086         case 1:
1087             strcpy( psz_children, ", 1 child" );
1088             break;
1089         default:
1090             snprintf( psz_children, 19, ", %i children",
1091                       vlc_internals( p_this )->i_children );
1092             break;
1093     }
1094
1095     psz_refcount[0] = '\0';
1096     if( vlc_internals( p_this )->i_refcount > 0 )
1097         snprintf( psz_refcount, 19, ", refcount %u",
1098                   vlc_internals( p_this )->i_refcount );
1099
1100     psz_thread[0] = '\0';
1101     if( vlc_internals( p_this )->b_thread )
1102         snprintf( psz_thread, 29, " (thread %lu)",
1103                   (unsigned long)vlc_internals( p_this )->thread_id );
1104
1105     psz_parent[0] = '\0';
1106     if( p_this->p_parent )
1107         snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1108
1109     printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1110             p_this, p_this->psz_object_type,
1111             psz_name, psz_thread, psz_refcount, psz_children,
1112             psz_parent );
1113     vlc_restorecancel (canc);
1114 }
1115
1116 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1117 {
1118     int i;
1119     char i_back = psz_foo[i_level];
1120     psz_foo[i_level] = '\0';
1121
1122     PrintObject( p_this, psz_foo );
1123
1124     psz_foo[i_level] = i_back;
1125
1126     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1127     {
1128         msg_Warn( p_this, "structure tree is too deep" );
1129         return;
1130     }
1131
1132     for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1133     {
1134         if( i_level )
1135         {
1136             psz_foo[i_level-1] = ' ';
1137
1138             if( psz_foo[i_level-2] == '`' )
1139             {
1140                 psz_foo[i_level-2] = ' ';
1141             }
1142         }
1143
1144         if( i == vlc_internals( p_this )->i_children - 1 )
1145         {
1146             psz_foo[i_level] = '`';
1147         }
1148         else
1149         {
1150             psz_foo[i_level] = '|';
1151         }
1152
1153         psz_foo[i_level+1] = '-';
1154         psz_foo[i_level+2] = '\0';
1155
1156         DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1157                        psz_foo );
1158     }
1159 }
1160
1161 static vlc_list_t * NewList( int i_count )
1162 {
1163     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1164     if( p_list == NULL )
1165     {
1166         return NULL;
1167     }
1168
1169     p_list->i_count = i_count;
1170
1171     if( i_count == 0 )
1172     {
1173         p_list->p_values = NULL;
1174         return p_list;
1175     }
1176
1177     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1178     if( p_list->p_values == NULL )
1179     {
1180         p_list->i_count = 0;
1181         return p_list;
1182     }
1183
1184     return p_list;
1185 }
1186
1187 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1188                          int i_index )
1189 {
1190     if( p_list == NULL || i_index >= p_list->i_count )
1191     {
1192         return;
1193     }
1194
1195     vlc_object_hold( p_object );
1196
1197     p_list->p_values[i_index].p_object = p_object;
1198
1199     return;
1200 }
1201
1202 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1203 {
1204     if( p_list == NULL )
1205     {
1206         return;
1207     }
1208
1209     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1210                                 * sizeof( vlc_value_t ) );
1211     if( p_list->p_values == NULL )
1212     {
1213         p_list->i_count = 0;
1214         return;
1215     }
1216
1217     vlc_object_hold( p_object );
1218
1219     p_list->p_values[p_list->i_count].p_object = p_object;
1220     p_list->i_count++;
1221
1222     return;
1223 }*/
1224
1225 static int CountChildren( vlc_object_t *p_this, int i_type )
1226 {
1227     vlc_object_t *p_tmp;
1228     int i, i_count = 0;
1229
1230     for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1231     {
1232         p_tmp = vlc_internals( p_this )->pp_children[i];
1233
1234         if( vlc_internals( p_tmp )->i_object_type == i_type )
1235         {
1236             i_count++;
1237         }
1238         i_count += CountChildren( p_tmp, i_type );
1239     }
1240
1241     return i_count;
1242 }
1243
1244 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1245 {
1246     vlc_object_t *p_tmp;
1247     int i;
1248
1249     for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1250     {
1251         p_tmp = vlc_internals( p_this )->pp_children[i];
1252
1253         if( vlc_internals( p_tmp )->i_object_type == i_type )
1254             ListReplace( p_list, p_tmp, p_list->i_count++ );
1255
1256         ListChildren( p_list, p_tmp, i_type );
1257     }
1258 }