osheap.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 SysAllocMem(size: ptruint): pointer;
  61. begin
  62. result := SysGetMem(size);
  63. if assigned(result) then
  64. FillChar(result^,SysMemSize(result),0);
  65. end;
  66. function SysReAllocMem(var p: pointer; size: ptruint):pointer;
  67. var
  68. oldsize: ptruint;
  69. begin
  70. result := nil;
  71. if assigned(p) then
  72. begin
  73. oldsize := SysMemSize(p);
  74. if size <> oldsize then
  75. begin
  76. if size > 0 then
  77. begin
  78. result := SysGetMem(size);
  79. if assigned(result) then
  80. begin
  81. if size < oldsize then
  82. oldsize := size;
  83. Move(p^,result^,oldsize);
  84. end;
  85. end;
  86. SysFreeMem(p);
  87. end
  88. else
  89. result := p;
  90. end
  91. else
  92. result := SysGetMem(size);
  93. p := result;
  94. end;
  95. function SysGetFPCHeapStatus : TFPCHeapStatus;
  96. begin
  97. FillChar(result,sizeof(result),0);
  98. end;
  99. function SysGetHeapStatus : THeapStatus;
  100. begin
  101. FillChar(result,sizeof(result),0);
  102. end;