sysheap.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. const
  36. IntHeapAllocFlags = $53;
  37. (* mfPag_Commit or mfObj_Tile or mfPag_Write or mfPag_Read *)
  38. {function GetHeapSize: longint; assembler;
  39. asm
  40. movl Int_HeapSize, %eax
  41. end ['EAX'];
  42. }
  43. function SysOSAlloc (Size: PtrInt): pointer;
  44. var
  45. P: pointer;
  46. RC: cardinal;
  47. begin
  48. {$IFDEF EXTDUMPGROW}
  49. if Int_HeapSize <> high (cardinal) then
  50. {
  51. if Int_HeapSize = high (cardinal) then
  52. WriteLn ('Trying to allocate first heap of size ', Size)
  53. else
  54. }
  55. WriteLn ('Trying to grow heap by ', Size, ' to ', Int_HeapSize);
  56. {$ENDIF}
  57. if UseHighMem then
  58. RC := DosAllocMem (P, Size, IntHeapAllocFlags or $400)
  59. else
  60. RC := DosAllocMem (P, Size, IntHeapAllocFlags);
  61. if RC = 0 then
  62. begin
  63. {$IFDEF EXTDUMPGROW}
  64. if Int_HeapSize <> high (cardinal) then
  65. WriteLn ('DosAllocMem returned memory at ', cardinal (P));
  66. {$ENDIF}
  67. SysOSAlloc := P;
  68. {$IFDEF EXTDUMPGROW}
  69. if Int_HeapSize = high (cardinal) then
  70. Int_HeapSize := Size
  71. else
  72. Inc (Int_HeapSize, Size);
  73. {$ENDIF EXTDUMPGROW}
  74. end
  75. else
  76. begin
  77. SysOSAlloc := nil;
  78. {$IFDEF EXTDUMPGROW}
  79. if Int_HeapSize <> high (cardinal) then
  80. begin
  81. WriteLn ('Error ', RC, ' during additional memory allocation (DosAllocMem)!');
  82. { if Int_HeapSize = high (cardinal) then
  83. WriteLn ('No memory allocated yet!')
  84. else
  85. }
  86. WriteLn ('Total allocated memory is ', Int_HeapSize);
  87. end;
  88. {$ENDIF EXTDUMPGROW}
  89. end;
  90. end;
  91. {$define HAS_SYSOSFREE}
  92. procedure SysOSFree (P: pointer; Size: PtrInt);
  93. {$IFDEF EXTDUMPGROW}
  94. var
  95. RC: cardinal;
  96. {$ENDIF EXTDUMPGROW}
  97. begin
  98. {$IFDEF EXTDUMPGROW}
  99. WriteLn ('Trying to free memory!');
  100. WriteLn ('Total allocated memory is ', Int_HeapSize);
  101. Dec (Int_HeapSize, Size);
  102. RC :=
  103. {$ENDIF EXTDUMPGROW}
  104. DosFreeMem (P);
  105. {$IFDEF EXTDUMPGROW}
  106. if RC <> 0 then
  107. begin
  108. WriteLn ('Error ', RC, ' during memory deallocation (DosFreeMem)!');
  109. WriteLn ('Total allocated memory is ', Int_HeapSize);
  110. end;
  111. {$ENDIF EXTDUMPGROW}
  112. end;