mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-18 03:09:11 +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;
|
function SysTinyReAllocMem(var p: pointer; size: ptruint):pointer;
|
||||||
var
|
var
|
||||||
sz: ptruint;
|
oldsize, OldAllocSize, NewAllocSize: ptruint;
|
||||||
begin
|
begin
|
||||||
{$ifdef DEBUG_TINY_HEAP}
|
{$ifdef DEBUG_TINY_HEAP}
|
||||||
Write('SysTinyReAllocMem(', HexStr(p), ',', size, ')=');
|
Write('SysTinyReAllocMem(', HexStr(p), ',', size, ')=');
|
||||||
{$endif DEBUG_TINY_HEAP}
|
{$endif DEBUG_TINY_HEAP}
|
||||||
if size=0 then
|
if size=0 then
|
||||||
result := nil
|
|
||||||
else
|
|
||||||
result := AllocMem(size);
|
|
||||||
if result <> nil then
|
|
||||||
begin
|
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
|
begin
|
||||||
sz := FindSize(p);
|
{ old and new size are the same after alignment, so the memory block is already allocated }
|
||||||
if sz > size then
|
{ we just need to update the size }
|
||||||
sz := size;
|
PTinyHeapMemBlockSize(p)[-1] := size;
|
||||||
move(pbyte(p)^, pbyte(result)^, sz);
|
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;
|
||||||
end;
|
end;
|
||||||
SysTinyFreeMem(p);
|
|
||||||
p := result;
|
|
||||||
{$ifdef DEBUG_TINY_HEAP}
|
{$ifdef DEBUG_TINY_HEAP}
|
||||||
Writeln(HexStr(result));
|
Writeln(HexStr(result));
|
||||||
{$endif DEBUG_TINY_HEAP}
|
{$endif DEBUG_TINY_HEAP}
|
||||||
|
Loading…
Reference in New Issue
Block a user