X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdlib%2Frealloc.c;h=67133b4a790747926a71accabc4d92df32700c25;hb=12e17136786afb1775c9dc946cbe41f5e230c24a;hp=ac615b8a296820c62938187618ab34cb10228f5d;hpb=dcc8a8e99f69e090a03b7b868443addbc0817820;p=pdclib.old diff --git a/functions/stdlib/realloc.c b/functions/stdlib/realloc.c index ac615b8..67133b4 100644 --- a/functions/stdlib/realloc.c +++ b/functions/stdlib/realloc.c @@ -1,8 +1,53 @@ -// ---------------------------------------------------------------------------- -// $Id$ -// ---------------------------------------------------------------------------- -// Public Domain C Library - http://pdclib.sourceforge.net -// This code is Public Domain. Use, modify, and redistribute at will. -// ---------------------------------------------------------------------------- - -void * realloc( void * ptr, size_t size ) { /* TODO */ }; +/* $Id$ */ + +/* Release $Name$ */ + +/* void * realloc( void *, size_t ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include +#include +#include + +#ifndef REGTEST + +/* TODO: Primitive placeholder. Improve. */ + +void * realloc( void * ptr, size_t size ) +{ + if ( ptr == NULL ) + { + return malloc( size ); + } + { + struct _PDCLIB_memnode_t * baseptr = (struct _PDCLIB_memnode_t *)( (char *)ptr - sizeof( struct _PDCLIB_memnode_t ) ); + if ( baseptr->size >= size ) + { + return ptr; + } + else + { + void * newptr = malloc( size ); + memcpy( newptr, ptr, baseptr->size ); + free( ptr ); + return newptr; + } + } + return NULL; +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + /* tests covered in malloc test driver */ + return TEST_RESULTS; +} + +#endif