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