]> pd.if.org Git - pdclib/blob - opt/malloc-solar/realloc.c
* Change the style of inclusion of the internal/ headers. Modern preprocessors
[pdclib] / opt / malloc-solar / 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     void * newptr = NULL;
20     if ( ptr == NULL )
21     {
22         return malloc( size );
23     }
24     if ( size > 0 )
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             /* Current memnode is large enough; nothing to do. */
30             return ptr;
31         }
32         else
33         {
34             /* Get larger memnode and copy over contents. */
35             if ( ( newptr = malloc( size ) ) == NULL )
36             {
37                 return NULL;
38             }
39             memcpy( newptr, ptr, baseptr->size );
40         }
41     }
42     free( ptr );
43     return newptr;
44 }
45
46 #endif
47
48 #ifdef TEST
49 #include <_PDCLIB_test.h>
50
51 int main( void )
52 {
53     /* tests covered in malloc test driver */
54     return TEST_RESULTS;
55 }
56
57 #endif
58