locheap.inc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 local 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 SysLocalGetMem(Size: ptruint): pointer;
  13. begin
  14. result:=NearPointer(LocalAlloc(LMEM_FIXED, Size));
  15. if not ReturnNilIfGrowHeapFails and (result=nil) then
  16. HandleError(203);
  17. end;
  18. function SysLocalFreeMem(Addr: Pointer): ptruint;
  19. begin
  20. if Addr<>nil then
  21. begin
  22. result:=LocalSize(THandle(Addr));
  23. if LocalFree(THandle(Addr))<>0 then
  24. HandleError(204);
  25. end
  26. else
  27. result:=0;
  28. end;
  29. function SysLocalFreeMemSize(Addr: Pointer; Size: Ptruint): ptruint;
  30. begin
  31. result:=SysLocalFreeMem(addr);
  32. end;
  33. function SysLocalAllocMem(size: ptruint): pointer;
  34. begin
  35. result:=NearPointer(LocalAlloc(LMEM_FIXED or LMEM_ZEROINIT, Size));
  36. if not ReturnNilIfGrowHeapFails and (result=nil) then
  37. HandleError(203);
  38. end;
  39. function SysLocalReAllocMem(var p: pointer; size: ptruint):pointer;
  40. begin
  41. if size=0 then
  42. begin
  43. SysLocalFreeMem(p);
  44. result := nil;
  45. end
  46. else if p=nil then
  47. result := SysLocalAllocMem(size)
  48. else
  49. begin
  50. result := NearPointer(LocalReAlloc(THandle(p), size, LMEM_MOVEABLE or LMEM_ZEROINIT));
  51. if not ReturnNilIfGrowHeapFails and (result=nil) then
  52. HandleError(203);
  53. end;
  54. p := result;
  55. end;
  56. function SysLocalMemSize(p: pointer): ptruint;
  57. begin
  58. result:=LocalSize(THandle(p));
  59. end;
  60. function MaxAvail: Word;
  61. begin
  62. result:=LocalCompact(0);
  63. end;
  64. const
  65. LocalHeapMemoryManager: TMemoryManager = (
  66. NeedLock: false; // Obsolete
  67. GetMem: @SysLocalGetMem;
  68. FreeMem: @SysLocalFreeMem;
  69. FreeMemSize: @SysLocalFreeMemSize;
  70. AllocMem: @SysLocalAllocMem;
  71. ReAllocMem: @SysLocalReAllocMem;
  72. MemSize: @SysLocalMemSize;
  73. InitThread: nil;
  74. DoneThread: nil;
  75. RelocateHeap: nil;
  76. GetHeapStatus: nil;
  77. GetFPCHeapStatus: nil;
  78. );