sysheap.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2001 by Free Pascal development team
  5. This file implements all the base types and limits required
  6. for a minimal POSIX compliant subset required to port the compiler
  7. to a new OS.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. {*****************************************************************************
  15. Heap Management
  16. *****************************************************************************}
  17. {$ifdef autoHeapRelease}
  18. const HeapInitialMaxBlocks = 32;
  19. type THeapSbrkBlockList = array [1.. HeapInitialMaxBlocks] of pointer;
  20. var HeapSbrkBlockList : ^THeapSbrkBlockList = nil;
  21. HeapSbrkLastUsed : dword = 0;
  22. HeapSbrkAllocated : dword = 0;
  23. HeapSbrkReleased : boolean = false;
  24. { function to allocate size bytes more for the program }
  25. { must return the first address of new data space or nil if fail }
  26. { for netware all allocated blocks are saved to free them at }
  27. { exit (to avoid message "Module did not release xx resources") }
  28. Function SysOSAlloc(size : longint):pointer;
  29. var P2 : POINTER;
  30. i : longint;
  31. Slept : longint;
  32. begin
  33. if HeapSbrkReleased then
  34. begin
  35. _ConsolePrintf ('Error: SysOSFree called after all heap memory was released'#13#10);
  36. exit(nil);
  37. end;
  38. SysOSAlloc := _Alloc (size,HeapAllocResourceTag);
  39. if SysOSAlloc <> nil then begin
  40. if HeapSbrkBlockList = nil then
  41. begin
  42. Pointer (HeapSbrkBlockList) := _Alloc (sizeof (HeapSbrkBlockList^),HeapListAllocResourceTag);
  43. if HeapSbrkBlockList = nil then
  44. begin
  45. _free (SysOSAlloc);
  46. SysOSAlloc := nil;
  47. exit;
  48. end;
  49. fillchar (HeapSbrkBlockList^,sizeof(HeapSbrkBlockList^),0);
  50. HeapSbrkAllocated := HeapInitialMaxBlocks;
  51. end;
  52. if (HeapSbrkLastUsed > 0) then
  53. for i := 1 to HeapSbrkLastUsed do
  54. if (HeapSbrkBlockList^[i] = nil) then
  55. begin // reuse free slot
  56. HeapSbrkBlockList^[i] := SysOSAlloc;
  57. exit;
  58. end;
  59. if (HeapSbrkLastUsed = HeapSbrkAllocated) then
  60. begin { grow }
  61. slept := 0;
  62. p2 := _ReallocSleepOK (HeapSbrkBlockList, (HeapSbrkAllocated + HeapInitialMaxBlocks) * sizeof(pointer),HeapListAllocResourceTag,Slept);
  63. if p2 = nil then // should we better terminate with error ?
  64. begin
  65. _free (SysOSAlloc);
  66. SysOSAlloc := nil;
  67. exit;
  68. end;
  69. HeapSbrkBlockList := p2;
  70. inc (HeapSbrkAllocated, HeapInitialMaxBlocks);
  71. end;
  72. inc (HeapSbrkLastUsed);
  73. HeapSbrkBlockList^[HeapSbrkLastUsed] := SysOSAlloc;
  74. end;
  75. end;
  76. procedure FreeSbrkMem;
  77. var i : longint;
  78. begin
  79. if HeapSbrkBlockList <> nil then
  80. begin
  81. for i := 1 to HeapSbrkLastUsed do
  82. if (HeapSbrkBlockList^[i] <> nil) then
  83. _free (HeapSbrkBlockList^[i]);
  84. _free (HeapSbrkBlockList);
  85. HeapSbrkAllocated := 0;
  86. HeapSbrkLastUsed := 0;
  87. HeapSbrkBlockList := nil;
  88. end;
  89. HeapSbrkReleased := true;
  90. {ReturnResourceTag(HeapAllocResourceTag,1);
  91. ReturnResourceTag(HeapListAllocResourceTag,1); not in netware.imp, seems to be not needed}
  92. end;
  93. {*****************************************************************************
  94. OS Memory allocation / deallocation
  95. ****************************************************************************}
  96. {$define HAS_SYSOSFREE}
  97. procedure SysOSFree(p: pointer; size: ptrint);
  98. var i : longint;
  99. begin
  100. if HeapSbrkReleased then
  101. begin
  102. _ConsolePrintf ('Error: SysOSFree called after all heap memory was released'#13#10);
  103. end else
  104. if (HeapSbrkLastUsed > 0) then
  105. for i := 1 to HeapSbrkLastUsed do
  106. if (HeapSbrkBlockList^[i] = p) then
  107. begin
  108. _free (p);
  109. HeapSbrkBlockList^[i] := nil;
  110. exit;
  111. end;
  112. HandleError (204); // invalid pointer operation
  113. end;
  114. {$else autoHeapRelease}
  115. {$define HAS_SYSOSFREE}
  116. procedure SysOSFree(p: pointer; size: ptrint);
  117. begin
  118. _free (p);
  119. end;
  120. function SysOSAlloc(size: ptrint): pointer;
  121. begin
  122. SysOSAlloc := _Alloc(size,HeapAllocResourceTag);
  123. end;
  124. {$endif autoHeapRelease}
  125. {
  126. $Log$
  127. Revision 1.1 2005-02-06 16:57:18 peter
  128. * threads for go32v2,os,emx,netware
  129. Revision 1.1 2005/02/06 13:06:20 peter
  130. * moved file and dir functions to sysfile/sysdir
  131. * win32 thread in systemunit
  132. }