]> pd.if.org Git - pdclib/blobdiff - functions/stdlib/realloc.c
Comment cleanups.
[pdclib] / functions / stdlib / realloc.c
index ae11da5c2cba6a509d97042dc8f4612fe42ca8fc..a7cc395d21a9f39d3669e4c7f4637c13f643c451 100644 (file)
@@ -1,32 +1,56 @@
-// ----------------------------------------------------------------------------
-// $Id$
-// ----------------------------------------------------------------------------
-// Public Domain C Library - http://pdclib.sourceforge.net
-// This code is Public Domain. Use, modify, and redistribute at will.
-// ----------------------------------------------------------------------------
+/* void * realloc( void *, size_t )
 
-void * realloc( void * ptr, size_t size ) { /* TODO */ };
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdlib.h>
+#include <string.h>
+#include <stddef.h>
+
+#ifndef REGTEST
+
+/* TODO: Primitive placeholder. Improve. */
 
-/* PDPC code - unreviewed
+void * realloc( void * ptr, size_t size )
 {
-    char *newptr;
-    size_t oldsize;
-    
-    newptr = malloc(size);
-    if (newptr == NULL)
+    void * newptr = NULL;
+    if ( ptr == NULL )
     {
-        return (NULL);
+        return malloc( size );
     }
-    if (ptr != NULL)
+    if ( size > 0 )
     {
-        oldsize = *(size_t *)((char *)ptr - 4);
-        if (oldsize < size)
+        struct _PDCLIB_memnode_t * baseptr = (struct _PDCLIB_memnode_t *)( (char *)ptr - sizeof( struct _PDCLIB_memnode_t ) );
+        if ( baseptr->size >= size )
         {
-            size = oldsize;
+            /* 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 );
         }
-        memcpy(newptr, ptr, size);
-        free(ptr);
     }
-    return (newptr);
+    free( ptr );
+    return newptr;
 }
-*/
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    /* tests covered in malloc test driver */
+    return TEST_RESULTS;
+}
+
+#endif
+