daf1e800fa6c399378d486a9abe062736329c0d4
[vlc.git] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: objects.c,v 1.30 2002/12/06 10:10:39 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28
29 #ifdef HAVE_STDLIB_H
30 #   include <stdlib.h>                                          /* realloc() */
31 #endif
32
33 #include "stream_control.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-dec.h"
36
37 #include "video.h"
38 #include "video_output.h"
39
40 #include "audio_output.h"
41 #include "aout_internal.h"
42 #include "stream_output.h"
43
44 #include "vlc_playlist.h"
45 #include "interface.h"
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static int  DumpCommand( vlc_object_t *, char *, char * );
51
52 static vlc_object_t * FindObject    ( vlc_object_t *, int, int );
53 static void           DetachObject  ( vlc_object_t * );
54 static void           PrintObject   ( vlc_object_t *, const char * );
55 static void           DumpStructure ( vlc_object_t *, int, char * );
56 static int            FindIndex     ( vlc_object_t *, vlc_object_t **, int );
57 static void           SetAttachment ( vlc_object_t *, vlc_bool_t );
58
59 static vlc_list_t *   NewList       ( void );
60 static vlc_list_t *   ListAppend    ( vlc_list_t *, vlc_object_t * );
61
62 /*****************************************************************************
63  * Local structure lock
64  *****************************************************************************/
65 static vlc_mutex_t    structure_lock;
66
67 /*****************************************************************************
68  * vlc_object_create: initialize a vlc object
69  *****************************************************************************
70  * This function allocates memory for a vlc object and initializes it. If
71  * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
72  * so on, vlc_object_create will use its value for the object size.
73  *****************************************************************************/
74 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
75 {
76     vlc_object_t * p_new;
77     char *         psz_type;
78     size_t         i_size;
79
80     switch( i_type )
81     {
82         case VLC_OBJECT_ROOT:
83             i_size = sizeof(libvlc_t);
84             psz_type = "root";
85             break;
86         case VLC_OBJECT_VLC:
87             i_size = sizeof(vlc_t);
88             psz_type = "vlc";
89             break;
90         case VLC_OBJECT_MODULE:
91             i_size = sizeof(module_t);
92             psz_type = "module";
93             break;
94         case VLC_OBJECT_INTF:
95             i_size = sizeof(intf_thread_t);
96             psz_type = "interface";
97             break;
98         case VLC_OBJECT_PLAYLIST:
99             i_size = sizeof(playlist_t);
100             psz_type = "playlist";
101             break;
102         case VLC_OBJECT_INPUT:
103             i_size = sizeof(input_thread_t);
104             psz_type = "input";
105             break;
106         case VLC_OBJECT_DECODER:
107             i_size = sizeof(decoder_fifo_t);
108             psz_type = "decoder";
109             break;
110         case VLC_OBJECT_VOUT:
111             i_size = sizeof(vout_thread_t);
112             psz_type = "video output";
113             break;
114         case VLC_OBJECT_AOUT:
115             i_size = sizeof(aout_instance_t);
116             psz_type = "audio output";
117             break;
118         case VLC_OBJECT_SOUT:
119             i_size = sizeof(sout_instance_t);
120             psz_type = "stream output";
121             break;
122         default:
123             i_size = i_type > 0
124                       ? i_type > (int)sizeof(vlc_object_t)
125                          ? i_type : (int)sizeof(vlc_object_t)
126                       : (int)sizeof(vlc_object_t);
127             i_type = VLC_OBJECT_GENERIC;
128             psz_type = "generic";
129             break;
130     }
131
132     if( i_type == VLC_OBJECT_ROOT )
133     {
134         p_new = p_this;
135     }
136     else
137     {
138         p_new = malloc( i_size );
139
140         if( !p_new )
141         {
142             return NULL;
143         }
144
145         memset( p_new, 0, i_size );
146     }
147
148     p_new->i_object_type = i_type;
149     p_new->psz_object_type = psz_type;
150
151     p_new->psz_object_name = NULL;
152
153     p_new->b_die = VLC_FALSE;
154     p_new->b_error = VLC_FALSE;
155     p_new->b_dead = VLC_FALSE;
156     p_new->b_attached = VLC_FALSE;
157
158     p_new->i_vars = 0;
159     p_new->p_vars = (variable_t *)malloc( 16 * sizeof( variable_t ) );
160
161     if( !p_new->p_vars )
162     {
163         free( p_new );
164         return NULL;
165     }
166
167     if( i_type == VLC_OBJECT_ROOT )
168     {
169         /* If i_type is root, then p_new is actually p_libvlc */
170         p_new->p_libvlc = (libvlc_t*)p_new;
171         p_new->p_vlc = NULL;
172
173         p_new->p_libvlc->i_counter = 0;
174         p_new->i_object_id = 0;
175
176         p_new->p_libvlc->i_objects = 1;
177         p_new->p_libvlc->pp_objects = malloc( sizeof(vlc_object_t *) );
178         p_new->p_libvlc->pp_objects[0] = p_new;
179         p_new->b_attached = VLC_TRUE;
180     }
181     else
182     {
183         p_new->p_libvlc = p_this->p_libvlc;
184         p_new->p_vlc = ( i_type == VLC_OBJECT_VLC ) ? (vlc_t*)p_new
185                                                     : p_this->p_vlc;
186
187         vlc_mutex_lock( &structure_lock );
188
189         p_new->p_libvlc->i_counter++;
190         p_new->i_object_id = p_new->p_libvlc->i_counter;
191
192         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
193          * useless to try and recover anything if pp_objects gets smashed. */
194         INSERT_ELEM( p_new->p_libvlc->pp_objects,
195                      p_new->p_libvlc->i_objects,
196                      p_new->p_libvlc->i_objects,
197                      p_new );
198
199         vlc_mutex_unlock( &structure_lock );
200     }
201
202     p_new->i_refcount = 0;
203     p_new->p_parent = NULL;
204     p_new->pp_children = NULL;
205     p_new->i_children = 0;
206
207     p_new->p_private = NULL;
208
209     /* Initialize mutexes and condvars */
210     vlc_mutex_init( p_new, &p_new->object_lock );
211     vlc_cond_init( p_new, &p_new->object_wait );
212     vlc_mutex_init( p_new, &p_new->var_lock );
213
214     if( i_type == VLC_OBJECT_ROOT )
215     {
216         vlc_value_t val;
217                 val.p_address = DumpCommand;
218
219         vlc_mutex_init( p_new, &structure_lock );
220
221         var_Create( p_new, "list", VLC_VAR_COMMAND );
222         var_Set( p_new, "list", val );
223         var_Create( p_new, "tree", VLC_VAR_COMMAND );
224         var_Set( p_new, "tree", val );
225     }
226
227     return p_new;
228 }
229
230 /*****************************************************************************
231  * vlc_object_destroy: destroy a vlc object
232  *****************************************************************************
233  * This function destroys an object that has been previously allocated with
234  * vlc_object_create. The object's refcount must be zero and it must not be
235  * attached to other objects in any way.
236  *****************************************************************************/
237 void __vlc_object_destroy( vlc_object_t *p_this )
238 {
239     int i_delay = 0;
240
241     if( p_this->i_children )
242     {
243         msg_Err( p_this, "cannot delete object with children" );
244         return;
245     }
246
247     if( p_this->p_parent )
248     {
249         msg_Err( p_this, "cannot delete object with a parent" );
250         return;
251     }
252
253     while( p_this->i_refcount )
254     {
255         i_delay++;
256
257         /* Don't warn immediately ... 100ms seems OK */
258         if( i_delay == 2 )
259         {
260             msg_Warn( p_this, "refcount is %i, delaying before deletion",
261                               p_this->i_refcount );
262         }
263         else if( i_delay == 12 )
264         {
265             msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
266                              p_this->i_refcount );
267         }
268         else if( i_delay == 42 )
269         {
270             msg_Err( p_this, "we waited too long, cancelling destruction" );
271             return;
272         }
273
274         msleep( 100000 );
275     }
276
277     /* Destroy the associated variables, starting from the end so that
278      * no memmove calls have to be done. */
279     while( p_this->i_vars )
280     {
281         var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
282     }
283
284     free( p_this->p_vars );
285     vlc_mutex_destroy( &p_this->var_lock );
286
287     if( p_this->i_object_type == VLC_OBJECT_ROOT )
288     {
289         /* We are the root object ... no need to lock. */
290         free( p_this->p_libvlc->pp_objects );
291         p_this->p_libvlc->pp_objects = NULL;
292         p_this->p_libvlc->i_objects--;
293
294         vlc_mutex_destroy( &structure_lock );
295     }
296     else
297     {
298         int i_index;
299
300         vlc_mutex_lock( &structure_lock );
301
302         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
303          * useless to try and recover anything if pp_objects gets smashed. */
304         i_index = FindIndex( p_this, p_this->p_libvlc->pp_objects,
305                              p_this->p_libvlc->i_objects );
306         REMOVE_ELEM( p_this->p_libvlc->pp_objects,
307                      p_this->p_libvlc->i_objects, i_index );
308
309         vlc_mutex_unlock( &structure_lock );
310     }
311
312     vlc_mutex_destroy( &p_this->object_lock );
313     vlc_cond_destroy( &p_this->object_wait );
314
315     free( p_this );
316 }
317
318 /*****************************************************************************
319  * vlc_object_get: find an object given its ID
320  *****************************************************************************
321  * This function looks for the object whose i_object_id field is i_id. We
322  * use a dichotomy so that lookups are in log2(n).
323  *****************************************************************************/
324 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
325 {
326     int i_max, i_middle;
327     vlc_object_t **pp_objects;
328
329     vlc_mutex_lock( &structure_lock );
330
331     pp_objects = p_this->p_libvlc->pp_objects;
332
333     /* Perform our dichotomy */
334     for( i_max = p_this->p_libvlc->i_objects - 1 ; ; )
335     {
336         i_middle = i_max / 2;
337
338         if( pp_objects[i_middle]->i_object_id > i_id )
339         {
340             i_max = i_middle;
341         }
342         else if( pp_objects[i_middle]->i_object_id < i_id )
343         {
344             if( i_middle )
345             {
346                 pp_objects += i_middle;
347                 i_max -= i_middle;
348             }
349             else
350             {
351                 /* This happens when there are only two remaining objects */
352                 if( pp_objects[i_middle+1]->i_object_id == i_id )
353                 {
354                     vlc_mutex_unlock( &structure_lock );
355                     return pp_objects[i_middle+1];
356                 }
357                 break;
358             }
359         }
360         else
361         {
362             vlc_mutex_unlock( &structure_lock );
363             return pp_objects[i_middle];
364         }
365
366         if( i_max == 0 )
367         {
368             /* this means that i_max == i_middle, and since we have already
369              * tested pp_objects[i_middle]), p_found is properly set. */
370             break;
371         }
372     }
373
374     vlc_mutex_unlock( &structure_lock );
375     return NULL;
376 }
377
378 /*****************************************************************************
379  * vlc_object_find: find a typed object and increment its refcount
380  *****************************************************************************
381  * This function recursively looks for a given object type. i_mode can be one
382  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
383  *****************************************************************************/
384 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
385 {
386     vlc_object_t *p_found;
387
388     vlc_mutex_lock( &structure_lock );
389
390     /* If we are of the requested type ourselves, don't look further */
391     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
392     {
393         p_this->i_refcount++;
394         vlc_mutex_unlock( &structure_lock );
395         return p_this;
396     }
397
398     /* Otherwise, recursively look for the object */
399     if( (i_mode & 0x000f) == FIND_ANYWHERE )
400     {
401         p_found = FindObject( VLC_OBJECT(p_this->p_vlc), i_type,
402                               (i_mode & ~0x000f) | FIND_CHILD );
403     }
404     else
405     {
406         p_found = FindObject( p_this, i_type, i_mode );
407     }
408
409     vlc_mutex_unlock( &structure_lock );
410
411     return p_found;
412 }
413
414 /*****************************************************************************
415  * vlc_object_yield: increment an object refcount
416  *****************************************************************************/
417 void __vlc_object_yield( vlc_object_t *p_this )
418 {
419     vlc_mutex_lock( &structure_lock );
420     p_this->i_refcount++;
421     vlc_mutex_unlock( &structure_lock );
422 }
423
424 /*****************************************************************************
425  * vlc_object_release: decrement an object refcount
426  *****************************************************************************/
427 void __vlc_object_release( vlc_object_t *p_this )
428 {
429     vlc_mutex_lock( &structure_lock );
430     p_this->i_refcount--;
431     vlc_mutex_unlock( &structure_lock );
432 }
433
434 /*****************************************************************************
435  * vlc_object_attach: attach object to a parent object
436  *****************************************************************************
437  * This function sets p_this as a child of p_parent, and p_parent as a parent
438  * of p_this. This link can be undone using vlc_object_detach.
439  *****************************************************************************/
440 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
441 {
442     vlc_mutex_lock( &structure_lock );
443
444     /* Attach the parent to its child */
445     p_this->p_parent = p_parent;
446
447     /* Attach the child to its parent */
448     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
449                  p_parent->i_children, p_this );
450
451     /* Climb up the tree to see whether we are connected with the root */
452     if( p_parent->b_attached )
453     {
454         SetAttachment( p_this, VLC_TRUE );
455     }
456
457     vlc_mutex_unlock( &structure_lock );
458 }
459
460 /*****************************************************************************
461  * vlc_object_detach: detach object from its parent
462  *****************************************************************************
463  * This function removes all links between an object and its parent.
464  *****************************************************************************/
465 void __vlc_object_detach( vlc_object_t *p_this )
466 {
467     vlc_mutex_lock( &structure_lock );
468     if( !p_this->p_parent )
469     {
470         msg_Err( p_this, "object is not attached" );
471         vlc_mutex_unlock( &structure_lock );
472         return;
473     }
474
475     /* Climb up the tree to see whether we are connected with the root */
476     if( p_this->p_parent->b_attached )
477     {
478         SetAttachment( p_this, VLC_FALSE );
479     }
480
481     DetachObject( p_this );
482     vlc_mutex_unlock( &structure_lock );
483 }
484
485 /*****************************************************************************
486  * vlc_list_find: find a list typed objects and increment their refcount
487  *****************************************************************************
488  * This function recursively looks for a given object type. i_mode can be one
489  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
490  *****************************************************************************/
491 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
492 {
493     vlc_list_t *p_list = NewList();
494
495     vlc_mutex_lock( &structure_lock );
496
497     /* Look for the objects */
498     if( (i_mode & 0x000f) == FIND_ANYWHERE )
499     {
500         vlc_object_t **pp_current, **pp_end;
501
502         pp_current = p_this->p_libvlc->pp_objects;
503         pp_end = pp_current + p_this->p_libvlc->i_objects;
504
505         for( ; pp_current < pp_end ; pp_current++ )
506         {
507             if( (*pp_current)->b_attached
508                  && (*pp_current)->i_object_type == i_type )
509             {
510                 p_list = ListAppend( p_list, *pp_current );
511             }
512         }
513     }
514     else
515     {
516         msg_Err( p_this, "unimplemented!" );
517     }
518
519     vlc_mutex_unlock( &structure_lock );
520
521     return p_list;
522 }
523
524 /*****************************************************************************
525  * DumpCommand: print the current vlc structure
526  *****************************************************************************
527  * This function prints either an ASCII tree showing the connections between
528  * vlc objects, and additional information such as their refcount, thread ID,
529  * etc. (command "tree"), or the same data as a simple list (command "list").
530  *****************************************************************************/
531 static int DumpCommand( vlc_object_t *p_this, char *psz_cmd, char *psz_arg )
532 {
533     if( *psz_cmd == 't' )
534     {
535         char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
536         vlc_object_t *p_object;
537
538         if( *psz_arg )
539         {
540             p_object = vlc_object_get( p_this, atoi(psz_arg) );
541
542             if( !p_object )
543             {
544                 return VLC_ENOOBJ;
545             }
546         }
547         else
548         {
549             p_object = p_this->p_vlc ? VLC_OBJECT(p_this->p_vlc) : p_this;
550         }
551
552         vlc_mutex_lock( &structure_lock );
553
554         psz_foo[0] = '|';
555         DumpStructure( p_object, 0, psz_foo );
556
557         vlc_mutex_unlock( &structure_lock );
558     }
559     else if( *psz_cmd == 'l' )
560     {
561         vlc_object_t **pp_current, **pp_end;
562
563         vlc_mutex_lock( &structure_lock );
564
565         pp_current = p_this->p_libvlc->pp_objects;
566         pp_end = pp_current + p_this->p_libvlc->i_objects;
567
568         for( ; pp_current < pp_end ; pp_current++ )
569         {
570             if( (*pp_current)->b_attached )
571             {
572                 PrintObject( *pp_current, "" );
573             }
574             else
575             {
576                 printf( " o %.8i %s (not attached)\n",
577                         (*pp_current)->i_object_id,
578                         (*pp_current)->psz_object_type );
579             }
580         }
581
582         vlc_mutex_unlock( &structure_lock );
583     }
584
585     return VLC_SUCCESS;
586 }
587
588 /*****************************************************************************
589  * vlc_list_release: free a list previously allocated by vlc_list_find
590  *****************************************************************************
591  * This function decreases the refcount of all objects in the list and
592  * frees the list.
593  *****************************************************************************/
594 void vlc_list_release( vlc_list_t *p_list )
595 {
596     if( p_list->i_count )
597     {
598         vlc_object_t ** pp_current = p_list->pp_objects;
599
600         vlc_mutex_lock( &structure_lock );
601
602         while( pp_current[0] )
603         {
604             pp_current[0]->i_refcount--;
605             pp_current++;
606         }
607
608         vlc_mutex_unlock( &structure_lock );
609     }
610
611     free( p_list );
612 }
613
614 /* Following functions are local */
615
616 /*****************************************************************************
617  * FindIndex: find the index of an object in an array of objects
618  *****************************************************************************
619  * This function assumes that p_this can be found in pp_objects. It will not
620  * crash if p_this cannot be found, but will return a wrong value. It is your
621  * duty to check the return value if you are not certain that the object could
622  * be found for sure.
623  *****************************************************************************/
624 static int FindIndex( vlc_object_t *p_this,
625                       vlc_object_t **pp_objects, int i_count )
626 {
627     int i_middle = i_count / 2;
628
629     if( i_count == 0 )
630     {
631         return 0;
632     }
633
634     if( pp_objects[i_middle] == p_this )
635     {
636         return i_middle;
637     }
638
639     if( i_count == 1 )
640     {
641         return 0;
642     }
643
644     /* We take advantage of the sorted array */
645     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
646     {
647         return i_middle + FindIndex( p_this, pp_objects + i_middle,
648                                              i_count - i_middle );
649     }
650     else
651     {
652         return FindIndex( p_this, pp_objects, i_middle );
653     }
654 }
655
656 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
657 {
658     int i;
659     vlc_object_t *p_tmp;
660
661     switch( i_mode & 0x000f )
662     {
663     case FIND_PARENT:
664         p_tmp = p_this->p_parent;
665         if( p_tmp )
666         {
667             if( p_tmp->i_object_type == i_type )
668             {
669                 p_tmp->i_refcount++;
670                 return p_tmp;
671             }
672             else
673             {
674                 return FindObject( p_tmp, i_type, i_mode );
675             }
676         }
677         break;
678
679     case FIND_CHILD:
680         for( i = p_this->i_children; i--; )
681         {
682             p_tmp = p_this->pp_children[i];
683             if( p_tmp->i_object_type == i_type )
684             {
685                 p_tmp->i_refcount++;
686                 return p_tmp;
687             }
688             else if( p_tmp->i_children )
689             {
690                 p_tmp = FindObject( p_tmp, i_type, i_mode );
691                 if( p_tmp )
692                 {
693                     return p_tmp;
694                 }
695             }
696         }
697         break;
698
699     case FIND_ANYWHERE:
700         /* Handled in vlc_object_find */
701         break;
702     }
703
704     return NULL;
705 }
706
707 static void DetachObject( vlc_object_t *p_this )
708 {
709     vlc_object_t *p_parent = p_this->p_parent;
710     int i_index, i;
711
712     /* Remove p_this's parent */
713     p_this->p_parent = NULL;
714
715     /* Remove all of p_parent's children which are p_this */
716     for( i_index = p_parent->i_children ; i_index-- ; )
717     {
718         if( p_parent->pp_children[i_index] == p_this )
719         {
720             p_parent->i_children--;
721             for( i = i_index ; i < p_parent->i_children ; i++ )
722             {
723                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
724             }
725         }
726     }
727
728     if( p_parent->i_children )
729     {
730         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
731                                p_parent->i_children * sizeof(vlc_object_t *) );
732     }
733     else
734     {
735         free( p_parent->pp_children );
736         p_parent->pp_children = NULL;
737     }
738 }
739
740 /*****************************************************************************
741  * SetAttachment: recursively set the b_attached flag of a subtree.
742  *****************************************************************************
743  * This function is used by the attach and detach functions to propagate
744  * the b_attached flag in a subtree.
745  *****************************************************************************/
746 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
747 {
748     int i_index;
749
750     for( i_index = p_this->i_children ; i_index-- ; )
751     {
752         SetAttachment( p_this->pp_children[i_index], b_attached );
753     }
754
755     p_this->b_attached = b_attached;
756 }
757
758 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
759 {
760     char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
761
762     psz_name[0] = '\0';
763     if( p_this->psz_object_name )
764     {
765         snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
766         psz_name[48] = '\"';
767         psz_name[49] = '\0';
768     }
769
770     psz_children[0] = '\0';
771     switch( p_this->i_children )
772     {
773         case 0:
774             break;
775         case 1:
776             strcpy( psz_children, ", 1 child" );
777             break;
778         default:
779             snprintf( psz_children, 20,
780                       ", %i children", p_this->i_children );
781             psz_children[19] = '\0';
782             break;
783     }
784
785     psz_refcount[0] = '\0';
786     if( p_this->i_refcount )
787     {
788         snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
789         psz_refcount[19] = '\0';
790     }
791
792     psz_thread[0] = '\0';
793     if( p_this->b_thread )
794     {
795         snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
796         psz_thread[19] = '\0';
797     }
798
799     printf( " %so %.8i %s%s%s%s%s\n", psz_prefix,
800             p_this->i_object_id, p_this->psz_object_type,
801             psz_name, psz_thread, psz_refcount, psz_children );
802 }
803
804 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
805 {
806     int i;
807     char i_back = psz_foo[i_level];
808     psz_foo[i_level] = '\0';
809
810     PrintObject( p_this, psz_foo );
811
812     psz_foo[i_level] = i_back;
813
814     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
815     {
816         msg_Warn( p_this, "structure tree is too deep" );
817         return;
818     }
819
820     for( i = 0 ; i < p_this->i_children ; i++ )
821     {
822         if( i_level )
823         {
824             psz_foo[i_level-1] = ' ';
825
826             if( psz_foo[i_level-2] == '`' )
827             {
828                 psz_foo[i_level-2] = ' ';
829             }
830         }
831
832         if( i == p_this->i_children - 1 )
833         {
834             psz_foo[i_level] = '`';
835         }
836         else
837         {
838             psz_foo[i_level] = '|';
839         }
840
841         psz_foo[i_level+1] = '-';
842         psz_foo[i_level+2] = '\0';
843
844         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
845     }
846 }
847
848 static vlc_list_t * NewList( void )
849 {
850     vlc_list_t *p_list = malloc( sizeof( vlc_list_t )
851                                      + 3 * sizeof( vlc_object_t * ) );
852
853     if( p_list == NULL )
854     {
855         return NULL;
856     }
857
858     p_list->i_count = 0;
859     p_list->pp_objects = &p_list->_p_first;
860
861     /* We allocated space for NULL and for three extra objects */
862     p_list->_i_extra = 3;
863     p_list->_p_first = NULL;
864
865     return p_list;
866 }
867
868 static vlc_list_t * ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
869 {
870     if( p_list == NULL )
871     {
872         return NULL;
873     }
874
875     if( p_list->_i_extra == 0 )
876     {
877         /* If we had X objects it means the array has a size of X+1, we
878          * make it size 2X+2, so we alloc 2X+1 because there is already
879          * one allocated in the real structure */
880         p_list = realloc( p_list, sizeof( vlc_list_t )
881                                    + (p_list->i_count * 2 + 1)
882                                        * sizeof( vlc_object_t * ) );
883         if( p_list == NULL ) 
884         {
885             return NULL;
886         }
887
888         /* We have X+1 extra slots */
889         p_list->_i_extra = p_list->i_count + 1;
890         p_list->pp_objects = &p_list->_p_first;
891     }
892
893     p_object->i_refcount++;
894
895     p_list->pp_objects[p_list->i_count] = p_object;
896     p_list->i_count++;
897     p_list->pp_objects[p_list->i_count] = NULL;
898     p_list->_i_extra--;
899
900     return p_list;
901 }
902