]> pd.if.org Git - pdclib/blob - opt/malloc-solar/realloc.c
Sweeping cleanups. Sorry for the massive commit; I got sidetracked once too often.
[pdclib] / opt / malloc-solar / realloc.c
1 /* void * realloc( void *, size_t )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <stddef.h>
10
11 #ifndef REGTEST
12
13 /* TODO: Primitive placeholder. Improve. */
14
15 void * realloc( void * ptr, size_t size )
16 {
17     void * newptr = NULL;
18     if ( ptr == NULL )
19     {
20         return malloc( size );
21     }
22     if ( size > 0 )
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             /* Current memnode is large enough; nothing to do. */
28             return ptr;
29         }
30         else
31         {
32             /* Get larger memnode and copy over contents. */
33             if ( ( newptr = malloc( size ) ) == NULL )
34             {
35                 return NULL;
36             }
37             memcpy( newptr, ptr, baseptr->size );
38         }
39     }
40     free( ptr );
41     return newptr;
42 }
43
44 #endif
45
46 #ifdef TEST
47 #include <_PDCLIB_test.h>
48
49 int main( void )
50 {
51     /* tests covered in malloc test driver */
52     return TEST_RESULTS;
53 }
54
55 #endif
56