From: RĂ©mi Denis-Courmont Date: Sun, 24 May 2009 17:52:40 +0000 (+0300) Subject: Error-proof REMOVE_ELEM X-Git-Tag: 1.1.0-ff~5788 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=982400e1dc0c83f55ad9c4c839509e9a06e5ce6e;p=vlc Error-proof REMOVE_ELEM --- diff --git a/include/vlc_arrays.h b/include/vlc_arrays.h index e085f0378b..90ef202247 100644 --- a/include/vlc_arrays.h +++ b/include/vlc_arrays.h @@ -30,6 +30,13 @@ * This file defines functions, structures and macros for handling arrays in vlc */ +/* realloc() that never fails *if* downsizing */ +static inline void *realloc_down( void *ptr, size_t size ) +{ + void *ret = realloc( ptr, size ); + return ret ? ret : ptr; +} + /** * Simple dynamic array handling. Array is realloced at each insert/removal */ @@ -53,25 +60,23 @@ } \ while( 0 ) -#define REMOVE_ELEM( p_ar, i_oldsize, i_pos ) \ +#define REMOVE_ELEM( p_ar, i_size, i_pos ) \ do \ { \ - if( (i_oldsize) - (i_pos) - 1 ) \ + if( (i_size) - (i_pos) - 1 ) \ { \ memmove( (p_ar) + (i_pos), \ (p_ar) + (i_pos) + 1, \ - ((i_oldsize) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \ - } \ - if( i_oldsize > 1 ) \ - { \ - (p_ar) = realloc( p_ar, ((i_oldsize) - 1) * sizeof( *(p_ar) ) ); \ + ((i_size) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \ } \ + if( i_size > 1 ) \ + (p_ar) = realloc_down( p_ar, ((i_size) - 1) * sizeof( *(p_ar) ) );\ else \ { \ free( p_ar ); \ (p_ar) = NULL; \ } \ - (i_oldsize)--; \ + (i_size)--; \ } \ while( 0 )