X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdlib%2Frealloc.c;h=67133b4a790747926a71accabc4d92df32700c25;hb=d02f38605b53cdff5460cc6b9e1b2a80c3a2ba4c;hp=ae11da5c2cba6a509d97042dc8f4612fe42ca8fc;hpb=0a5395faab237ba9008352b0f4bee9659bbd3d5f;p=pdclib diff --git a/functions/stdlib/realloc.c b/functions/stdlib/realloc.c index ae11da5..67133b4 100644 --- a/functions/stdlib/realloc.c +++ b/functions/stdlib/realloc.c @@ -1,32 +1,53 @@ -// ---------------------------------------------------------------------------- -// $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 */ }; +/* Release $Name$ */ -/* PDPC code - unreviewed +/* 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 ) { - char *newptr; - size_t oldsize; - - newptr = malloc(size); - if (newptr == NULL) + 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 (NULL); + return ptr; } - if (ptr != NULL) + else { - oldsize = *(size_t *)((char *)ptr - 4); - if (oldsize < size) - { - size = oldsize; - } - memcpy(newptr, ptr, size); - free(ptr); + void * newptr = malloc( size ); + memcpy( newptr, ptr, baseptr->size ); + free( ptr ); + return newptr; } - return (newptr); + } + return NULL; } -*/ + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + /* tests covered in malloc test driver */ + return TEST_RESULTS; +} + +#endif