Playlist: fix scrolling events in selector
[vlc/vlc-skelet.git] / src / misc / objects.c
blobdcf76fcec3f04d206d7a8c8260e7f679491edb60
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 *****************************************************************************/
24 /**
25 * \file
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 /*****************************************************************************
34 * Preamble
35 *****************************************************************************/
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
40 #include <vlc_common.h>
42 #include "../libvlc.h"
43 #include <vlc_aout.h>
44 #include "audio_output/aout_internal.h"
46 #include "vlc_interface.h"
47 #include "vlc_codec.h"
49 #include "variables.h"
51 #ifdef HAVE_SEARCH_H
52 # include <search.h>
53 #endif
55 #ifndef WIN32
56 # include <vlc_fs.h>
57 # include <unistd.h>
58 #else
59 # include <io.h>
60 # include <winsock2.h>
61 # include <ws2tcpip.h>
62 # undef read
63 # define read( a, b, c ) recv (a, b, c, 0)
64 # undef write
65 # define write( a, b, c ) send (a, b, c, 0)
66 # undef close
67 # define close( a ) closesocket (a)
68 #endif
70 #include <limits.h>
71 #include <assert.h>
73 #if defined (HAVE_SYS_EVENTFD_H)
74 # include <sys/eventfd.h>
75 # ifndef EFD_CLOEXEC
76 # define EFD_CLOEXEC 0
77 # warning EFD_CLOEXEC missing. Consider updating libc.
78 # endif
79 #endif
82 /*****************************************************************************
83 * Local prototypes
84 *****************************************************************************/
85 static int DumpCommand( vlc_object_t *, char const *,
86 vlc_value_t, vlc_value_t, void * );
88 static vlc_object_t * FindParentName( vlc_object_t *, const char * );
89 static vlc_object_t * FindChildName ( vlc_object_internals_t *, const char * );
90 static void PrintObject( vlc_object_internals_t *, const char * );
91 static void DumpStructure( vlc_object_internals_t *, unsigned, char * );
93 static vlc_list_t * NewList ( int );
95 static void vlc_object_destroy( vlc_object_t *p_this );
97 /*****************************************************************************
98 * Local structure lock
99 *****************************************************************************/
100 static void libvlc_lock (libvlc_int_t *p_libvlc)
102 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
105 static void libvlc_unlock (libvlc_int_t *p_libvlc)
107 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
110 #undef vlc_custom_create
111 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
112 int i_type, const char *psz_type )
114 vlc_object_t *p_new;
115 vlc_object_internals_t *p_priv;
117 /* NOTE:
118 * VLC objects are laid out as follow:
119 * - first the LibVLC-private per-object data,
120 * - then VLC_COMMON members from vlc_object_t,
121 * - finally, the type-specific data (if any).
123 * This function initializes the LibVLC and common data,
124 * and zeroes the rest.
126 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
127 if( p_priv == NULL )
128 return NULL;
130 assert (i_size >= sizeof (vlc_object_t));
131 p_new = (vlc_object_t *)(p_priv + 1);
133 p_priv->i_object_type = i_type;
134 p_new->psz_object_type = psz_type;
135 p_priv->psz_name = NULL;
137 p_new->b_die = false;
138 p_new->b_force = false;
140 p_new->psz_header = NULL;
142 if (p_this)
143 p_new->i_flags = p_this->i_flags
144 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
146 p_priv->var_root = NULL;
148 if( p_this == NULL )
150 libvlc_int_t *self = (libvlc_int_t*)p_new;
151 p_new->p_libvlc = self;
152 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
153 p_this = p_new;
155 else
156 p_new->p_libvlc = p_this->p_libvlc;
158 vlc_spin_init( &p_priv->ref_spin );
159 p_priv->i_refcount = 1;
160 p_priv->pf_destructor = NULL;
161 p_priv->b_thread = false;
162 p_new->p_parent = NULL;
163 p_priv->first = NULL;
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;
170 if (p_new == VLC_OBJECT(p_new->p_libvlc))
171 { /* TODO: should be in src/libvlc.c */
172 int canc = vlc_savecancel ();
173 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
174 var_AddCallback( p_new, "tree", DumpCommand, NULL );
175 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
176 var_AddCallback( p_new, "vars", DumpCommand, NULL );
177 vlc_restorecancel (canc);
180 return p_new;
183 #undef vlc_object_create
185 * Allocates and initializes a vlc object.
187 * @param i_size object byte size
189 * @return the new object, or NULL on error.
191 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
193 return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
196 #undef vlc_object_set_destructor
198 ****************************************************************************
199 * Set the destructor of a vlc object
201 * This function sets the destructor of the vlc object. It will be called
202 * when the object is destroyed when the its refcount reaches 0.
203 * (It is called by the internal function vlc_object_destroy())
204 *****************************************************************************/
205 void vlc_object_set_destructor( vlc_object_t *p_this,
206 vlc_destructor_t pf_destructor )
208 vlc_object_internals_t *p_priv = vlc_internals(p_this );
210 vlc_spin_lock( &p_priv->ref_spin );
211 p_priv->pf_destructor = pf_destructor;
212 vlc_spin_unlock( &p_priv->ref_spin );
215 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
217 #undef vlc_object_set_name
218 int vlc_object_set_name(vlc_object_t *obj, const char *name)
220 vlc_object_internals_t *priv = vlc_internals(obj);
221 char *newname = name ? strdup (name) : NULL;
222 char *oldname;
224 vlc_mutex_lock (&name_lock);
225 oldname = priv->psz_name;
226 priv->psz_name = newname;
227 vlc_mutex_unlock (&name_lock);
229 free (oldname);
230 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
233 #undef vlc_object_get_name
234 char *vlc_object_get_name(const vlc_object_t *obj)
236 vlc_object_internals_t *priv = vlc_internals(obj);
237 char *name;
239 vlc_mutex_lock (&name_lock);
240 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
241 vlc_mutex_unlock (&name_lock);
243 return name;
247 ****************************************************************************
248 * Destroy a vlc object (Internal)
250 * This function destroys an object that has been previously allocated with
251 * vlc_object_create. The object's refcount must be zero and it must not be
252 * attached to other objects in any way.
254 * This function must be called with cancellation disabled (currently).
255 *****************************************************************************/
256 static void vlc_object_destroy( vlc_object_t *p_this )
258 vlc_object_internals_t *p_priv = vlc_internals( p_this );
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));
288 free( p_priv );
292 #ifdef WIN32
294 * select()-able pipes emulated using Winsock
296 # define vlc_pipe selectable_pipe
297 static int selectable_pipe (int fd[2])
299 SOCKADDR_IN addr;
300 int addrlen = sizeof (addr);
302 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
303 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
304 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
305 goto error;
307 memset (&addr, 0, sizeof (addr));
308 addr.sin_family = AF_INET;
309 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
310 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
311 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
312 || listen (l, 1)
313 || connect (c, (PSOCKADDR)&addr, addrlen))
314 goto error;
316 a = accept (l, NULL, NULL);
317 if (a == INVALID_SOCKET)
318 goto error;
320 closesocket (l);
321 //shutdown (a, 0);
322 //shutdown (c, 1);
323 fd[0] = c;
324 fd[1] = a;
325 return 0;
327 error:
328 if (l != INVALID_SOCKET)
329 closesocket (l);
330 if (c != INVALID_SOCKET)
331 closesocket (c);
332 return -1;
334 #endif /* WIN32 */
336 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
339 * Returns the readable end of a pipe that becomes readable once termination
340 * of the object is requested (vlc_object_kill()).
341 * This can be used to wake-up out of a select() or poll() event loop, such
342 * typically when doing network I/O.
344 * Note that the pipe will remain the same for the lifetime of the object.
345 * DO NOT read the pipe nor close it yourself. Ever.
347 * @param obj object that would be "killed"
348 * @return a readable pipe descriptor, or -1 on error.
350 int vlc_object_waitpipe( vlc_object_t *obj )
352 vlc_object_internals_t *internals = vlc_internals( obj );
354 vlc_mutex_lock (&pipe_lock);
355 if (internals->pipes[0] == -1)
357 /* This can only ever happen if someone killed us without locking: */
358 assert (internals->pipes[1] == -1);
360 /* pipe() is not a cancellation point, but write() is and eventfd() is
361 * unspecified (not in POSIX). */
362 int canc = vlc_savecancel ();
363 #if defined (HAVE_SYS_EVENTFD_H)
364 internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC);
365 if (internals->pipes[0] == -1)
366 #endif
368 if (vlc_pipe (internals->pipes))
369 internals->pipes[0] = internals->pipes[1] = -1;
372 if (internals->pipes[0] != -1 && obj->b_die)
373 { /* Race condition: vlc_object_kill() already invoked! */
374 msg_Dbg (obj, "waitpipe: object already dying");
375 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
377 vlc_restorecancel (canc);
379 vlc_mutex_unlock (&pipe_lock);
380 return internals->pipes[0];
383 #undef vlc_object_kill
385 * Requests termination of an object, cancels the object thread, and make the
386 * object wait pipe (if it exists) readable. Not a cancellation point.
388 void vlc_object_kill( vlc_object_t *p_this )
390 vlc_object_internals_t *priv = vlc_internals( p_this );
391 int fd = -1;
393 vlc_thread_cancel( p_this );
394 vlc_mutex_lock( &pipe_lock );
395 if( !p_this->b_die )
397 fd = priv->pipes[1];
398 p_this->b_die = true;
401 /* This also serves as a memory barrier toward vlc_object_alive(): */
402 vlc_mutex_unlock( &pipe_lock );
404 if (fd != -1)
406 int canc = vlc_savecancel ();
408 /* write _after_ setting b_die, so vlc_object_alive() returns false */
409 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
410 msg_Dbg (p_this, "waitpipe: object killed");
411 vlc_restorecancel (canc);
415 static int objnamecmp(const vlc_object_t *obj, const char *name)
417 char *objname = vlc_object_get_name(obj);
418 if (objname == NULL)
419 return INT_MIN;
421 int ret = strcmp (objname, name);
422 free (objname);
423 return ret;
426 #undef vlc_object_find_name
428 * Finds a named object and increment its reference count.
429 * Beware that objects found in this manner can be "owned" by another thread,
430 * be of _any_ type, and be attached to any module (if any). With such an
431 * object reference, you can set or get object variables, emit log messages,
432 * and read write-once object parameters (psz_object_type, etc).
433 * You CANNOT cast the object to a more specific object type, and you
434 * definitely cannot invoke object type-specific callbacks with this.
436 * @param p_this object to search from
437 * @param psz_name name of the object to search for
438 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
440 * @return a matching object (must be released by the caller),
441 * or NULL on error.
443 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
444 const char *psz_name, int i_mode )
446 vlc_object_t *p_found;
448 /* Reading psz_object_name from a separate inhibits thread-safety.
449 * Use a libvlc address variable instead for that sort of things! */
450 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
451 /* If have the requested name ourselves, don't look further */
452 if( !objnamecmp(p_this, psz_name) )
454 vlc_object_hold( p_this );
455 return p_this;
458 /* Otherwise, recursively look for the object */
459 if (i_mode == FIND_ANYWHERE)
460 return vlc_object_find_name (VLC_OBJECT(p_this->p_libvlc), psz_name,
461 FIND_CHILD);
463 libvlc_lock (p_this->p_libvlc);
464 vlc_mutex_lock (&name_lock);
465 switch (i_mode)
467 case FIND_PARENT:
468 p_found = FindParentName (p_this, psz_name);
469 break;
470 case FIND_CHILD:
471 p_found = FindChildName (vlc_internals (p_this), psz_name);
472 break;
473 default:
474 assert (0);
476 vlc_mutex_unlock (&name_lock);
477 libvlc_unlock (p_this->p_libvlc);
478 return p_found;
481 #undef vlc_object_hold
483 * Increment an object reference counter.
485 void * vlc_object_hold( vlc_object_t *p_this )
487 vlc_object_internals_t *internals = vlc_internals( p_this );
489 vlc_spin_lock( &internals->ref_spin );
490 /* Avoid obvious freed object uses */
491 assert( internals->i_refcount > 0 );
492 /* Increment the counter */
493 internals->i_refcount++;
494 vlc_spin_unlock( &internals->ref_spin );
495 return p_this;
498 #undef vlc_object_release
499 /*****************************************************************************
500 * Decrement an object refcount
501 * And destroy the object if its refcount reach zero.
502 *****************************************************************************/
503 void vlc_object_release( vlc_object_t *p_this )
505 vlc_object_internals_t *internals = vlc_internals( p_this );
506 vlc_object_t *parent = NULL;
507 bool b_should_destroy;
509 vlc_spin_lock( &internals->ref_spin );
510 assert( internals->i_refcount > 0 );
512 if( internals->i_refcount > 1 )
514 /* Fast path */
515 /* There are still other references to the object */
516 internals->i_refcount--;
517 vlc_spin_unlock( &internals->ref_spin );
518 return;
520 vlc_spin_unlock( &internals->ref_spin );
522 /* Slow path */
523 /* Remember that we cannot hold the spin while waiting on the mutex */
524 libvlc_lock (p_this->p_libvlc);
525 /* Take the spin again. Note that another thread may have held the
526 * object in the (very short) mean time. */
527 vlc_spin_lock( &internals->ref_spin );
528 b_should_destroy = --internals->i_refcount == 0;
529 vlc_spin_unlock( &internals->ref_spin );
531 if( b_should_destroy )
533 /* Detach from parent to protect against FIND_CHILDREN */
534 parent = p_this->p_parent;
535 if (likely(parent))
537 /* Unlink */
538 if (internals->prev != NULL)
539 internals->prev->next = internals->next;
540 else
541 vlc_internals(parent)->first = internals->next;
542 if (internals->next != NULL)
543 internals->next->prev = internals->prev;
546 /* We have no children */
547 assert (internals->first == NULL);
549 libvlc_unlock (p_this->p_libvlc);
551 if( b_should_destroy )
553 int canc;
555 canc = vlc_savecancel ();
556 vlc_object_destroy( p_this );
557 vlc_restorecancel (canc);
558 if (parent)
559 vlc_object_release (parent);
563 #undef vlc_object_attach
565 ****************************************************************************
566 * attach object to a parent object
567 *****************************************************************************
568 * This function sets p_this as a child of p_parent, and p_parent as a parent
569 * of p_this.
570 *****************************************************************************/
571 void vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
573 if( !p_this ) return;
575 vlc_object_internals_t *pap = vlc_internals (p_parent);
576 vlc_object_internals_t *priv = vlc_internals (p_this);
578 priv->prev = NULL;
579 vlc_object_hold (p_parent);
580 libvlc_lock (p_this->p_libvlc);
582 /* Attach the parent to its child */
583 assert (p_this->p_parent == NULL);
584 p_this->p_parent = p_parent;
586 /* Attach the child to its parent */
587 priv->next = pap->first;
588 if (priv->next != NULL)
589 priv->next->prev = priv;
590 pap->first = priv;
591 libvlc_unlock (p_this->p_libvlc);
595 #undef vlc_list_children
597 * Gets the list of children of an objects, and increment their reference
598 * count.
599 * @return a list (possibly empty) or NULL in case of error.
601 vlc_list_t *vlc_list_children( vlc_object_t *obj )
603 vlc_list_t *l;
604 vlc_object_internals_t *priv;
605 unsigned count = 0;
607 libvlc_lock (obj->p_libvlc);
608 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
609 count++;
610 l = NewList (count);
611 if (likely(l != NULL))
613 unsigned i = 0;
615 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
616 l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
618 libvlc_unlock (obj->p_libvlc);
619 return l;
622 static void DumpVariable (const void *data, const VISIT which, const int depth)
624 if (which != postorder && which != leaf)
625 return;
626 (void) depth;
628 const variable_t *p_var = *(const variable_t **)data;
629 const char *psz_type = "unknown";
631 switch( p_var->i_type & VLC_VAR_TYPE )
633 #define MYCASE( type, nice ) \
634 case VLC_VAR_ ## type: \
635 psz_type = nice; \
636 break;
637 MYCASE( VOID, "void" );
638 MYCASE( BOOL, "bool" );
639 MYCASE( INTEGER, "integer" );
640 MYCASE( HOTKEY, "hotkey" );
641 MYCASE( STRING, "string" );
642 MYCASE( VARIABLE, "variable" );
643 MYCASE( FLOAT, "float" );
644 MYCASE( TIME, "time" );
645 MYCASE( COORDS, "coords" );
646 MYCASE( ADDRESS, "address" );
647 MYCASE( MUTEX, "mutex" );
648 #undef MYCASE
650 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
651 if( p_var->psz_text )
652 printf( ", %s", p_var->psz_text );
653 fputc( ')', stdout );
654 if( p_var->i_type & VLC_VAR_HASCHOICE )
655 fputs( ", has choices", stdout );
656 if( p_var->i_type & VLC_VAR_ISCOMMAND )
657 fputs( ", command", stdout );
658 if( p_var->i_entries )
659 printf( ", %d callbacks", p_var->i_entries );
660 switch( p_var->i_type & VLC_VAR_CLASS )
662 case VLC_VAR_VOID:
663 case VLC_VAR_MUTEX:
664 break;
665 case VLC_VAR_BOOL:
666 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
667 break;
668 case VLC_VAR_INTEGER:
669 printf( ": %"PRId64, p_var->val.i_int );
670 break;
671 case VLC_VAR_STRING:
672 printf( ": \"%s\"", p_var->val.psz_string );
673 break;
674 case VLC_VAR_FLOAT:
675 printf( ": %f", p_var->val.f_float );
676 break;
677 case VLC_VAR_TIME:
678 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
679 break;
680 case VLC_VAR_COORDS:
681 printf( ": %"PRId32"x%"PRId32,
682 p_var->val.coords.x, p_var->val.coords.y );
683 break;
684 case VLC_VAR_ADDRESS:
685 printf( ": %p", p_var->val.p_address );
686 break;
688 fputc( '\n', stdout );
691 /*****************************************************************************
692 * DumpCommand: print the current vlc structure
693 *****************************************************************************
694 * This function prints either an ASCII tree showing the connections between
695 * vlc objects, and additional information such as their refcount, thread ID,
696 * etc. (command "tree"), or the same data as a simple list (command "list").
697 *****************************************************************************/
698 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
699 vlc_value_t oldval, vlc_value_t newval, void *p_data )
701 (void)oldval; (void)p_data;
702 vlc_object_t *p_object = NULL;
704 if( *newval.psz_string )
706 /* try using the object's name to find it */
707 p_object = vlc_object_find_name( p_this, newval.psz_string,
708 FIND_ANYWHERE );
709 if( !p_object )
711 return VLC_ENOOBJ;
715 libvlc_lock (p_this->p_libvlc);
716 if( *psz_cmd == 't' )
718 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
720 if( !p_object )
721 p_object = VLC_OBJECT(p_this->p_libvlc);
723 psz_foo[0] = '|';
724 DumpStructure( vlc_internals(p_object), 0, psz_foo );
726 else if( *psz_cmd == 'v' )
728 if( !p_object )
729 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
731 PrintObject( vlc_internals(p_object), "" );
732 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
733 if( vlc_internals( p_object )->var_root == NULL )
734 puts( " `-o No variables" );
735 else
736 twalk( vlc_internals( p_object )->var_root, DumpVariable );
737 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
739 libvlc_unlock (p_this->p_libvlc);
741 if( *newval.psz_string )
743 vlc_object_release( p_object );
745 return VLC_SUCCESS;
748 /*****************************************************************************
749 * vlc_list_release: free a list previously allocated by vlc_list_find
750 *****************************************************************************
751 * This function decreases the refcount of all objects in the list and
752 * frees the list.
753 *****************************************************************************/
754 void vlc_list_release( vlc_list_t *p_list )
756 int i_index;
758 for( i_index = 0; i_index < p_list->i_count; i_index++ )
760 vlc_object_release( p_list->p_values[i_index].p_object );
763 free( p_list->p_values );
764 free( p_list );
767 /* Following functions are local */
769 static vlc_object_t *FindParentName (vlc_object_t *p_this, const char *name)
771 for (vlc_object_t *parent = p_this->p_parent;
772 parent != NULL;
773 parent = parent->p_parent)
775 const char *objname = vlc_internals (parent)->psz_name;
776 if (objname && !strcmp (objname, name))
777 return vlc_object_hold (parent);
779 return NULL;
782 static vlc_object_t *FindChildName (vlc_object_internals_t *priv,
783 const char *name)
785 for (priv = priv->first; priv != NULL; priv = priv->next)
787 if (priv->psz_name && !strcmp (priv->psz_name, name))
788 return vlc_object_hold (vlc_externals (priv));
790 vlc_object_t *found = FindChildName (priv, name);
791 if (found != NULL)
792 return found;
794 return NULL;
797 static void PrintObject( vlc_object_internals_t *priv,
798 const char *psz_prefix )
800 char psz_refcount[20], psz_thread[30], psz_name[50], psz_parent[20];
802 int canc = vlc_savecancel ();
803 memset( &psz_name, 0, sizeof(psz_name) );
805 vlc_mutex_lock (&name_lock);
806 if (priv->psz_name != NULL)
808 snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
809 if( psz_name[48] )
810 psz_name[48] = '\"';
812 vlc_mutex_unlock (&name_lock);
814 psz_refcount[0] = '\0';
815 if( priv->i_refcount > 0 )
816 snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
818 psz_thread[0] = '\0';
819 if( priv->b_thread )
820 snprintf( psz_thread, 29, " (thread %lu)",
821 (unsigned long)priv->thread_id );
823 psz_parent[0] = '\0';
824 /* FIXME: need structure lock!!! */
825 if( vlc_externals(priv)->p_parent )
826 snprintf( psz_parent, 19, ", parent %p",
827 vlc_externals(priv)->p_parent );
829 printf( " %so %p %s%s%s%s%s\n", psz_prefix,
830 vlc_externals(priv), vlc_externals(priv)->psz_object_type,
831 psz_name, psz_thread, psz_refcount, psz_parent );
832 vlc_restorecancel (canc);
835 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
836 char *psz_foo)
838 char i_back = psz_foo[i_level];
839 psz_foo[i_level] = '\0';
841 PrintObject (priv, psz_foo);
843 psz_foo[i_level] = i_back;
845 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
847 msg_Warn( vlc_externals(priv), "structure tree is too deep" );
848 return;
851 for (priv = priv->first; priv != NULL; priv = priv->next)
853 if( i_level )
855 psz_foo[i_level-1] = ' ';
857 if( psz_foo[i_level-2] == '`' )
859 psz_foo[i_level-2] = ' ';
863 psz_foo[i_level] = priv->next ? '|' : '`';
864 psz_foo[i_level+1] = '-';
865 psz_foo[i_level+2] = '\0';
867 DumpStructure (priv, i_level + 2, psz_foo);
871 static vlc_list_t * NewList( int i_count )
873 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
874 if( p_list == NULL )
875 return NULL;
877 p_list->i_count = i_count;
879 if( i_count == 0 )
881 p_list->p_values = NULL;
882 return p_list;
885 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
886 if( p_list->p_values == NULL )
888 p_list->i_count = 0;
889 return p_list;
892 return p_list;