sysheap.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. {$ifdef autoHeapRelease}
  17. const HeapInitialMaxBlocks = 32;
  18. type THeapSbrkBlockList = array [1.. HeapInitialMaxBlocks] of pointer;
  19. var HeapSbrkBlockList : ^THeapSbrkBlockList = nil;
  20. HeapSbrkLastUsed : dword = 0;
  21. HeapSbrkAllocated : dword = 0;
  22. HeapSbrkReleased : boolean = false;
  23. { function to allocate size bytes more for the program }
  24. { must return the first address of new data space or nil if fail }
  25. { for netware all allocated blocks are saved to free them at }
  26. { exit (to avoid message "Module did not release xx resources") }
  27. Function SysOSAlloc(size : longint):pointer;
  28. var P2 : POINTER;
  29. i : longint;
  30. Slept : longint;
  31. begin
  32. if HeapSbrkReleased then
  33. begin
  34. _ConsolePrintf ('Error: SysOSFree called after all heap memory was released'#13#10);
  35. exit(nil);
  36. end;
  37. SysOSAlloc := _Alloc (size,HeapAllocResourceTag);
  38. if SysOSAlloc <> nil then begin
  39. if HeapSbrkBlockList = nil then
  40. begin
  41. Pointer (HeapSbrkBlockList) := _Alloc (sizeof (HeapSbrkBlockList^),HeapListAllocResourceTag);
  42. if HeapSbrkBlockList = nil then
  43. begin
  44. _free (SysOSAlloc);
  45. SysOSAlloc := nil;
  46. exit;
  47. end;
  48. fillchar (HeapSbrkBlockList^,sizeof(HeapSbrkBlockList^),0);
  49. HeapSbrkAllocated := HeapInitialMaxBlocks;
  50. end;
  51. if (HeapSbrkLastUsed > 0) then
  52. for i := 1 to HeapSbrkLastUsed do
  53. if (HeapSbrkBlockList^[i] = nil) then
  54. begin // reuse free slot
  55. HeapSbrkBlockList^[i] := SysOSAlloc;
  56. exit;
  57. end;
  58. if (HeapSbrkLastUsed = HeapSbrkAllocated) then
  59. begin { grow }
  60. slept := 0;
  61. p2 := _ReallocSleepOK (HeapSbrkBlockList, (HeapSbrkAllocated + HeapInitialMaxBlocks) * sizeof(pointer),HeapListAllocResourceTag,Slept);
  62. if p2 = nil then // should we better terminate with error ?
  63. begin
  64. _free (SysOSAlloc);
  65. SysOSAlloc := nil;
  66. exit;
  67. end;
  68. HeapSbrkBlockList := p2;
  69. inc (HeapSbrkAllocated, HeapInitialMaxBlocks);
  70. end;
  71. inc (HeapSbrkLastUsed);
  72. HeapSbrkBlockList^[HeapSbrkLastUsed] := SysOSAlloc;
  73. end;
  74. end;
  75. procedure FreeSbrkMem;
  76. var i : longint;
  77. begin
  78. if HeapSbrkBlockList <> nil then
  79. begin
  80. for i := 1 to HeapSbrkLastUsed do
  81. if (HeapSbrkBlockList^[i] <> nil) then
  82. _free (HeapSbrkBlockList^[i]);
  83. _free (HeapSbrkBlockList);
  84. HeapSbrkAllocated := 0;
  85. HeapSbrkLastUsed := 0;
  86. HeapSbrkBlockList := nil;
  87. end;
  88. HeapSbrkReleased := true;
  89. {ReturnResourceTag(HeapAllocResourceTag,1);
  90. ReturnResourceTag(HeapListAllocResourceTag,1); not in netware.imp, seems to be not needed}
  91. end;
  92. {*****************************************************************************
  93. OS Memory allocation / deallocation
  94. ****************************************************************************}
  95. {$define HAS_SYSOSFREE}
  96. procedure SysOSFree(p: pointer; size: ptrint);
  97. var i : longint;
  98. begin
  99. if HeapSbrkReleased then
  100. begin
  101. _ConsolePrintf ('Error: SysOSFree called after all heap memory was released'#13#10);
  102. end else
  103. if (HeapSbrkLastUsed > 0) then
  104. for i := 1 to HeapSbrkLastUsed do
  105. if (HeapSbrkBlockList^[i] = p) then
  106. begin
  107. _free (p);
  108. HeapSbrkBlockList^[i] := nil;
  109. exit;
  110. end;
  111. HandleError (204); // invalid pointer operation
  112. end;
  113. {$else autoHeapRelease}
  114. {$define HAS_SYSOSFREE}
  115. procedure SysOSFree(p: pointer; size: ptrint);
  116. begin
  117. _free (p);
  118. end;
  119. function SysOSAlloc(size: ptrint): pointer;
  120. begin
  121. SysOSAlloc := _Alloc(size,HeapAllocResourceTag);
  122. end;
  123. {$endif autoHeapRelease}