Changeset 319e629d2c640cde2f7c42ab7429d5f324273a0b

Show
Ignore:
Timestamp:
29/10/02 14:22:48 (6 years ago)
Author:
Sam Hocevar <sam@videolan.org>
git-committer:
Sam Hocevar <sam@videolan.org> 1035897768 +0000
git-parent:

[1063d36a28921d6517718d91ccf06c7091c6c38d]

git-author:
Sam Hocevar <sam@videolan.org> 1035897768 +0000
Message:
  • ./include/vlc_common.h: defined the INSERT_ELEM and REMOVE_ELEM macros
    which are a generic use of the realloc/memmove/index++ scheme we use for
    dynamic arrays.
  • ./src/misc/variables.c: properly free the choice list upon variable
    destruction.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • include/vlc_common.h

    r93848f0 r319e629  
    44 ***************************************************************************** 
    55 * Copyright (C) 1998, 1999, 2000 VideoLAN 
    6  * $Id: vlc_common.h,v 1.32 2002/10/25 09:21:09 sam Exp $ 
     6 * $Id: vlc_common.h,v 1.33 2002/10/29 13:22:47 sam Exp $ 
    77 * 
    88 * Authors: Samuel Hocevar <sam@via.ecp.fr> 
     
    346346#   define __MIN(a, b)   ( ((a) < (b)) ? (a) : (b) ) 
    347347#endif 
     348 
     349/* Dynamic array handling: realloc array, move data, increment position */ 
     350#define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem )                           \ 
     351    do                                                                        \ 
     352    {                                                                         \ 
     353        if( i_oldsize )                                                       \ 
     354        {                                                                     \ 
     355            (p_ar) = realloc( p_ar, ((i_oldsize) + 1) * sizeof( *(p_ar) ) );  \ 
     356        }                                                                     \ 
     357        else                                                                  \ 
     358        {                                                                     \ 
     359            (p_ar) = malloc( ((i_oldsize) + 1) * sizeof( *(p_ar) ) );         \ 
     360        }                                                                     \ 
     361        memmove( (p_ar) + (i_pos) + 1,                                        \ 
     362                 (p_ar) + (i_pos),                                            \ 
     363                 ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) );               \ 
     364        (p_ar)[i_pos] = elem;                                                 \ 
     365        (i_oldsize)++;                                                        \ 
     366    }                                                                         \ 
     367    while( 0 ) 
     368 
     369#define REMOVE_ELEM( p_ar, i_oldsize, i_pos )                                 \ 
     370    do                                                                        \ 
     371    {                                                                         \ 
     372        memmove( (p_ar) + (i_pos),                                            \ 
     373                 (p_ar) + (i_pos) + 1,                                        \ 
     374                 ((i_oldsize) - (i_pos) - 1) * sizeof( *(p_ar) ) );           \ 
     375        if( i_oldsize > 1 )                                                   \ 
     376        {                                                                     \ 
     377            (p_ar) = realloc( p_ar, ((i_oldsize) - 1) * sizeof( *(p_ar) ) );  \ 
     378        }                                                                     \ 
     379        else                                                                  \ 
     380        {                                                                     \ 
     381            free( p_ar );                                                     \ 
     382            (p_ar) = NULL;                                                    \ 
     383        }                                                                     \ 
     384        (i_oldsize)--;                                                        \ 
     385    }                                                                         \ 
     386    while( 0 ) 
     387 
    348388 
    349389/* MSB (big endian)/LSB (little endian) conversions - network order is always 
  • src/input/input_dec.c

    r6d6601e r319e629  
    33 ***************************************************************************** 
    44 * Copyright (C) 1999-2001 VideoLAN 
    5  * $Id: input_dec.c,v 1.48 2002/10/27 16:58:12 gbazin Exp $ 
     5 * $Id: input_dec.c,v 1.49 2002/10/29 13:22:48 sam Exp $ 
    66 * 
    77 * Authors: Christophe Massiot <massiot@via.ecp.fr> 
     
    285285 
    286286    /* Select a new ES */ 
    287     p_input->stream.i_selected_es_number++; 
    288     p_input->stream.pp_selected_es = realloc( 
    289                                           p_input->stream.pp_selected_es, 
    290                                           p_input->stream.i_selected_es_number 
    291                                            * sizeof(es_descriptor_t *) ); 
    292     if( p_input->stream.pp_selected_es == NULL ) 
    293     { 
    294         msg_Err( p_input, "out of memory" ); 
    295         vlc_object_destroy( p_fifo ); 
    296         return NULL; 
    297     } 
    298  
    299     p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1] 
    300             = p_es; 
     287    INSERT_ELEM( p_input->stream.pp_selected_es, 
     288                 p_input->stream.i_selected_es_number, 
     289                 p_input->stream.i_selected_es_number, 
     290                 p_es ); 
    301291 
    302292    /* Initialize the p_fifo structure */ 
  • src/input/input_programs.c

    r976dfc3 r319e629  
    33 ***************************************************************************** 
    44 * Copyright (C) 1999-2002 VideoLAN 
    5  * $Id: input_programs.c,v 1.94 2002/07/31 20:56:52 sam Exp $ 
     5 * $Id: input_programs.c,v 1.95 2002/10/29 13:22:48 sam Exp $ 
    66 * 
    77 * Authors: Christophe Massiot <massiot@via.ecp.fr> 
     
    139139{ 
    140140    /* Where to add the pgrm */ 
    141     int i_pgrm_index = p_input->stream.i_pgrm_number; 
     141    pgrm_descriptor_t * p_pgrm = malloc( sizeof(pgrm_descriptor_t) ); 
     142 
     143    if( p_pgrm == NULL ) 
     144    { 
     145        msg_Err( p_input, "out of memory" ); 
     146        return NULL; 
     147    } 
     148 
     149    /* Init this entry */ 
     150    p_pgrm->i_number = i_pgrm_id; 
     151    p_pgrm->b_is_ok = 0; 
     152    p_pgrm->i_version = 0; 
     153 
     154    p_pgrm->i_es_number = 0; 
     155    p_pgrm->pp_es = NULL; 
     156 
     157    input_ClockInit( p_pgrm ); 
     158 
     159    p_pgrm->i_synchro_state = SYNCHRO_START; 
     160 
     161    if( i_data_len ) 
     162    { 
     163        p_pgrm->p_demux_data = malloc( i_data_len ); 
     164        if( p_pgrm->p_demux_data == NULL ) 
     165        { 
     166            msg_Err( p_input, "out of memory" ); 
     167            return NULL; 
     168        } 
     169        memset( p_pgrm->p_demux_data, 0, i_data_len ); 
     170    } 
     171    else 
     172    { 
     173        p_pgrm->p_demux_data = NULL; 
     174    } 
    142175 
    143176    /* Add an entry to the list of program associated with the stream */ 
    144     p_input->stream.i_pgrm_number++; 
    145     p_input->stream.pp_programs = realloc( p_input->stream.pp_programs, 
    146                                            p_input->stream.i_pgrm_number 
    147                                            * sizeof(pgrm_descriptor_t *) ); 
    148     if( p_input->stream.pp_programs == NULL ) 
    149     { 
    150         msg_Err( p_input, "out of memory" ); 
    151         return( NULL ); 
    152     } 
    153  
    154     /* Allocate the structure to store this description */ 
    155     p_input->stream.pp_programs[i_pgrm_index] = 
    156                                         malloc( sizeof(pgrm_descriptor_t) ); 
    157     if( p_input->stream.pp_programs[i_pgrm_index] == NULL ) 
    158     { 
    159         msg_Err( p_input, "out of memory" ); 
    160         return( NULL ); 
    161     } 
    162      
    163     /* Init this entry */ 
    164     p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id; 
    165     p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0; 
    166     p_input->stream.pp_programs[i_pgrm_index]->i_version = 0; 
    167  
    168     p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0; 
    169     p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL; 
    170  
    171     input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] ); 
    172  
    173     p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state 
    174                                                 = SYNCHRO_START; 
    175  
    176     if( i_data_len ) 
    177     { 
    178         p_input->stream.pp_programs[i_pgrm_index]->p_demux_data = 
    179             malloc( i_data_len ); 
    180         if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL ) 
    181         { 
    182             msg_Err( p_input, "out of memory" ); 
    183             return( NULL ); 
    184         } 
    185         memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0, 
    186                 i_data_len ); 
    187     } 
    188     else 
    189     { 
    190         p_input->stream.pp_programs[i_pgrm_index]->p_demux_data = NULL; 
    191     } 
    192  
    193     return p_input->stream.pp_programs[i_pgrm_index]; 
     177    INSERT_ELEM( p_input->stream.pp_programs, 
     178                 p_input->stream.i_pgrm_number, 
     179                 p_input->stream.i_pgrm_number, 
     180                 p_pgrm ); 
     181 
     182    return p_pgrm; 
    194183} 
    195184 
     
    231220 
    232221    /* Remove this program from the stream's list of programs */ 
    233     p_input->stream.i_pgrm_number--; 
    234  
    235     p_input->stream.pp_programs[i_pgrm_index] = 
    236         p_input->stream.pp_programs[p_input->stream.i_pgrm_number]; 
    237     if( p_input->stream.i_pgrm_number )  
    238     { 
    239         p_input->stream.pp_programs = realloc( p_input->stream.pp_programs, 
    240                                                p_input->stream.i_pgrm_number 
    241                                                * sizeof(pgrm_descriptor_t *) ); 
    242         if( p_input->stream.pp_programs == NULL ) 
    243         { 
    244             msg_Err( p_input, "cannot realloc memory" ); 
    245         } 
    246     } 
    247     else 
    248     { 
    249         free( p_input->stream.pp_programs ); 
    250         p_input->stream.pp_programs = NULL; 
    251     } 
     222    REMOVE_ELEM( p_input->stream.pp_programs, 
     223                 p_input->stream.i_pgrm_number, 
     224                 i_pgrm_index ); 
    252225 
    253226    /* Free the description of this program */ 
     
    263236{ 
    264237    /* Where to add the pgrm */ 
    265     int i_area_index = p_input->stream.i_area_nb; 
     238    input_area_t * p_area = malloc( sizeof(input_area_t) ); 
     239 
     240    if( p_area == NULL ) 
     241    { 
     242        msg_Err( p_input, "out of memory" ); 
     243        return NULL; 
     244    } 
     245 
     246    /* Init this entry */ 
     247    p_area->i_id = 0; 
     248    p_area->i_start = 0; 
     249    p_area->i_size = 0; 
     250    p_area->i_tell = 0; 
     251    p_area->i_seek = NO_SEEK; 
     252    p_area->i_part_nb = 1; 
     253    p_area->i_part= 0; 
    266254 
    267255    /* Add an entry to the list of program associated with the stream */ 
    268     p_input->stream.i_area_nb++; 
    269     p_input->stream.pp_areas = realloc( p_input->stream.pp_areas, 
    270                                         p_input->stream.i_area_nb 
    271                                         * sizeof(input_area_t *) ); 
    272     if( p_input->stream.pp_areas == NULL ) 
    273     { 
    274         msg_Err( p_input, "out of memory" ); 
    275         return( NULL ); 
    276     } 
    277  
    278     /* Allocate the structure to store this description */ 
    279     p_input->stream.pp_areas[i_area_index] = 
    280                                         malloc( sizeof(input_area_t) ); 
    281     if( p_input->stream.pp_areas[i_area_index] == NULL ) 
    282     { 
    283         msg_Err( p_input, "out of memory" ); 
    284         return( NULL ); 
    285     } 
    286      
    287     /* Init this entry */ 
    288     p_input->stream.pp_areas[i_area_index]->i_id = 0; 
    289     p_input->stream.pp_areas[i_area_index]->i_start = 0; 
    290     p_input->stream.pp_areas[i_area_index]->i_size = 0; 
    291     p_input->stream.pp_areas[i_area_index]->i_tell = 0; 
    292     p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK; 
    293     p_input->stream.pp_areas[i_area_index]->i_part_nb = 1; 
    294     p_input->stream.pp_areas[i_area_index]->i_part= 0; 
    295  
    296     return p_input->stream.pp_areas[i_area_index]; 
     256    INSERT_ELEM( p_input->stream.pp_areas, 
     257                 p_input->stream.i_area_nb, 
     258                 p_input->stream.i_area_nb, 
     259                 p_area ); 
     260 
     261    return p_area; 
    297262} 
    298263 
     
    421386 
    422387    /* Remove this area from the stream's list of areas */ 
    423     p_input->stream.i_area_nb--; 
    424  
    425     p_input->stream.pp_areas[i_area_index] = 
    426         p_input->stream.pp_areas[p_input->stream.i_area_nb]; 
    427     if( p_input->stream.i_area_nb ) 
    428     { 
    429         p_input->stream.pp_areas = realloc( p_input->stream.pp_areas, 
    430                                             p_input->stream.i_area_nb 
    431                                             * sizeof(input_area_t *) ); 
    432  
    433         if( p_input->stream.pp_areas == NULL ) 
    434         { 
    435             msg_Err( p_input, "cannot realloc memory" ); 
    436         } 
    437     } 
    438     else 
    439     { 
    440         free( p_input->stream.pp_areas ); 
    441         p_input->stream.pp_areas = NULL; 
    442     } 
     388    REMOVE_ELEM( p_input->stream.pp_areas, 
     389                 p_input->stream.i_area_nb, 
     390                 i_area_index ); 
    443391 
    444392    /* Free the description of this area */ 
     
    484432        return( NULL); 
    485433    } 
    486     p_input->stream.i_es_number++; 
    487     p_input->stream.pp_es = realloc( p_input->stream.pp_es, 
    488                                      p_input->stream.i_es_number 
    489                                       * sizeof(es_descriptor_t *) ); 
    490     if( p_input->stream.pp_es == NULL ) 
    491     { 
    492         msg_Err( p_input, "out of memory" ); 
    493         return( NULL ); 
    494     } 
    495  
    496     p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es; 
     434 
     435    INSERT_ELEM( p_input->stream.pp_es, 
     436                 p_input->stream.i_es_number, 
     437                 p_input->stream.i_es_number, 
     438                 p_es ); 
    497439 
    498440    /* Init its values */ 
     
    524466    if( p_pgrm ) 
    525467    { 
    526         p_pgrm->i_es_number++; 
    527         p_pgrm->pp_es = realloc( p_pgrm->pp_es, 
    528                                  p_pgrm->i_es_number 
    529                                   * sizeof(es_descriptor_t *) ); 
    530         if( p_pgrm->pp_es == NULL ) 
    531         { 
    532             msg_Err( p_input, "out of memory" ); 
    533             return( NULL ); 
    534         } 
    535  
    536         p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es; 
     468        INSERT_ELEM( p_pgrm->pp_es, 
     469                     p_pgrm->i_es_number, 
     470                     p_pgrm->i_es_number, 
     471                     p_es ); 
    537472        p_es->p_pgrm = p_pgrm; 
    538473    } 
     
    584519            if( p_pgrm->pp_es[i_index] == p_es ) 
    585520            { 
    586                 p_pgrm->i_es_number--; 
    587                 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number]; 
    588                 if( p_pgrm->i_es_number ) 
    589                 { 
    590                     p_pgrm->pp_es = realloc( p_pgrm->pp_es, 
    591                                              p_pgrm->i_es_number 
    592                                               * sizeof(es_descriptor_t *)); 
    593                     if( p_pgrm->pp_es == NULL ) 
    594                     { 
    595                         msg_Err( p_input, "cannot realloc memory" ); 
    596                     } 
    597                 } 
    598                 else 
    599                 { 
    600                     free( p_pgrm->pp_es ); 
    601                     p_pgrm->pp_es = NULL; 
    602                 } 
     521                REMOVE_ELEM( p_pgrm->pp_es, 
     522                             p_pgrm->i_es_number, 
     523                             i_index ); 
    603524                break; 
    604525            } 
     
    621542 
    622543    /* Remove this ES from the stream's list of ES */ 
    623     p_input->stream.i_es_number--; 
    624     p_input->stream.pp_es[i_es_index] = 
    625                     p_input->stream.pp_es[p_input->stream.i_es_number]; 
    626     if( p_input->stream.i_es_number ) 
    627     { 
    628         p_input->stream.pp_es = realloc( p_input->stream.pp_es, 
    629                                          p_input->stream.i_es_number 
    630                                           * sizeof(es_descriptor_t *)); 
    631         if( p_input->stream.pp_es == NULL ) 
    632         { 
    633             msg_Err( p_input, "cannot realloc memory" ); 
    634         } 
    635     } 
    636     else 
    637     { 
    638         free( p_input->stream.pp_es ); 
    639         p_input->stream.pp_es = NULL; 
    640     } 
    641      
     544    REMOVE_ELEM( p_input->stream.pp_es, 
     545                 p_input->stream.i_es_number, 
     546                 i_es_index ); 
     547 
    642548    /* Free the ES */ 
    643549    free( p_es ); 
     
    708614        ( p_input->stream.i_selected_es_number > 0 ) ) 
    709615    { 
    710         p_input->stream.i_selected_es_number--; 
    711  
    712         while( ( i_index < p_input->stream.i_selected_es_number ) && 
     616        while( ( i_index < p_input->stream.i_selected_es_number - 1 ) && 
    713617               ( p_input->stream.pp_selected_es[i_index] != p_es ) ) 
    714618        { 
     
    716620        } 
    717621 
    718         p_input->stream.pp_selected_es[i_index] =  
    719           p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number]; 
    720  
    721         if( p_input->stream.i_selected_es_number ) 
    722         { 
    723             p_input->stream.pp_selected_es = realloc( 
    724                                            p_input->stream.pp_selected_es, 
    725                                            p_input->stream.i_selected_es_number 
    726                                            * sizeof(es_descriptor_t *) ); 
    727             if( p_input->stream.pp_selected_es == NULL ) 
    728             { 
    729                 msg_Err( p_input, "cannot realloc memory" ); 
    730                 return( -1 ); 
    731             } 
    732         } 
    733         else 
    734         { 
    735             free( p_input->stream.pp_selected_es );    
    736             p_input->stream.pp_selected_es = NULL; 
     622        /* XXX: no need to memmove, we have unordered data */ 
     623        REMOVE_ELEM( p_input->stream.pp_selected_es, 
     624                     p_input->stream.i_selected_es_number, 
     625                     i_index ); 
     626 
     627        if( p_input->stream.i_selected_es_number == 0 ) 
     628        { 
    737629            msg_Dbg( p_input, "no more selected ES" ); 
    738             return( 1 )
    739         } 
    740     } 
    741  
    742     return( 0 )
    743 } 
     630            return 1
     631        } 
     632    } 
     633 
     634    return 0
     635} 
  • src/misc/messages.c

    rb316e6e r319e629  
    55 ***************************************************************************** 
    66 * Copyright (C) 1998-2002 VideoLAN 
    7  * $Id: messages.c,v 1.17 2002/10/28 16:26:44 sam Exp $ 
     7 * $Id: messages.c,v 1.18 2002/10/29 13:22:48 sam Exp $ 
    88 * 
    99 * Authors: Vincent Seguin <seguin@via.ecp.fr> 
     
    138138 
    139139    /* Add subscription to the list */ 
    140     p_bank->i_sub++; 
    141     p_bank->pp_sub = realloc( p_bank->pp_sub, 
    142                               p_bank->i_sub * sizeof( msg_subscription_t* ) ); 
    143  
    144     p_bank->pp_sub[ p_bank->i_sub - 1 ] = p_sub; 
     140    INSERT_ELEM( p_bank->pp_sub, p_bank->i_sub, p_bank->i_sub, p_sub ); 
    145141 
    146142    p_sub->i_start = p_bank->i_start; 
     
    189185 
    190186    /* Remove this subscription */ 
    191     for( ; i_index < (p_bank->i_sub - 1); i_index++ ) 
    192     { 
    193         p_bank->pp_sub[ i_index ] = p_bank->pp_sub[ i_index+1 ]; 
    194     } 
    195  
    196     p_bank->i_sub--; 
    197     if( p_bank->i_sub ) 
    198     { 
    199         p_bank->pp_sub = realloc( p_bank->pp_sub, p_bank->i_sub 
    200                                    * sizeof( msg_subscription_t* ) ); 
    201     } 
    202     else 
    203     { 
    204         free( p_bank->pp_sub ); 
    205         p_bank->pp_sub = NULL; 
    206     } 
    207  
     187    REMOVE_ELEM( p_bank->pp_sub, p_bank->i_sub, i_index ); 
    208188 
    209189    vlc_mutex_unlock( &p_bank->lock ); 
  • src/misc/objects.c

    rce7d29b r319e629  
    33 ***************************************************************************** 
    44 * Copyright (C) 2002 VideoLAN 
    5  * $Id: objects.c,v 1.26 2002/10/17 13:15:31 sam Exp $ 
     5 * $Id: objects.c,v 1.27 2002/10/29 13:22:48 sam Exp $ 
    66 * 
    77 * Authors: Samuel Hocevar <sam@zoy.org> 
     
    187187        /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's 
    188188         * useless to try and recover anything if pp_objects gets smashed. */ 
    189         p_new->p_libvlc->i_objects++; 
    190         p_new->p_libvlc->pp_objects = 
    191                 realloc( p_new->p_libvlc->pp_objects, 
    192                          p_new->p_libvlc->i_objects * sizeof(vlc_object_t *) ); 
    193         p_new->p_libvlc->pp_objects[ p_new->p_libvlc->i_objects - 1 ] = p_new; 
     189        INSERT_ELEM( p_new->p_libvlc->pp_objects, 
     190                     p_new->p_libvlc->i_objects, 
     191                     p_new->p_libvlc->i_objects, 
     192                     p_new ); 
    194193 
    195194        vlc_mutex_unlock( &structure_lock ); 
     
    283282        free( p_this->p_libvlc->pp_objects ); 
    284283        p_this->p_libvlc->pp_objects = NULL; 
     284        p_this->p_libvlc->i_objects--; 
    285285 
    286286        vlc_mutex_destroy( &structure_lock ); 
     
    296296        i_index = FindIndex( p_this, p_this->p_libvlc->pp_objects, 
    297297                             p_this->p_libvlc->i_objects ); 
    298         memmove( p_this->p_libvlc->pp_objects + i_index, 
    299                  p_this->p_libvlc->pp_objects + i_index + 1, 
    300                  (p_this->p_libvlc->i_objects - i_index - 1) 
    301                    * sizeof( vlc_object_t *) ); 
    302  
    303         p_this->p_libvlc->pp_objects = 
    304             realloc( p_this->p_libvlc->pp_objects, 
    305                  (p_this->p_libvlc->i_objects - 1) * sizeof(vlc_object_t *) ); 
     298        REMOVE_ELEM( p_this->p_libvlc->pp_objects, 
     299                     p_this->p_libvlc->i_objects, i_index ); 
    306300 
    307301        vlc_mutex_unlock( &structure_lock ); 
    308302    } 
    309  
    310     p_this->p_libvlc->i_objects--; 
    311303 
    312304    vlc_mutex_destroy( &p_this->object_lock ); 
     
    446438 
    447439    /* Attach the child to its parent */ 
    448     p_parent->i_children++; 
    449     p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children, 
    450                                p_parent->i_children * sizeof(vlc_object_t *) ); 
    451     p_parent->pp_children[p_parent->i_children - 1] = p_this; 
     440    INSERT_ELEM( p_parent->pp_children, p_parent->i_children, 
     441                 p_parent->i_children, p_this ); 
    452442 
    453443    /* Climb up the tree to see whether we are connected with the root */ 
  • src/misc/variables.c

    r50d45d7 r319e629  
    33 ***************************************************************************** 
    44 * Copyright (C) 2002 VideoLAN 
    5  * $Id: variables.c,v 1.10 2002/10/28 20:57:02 sam Exp $ 
     5 * $Id: variables.c,v 1.11 2002/10/29 13:22:48 sam Exp $ 
    66 * 
    77 * Authors: Samuel Hocevar <sam@zoy.org> 
     
    4343 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w 
    4444 *****************************************************************************/ 
    45 static int CmpBool( vlc_value_t v, vlc_value_t w ) { return v.b_bool != w.b_bool; } 
     45static int CmpBool( vlc_value_t v, vlc_value_t w ) { return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0; } 
    4646static int CmpInt( vlc_value_t v, vlc_value_t w ) { return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1; } 
    4747static int CmpString( vlc_value_t v, vlc_value_t w ) { return strcmp( v.psz_string, w.psz_string ); } 
     
    195195int __var_Destroy( vlc_object_t *p_this, const char *psz_name ) 
    196196{ 
    197     int i_var
     197    int i_var, i
    198198    variable_t *p_var; 
    199199 
     
    228228    } 
    229229 
     230    /* Free choice list if needed */ 
     231    if( p_var->pp_choices ) 
     232    { 
     233        for( i = 0 ; i < p_var->i_choices ; i++ ) 
     234        { 
     235            p_var->pf_free( &p_var->pp_choices[i] ); 
     236        } 
     237        free( p_var->pp_choices ); 
     238    } 
     239 
    230240    /* Free callbacks if needed */ 
    231241    if( p_var->p_entries ) 
     
    327337            } 
    328338 
    329             if( p_var->i_choices ) 
    330             { 
    331                 p_var->pp_choices = realloc( p_var->pp_choices, 
    332                                              (p_var->i_choices + 1) 
    333                                               * sizeof(vlc_value_t) ); 
    334             } 
    335             else 
    336             { 
    337                 p_var->pp_choices = malloc( (p_var->i_choices + 1) 
    338                                              * sizeof(vlc_value_t) ); 
    339             } 
    340  
    341             memmove( p_var->pp_choices + i + 1, 
    342                      p_var->pp_choices + i, 
    343                      (p_var->i_choices - i) * sizeof(vlc_value_t) ); 
    344  
    345             p_var->i_choices++; 
    346             p_var->pp_choices[i] = *p_val; 
     339            INSERT_ELEM( p_var->pp_choices, p_var->i_choices, i, *p_val ); 
    347340            p_var->pf_dup( &p_var->pp_choices[i] ); 
     341 
    348342            CheckValue( p_var, &p_var->val ); 
    349343            break; 
     
    375369 
    376370            p_var->pf_free( &p_var->pp_choices[i] ); 
    377  
    378             memmove( p_var->pp_choices + i, 
    379                      p_var->pp_choices + i + 1, 
    380                      (p_var->i_choices - i - 1) * sizeof(vlc_value_t) ); 
    381  
    382             p_var->i_choices--; 
    383             if( p_var->i_choices ) 
    384             { 
    385                 p_var->pp_choices = realloc( p_var->pp_choices, 
    386                                              p_var->i_choices 
    387                                               * sizeof(vlc_value_t) ); 
    388             } 
    389             else 
    390             { 
    391                 free( p_var->pp_choices ); 
    392             } 
     371            REMOVE_ELEM( p_var->pp_choices, p_var->i_choices, i ); 
     372 
    393373            CheckValue( p_var, &p_var->val ); 
    394374            break; 
     
    607587                       vlc_callback_t pf_callback, void *p_data ) 
    608588{ 
     589    int i_var; 
     590    variable_t *p_var; 
     591    callback_entry_t entry; 
     592 
     593    entry.pf_callback = pf_callback; 
     594    entry.p_data = p_data; 
     595         
     596    vlc_mutex_lock( &p_this->var_lock ); 
     597 
     598    i_var = GetUnused( p_this, psz_name ); 
     599    if( i_var < 0 ) 
     600    { 
     601        vlc_mutex_unlock( &p_this->var_lock ); 
     602        return i_var; 
     603    } 
     604 
     605    p_var = &p_this->p_vars[i_var]; 
     606 
     607    INSERT_ELEM( p_var->p_entries, 
     608                 p_var->i_entries, 
     609                 p_var->i_entries, 
     610                 entry ); 
     611 
     612    vlc_mutex_unlock( &p_this->var_lock ); 
     613 
     614    return VLC_SUCCESS; 
     615} 
     616 
     617/***************************************************************************** 
     618 * var_DelCallback: remove a callback from a variable 
     619 ***************************************************************************** 
     620 * pf_callback and p_data have to be given again, because different objects 
     621 * might have registered the same callback function. 
     622 *****************************************************************************/ 
     623int __var_DelCallback( vlc_object_t *p_this, const char *psz_name, 
     624                       vlc_callback_t pf_callback, void *p_data ) 
     625{ 
    609626    int i_entry, i_var; 
    610627    variable_t *p_var; 
     
    620637 
    621638    p_var = &p_this->p_vars[i_var]; 
    622     i_entry = p_var->i_entries++; 
    623  
    624     if( i_entry ) 
    625     { 
    626         p_var->p_entries = realloc( p_var->p_entries, 
    627                                sizeof( callback_entry_t ) * p_var->i_entries ); 
    628     } 
    629     else 
    630     { 
    631         p_var->p_entries = malloc( sizeof( callback_entry_t ) ); 
    632     } 
    633  
    634     p_var->p_entries[ i_entry ].pf_callback = pf_callback; 
    635     p_var->p_entries[ i_entry ].p_data = p_data; 
    636          
    637     vlc_mutex_unlock( &p_this->var_lock ); 
    638  
    639     return VLC_SUCCESS; 
    640 } 
    641  
    642 /***************************************************************************** 
    643  * var_DelCallback: remove a callback from a variable 
    644  ***************************************************************************** 
    645  * pf_callback and p_data have to be given again, because different objects 
    646  * might have registered the same callback function. 
    647  *****************************************************************************/ 
    648 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name, 
    649                        vlc_callback_t pf_callback, void *p_data ) 
    650 { 
    651     int i_entry, i_var; 
    652     variable_t *p_var; 
    653  
    654     vlc_mutex_lock( &p_this->var_lock ); 
    655  
    656     i_var = GetUnused( p_this, psz_name ); 
    657     if( i_var < 0 ) 
    658     { 
    659         vlc_mutex_unlock( &p_this->var_lock ); 
    660         return i_var; 
    661     } 
    662  
    663     p_var = &p_this->p_vars[i_var]; 
    664639 
    665640    for( i_entry = p_var->i_entries ; i_entry-- ; ) 
     
    678653    } 
    679654 
    680     p_var->i_entries--; 
    681  
    682     memmove( p_var->p_entries + i_entry, 
    683              p_var->p_entries + i_entry + 1, 
    684              sizeof( callback_entry_t ) * ( p_var->i_entries - i_entry ) ); 
    685  
    686     if( p_var->i_entries ) 
    687     { 
    688         p_var->p_entries = realloc( p_var->p_entries, 
    689                        sizeof( callback_entry_t ) * ( p_var->i_entries ) ); 
    690     } 
    691     else 
    692     { 
    693         free( p_var->p_entries ); 
    694         p_var->p_entries = NULL; 
    695     } 
     655    REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry ); 
    696656 
    697657    vlc_mutex_unlock( &p_this->var_lock ); 
  • src/playlist/playlist.c

    r1354358 r319e629  
    33 ***************************************************************************** 
    44 * Copyright (C) 1999-2001 VideoLAN 
    5  * $Id: playlist.c,v 1.14 2002/09/29 18:19:53 sam Exp $ 
     5 * $Id: playlist.c,v 1.15 2002/10/29 13:22:48 sam Exp $ 
    66 * 
    77 * Authors: Samuel Hocevar <sam@zoy.org> 
     
    139139        int i_index; 
    140140 
    141         p_playlist->i_size++; 
    142         p_playlist->pp_items = realloc( p_playlist->pp_items, 
    143                                         p_playlist->i_size * sizeof(void*) ); 
    144         if( p_playlist->pp_items == NULL ) 
    145         { 
    146             msg_Err( p_playlist, "out of memory" ); 
    147             free( p_item->psz_name ); 
    148             free( p_item ); 
    149             vlc_mutex_unlock( &p_playlist->object_lock ); 
    150             return -1; 
    151         } 
    152  
    153141        /* Additional boundary checks */ 
    154142        if( i_mode & PLAYLIST_APPEND ) 
     
    161149            i_pos = 0; 
    162150        } 
    163         else if( i_pos > p_playlist->i_size - 1 ) 
    164         { 
    165             i_pos = p_playlist->i_size - 1; 
    166         } 
    167  
    168         /* Now we know exactly where it goes. Just renumber the playlist */ 
    169         for( i_index = p_playlist->i_size - 1; i_index > i_pos ; i_index-- ) 
    170         { 
    171             p_playlist->pp_items[i_index] = p_playlist->pp_items[i_index - 1]; 
    172         } 
     151        else if( i_pos > p_playlist->i_size ) 
     152        { 
     153            i_pos = p_playlist->i_size; 
     154        } 
     155 
     156        INSERT_ELEM( p_playlist->pp_items, 
     157                     p_playlist->i_size, 
     158                     i_pos, 
     159                     p_item ); 
    173160 
    174161        if( p_playlist->i_index >= i_pos ) 
     
    181168        /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */ 
    182169        free( p_playlist->pp_items[i_pos]->psz_name ); 
     170        /* XXX: what if the item is still in use? */ 
    183171        free( p_playlist->pp_items[i_pos] ); 
    184         /* XXX: what if the item is still in use? */ 
    185     } 
    186  
    187     p_playlist->pp_items[i_pos] = p_item; 
     172        p_playlist->pp_items[i_pos] = p_item; 
     173    } 
    188174 
    189175    if( i_mode & PLAYLIST_GO ) 
     
    219205 
    220206        free( p_playlist->pp_items[i_pos]->psz_name ); 
     207        /* XXX: what if the item is still in use? */ 
    221208        free( p_playlist->pp_items[i_pos] ); 
    222         /* XXX: what if the item is still in use? */ 
    223209 
    224210        if( i_pos < p_playlist->i_index ) 
     
    228214 
    229215        /* Renumber the playlist */ 
    230         for( i_index = i_pos + 1; i_index < p_playlist->i_size; i_index++ ) 
    231         { 
    232             p_playlist->pp_items[i_index - 1] = p_playlist->pp_items[i_index]; 
    233         } 
    234  
    235         p_playlist->i_size--; 
    236         if( p_playlist->i_size ) 
    237         { 
    238             p_playlist->pp_items = realloc( p_playlist->pp_items, 
    239                                         p_playlist->i_size * sizeof(void*) ); 
    240         } 
    241         else 
    242         { 
    243             free( p_playlist->pp_items ); 
    244             p_playlist->pp_items = NULL; 
    245         } 
     216        REMOVE_ELEM( p_playlist->pp_items, 
     217                     p_playlist->i_size, 
     218                     i_pos ); 
    246219    } 
    247220