1 /*****************************************************************************/
5 /* Change the size of an allocated memory block */
9 /* (C) 1998-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
42 void* __fastcall__ realloc (void* block, size_t size)
49 /* Check the block parameter */
51 /* Block is NULL, same as malloc */
55 /* Check the size parameter */
57 /* Block is not NULL, but size is: free the block */
62 /* Make the internal used size from the given size */
63 size += HEAP_ADMIN_SPACE;
64 if (size < sizeof (struct freeblock)) {
65 size = sizeof (struct freeblock);
68 /* Get a pointer to the real block, get the old block size */
69 b = (unsigned*) (((int) block) - 2);
72 /* Get the size difference as a signed quantity */
73 diff = size - oldsize;
75 /* Is the block at the current heap top? */
76 if (((int) b) + oldsize == ((int) _hptr)) {
77 /* Check if we've enough memory at the heap top */
79 newhptr = ((int) _hptr) + diff;
80 if (newhptr <= ((int) _hend)) {
81 /* Ok, there's space enough */
82 _hptr = (unsigned*) newhptr;
88 /* The given block was not located on top of the heap, or there's no
89 * room left. Try to allocate a new block and copy the data.
91 if (newblock = malloc (size)) {
93 /* Adjust the old size to the user visible portion */
94 oldsize -= sizeof (unsigned);
96 /* If the new block is larger than the old one, copy the old
103 /* Copy the block data */
104 memcpy (newblock, block, size);