]> pd.if.org Git - pdclib/blob - functions/stdlib/realloc.c
c1ba38f4d1cd2b674aa0ae5013ff0ebf54070379
[pdclib] / functions / stdlib / realloc.c
1 /* $Id$ */
2
3 /* void * realloc( void *, size_t )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stddef.h>
12
13 #ifndef REGTEST
14
15 /* TODO: Primitive placeholder. Improve. */
16
17 void * realloc( void * ptr, size_t size )
18 {
19     if ( ptr == NULL )
20     {
21         return malloc( size );
22     }
23     {
24     struct _PDCLIB_memnode_t * baseptr = (struct _PDCLIB_memnode_t *)( (char *)ptr - sizeof( struct _PDCLIB_memnode_t ) );
25     if ( baseptr->size >= size )
26     {
27         return ptr;
28     }
29     else
30     {
31         void * newptr = malloc( size );
32         memcpy( newptr, ptr, baseptr->size );
33         free( ptr );
34         return newptr;
35     }
36     }
37     return NULL;
38 }
39
40 #endif
41
42 #ifdef TEST
43 #include <_PDCLIB_test.h>
44
45 int main( void )
46 {
47     /* tests covered in malloc test driver */
48     return TEST_RESULTS;
49 }
50
51 #endif