[vlc-devel] commit: Error-proof REMOVE_ELEM ( Rémi Denis-Courmont )

git version control git at videolan.org
Sun May 24 20:10:47 CEST 2009


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun May 24 20:52:40 2009 +0300| [982400e1dc0c83f55ad9c4c839509e9a06e5ce6e] | committer: Rémi Denis-Courmont 

Error-proof REMOVE_ELEM

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=982400e1dc0c83f55ad9c4c839509e9a06e5ce6e
---

 include/vlc_arrays.h |   21 +++++++++++++--------
 1 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/include/vlc_arrays.h b/include/vlc_arrays.h
index e085f03..90ef202 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 )
 




More information about the vlc-devel mailing list