osheap.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2021 by the Free Pascal development team.
  4. OS heap manager for small targets
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. The OS heap manager is a small heap manager for smaller targets with an
  13. operating system. It's similar in comcept to the "cmem" memory manager
  14. for systems with libc support, but it aims systems that have a direct
  15. heap management API (Sinclair QL, AmigaOS, MacOS Classic, some
  16. embedded systems, etc), but not necessarily libc. It's also designed
  17. to be included in the System unit directly, unlike cmem.
  18. The main advantage is that it's much smaller than the regular heap
  19. manager, and it's a thinner layer towards the OS as well, therefore it
  20. can be slightly faster on resource constrained systems.
  21. The disadvantage is that it may not be well suited for bigger Pascal
  22. programs that rely heavily on consistent heap performance, as the
  23. properties of the OS heap manager like alignment and size constraints,
  24. and allocation time are also directly exposed towards the Pascal code.
  25. Additionally, querying the heap status is not supported.
  26. Its main difference to tinyheap is that it can release the allocated
  27. memory back to the system, therefore it suits systems that can run more
  28. than one software in parallel better.
  29. OSHeap needs SysOSAlloc and SysOSFree implemented in the system-specific
  30. code, and nothing else.
  31. }
  32. function SysGetMem(Size: ptruint): pointer;
  33. begin
  34. Inc(size,sizeof(size));
  35. result := SysOSAlloc(size);
  36. if assigned(result) then
  37. begin
  38. pptruint(result)[0] := size;
  39. Inc(result,sizeof(size));
  40. end;
  41. end;
  42. function SysFreeMem(p: Pointer): ptruint;
  43. begin
  44. if assigned(p) then
  45. begin
  46. Dec(p,sizeof(ptruint));
  47. SysOSFree(p,pptruint(p)[0]);
  48. end;
  49. result := 0;
  50. end;
  51. function SysFreeMemSize(p: Pointer; Size: Ptruint): ptruint;
  52. begin
  53. result := SysFreeMem(p);
  54. end;
  55. function SysMemSize(p: pointer): ptruint;
  56. begin
  57. Dec(p,sizeof(ptruint));
  58. result:=pptruint(p)[0]-sizeof(ptruint);
  59. end;
  60. function SysTryResizeMem(var p: pointer; size: ptruint) : boolean;
  61. begin
  62. result := false;
  63. end;
  64. function SysAllocMem(size: ptruint): pointer;
  65. begin
  66. result := SysGetMem(size);
  67. if not assigned(result) then
  68. FillChar(result^,SysMemSize(result),0);
  69. end;
  70. function SysReAllocMem(var p: pointer; size: ptruint):pointer;
  71. var
  72. oldsize: ptruint;
  73. begin
  74. result := nil;
  75. if assigned(p) then
  76. begin
  77. oldsize := SysMemSize(p);
  78. if size <> oldsize then
  79. begin
  80. if size > 0 then
  81. begin
  82. result := SysGetMem(size);
  83. if assigned(result) then
  84. begin
  85. if size < oldsize then
  86. oldsize := size;
  87. Move(p^,result^,oldsize);
  88. end;
  89. end;
  90. SysFreeMem(p);
  91. end
  92. else
  93. result := p;
  94. end
  95. else
  96. result := SysGetMem(size);
  97. p := result;
  98. end;
  99. function SysGetFPCHeapStatus : TFPCHeapStatus;
  100. begin
  101. FillChar(result,sizeof(result),0);
  102. end;
  103. function SysGetHeapStatus : THeapStatus;
  104. begin
  105. FillChar(result,sizeof(result),0);
  106. end;