cmem.pp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999 by Michael Van Canneyt, member of the
  4. Free Pascal development team
  5. Implements a memory manager that uses the C memory management.
  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. unit cmem;
  13. interface
  14. Const
  15. {$if defined(win32)}
  16. LibName = 'msvcrt';
  17. {$elseif defined(win64)}
  18. LibName = 'msvcrt';
  19. {$elseif defined(wince)}
  20. LibName = 'coredll';
  21. {$elseif defined(netware)}
  22. LibName = 'clib';
  23. {$elseif defined(netwlibc)}
  24. LibName = 'libc';
  25. {$elseif defined(macos)}
  26. LibName = 'StdCLib';
  27. {$else}
  28. LibName = 'c';
  29. {$endif}
  30. Function Malloc (Size : ptruint) : Pointer; {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'malloc';
  31. Procedure Free (P : pointer); {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'free';
  32. function ReAlloc (P : Pointer; Size : ptruint) : pointer; {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'realloc';
  33. Function CAlloc (unitSize,UnitCount : ptruint) : pointer; {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'calloc';
  34. implementation
  35. Function CGetMem (Size : ptruint) : Pointer;
  36. begin
  37. CGetMem:=Malloc(Size+sizeof(ptruint));
  38. if (CGetMem <> nil) then
  39. begin
  40. Pptruint(CGetMem)^ := size;
  41. inc(CGetMem,sizeof(ptruint));
  42. end;
  43. end;
  44. Function CFreeMem (P : pointer) : ptruint;
  45. begin
  46. if (p <> nil) then
  47. dec(p,sizeof(ptruint));
  48. Free(P);
  49. CFreeMem:=0;
  50. end;
  51. Function CFreeMemSize(p:pointer;Size:ptruint):ptruint;
  52. begin
  53. if size<=0 then
  54. begin
  55. if size<0 then
  56. runerror(204);
  57. exit;
  58. end;
  59. if (p <> nil) then
  60. begin
  61. if (size <> Pptruint(p-sizeof(ptruint))^) then
  62. runerror(204);
  63. end;
  64. CFreeMemSize:=CFreeMem(P);
  65. end;
  66. Function CAllocMem(Size : ptruint) : Pointer;
  67. begin
  68. CAllocMem:=calloc(Size+sizeof(ptruint),1);
  69. if (CAllocMem <> nil) then
  70. begin
  71. Pptruint(CAllocMem)^ := size;
  72. inc(CAllocMem,sizeof(ptruint));
  73. end;
  74. end;
  75. Function CReAllocMem (var p:pointer;Size:ptruint):Pointer;
  76. begin
  77. if size=0 then
  78. begin
  79. if p<>nil then
  80. begin
  81. dec(p,sizeof(ptruint));
  82. free(p);
  83. p:=nil;
  84. end;
  85. end
  86. else
  87. begin
  88. inc(size,sizeof(ptruint));
  89. if p=nil then
  90. p:=malloc(Size)
  91. else
  92. begin
  93. dec(p,sizeof(ptruint));
  94. p:=realloc(p,size);
  95. end;
  96. if (p <> nil) then
  97. begin
  98. Pptruint(p)^ := size-sizeof(ptruint);
  99. inc(p,sizeof(ptruint));
  100. end;
  101. end;
  102. CReAllocMem:=p;
  103. end;
  104. Function CMemSize (p:pointer): ptruint;
  105. begin
  106. CMemSize:=Pptruint(p-sizeof(ptruint))^;
  107. end;
  108. function CGetHeapStatus:THeapStatus;
  109. var res: THeapStatus;
  110. begin
  111. fillchar(res,sizeof(res),0);
  112. CGetHeapStatus:=res;
  113. end;
  114. function CGetFPCHeapStatus:TFPCHeapStatus;
  115. begin
  116. fillchar(CGetFPCHeapStatus,sizeof(CGetFPCHeapStatus),0);
  117. end;
  118. Const
  119. CMemoryManager : TMemoryManager =
  120. (
  121. NeedLock : false;
  122. GetMem : @CGetmem;
  123. FreeMem : @CFreeMem;
  124. FreememSize : @CFreememSize;
  125. AllocMem : @CAllocMem;
  126. ReallocMem : @CReAllocMem;
  127. MemSize : @CMemSize;
  128. InitThread : nil;
  129. DoneThread : nil;
  130. RelocateHeap : nil;
  131. GetHeapStatus : @CGetHeapStatus;
  132. GetFPCHeapStatus: @CGetFPCHeapStatus;
  133. );
  134. Var
  135. OldMemoryManager : TMemoryManager;
  136. Initialization
  137. GetMemoryManager (OldMemoryManager);
  138. SetMemoryManager (CmemoryManager);
  139. Finalization
  140. SetMemoryManager (OldMemoryManager);
  141. end.