sysheap.inc 4.4 KB

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