4 * Ullrich von Bassewitz, 06.06.1998
15 void* realloc (void* block, size_t size)
22 /* Check the block parameter */
24 /* Block is NULL, same as malloc */
28 /* Check the size parameter */
30 /* Block is not NULL, but size is: free the block */
35 /* Make the internal used size from the given size */
36 size += HEAP_ADMIN_SPACE;
37 if (size < sizeof (struct freeblock)) {
38 size = sizeof (struct freeblock);
41 /* Get a pointer to the real block, get the old block size */
42 b = (unsigned*) (((int) block) - 2);
45 /* Get the size difference as a signed quantity */
46 diff = size - oldsize;
48 /* Is the block at the current heap top? */
49 if (((int) b) + oldsize == ((int) _hptr)) {
50 /* Check if we've enough memory at the heap top */
52 newhptr = ((int) _hptr) + diff;
53 if (newhptr <= ((int) _hend)) {
54 /* Ok, there's space enough */
55 _hptr = (unsigned*) newhptr;
61 /* The given block was not located on top of the heap, or there's no
62 * room left. Try to allocate a new block and copy the data.
64 if (newblock = malloc (size)) {
66 /* Adjust the old size to the user visible portion */
67 oldsize -= sizeof (unsigned);
69 /* If the new block is larger than the old one, copy the old
76 /* Copy the block data */
77 memcpy (newblock, block, size);