function MyGetMem(Size: NativeInt): Pointer;
begin
Inc(Size, SizeOf(Integer));
Result := SysGetMem(Size);
PInteger(Result)^ := Size;
Inc(NativeInt(Result), SizeOf(Integer));
end;
function MyFreeMem(P: Pointer): Integer;
begin
Dec(NativeInt(P), SizeOf(Integer));
FillMemory(P, PInteger(P)^, $A5);
// 1010 0101 = HighBit + Odd
Result := SysFreeMem(P);
end;
function MyReallocMem(P: Pointer; Size: NativeInt): Pointer;
begin
Dec(NativeInt(P), SizeOf(Integer));
Inc(Size, SizeOf(Integer));
Result := SysReallocMem(P, Size);
PInteger(Result)^ := Size;
Inc(NativeInt(Result), SizeOf(Integer));
end;
function MyAllocMem(Size: NativeInt): Pointer;
begin
Inc(Size, SizeOf(Integer));
Result := SysAllocMem(Size);
PInteger(Result)^ := Size;
Inc(NativeInt(Result), SizeOf(Integer));
end;
var
MemMgr: TMemoryManagerEx;
initialization
GetMemoryManager(MemMgr);
MemMgr.GetMem := MyGetMem;
MemMgr.FreeMem := MyFreeMem;
MemMgr.ReallocMem := MyReallocMem;
MemMgr.AllocMem := MyAllocMem;
SetMemoryManager(MemMgr);
finalization
GetMemoryManager(MemMgr);
MemMgr.GetMem := SysGetMem;
MemMgr.FreeMem := SysFreeMem;
MemMgr.ReallocMem := SysReallocMem;
MemMgr.AllocMem := SysAllocMem;
SetMemoryManager(MemMgr);
end.