]> pd.if.org Git - pdclib/blobdiff - functions/stdlib/realloc.c
Comment cleanups.
[pdclib] / functions / stdlib / realloc.c
index 67133b4a790747926a71accabc4d92df32700c25..a7cc395d21a9f39d3669e4c7f4637c13f643c451 100644 (file)
@@ -1,7 +1,3 @@
-/* $Id$ */
-
-/* Release $Name$ */
-
 /* void * realloc( void *, size_t )
 
    This file is part of the Public Domain C Library (PDCLib).
 
 void * realloc( void * ptr, size_t size )
 {
+    void * newptr = NULL;
     if ( ptr == NULL )
     {
         return malloc( size );
     }
+    if ( size > 0 )
     {
-    struct _PDCLIB_memnode_t * baseptr = (struct _PDCLIB_memnode_t *)( (char *)ptr - sizeof( struct _PDCLIB_memnode_t ) );
-    if ( baseptr->size >= size )
-    {
-        return ptr;
+        struct _PDCLIB_memnode_t * baseptr = (struct _PDCLIB_memnode_t *)( (char *)ptr - sizeof( struct _PDCLIB_memnode_t ) );
+        if ( baseptr->size >= size )
+        {
+            /* Current memnode is large enough; nothing to do. */
+            return ptr;
+        }
+        else
+        {
+            /* Get larger memnode and copy over contents. */
+            if ( ( newptr = malloc( size ) ) == NULL )
+            {
+                return NULL;
+            }
+            memcpy( newptr, ptr, baseptr->size );
+        }
     }
-    else
-    {
-        void * newptr = malloc( size );
-        memcpy( newptr, ptr, baseptr->size );
-        free( ptr );
-        return newptr;
-    }
-    }
-    return NULL;
+    free( ptr );
+    return newptr;
 }
 
 #endif
@@ -51,3 +53,4 @@ int main( void )
 }
 
 #endif
+