SysReallocMem Function
The SysReallocMem
function calls Delphi’s
built-in memory manager to reallocate a block of memory with a new
size. The new block might be at the same address as the old block, or
it might be copied to a new address. In either case,
SysReallocMem
returns a pointer to the start of
the
block.
If the new size is larger than the old size, the extra memory is
uninitialized. If the request cannot be fulfilled,
SysReallocMem
leaves the old block alone and
returns nil
. SysReallocMem
is a
real function.
// See SetMemoryManager for an explanation of this memory manager.
// See SysFreeMem for the GuardsAreOkay function.
// See SetMemoryManager for the RoundUpSize function.
// Reallocate the block to the new size. Return a pointer to the
// new memory block or nil for failure.
function DebugRealloc(Mem: Pointer; Size: Integer): Pointer;
var
PArray: PIntegerArray;
begin
// Get the pointer to the true start of the memory block.
PArray := PIntegerArray(PChar(Mem) - GuardSize);
if not GuardsAreOkay(PArray, False) then
ReportError(2); // invalid pointer
// Round up Size to a multiple of the guard word size
Size := RoundUpSize(Size);
PArray := SysReallocMem (PArray, Size);
if PArray = nil then
Result := nil
else
begin
// Store the guard words.
PArray[0] := AllocatedGuard;
PArray[Size div GuardSize + 1] := AllocatedGuard;
// Return a pointer to the memory just past the first guard.
Result := @PArray[1];
end;
end;