Commit 982400e1 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Error-proof REMOVE_ELEM

parent 90daa759
...@@ -30,6 +30,13 @@ ...@@ -30,6 +30,13 @@
* This file defines functions, structures and macros for handling arrays in vlc * 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 * Simple dynamic array handling. Array is realloced at each insert/removal
*/ */
...@@ -53,25 +60,23 @@ ...@@ -53,25 +60,23 @@
} \ } \
while( 0 ) while( 0 )
#define REMOVE_ELEM( p_ar, i_oldsize, i_pos ) \ #define REMOVE_ELEM( p_ar, i_size, i_pos ) \
do \ do \
{ \ { \
if( (i_oldsize) - (i_pos) - 1 ) \ if( (i_size) - (i_pos) - 1 ) \
{ \ { \
memmove( (p_ar) + (i_pos), \ memmove( (p_ar) + (i_pos), \
(p_ar) + (i_pos) + 1, \ (p_ar) + (i_pos) + 1, \
((i_oldsize) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \ ((i_size) - (i_pos) - 1) * sizeof( *(p_ar) ) ); \
} \
if( i_oldsize > 1 ) \
{ \
(p_ar) = realloc( p_ar, ((i_oldsize) - 1) * sizeof( *(p_ar) ) ); \
} \ } \
if( i_size > 1 ) \
(p_ar) = realloc_down( p_ar, ((i_size) - 1) * sizeof( *(p_ar) ) );\
else \ else \
{ \ { \
free( p_ar ); \ free( p_ar ); \
(p_ar) = NULL; \ (p_ar) = NULL; \
} \ } \
(i_oldsize)--; \ (i_size)--; \
} \ } \
while( 0 ) while( 0 )
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment