sysheap.inc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2001 by Free Pascal development team
  4. This file implements all the base types and limits required
  5. for a minimal POSIX compliant subset required to port the compiler
  6. to a new OS.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {*****************************************************************************
  14. Heap Management
  15. *****************************************************************************}
  16. const
  17. WasmMemoryPageSize=65536;
  18. var
  19. InitialHeapBlockStart: Pointer;
  20. InitialHeapBlockEnd: Pointer;
  21. {$ifdef FPC_WASM_THREADS}
  22. InitialHeapCriticalSection: TRtlCriticalSection;
  23. InitialHeapCriticalSectionInitialized: Boolean = false;
  24. {$endif FPC_WASM_THREADS}
  25. procedure SetInitialHeapBlockStart(p: Pointer);[Public, Alias : 'FPC_WASM_SETINITIALHEAPBLOCKSTART'];
  26. begin
  27. InitialHeapBlockStart:=p;
  28. end;
  29. procedure InitInitialHeapBlock;
  30. begin
  31. InitialHeapBlockEnd:=Pointer(PtrUInt(fpc_wasm32_memory_size)*WasmMemoryPageSize);
  32. end;
  33. function SysOSAlloc(size: ptruint): pointer;
  34. const
  35. err = high(longword);
  36. var
  37. res: ptruint;
  38. avail: SizeUInt;
  39. grow_pages: LongInt;
  40. begin
  41. {$ifdef FPC_WASM_THREADS}
  42. if InitialHeapCriticalSectionInitialized then
  43. EnterCriticalSection(InitialHeapCriticalSection);
  44. {$endif FPC_WASM_THREADS}
  45. avail:=PtrUInt(InitialHeapBlockEnd)-PtrUInt(InitialHeapBlockStart);
  46. if avail>=size then
  47. begin
  48. SysOSAlloc:=InitialHeapBlockStart;
  49. Inc(InitialHeapBlockStart,size);
  50. end
  51. else
  52. begin
  53. grow_pages:=(size-avail+WasmMemoryPageSize-1) div WasmMemoryPageSize;
  54. res:=fpc_wasm32_memory_grow(grow_pages);
  55. if res<>err then
  56. begin
  57. SysOSAlloc:=InitialHeapBlockStart;//pointer(res*WasmMemoryPageSize)
  58. Inc(InitialHeapBlockStart,size);
  59. Inc(InitialHeapBlockEnd,grow_pages*WasmMemoryPageSize);
  60. end
  61. else
  62. SysOSAlloc:=nil;
  63. if assigned(WasmGrowMemoryCallback) then
  64. WasmGrowMemoryCallback(grow_pages);
  65. end;
  66. {$ifdef FPC_WASM_THREADS}
  67. if InitialHeapCriticalSectionInitialized then
  68. LeaveCriticalSection(InitialHeapCriticalSection);
  69. {$endif FPC_WASM_THREADS}
  70. end;
  71. procedure SysOSFree(p: pointer; size: ptruint);
  72. begin
  73. end;