* WebAssembly: optimized SysOSAlloc to use memory area left at the end of the

page (64kb). This reduces memory use, when SysOSAlloc is called with sizes,
  smaller than 64kb.
This commit is contained in:
Nikolay Nikolov 2024-08-04 18:04:05 +03:00
parent a0c11becde
commit 6be3a42a2f

View File

@ -46,21 +46,29 @@ const
err = high(longword);
var
res: ptruint;
avail: SizeUInt;
grow_pages: LongInt;
begin
{$ifdef FPC_WASM_THREADS}
if InitialHeapCriticalSectionInitialized then
EnterCriticalSection(InitialHeapCriticalSection);
{$endif FPC_WASM_THREADS}
if (PtrUInt(InitialHeapBlockEnd)-PtrUInt(InitialHeapBlockStart))>=size then
avail:=PtrUInt(InitialHeapBlockEnd)-PtrUInt(InitialHeapBlockStart);
if avail>=size then
begin
SysOSAlloc:=InitialHeapBlockStart;
Inc(InitialHeapBlockStart,size);
end
else
begin
res:=fpc_wasm32_memory_grow((size + WasmMemoryPageSize - 1) div WasmMemoryPageSize);
grow_pages:=(size-avail+WasmMemoryPageSize-1) div WasmMemoryPageSize;
res:=fpc_wasm32_memory_grow(grow_pages);
if res<>err then
SysOSAlloc:=pointer(res*WasmMemoryPageSize)
begin
SysOSAlloc:=InitialHeapBlockStart;//pointer(res*WasmMemoryPageSize)
Inc(InitialHeapBlockStart,size);
Inc(InitialHeapBlockEnd,grow_pages*WasmMemoryPageSize);
end
else
SysOSAlloc:=nil;
end;