sysheap.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. { function to allocate size bytes more for the program }
  24. { must return the first address of new data space or nil if fail }
  25. { for netware all allocated blocks are saved to free them at }
  26. { exit (to avoid message "Module did not release xx resources") }
  27. Function Sbrk(size : longint):pointer;
  28. var P2 : POINTER;
  29. i : longint;
  30. begin
  31. Sbrk := _malloc (size);
  32. if Sbrk <> nil then begin
  33. if HeapSbrkBlockList = nil then
  34. begin
  35. Pointer (HeapSbrkBlockList) := _malloc (sizeof (HeapSbrkBlockList^));
  36. if HeapSbrkBlockList = nil then
  37. begin
  38. _free (Sbrk);
  39. Sbrk := nil;
  40. exit;
  41. end;
  42. fillchar (HeapSbrkBlockList^,sizeof(HeapSbrkBlockList^),0);
  43. HeapSbrkAllocated := HeapInitialMaxBlocks;
  44. end;
  45. if (HeapSbrkLastUsed > 0) then
  46. for i := 1 to HeapSbrkLastUsed do
  47. if (HeapSbrkBlockList^[i] = nil) then
  48. begin // reuse free slot
  49. HeapSbrkBlockList^[i] := Sbrk;
  50. exit;
  51. end;
  52. if (HeapSbrkLastUsed = HeapSbrkAllocated) then
  53. begin { grow }
  54. p2 := _realloc (HeapSbrkBlockList, (HeapSbrkAllocated + HeapInitialMaxBlocks) * sizeof(pointer));
  55. if p2 = nil then // should we better terminate with error ?
  56. begin
  57. _free (Sbrk);
  58. Sbrk := nil;
  59. exit;
  60. end;
  61. HeapSbrkBlockList := p2;
  62. inc (HeapSbrkAllocated, HeapInitialMaxBlocks);
  63. end;
  64. inc (HeapSbrkLastUsed);
  65. HeapSbrkBlockList^[HeapSbrkLastUsed] := Sbrk;
  66. end;
  67. end;
  68. procedure FreeSbrkMem;
  69. var i : longint;
  70. begin
  71. if HeapSbrkBlockList <> nil then
  72. begin
  73. for i := 1 to HeapSbrkLastUsed do
  74. if (HeapSbrkBlockList^[i] <> nil) then
  75. _free (HeapSbrkBlockList^[i]);
  76. _free (HeapSbrkBlockList);
  77. HeapSbrkAllocated := 0;
  78. HeapSbrkLastUsed := 0;
  79. HeapSbrkBlockList := nil;
  80. end;
  81. end;
  82. {*****************************************************************************
  83. OS Memory allocation / deallocation
  84. ****************************************************************************}
  85. function SysOSAlloc(size: ptrint): pointer;
  86. begin
  87. result := sbrk(size);
  88. end;
  89. {$define HAS_SYSOSFREE}
  90. procedure SysOSFree(p: pointer; size: ptrint);
  91. var i : longint;
  92. begin
  93. //fpmunmap(p, size);
  94. if (HeapSbrkLastUsed > 0) then
  95. for i := 1 to HeapSbrkLastUsed do
  96. if (HeapSbrkBlockList^[i] = p) then
  97. begin
  98. _free (p);
  99. HeapSbrkBlockList^[i] := nil;
  100. exit;
  101. end;
  102. HandleError (204); // invalid pointer operation
  103. end;
  104. {$else autoHeapRelease}
  105. {$define HAS_SYSOSFREE}
  106. procedure SysOSFree(p: pointer; size: ptrint);
  107. begin
  108. _free (p);
  109. end;
  110. function SysOSAlloc(size: ptrint): pointer;
  111. begin
  112. SysOSAlloc := _malloc (size);
  113. end;
  114. {$endif autoHeapRelease}
  115. {
  116. $Log$
  117. Revision 1.2 2005-02-14 17:13:30 peter
  118. * truncate log
  119. Revision 1.1 2005/02/06 16:57:18 peter
  120. * threads for go32v2,os,emx,netware
  121. Revision 1.1 2005/02/06 13:06:20 peter
  122. * moved file and dir functions to sysfile/sysdir
  123. * win32 thread in systemunit
  124. }