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