]> pd.if.org Git - pdclib/blob - functions/stdlib/realloc.c
Adding malloc(), free(), and realloc().
[pdclib] / 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     struct _PDCLIB_memnode_t * baseptr = (struct _PDCLIB_memnode_t *)( (char *)ptr - sizeof( struct _PDCLIB_memnode_t ) );
22     if ( baseptr->size >= size )
23     {
24         return ptr;
25     }
26     else
27     {
28         void * newptr = malloc( size );
29         memcpy( newptr, ptr, baseptr->size );
30         free( ptr );
31         return newptr;
32     }
33     return NULL;
34 }
35
36 #endif
37
38 #ifdef TEST
39 #include <_PDCLIB_test.h>
40
41 int main()
42 {
43     BEGIN_TESTS;
44     /* tests covered in malloc test driver */
45     return TEST_RESULTS;
46 }
47
48 #endif