X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdlib%2Frealloc.c;h=c2bebd47a14079d8e91b271749e9efb48d9fc5fd;hb=640b93981b2ecade875371ecb873e43f750d2976;hp=df5fca1bc75904411404315cac347ea32f8d164c;hpb=137bef7f4838fc430682213fa628e16b2237ed63;p=pdclib diff --git a/functions/stdlib/realloc.c b/functions/stdlib/realloc.c index df5fca1..c2bebd4 100644 --- a/functions/stdlib/realloc.c +++ b/functions/stdlib/realloc.c @@ -1,7 +1,5 @@ /* $Id$ */ -/* Release $Name$ */ - /* void * realloc( void *, size_t ) This file is part of the Public Domain C Library (PDCLib). @@ -18,19 +16,28 @@ void * realloc( void * ptr, size_t size ) { - struct _PDCLIB_memnode_t * baseptr = (struct _PDCLIB_memnode_t *)( (char *)ptr - sizeof( struct _PDCLIB_memnode_t ) ); - if ( baseptr->size >= size ) + void * newptr = NULL; + if ( ptr == NULL ) { - return ptr; + return malloc( size ); } - else + if ( size > 0 ) { - void * newptr = malloc( size ); - memcpy( newptr, ptr, baseptr->size ); - free( ptr ); - return newptr; + 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. */ + newptr = malloc( size ); + memcpy( newptr, ptr, baseptr->size ); + } } - return NULL; + free( ptr ); + return newptr; } #endif @@ -38,9 +45,8 @@ void * realloc( void * ptr, size_t size ) #ifdef TEST #include <_PDCLIB_test.h> -int main() +int main( void ) { - BEGIN_TESTS; /* tests covered in malloc test driver */ return TEST_RESULTS; }