1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
5 * Copyright (C) 2006-2010 RĂ©mi Denis-Courmont
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
26 * This file contains the functions to handle the vlc_object_t type
28 * Unless otherwise stated, functions in this file are not cancellation point.
29 * All functions in this file are safe w.r.t. deferred cancellation.
33 /*****************************************************************************
35 *****************************************************************************/
40 #include <vlc_common.h>
42 #include "../libvlc.h"
44 #include "audio_output/aout_internal.h"
46 #include "vlc_interface.h"
47 #include "vlc_codec.h"
49 #include "variables.h"
54 # include <winsock2.h>
55 # include <ws2tcpip.h>
57 # define read( a, b, c ) recv (a, b, c, 0)
59 # define write( a, b, c ) send (a, b, c, 0)
61 # define close( a ) closesocket (a)
68 #if defined (HAVE_SYS_EVENTFD_H)
69 # include <sys/eventfd.h>
73 /*****************************************************************************
75 *****************************************************************************/
76 static int DumpCommand( vlc_object_t *, char const *,
77 vlc_value_t, vlc_value_t, void * );
79 static vlc_object_t * FindParent ( vlc_object_t *, int );
80 static vlc_object_t * FindChild ( vlc_object_internals_t *, int );
81 static vlc_object_t * FindParentName( vlc_object_t *, const char * );
82 static vlc_object_t * FindChildName ( vlc_object_internals_t *, const char * );
83 static void PrintObject( vlc_object_internals_t *, const char * );
84 static void DumpStructure( vlc_object_internals_t *, unsigned, char * );
86 static vlc_list_t * NewList ( int );
88 static void vlc_object_destroy( vlc_object_t *p_this );
89 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
91 /*****************************************************************************
92 * Local structure lock
93 *****************************************************************************/
94 static void libvlc_lock (libvlc_int_t *p_libvlc)
96 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
99 static void libvlc_unlock (libvlc_int_t *p_libvlc)
101 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
104 #undef vlc_custom_create
105 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
106 int i_type, const char *psz_type )
109 vlc_object_internals_t *p_priv;
112 * VLC objects are laid out as follow:
113 * - first the LibVLC-private per-object data,
114 * - then VLC_COMMON members from vlc_object_t,
115 * - finally, the type-specific data (if any).
117 * This function initializes the LibVLC and common data,
118 * and zeroes the rest.
120 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
124 assert (i_size >= sizeof (vlc_object_t));
125 p_new = (vlc_object_t *)(p_priv + 1);
127 p_priv->i_object_type = i_type;
128 p_new->psz_object_type = psz_type;
129 p_priv->psz_name = NULL;
131 p_new->b_die = false;
132 p_new->b_force = false;
134 p_new->psz_header = NULL;
137 p_new->i_flags = p_this->i_flags
138 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
140 p_priv->var_root = NULL;
144 libvlc_int_t *self = (libvlc_int_t*)p_new;
145 p_new->p_libvlc = self;
146 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
150 p_new->p_libvlc = p_this->p_libvlc;
152 vlc_spin_init( &p_priv->ref_spin );
153 p_priv->i_refcount = 1;
154 p_priv->pf_destructor = NULL;
155 p_priv->b_thread = false;
156 p_new->p_parent = NULL;
157 p_priv->first = NULL;
159 p_priv->old_parent = NULL;
162 /* Initialize mutexes and condvars */
163 vlc_mutex_init( &p_priv->var_lock );
164 vlc_cond_init( &p_priv->var_wait );
165 p_priv->pipes[0] = p_priv->pipes[1] = -1;
167 if (p_new == VLC_OBJECT(p_new->p_libvlc))
168 { /* TODO: should be in src/libvlc.c */
169 int canc = vlc_savecancel ();
170 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
171 var_AddCallback( p_new, "tree", DumpCommand, NULL );
172 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
173 var_AddCallback( p_new, "vars", DumpCommand, NULL );
174 vlc_restorecancel (canc);
180 #undef vlc_object_create
182 * Allocates and initializes a vlc object.
184 * @param i_size object byte size
186 * @return the new object, or NULL on error.
188 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
190 return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
193 #undef vlc_object_set_destructor
195 ****************************************************************************
196 * Set the destructor of a vlc object
198 * This function sets the destructor of the vlc object. It will be called
199 * when the object is destroyed when the its refcount reaches 0.
200 * (It is called by the internal function vlc_object_destroy())
201 *****************************************************************************/
202 void vlc_object_set_destructor( vlc_object_t *p_this,
203 vlc_destructor_t pf_destructor )
205 vlc_object_internals_t *p_priv = vlc_internals(p_this );
207 vlc_spin_lock( &p_priv->ref_spin );
208 p_priv->pf_destructor = pf_destructor;
209 vlc_spin_unlock( &p_priv->ref_spin );
212 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
214 #undef vlc_object_set_name
215 int vlc_object_set_name(vlc_object_t *obj, const char *name)
217 vlc_object_internals_t *priv = vlc_internals(obj);
218 char *newname = name ? strdup (name) : NULL;
221 vlc_mutex_lock (&name_lock);
222 oldname = priv->psz_name;
223 priv->psz_name = newname;
224 vlc_mutex_unlock (&name_lock);
227 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
230 #undef vlc_object_get_name
231 char *vlc_object_get_name(const vlc_object_t *obj)
233 vlc_object_internals_t *priv = vlc_internals(obj);
236 vlc_mutex_lock (&name_lock);
237 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
238 vlc_mutex_unlock (&name_lock);
244 ****************************************************************************
245 * Destroy a vlc object (Internal)
247 * This function destroys an object that has been previously allocated with
248 * vlc_object_create. The object's refcount must be zero and it must not be
249 * attached to other objects in any way.
251 * This function must be called with cancellation disabled (currently).
252 *****************************************************************************/
253 static void vlc_object_destroy( vlc_object_t *p_this )
255 vlc_object_internals_t *p_priv = vlc_internals( p_this );
257 /* Objects are always detached beforehand */
258 assert( !p_this->p_parent );
260 /* Send a kill to the object's thread if applicable */
261 vlc_object_kill( p_this );
263 /* Call the custom "subclass" destructor */
264 if( p_priv->pf_destructor )
265 p_priv->pf_destructor( p_this );
267 /* Any thread must have been cleaned up at this point. */
268 assert( !p_priv->b_thread );
270 /* Destroy the associated variables. */
271 var_DestroyAll( p_this );
273 vlc_cond_destroy( &p_priv->var_wait );
274 vlc_mutex_destroy( &p_priv->var_lock );
276 free( p_this->psz_header );
278 free( p_priv->psz_name );
280 vlc_spin_destroy( &p_priv->ref_spin );
281 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
282 close( p_priv->pipes[1] );
283 if( p_priv->pipes[0] != -1 )
284 close( p_priv->pipes[0] );
285 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
286 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
294 * select()-able pipes emulated using Winsock
296 static int pipe (int fd[2])
299 int addrlen = sizeof (addr);
301 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
302 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
303 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
306 memset (&addr, 0, sizeof (addr));
307 addr.sin_family = AF_INET;
308 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
309 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
310 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
312 || connect (c, (PSOCKADDR)&addr, addrlen))
315 a = accept (l, NULL, NULL);
316 if (a == INVALID_SOCKET)
327 if (l != INVALID_SOCKET)
329 if (c != INVALID_SOCKET)
335 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
338 * Returns the readable end of a pipe that becomes readable once termination
339 * of the object is requested (vlc_object_kill()).
340 * This can be used to wake-up out of a select() or poll() event loop, such
341 * typically when doing network I/O.
343 * Note that the pipe will remain the same for the lifetime of the object.
344 * DO NOT read the pipe nor close it yourself. Ever.
346 * @param obj object that would be "killed"
347 * @return a readable pipe descriptor, or -1 on error.
349 int vlc_object_waitpipe( vlc_object_t *obj )
351 vlc_object_internals_t *internals = vlc_internals( obj );
353 vlc_mutex_lock (&pipe_lock);
354 if (internals->pipes[0] == -1)
356 /* This can only ever happen if someone killed us without locking: */
357 assert (internals->pipes[1] == -1);
359 #if defined (HAVE_SYS_EVENTFD_H)
360 internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC);
361 if (internals->pipes[0] == -1)
364 if (pipe (internals->pipes))
365 internals->pipes[0] = internals->pipes[1] = -1;
368 if (internals->pipes[0] != -1 && obj->b_die)
369 { /* Race condition: vlc_object_kill() already invoked! */
370 msg_Dbg (obj, "waitpipe: object already dying");
371 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
374 vlc_mutex_unlock (&pipe_lock);
375 return internals->pipes[0];
378 #undef vlc_object_kill
380 * Requests termination of an object, cancels the object thread, and make the
381 * object wait pipe (if it exists) readable. Not a cancellation point.
383 void vlc_object_kill( vlc_object_t *p_this )
385 vlc_object_internals_t *priv = vlc_internals( p_this );
388 vlc_thread_cancel( p_this );
389 vlc_mutex_lock( &pipe_lock );
393 p_this->b_die = true;
396 /* This also serves as a memory barrier toward vlc_object_alive(): */
397 vlc_mutex_unlock( &pipe_lock );
401 int canc = vlc_savecancel ();
403 /* write _after_ setting b_die, so vlc_object_alive() returns false */
404 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
405 msg_Dbg (p_this, "waitpipe: object killed");
406 vlc_restorecancel (canc);
410 #undef vlc_object_find
411 /*****************************************************************************
412 * find a typed object and increment its refcount
413 *****************************************************************************
414 * This function recursively looks for a given object type. i_mode can be one
415 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
416 *****************************************************************************/
417 void * vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
419 vlc_object_t *p_found;
421 /* If we are of the requested type ourselves, don't look further */
422 if( vlc_internals (p_this)->i_object_type == i_type )
424 vlc_object_hold( p_this );
428 /* Otherwise, recursively look for the object */
429 if (i_mode == FIND_ANYWHERE)
430 return vlc_object_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
434 case VLC_OBJECT_VOUT:
435 case VLC_OBJECT_AOUT:
437 case VLC_OBJECT_INPUT:
438 /* input can only be accessed like this from children,
439 * otherwise we could not promise that it is initialized */
440 if (i_mode != FIND_PARENT)
447 libvlc_lock (p_this->p_libvlc);
451 p_found = FindParent (p_this, i_type);
454 p_found = FindChild (vlc_internals (p_this), i_type);
459 libvlc_unlock (p_this->p_libvlc);
464 static int objnamecmp(const vlc_object_t *obj, const char *name)
466 char *objname = vlc_object_get_name(obj);
470 int ret = strcmp (objname, name);
475 #undef vlc_object_find_name
477 * Finds a named object and increment its reference count.
478 * Beware that objects found in this manner can be "owned" by another thread,
479 * be of _any_ type, and be attached to any module (if any). With such an
480 * object reference, you can set or get object variables, emit log messages,
481 * and read write-once object parameters (psz_object_type, etc).
482 * You CANNOT cast the object to a more specific object type, and you
483 * definitely cannot invoke object type-specific callbacks with this.
485 * @param p_this object to search from
486 * @param psz_name name of the object to search for
487 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
489 * @return a matching object (must be released by the caller),
492 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
493 const char *psz_name, int i_mode )
495 vlc_object_t *p_found;
497 /* Reading psz_object_name from a separate inhibits thread-safety.
498 * Use a libvlc address variable instead for that sort of things! */
499 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
500 /* If have the requested name ourselves, don't look further */
501 if( !objnamecmp(p_this, psz_name) )
503 vlc_object_hold( p_this );
507 /* Otherwise, recursively look for the object */
508 if (i_mode == FIND_ANYWHERE)
509 return vlc_object_find_name (VLC_OBJECT(p_this->p_libvlc), psz_name,
512 libvlc_lock (p_this->p_libvlc);
513 vlc_mutex_lock (&name_lock);
517 p_found = FindParentName (p_this, psz_name);
520 p_found = FindChildName (vlc_internals (p_this), psz_name);
525 vlc_mutex_unlock (&name_lock);
526 libvlc_unlock (p_this->p_libvlc);
530 #undef vlc_object_hold
532 * Increment an object reference counter.
534 void * vlc_object_hold( vlc_object_t *p_this )
536 vlc_object_internals_t *internals = vlc_internals( p_this );
538 vlc_spin_lock( &internals->ref_spin );
539 /* Avoid obvious freed object uses */
540 assert( internals->i_refcount > 0 );
541 /* Increment the counter */
542 internals->i_refcount++;
543 vlc_spin_unlock( &internals->ref_spin );
547 #undef vlc_object_release
548 /*****************************************************************************
549 * Decrement an object refcount
550 * And destroy the object if its refcount reach zero.
551 *****************************************************************************/
552 void vlc_object_release( vlc_object_t *p_this )
554 vlc_object_internals_t *internals = vlc_internals( p_this );
555 vlc_object_t *parent = NULL;
556 bool b_should_destroy;
558 vlc_spin_lock( &internals->ref_spin );
559 assert( internals->i_refcount > 0 );
561 if( internals->i_refcount > 1 )
564 /* There are still other references to the object */
565 internals->i_refcount--;
566 vlc_spin_unlock( &internals->ref_spin );
569 vlc_spin_unlock( &internals->ref_spin );
572 /* Remember that we cannot hold the spin while waiting on the mutex */
573 libvlc_lock (p_this->p_libvlc);
574 /* Take the spin again. Note that another thread may have held the
575 * object in the (very short) mean time. */
576 vlc_spin_lock( &internals->ref_spin );
577 b_should_destroy = --internals->i_refcount == 0;
578 vlc_spin_unlock( &internals->ref_spin );
580 if( b_should_destroy )
582 parent = p_this->p_parent;
584 /* Detach from parent to protect against FIND_CHILDREN */
585 vlc_object_detach_unlocked (p_this);
587 /* We have no children */
588 assert (internals->first == NULL);
590 libvlc_unlock (p_this->p_libvlc);
592 if( b_should_destroy )
596 canc = vlc_savecancel ();
597 vlc_object_destroy( p_this );
598 vlc_restorecancel (canc);
600 vlc_object_release (parent);
604 #undef vlc_object_attach
606 ****************************************************************************
607 * attach object to a parent object
608 *****************************************************************************
609 * This function sets p_this as a child of p_parent, and p_parent as a parent
610 * of p_this. This link can be undone using vlc_object_detach.
611 *****************************************************************************/
612 void vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
614 if( !p_this ) return;
616 vlc_object_internals_t *pap = vlc_internals (p_parent);
617 vlc_object_internals_t *priv = vlc_internals (p_this);
618 vlc_object_t *p_old_parent;
621 vlc_object_hold (p_parent);
622 libvlc_lock (p_this->p_libvlc);
624 /* Reparenting an object carries a risk of invalid access to the parent,
625 * from another thread. This can happen when inheriting a variable, or
626 * through any direct access to vlc_object_t.p_parent. Also, reparenting
627 * brings a functional bug, whereby the reparented object uses incorrect
628 * old values for inherited variables (as the new parent may have different
629 * variable values, especially if it is an input).
630 * Note that the old parent may be already destroyed.
631 * So its pointer must not be dereferenced.
633 if (priv->old_parent)
634 msg_Info (p_this, "Reparenting an object is dangerous (%p -> %p)!",
635 priv->old_parent, p_parent);
638 p_old_parent = p_this->p_parent;
640 vlc_object_detach_unlocked (p_this);
642 /* Attach the parent to its child */
643 p_this->p_parent = p_parent;
645 /* Attach the child to its parent */
646 priv->next = pap->first;
647 if (priv->next != NULL)
648 priv->next->prev = priv;
650 libvlc_unlock (p_this->p_libvlc);
653 vlc_object_release (p_old_parent);
657 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
659 assert (p_this->p_parent != NULL);
661 vlc_object_internals_t *pap = vlc_internals (p_this->p_parent);
662 vlc_object_internals_t *priv = vlc_internals (p_this);
665 if (priv->prev != NULL)
666 priv->prev->next = priv->next;
668 pap->first = priv->next;
669 if (priv->next != NULL)
670 priv->next->prev = priv->prev;
672 /* Remove p_this's parent */
674 priv->old_parent = p_this->p_parent;
676 p_this->p_parent = NULL;
679 #undef vlc_object_detach
681 ****************************************************************************
682 * detach object from its parent
683 *****************************************************************************
684 * This function removes all links between an object and its parent.
685 *****************************************************************************/
686 void vlc_object_detach( vlc_object_t *p_this )
688 vlc_object_t *p_parent;
689 if( !p_this ) return;
691 libvlc_lock (p_this->p_libvlc);
692 p_parent = p_this->p_parent;
694 vlc_object_detach_unlocked( p_this );
695 libvlc_unlock (p_this->p_libvlc);
698 vlc_object_release (p_parent);
701 #undef vlc_list_children
703 * Gets the list of children of an objects, and increment their reference
705 * @return a list (possibly empty) or NULL in case of error.
707 vlc_list_t *vlc_list_children( vlc_object_t *obj )
710 vlc_object_internals_t *priv;
713 libvlc_lock (obj->p_libvlc);
714 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
717 if (likely(l != NULL))
721 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
722 l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
724 libvlc_unlock (obj->p_libvlc);
728 static void DumpVariable (const void *data, const VISIT which, const int depth)
730 if (which != postorder && which != leaf)
734 const variable_t *p_var = *(const variable_t **)data;
735 const char *psz_type = "unknown";
737 switch( p_var->i_type & VLC_VAR_TYPE )
739 #define MYCASE( type, nice ) \
740 case VLC_VAR_ ## type: \
743 MYCASE( VOID, "void" );
744 MYCASE( BOOL, "bool" );
745 MYCASE( INTEGER, "integer" );
746 MYCASE( HOTKEY, "hotkey" );
747 MYCASE( STRING, "string" );
748 MYCASE( MODULE, "module" );
749 MYCASE( FILE, "file" );
750 MYCASE( DIRECTORY, "directory" );
751 MYCASE( VARIABLE, "variable" );
752 MYCASE( FLOAT, "float" );
753 MYCASE( TIME, "time" );
754 MYCASE( ADDRESS, "address" );
755 MYCASE( MUTEX, "mutex" );
756 MYCASE( LIST, "list" );
759 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
760 if( p_var->psz_text )
761 printf( ", %s", p_var->psz_text );
762 fputc( ')', stdout );
763 if( p_var->i_type & VLC_VAR_HASCHOICE )
764 fputs( ", has choices", stdout );
765 if( p_var->i_type & VLC_VAR_ISCOMMAND )
766 fputs( ", command", stdout );
767 if( p_var->i_entries )
768 printf( ", %d callbacks", p_var->i_entries );
769 switch( p_var->i_type & VLC_VAR_CLASS )
775 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
777 case VLC_VAR_INTEGER:
778 printf( ": %d", p_var->val.i_int );
781 printf( ": \"%s\"", p_var->val.psz_string );
784 printf( ": %f", p_var->val.f_float );
787 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
789 case VLC_VAR_ADDRESS:
790 printf( ": %p", p_var->val.p_address );
793 fputs( ": TODO", stdout );
796 fputc( '\n', stdout );
799 /*****************************************************************************
800 * DumpCommand: print the current vlc structure
801 *****************************************************************************
802 * This function prints either an ASCII tree showing the connections between
803 * vlc objects, and additional information such as their refcount, thread ID,
804 * etc. (command "tree"), or the same data as a simple list (command "list").
805 *****************************************************************************/
806 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
807 vlc_value_t oldval, vlc_value_t newval, void *p_data )
809 (void)oldval; (void)p_data;
810 vlc_object_t *p_object = NULL;
812 if( *newval.psz_string )
814 /* try using the object's name to find it */
815 p_object = vlc_object_find_name( p_this, newval.psz_string,
823 libvlc_lock (p_this->p_libvlc);
824 if( *psz_cmd == 't' )
826 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
829 p_object = VLC_OBJECT(p_this->p_libvlc);
832 DumpStructure( vlc_internals(p_object), 0, psz_foo );
834 else if( *psz_cmd == 'v' )
837 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
839 PrintObject( vlc_internals(p_object), "" );
840 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
841 if( vlc_internals( p_object )->var_root == NULL )
842 puts( " `-o No variables" );
844 twalk( vlc_internals( p_object )->var_root, DumpVariable );
845 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
847 libvlc_unlock (p_this->p_libvlc);
849 if( *newval.psz_string )
851 vlc_object_release( p_object );
856 /*****************************************************************************
857 * vlc_list_release: free a list previously allocated by vlc_list_find
858 *****************************************************************************
859 * This function decreases the refcount of all objects in the list and
861 *****************************************************************************/
862 void vlc_list_release( vlc_list_t *p_list )
866 for( i_index = 0; i_index < p_list->i_count; i_index++ )
868 vlc_object_release( p_list->p_values[i_index].p_object );
871 free( p_list->p_values );
875 /* Following functions are local */
877 static vlc_object_t *FindParent (vlc_object_t *p_this, int i_type)
879 for (vlc_object_t *parent = p_this->p_parent;
881 parent = parent->p_parent)
883 if (vlc_internals (parent)->i_object_type == i_type)
884 return vlc_object_hold (parent);
889 static vlc_object_t *FindParentName (vlc_object_t *p_this, const char *name)
891 for (vlc_object_t *parent = p_this->p_parent;
893 parent = parent->p_parent)
895 const char *objname = vlc_internals (parent)->psz_name;
896 if (objname && !strcmp (objname, name))
897 return vlc_object_hold (parent);
902 static vlc_object_t *FindChild (vlc_object_internals_t *priv, int i_type)
904 for (priv = priv->first; priv != NULL; priv = priv->next)
906 if (priv->i_object_type == i_type)
907 return vlc_object_hold (vlc_externals (priv));
909 vlc_object_t *found = FindChild (priv, i_type);
916 static vlc_object_t *FindChildName (vlc_object_internals_t *priv,
919 for (priv = priv->first; priv != NULL; priv = priv->next)
921 if (priv->psz_name && !strcmp (priv->psz_name, name))
922 return vlc_object_hold (vlc_externals (priv));
924 vlc_object_t *found = FindChildName (priv, name);
931 static void PrintObject( vlc_object_internals_t *priv,
932 const char *psz_prefix )
934 char psz_refcount[20], psz_thread[30], psz_name[50], psz_parent[20];
936 int canc = vlc_savecancel ();
937 memset( &psz_name, 0, sizeof(psz_name) );
939 vlc_mutex_lock (&name_lock);
940 if (priv->psz_name != NULL)
942 snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
946 vlc_mutex_unlock (&name_lock);
948 psz_refcount[0] = '\0';
949 if( priv->i_refcount > 0 )
950 snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
952 psz_thread[0] = '\0';
954 snprintf( psz_thread, 29, " (thread %lu)",
955 (unsigned long)priv->thread_id );
957 psz_parent[0] = '\0';
958 /* FIXME: need structure lock!!! */
959 if( vlc_externals(priv)->p_parent )
960 snprintf( psz_parent, 19, ", parent %p",
961 vlc_externals(priv)->p_parent );
963 printf( " %so %p %s%s%s%s%s\n", psz_prefix,
964 vlc_externals(priv), vlc_externals(priv)->psz_object_type,
965 psz_name, psz_thread, psz_refcount, psz_parent );
966 vlc_restorecancel (canc);
969 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
972 char i_back = psz_foo[i_level];
973 psz_foo[i_level] = '\0';
975 PrintObject (priv, psz_foo);
977 psz_foo[i_level] = i_back;
979 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
981 msg_Warn( vlc_externals(priv), "structure tree is too deep" );
985 for (priv = priv->first; priv != NULL; priv = priv->next)
989 psz_foo[i_level-1] = ' ';
991 if( psz_foo[i_level-2] == '`' )
993 psz_foo[i_level-2] = ' ';
997 psz_foo[i_level] = priv->next ? '|' : '`';
998 psz_foo[i_level+1] = '-';
999 psz_foo[i_level+2] = '\0';
1001 DumpStructure (priv, i_level + 2, psz_foo);
1005 static vlc_list_t * NewList( int i_count )
1007 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
1008 if( p_list == NULL )
1011 p_list->i_count = i_count;
1015 p_list->p_values = NULL;
1019 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1020 if( p_list->p_values == NULL )
1022 p_list->i_count = 0;