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