sysheap.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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: ptruint): 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. RC := DosAllocMem (P, Size, HeapAllocFlags);
  55. if RC = 0 then
  56. begin
  57. {$IFDEF EXTDUMPGROW}
  58. if Int_HeapSize <> high (cardinal) then
  59. WriteLn ('DosAllocMem returned memory at ', cardinal (P));
  60. {$ENDIF}
  61. SysOSAlloc := P;
  62. {$IFDEF EXTDUMPGROW}
  63. if Int_HeapSize = high (cardinal) then
  64. Int_HeapSize := Size
  65. else
  66. Inc (Int_HeapSize, Size);
  67. {$ENDIF EXTDUMPGROW}
  68. end
  69. else
  70. begin
  71. SysOSAlloc := nil;
  72. {$IFDEF EXTDUMPGROW}
  73. if Int_HeapSize <> high (cardinal) then
  74. begin
  75. WriteLn ('Error ', RC, ' during additional memory allocation (DosAllocMem)!');
  76. { if Int_HeapSize = high (cardinal) then
  77. WriteLn ('No memory allocated yet!')
  78. else
  79. }
  80. WriteLn ('Total allocated memory is ', Int_HeapSize);
  81. end;
  82. {$ENDIF EXTDUMPGROW}
  83. end;
  84. end;
  85. {$define HAS_SYSOSFREE}
  86. procedure SysOSFree (P: pointer; Size: ptruint);
  87. {$IFDEF EXTDUMPGROW}
  88. var
  89. RC: cardinal;
  90. {$ENDIF EXTDUMPGROW}
  91. begin
  92. {$IFDEF EXTDUMPGROW}
  93. WriteLn ('Trying to free memory!');
  94. WriteLn ('Total allocated memory is ', Int_HeapSize);
  95. Dec (Int_HeapSize, Size);
  96. RC :=
  97. {$ENDIF EXTDUMPGROW}
  98. DosFreeMem (P);
  99. {$IFDEF EXTDUMPGROW}
  100. if RC <> 0 then
  101. begin
  102. WriteLn ('Error ', RC, ' during memory deallocation (DosFreeMem)!');
  103. WriteLn ('Total allocated memory is ', Int_HeapSize);
  104. end;
  105. {$ENDIF EXTDUMPGROW}
  106. end;
  107. function ReadUseHighMem: boolean;
  108. begin
  109. ReadUseHighMem := HeapAllocFlags and $400 = $400;
  110. end;
  111. procedure WriteUseHighMem (B: boolean);
  112. begin
  113. if B then
  114. HeapAllocFlags := HeapAllocFlags or $400
  115. else
  116. HeapAllocFlags := HeapAllocFlags and not ($400);
  117. end;