]> pd.if.org Git - pdclib.old/blobdiff - functions/stdlib/realloc.c
Removed the $Name$ tags (not supported by SVN). Added $Id$ to Makefile / text files.
[pdclib.old] / functions / stdlib / realloc.c
index 93a2aa418d2280d0853bf7cae2ee474abd8b0081..c1ba38f4d1cd2b674aa0ae5013ff0ebf54070379 100644 (file)
@@ -1,71 +1,51 @@
-/* ----------------------------------------------------------------------------
- * $Id$
- * ----------------------------------------------------------------------------
- * Public Domain C Library - http://pdclib.sourceforge.net
- * This code is Public Domain. Use, modify, and redistribute at will.
- * --------------------------------------------------------------------------*/
+/* $Id$ */
 
-void * realloc( void * ptr, size_t size ) { /* TODO */ };
+/* void * realloc( void *, size_t )
 
-/* PDPC code - assumes size_t of memory chunk at offset -4.
-{
-    char * newptr;
-    size_t oldsize;
-
-    if ( ( newptr = malloc(size) ) != NULL )
-    {
-        if ( ptr != NULL )
-        {
-            // reading size information from hidden chunk header
-            oldsize = * (size_t *) ( (char *) ptr - 4 );
-            // correctly handle *down*sizing as well as *up*sizing
-            memcpy( newptr, ptr, ( oldsize < size ) ? oldsize : size );
-            free( ptr );
-        }
-    }
-    return ( newptr );
-}
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
 */
 
-/* Thomas "Beyond Infinity" Kreitner's code - unchecked, unoptimized
+#include <stdlib.h>
+#include <string.h>
+#include <stddef.h>
+
+#ifndef REGTEST
+
+/* TODO: Primitive placeholder. Improve. */
+
+void * realloc( void * ptr, size_t size )
 {
-    uint_t found = 0;
-    uint_t length;
-    chunk_t * new;
-    chunk_t * chunk;
-    // searching the block to-be-allocated
-    chunk = used_mem.start;
-    while ( ! found && ( chunk->next != NULL )
+    if ( ptr == NULL )
     {
-        if ( chunk->address == ptr )
-        {
-            found++;
-        }
-        else
-        {
-            chunk=chunk->next;
-        }
+        return malloc( size );
     }
-    if ( found )
     {
-        length = chunk->length;
-        // allocate new memory area
-        if ( ( new = (void *) malloc( size ) ) != NULL )
-        {
-            // copy the data
-            memcpy( ptr, new, length );
-            // release the old memory
-            free( ptr );
-        }
-        else
-        {
-            new = NULL;
-        }
+    struct _PDCLIB_memnode_t * baseptr = (struct _PDCLIB_memnode_t *)( (char *)ptr - sizeof( struct _PDCLIB_memnode_t ) );
+    if ( baseptr->size >= size )
+    {
+        return ptr;
     }
     else
     {
-        new = NULL;
+        void * newptr = malloc( size );
+        memcpy( newptr, ptr, baseptr->size );
+        free( ptr );
+        return newptr;
     }
-    return new;
+    }
+    return NULL;
 }
-*/
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    /* tests covered in malloc test driver */
+    return TEST_RESULTS;
+}
+
+#endif