gcmem.pp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. unit gcmem;
  2. interface
  3. {$IFDEF FPC}
  4. {$PACKRECORDS C}
  5. {$ENDIF}
  6. Const
  7. LibName = 'gc';
  8. type
  9. ptrdiff_t = longint;
  10. size_t = dword;
  11. wchar_t = longint;
  12. GC_PTR = pointer;
  13. GC_word = dword;
  14. GC_signed_word = longint;
  15. // procedure GC_init;cdecl; external LibName name 'GC_init';
  16. // not needed
  17. Function Malloc(Size : size_t) : Pointer; cdecl; external LibName name 'GC_malloc';
  18. Procedure Free(P : pointer); cdecl; external LibName name 'GC_free';
  19. function ReAlloc(P : Pointer; Size : size_t) : pointer; cdecl; external LibName name 'GC_realloc';
  20. Function Calloc(unitSize,UnitCount : size_t) : pointer;
  21. implementation
  22. Function Calloc(unitSize,UnitCount : size_t) : pointer;
  23. var p:pointer;
  24. begin
  25. p:=Malloc(unitSize*UnitCount);
  26. if p<>nil then FillChar(p^, unitSize*UnitCount,0); //not needed
  27. //GC_malloc seems to clear memory
  28. Calloc:=p;
  29. end;
  30. Function CGetMem (Size : ptruint) : Pointer;
  31. begin
  32. CGetMem:=Malloc(size_t(Size+sizeof(ptruint)));
  33. if (CGetMem <> nil) then
  34. begin
  35. Pptruint(CGetMem)^ := size;
  36. inc(CGetMem,sizeof(ptruint));
  37. end;
  38. end;
  39. Function CFreeMem (P : pointer) : ptruint;
  40. begin
  41. if (p <> nil) then
  42. dec(p,sizeof(ptruint));
  43. Free(P);
  44. CFreeMem:=0;
  45. end;
  46. Function CFreeMemSize(p:pointer;Size:ptruint):ptruint;
  47. begin
  48. if size<=0 then
  49. exit;
  50. if (p <> nil) then
  51. begin
  52. if (size <> Pptruint(p-sizeof(ptruint))^) then
  53. runerror(204);
  54. end;
  55. CFreeMemSize:=CFreeMem(P);
  56. end;
  57. Function CAllocMem(Size : ptruint) : Pointer;
  58. begin
  59. CAllocMem:=calloc(size_t(Size+sizeof(ptruint)),size_t(1));
  60. if (CAllocMem <> nil) then
  61. begin
  62. Pptruint(CAllocMem)^ := size;
  63. inc(CAllocMem,sizeof(ptruint));
  64. end;
  65. end;
  66. Function CReAllocMem (var p:pointer;Size:ptruint):Pointer;
  67. begin
  68. if size=0 then
  69. begin
  70. if p<>nil then
  71. begin
  72. dec(p,sizeof(ptruint));
  73. free(p);
  74. p:=nil;
  75. end;
  76. end
  77. else
  78. begin
  79. inc(size,sizeof(ptruint));
  80. if p=nil then
  81. p:=malloc(size_t(Size))
  82. else
  83. begin
  84. dec(p,sizeof(ptruint));
  85. p:=realloc(p,size_t(size));
  86. end;
  87. if (p <> nil) then
  88. begin
  89. Pptruint(p)^ := size-sizeof(ptruint);
  90. inc(p,sizeof(ptruint));
  91. end;
  92. end;
  93. CReAllocMem:=p;
  94. end;
  95. Function CMemSize (p:pointer): ptruint;
  96. begin
  97. CMemSize:=Pptruint(p-sizeof(ptruint))^;
  98. end;
  99. function CGetHeapStatus:THeapStatus;
  100. var res: THeapStatus;
  101. begin
  102. fillchar(res,sizeof(res),0);
  103. CGetHeapStatus:=res;
  104. end;
  105. function CGetFPCHeapStatus:TFPCHeapStatus;
  106. begin
  107. fillchar(CGetFPCHeapStatus,sizeof(CGetFPCHeapStatus),0);
  108. end;
  109. Const
  110. CMemoryManager : TMemoryManager =
  111. (
  112. NeedLock : false;
  113. GetMem : @CGetmem;
  114. FreeMem : @CFreeMem;
  115. FreememSize : @CFreememSize;
  116. AllocMem : @CAllocMem;
  117. ReallocMem : @CReAllocMem;
  118. MemSize : @CMemSize;
  119. InitThread : nil;
  120. DoneThread : nil;
  121. RelocateHeap : nil;
  122. GetHeapStatus : @CGetHeapStatus;
  123. GetFPCHeapStatus: @CGetFPCHeapStatus;
  124. );
  125. Var
  126. OldMemoryManager : TMemoryManager;
  127. Initialization
  128. GetMemoryManager (OldMemoryManager);
  129. SetMemoryManager (CmemoryManager);
  130. Finalization
  131. SetMemoryManager (OldMemoryManager);
  132. end.