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