summaryrefslogtreecommitdiff
path: root/rtl/inc
diff options
context:
space:
mode:
authornickysn <nickysn@3ad0048d-3df7-0310-abae-a5850022a9f2>2014-09-22 12:48:12 +0000
committernickysn <nickysn@3ad0048d-3df7-0310-abae-a5850022a9f2>2014-09-22 12:48:12 +0000
commit72b3ae91a49f92238bfc638a40341fc69b893a6c (patch)
tree54b343fbc90915eff0d231ec4edd9413e6cb3e50 /rtl/inc
parentec4fbd0ea45d713c8c59555c28fd664164e91fde (diff)
downloadfpc-72b3ae91a49f92238bfc638a40341fc69b893a6c.tar.gz
* optimized SysTinyReAllocMem for the case when the new and old size are the
same after alignment to TinyHeapAllocGranularity git-svn-id: http://svn.freepascal.org/svn/fpc/trunk@28706 3ad0048d-3df7-0310-abae-a5850022a9f2
Diffstat (limited to 'rtl/inc')
-rw-r--r--rtl/inc/tinyheap.inc43
1 files changed, 32 insertions, 11 deletions
diff --git a/rtl/inc/tinyheap.inc b/rtl/inc/tinyheap.inc
index 955baf7bbf..57a00a43cf 100644
--- a/rtl/inc/tinyheap.inc
+++ b/rtl/inc/tinyheap.inc
@@ -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
+ begin
+ SysTinyFreeMem(p);
+ result := nil;
+ p := nil;
+ end
+ else if p=nil then
+ begin
+ result := AllocMem(size);
+ p := result;
+ end
else
- result := AllocMem(size);
- if result <> nil then
begin
- if p <> nil then
+ 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}