mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-13 00:09:32 +02:00
* optimized SysTinyReAllocMem for the case when the new and old size are the
same after alignment to TinyHeapAllocGranularity git-svn-id: trunk@28706 -
This commit is contained in:
parent
16774350d7
commit
782b033acd
@ -259,27 +259,48 @@
|
||||
|
||||
function SysTinyReAllocMem(var p: pointer; size: ptruint):pointer;
|
||||
var
|
||||
sz: ptruint;
|
||||
oldsize, OldAllocSize, NewAllocSize: ptruint;
|
||||
begin
|
||||
{$ifdef DEBUG_TINY_HEAP}
|
||||
Write('SysTinyReAllocMem(', HexStr(p), ',', size, ')=');
|
||||
{$endif DEBUG_TINY_HEAP}
|
||||
if size=0 then
|
||||
result := nil
|
||||
else
|
||||
result := AllocMem(size);
|
||||
if result <> nil then
|
||||
begin
|
||||
if p <> nil then
|
||||
SysTinyFreeMem(p);
|
||||
result := nil;
|
||||
p := nil;
|
||||
end
|
||||
else if p=nil then
|
||||
begin
|
||||
result := AllocMem(size);
|
||||
p := result;
|
||||
end
|
||||
else
|
||||
begin
|
||||
oldsize := FindSize(p);
|
||||
OldAllocSize := align(oldsize+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
|
||||
NewAllocSize := align(size+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
|
||||
if OldAllocSize = NewAllocSize then
|
||||
begin
|
||||
sz := FindSize(p);
|
||||
if sz > size then
|
||||
sz := size;
|
||||
move(pbyte(p)^, pbyte(result)^, sz);
|
||||
{ old and new size are the same after alignment, so the memory block is already allocated }
|
||||
{ we just need to update the size }
|
||||
PTinyHeapMemBlockSize(p)[-1] := size;
|
||||
if size > oldsize then
|
||||
FillChar((TTinyHeapPointerArithmeticType(p)+oldsize)^, size-oldsize, 0);
|
||||
end
|
||||
else
|
||||
begin
|
||||
result := AllocMem(size);
|
||||
if result <> nil then
|
||||
begin
|
||||
if oldsize > size then
|
||||
oldsize := size;
|
||||
move(pbyte(p)^, pbyte(result)^, oldsize);
|
||||
end;
|
||||
SysTinyFreeMem(p);
|
||||
p := result;
|
||||
end;
|
||||
end;
|
||||
SysTinyFreeMem(p);
|
||||
p := result;
|
||||
{$ifdef DEBUG_TINY_HEAP}
|
||||
Writeln(HexStr(result));
|
||||
{$endif DEBUG_TINY_HEAP}
|
||||
|
Loading…
Reference in New Issue
Block a user