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