sysheap.inc 3.7 KB

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