glbheap.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2015 by the Free Pascal development team
  4. This file implements heap management for 16-bit Windows
  5. using the Windows global heap.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. function SysGlobalGetMem(Size: ptruint): pointer;
  13. var
  14. hglob: HGLOBAL;
  15. begin
  16. hglob:=GlobalAlloc(HeapAllocFlags, Size);
  17. if hglob=0 then
  18. if ReturnNilIfGrowHeapFails then
  19. begin
  20. result:=nil;
  21. exit;
  22. end
  23. else
  24. HandleError(203);
  25. result:=GlobalLock(hglob);
  26. if result=nil then
  27. HandleError(204);
  28. end;
  29. function SysGlobalFreeMem(Addr: Pointer): ptruint;
  30. var
  31. hglob: HGLOBAL;
  32. begin
  33. if Addr<>nil then
  34. begin
  35. hglob:=HGLOBAL(GlobalHandle(Seg(Addr^)));
  36. if hglob=0 then
  37. HandleError(204);
  38. result:=GlobalSize(hglob);
  39. if GlobalUnlock(hglob) then
  40. HandleError(204);
  41. if GlobalFree(hglob)<>0 then
  42. HandleError(204);
  43. end
  44. else
  45. result:=0;
  46. end;
  47. function SysGlobalFreeMemSize(Addr: Pointer; Size: Ptruint): ptruint;
  48. begin
  49. result:=SysGlobalFreeMem(addr);
  50. end;
  51. function SysGlobalAllocMem(size: ptruint): pointer;
  52. var
  53. hglob: HGLOBAL;
  54. begin
  55. hglob:=GlobalAlloc(HeapAllocFlags or GMEM_ZEROINIT, Size);
  56. if hglob=0 then
  57. if ReturnNilIfGrowHeapFails then
  58. begin
  59. result:=nil;
  60. exit;
  61. end
  62. else
  63. HandleError(203);
  64. result:=GlobalLock(hglob);
  65. if result=nil then
  66. HandleError(204);
  67. end;
  68. function SysGlobalReAllocMem(var p: pointer; size: ptruint):pointer;
  69. var
  70. hglob: HGLOBAL;
  71. begin
  72. if size=0 then
  73. begin
  74. SysGlobalFreeMem(p);
  75. result := nil;
  76. end
  77. else if p=nil then
  78. result := SysGlobalAllocMem(size)
  79. else
  80. begin
  81. hglob:=HGLOBAL(GlobalHandle(Seg(p^)));
  82. if hglob=0 then
  83. HandleError(204);
  84. if GlobalUnlock(hglob) then
  85. HandleError(204);
  86. hglob:=GlobalReAlloc(hglob,size,HeapAllocFlags or GMEM_ZEROINIT);
  87. if hglob=0 then
  88. if ReturnNilIfGrowHeapFails then
  89. begin
  90. result:=nil;
  91. p:=nil;
  92. exit;
  93. end
  94. else
  95. HandleError(203);
  96. result:=GlobalLock(hglob);
  97. if result=nil then
  98. HandleError(204);
  99. end;
  100. p := result;
  101. end;
  102. function SysGlobalMemSize(p: pointer): ptruint;
  103. var
  104. hglob: HGLOBAL;
  105. begin
  106. hglob:=HGLOBAL(GlobalHandle(Seg(p^)));
  107. if hglob=0 then
  108. HandleError(204);
  109. result:=GlobalSize(hglob);
  110. end;
  111. const
  112. GlobalHeapMemoryManager: TMemoryManager = (
  113. NeedLock: false; // Obsolete
  114. GetMem: @SysGlobalGetMem;
  115. FreeMem: @SysGlobalFreeMem;
  116. FreeMemSize: @SysGlobalFreeMemSize;
  117. AllocMem: @SysGlobalAllocMem;
  118. ReAllocMem: @SysGlobalReAllocMem;
  119. MemSize: @SysGlobalMemSize;
  120. InitThread: nil;
  121. DoneThread: nil;
  122. RelocateHeap: nil;
  123. GetHeapStatus: nil;
  124. GetFPCHeapStatus: nil;
  125. );