]> pd.if.org Git - pdclib/blob - functions/stdlib/realloc.c
Re-import from Subversion.
[pdclib] / functions / stdlib / realloc.c
1 /* ----------------------------------------------------------------------------
2  * $Id$
3  * ----------------------------------------------------------------------------
4  * Public Domain C Library - http://pdclib.sourceforge.net
5  * This code is Public Domain. Use, modify, and redistribute at will.
6  * --------------------------------------------------------------------------*/
7
8 void * realloc( void * ptr, size_t size ) { /* TODO */ };
9
10 /* PDPC code - assumes size_t of memory chunk at offset -4.
11 {
12     char * newptr;
13     size_t oldsize;
14
15     if ( ( newptr = malloc(size) ) != NULL )
16     {
17         if ( ptr != NULL )
18         {
19             // reading size information from hidden chunk header
20             oldsize = * (size_t *) ( (char *) ptr - 4 );
21             // correctly handle *down*sizing as well as *up*sizing
22             memcpy( newptr, ptr, ( oldsize < size ) ? oldsize : size );
23             free( ptr );
24         }
25     }
26     return ( newptr );
27 }
28 */
29
30 /* Thomas "Beyond Infinity" Kreitner's code - unchecked, unoptimized
31 {
32     uint_t found = 0;
33     uint_t length;
34     chunk_t * new;
35     chunk_t * chunk;
36     // searching the block to-be-allocated
37     chunk = used_mem.start;
38     while ( ! found && ( chunk->next != NULL )
39     {
40         if ( chunk->address == ptr )
41         {
42             found++;
43         }
44         else
45         {
46             chunk=chunk->next;
47         }
48     }
49     if ( found )
50     {
51         length = chunk->length;
52         // allocate new memory area
53         if ( ( new = (void *) malloc( size ) ) != NULL )
54         {
55             // copy the data
56             memcpy( ptr, new, length );
57             // release the old memory
58             free( ptr );
59         }
60         else
61         {
62             new = NULL;
63         }
64     }
65     else
66     {
67         new = NULL;
68     }
69     return new;
70 }
71 */