sysheap.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 releated routines.
  15. ****************************************************************************}
  16. {Get some memory.
  17. P = Pointer to memory will be returned here.
  18. Size = Number of bytes to get. The size is rounded up to a multiple
  19. of 4096. This is probably not the case on non-intel 386
  20. versions of OS/2.
  21. Flags = One or more of the mfXXXX constants.}
  22. function DosAllocMem(var P:pointer;Size,Flag:cardinal): cardinal; cdecl;
  23. external 'DOSCALLS' index 299;
  24. function DosSetMem(P:pointer;Size,Flag:cardinal): cardinal; cdecl;
  25. external 'DOSCALLS' index 305;
  26. function DosFreeMem (P: pointer): cardinal; cdecl;
  27. external 'DOSCALLS' index 304;
  28. {$IFDEF DUMPGROW}
  29. {$DEFINE EXTDUMPGROW}
  30. {$ENDIF DUMPGROW}
  31. {$IFDEF EXTDUMPGROW}
  32. var
  33. Int_HeapSize: cardinal;
  34. {$ENDIF EXTDUMPGROW}
  35. {function GetHeapSize: longint; assembler;
  36. asm
  37. movl Int_HeapSize, %eax
  38. end ['EAX'];
  39. }
  40. function SysOSAlloc (Size: PtrInt): pointer;
  41. var
  42. P: pointer;
  43. RC: cardinal;
  44. begin
  45. {$IFDEF EXTDUMPGROW}
  46. if Int_HeapSize <> high (cardinal) then
  47. {
  48. if Int_HeapSize = high (cardinal) then
  49. WriteLn ('Trying to allocate first heap of size ', Size)
  50. else
  51. }
  52. WriteLn ('Trying to grow heap by ', Size, ' to ', Int_HeapSize);
  53. {$ENDIF}
  54. if UseHighMem then
  55. RC := DosAllocMem (P, Size, $403)
  56. else
  57. RC := DosAllocMem (P, Size, 3);
  58. if RC = 0 then
  59. begin
  60. {$IFDEF EXTDUMPGROW}
  61. if Int_HeapSize <> high (cardinal) then
  62. WriteLn ('DosAllocMem returned memory at ', cardinal (P));
  63. {$ENDIF}
  64. RC := DosSetMem (P, Size, $410);
  65. if RC = 0 then
  66. begin
  67. {$IFDEF EXTDUMPGROW}
  68. if Int_HeapSize <> high (cardinal) then
  69. WriteLn ('New heap at ', cardinal (P));
  70. {$ENDIF EXTDUMPGROW}
  71. SysOSAlloc := P;
  72. {$IFDEF EXTDUMPGROW}
  73. if Int_HeapSize = high (cardinal) then
  74. Int_HeapSize := Size
  75. else
  76. Inc (Int_HeapSize, Size);
  77. {$ENDIF EXTDUMPGROW}
  78. end
  79. else
  80. begin
  81. {$IFDEF EXTDUMPGROW}
  82. if Int_HeapSize <> high (cardinal) then
  83. begin
  84. WriteLn ('Error ', RC, ' in DosSetMem while trying to commit memory!');
  85. { if Int_HeapSize = high (cardinal) then
  86. WriteLn ('No allocated memory comitted yet!')
  87. else
  88. }
  89. WriteLn ('Total allocated memory is ', Int_HeapSize);
  90. end;
  91. {$ENDIF EXTDUMPGROW}
  92. RC := DosFreeMem (P);
  93. SysOSAlloc := nil;
  94. end;
  95. end
  96. else
  97. begin
  98. SysOSAlloc := nil;
  99. {$IFDEF EXTDUMPGROW}
  100. if Int_HeapSize <> high (cardinal) then
  101. begin
  102. WriteLn ('Error ', RC, ' during additional memory allocation (DosAllocMem)!');
  103. { if Int_HeapSize = high (cardinal) then
  104. WriteLn ('No memory allocated yet!')
  105. else
  106. }
  107. WriteLn ('Total allocated memory is ', Int_HeapSize);
  108. end;
  109. {$ENDIF EXTDUMPGROW}
  110. end;
  111. end;
  112. {$define HAS_SYSOSFREE}
  113. procedure SysOSFree (P: pointer; Size: PtrInt);
  114. var
  115. RC: cardinal;
  116. begin
  117. {$IFDEF EXTDUMPGROW}
  118. WriteLn ('Trying to free memory!');
  119. WriteLn ('Total allocated memory is ', Int_HeapSize);
  120. Dec (Int_HeapSize, Size);
  121. {$ENDIF EXTDUMPGROW}
  122. RC := DosSetMem (P, Size, $20);
  123. if RC = 0 then
  124. begin
  125. RC := DosFreeMem (P);
  126. {$IFDEF EXTDUMPGROW}
  127. if RC <> 0 then
  128. begin
  129. WriteLn ('Error ', RC, ' during memory deallocation (DosFreeMem)!');
  130. WriteLn ('Total allocated memory is ', Int_HeapSize);
  131. end;
  132. {$ENDIF EXTDUMPGROW}
  133. end
  134. {$IFDEF EXTDUMPGROW}
  135. else
  136. begin
  137. WriteLn ('Error ', RC, ' in DosSetMem while trying to decommit memory!');
  138. WriteLn ('Total allocated memory is ', Int_HeapSize);
  139. end;
  140. {$ENDIF EXTDUMPGROW}
  141. end;