cmem.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. {$ifndef ver1_0}
  16. {$if defined(win32)}
  17. LibName = 'msvcrt';
  18. {$elseif defined(netware)}
  19. LibName = 'clib';
  20. {$elseif defined(netwlibc)}
  21. LibName = 'libc';
  22. {$elseif defined(macos)}
  23. LibName = 'StdCLib';
  24. {$else}
  25. LibName = 'c';
  26. {$endif}
  27. {$else}
  28. {$ifndef win32}
  29. {$ifdef netware}
  30. LibName = 'clib';
  31. {$else}
  32. {$ifdef netwlibc}
  33. LibName = 'libc';
  34. {$else}
  35. {$ifdef macos}
  36. LibName = 'StdCLib';
  37. {$else}
  38. LibName = 'c';
  39. {$endif macos}
  40. {$endif netwlibc}
  41. {$endif}
  42. {$else}
  43. LibName = 'msvcrt';
  44. {$endif}
  45. {$endif}
  46. Function Malloc (Size : ptrint) : Pointer; {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'malloc';
  47. Procedure Free (P : pointer); {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'free';
  48. function ReAlloc (P : Pointer; Size : ptrint) : pointer; {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'realloc';
  49. Function CAlloc (unitSize,UnitCount : ptrint) : pointer; {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'calloc';
  50. implementation
  51. type
  52. pptrint = ^ptrint;
  53. Function CGetMem (Size : ptrint) : Pointer;
  54. begin
  55. CGetMem:=Malloc(Size+sizeof(ptrint));
  56. if (CGetMem <> nil) then
  57. begin
  58. pptrint(CGetMem)^ := size;
  59. inc(CGetMem,sizeof(ptrint));
  60. end;
  61. end;
  62. Function CFreeMem (P : pointer) : ptrint;
  63. begin
  64. if (p <> nil) then
  65. dec(p,sizeof(ptrint));
  66. Free(P);
  67. CFreeMem:=0;
  68. end;
  69. Function CFreeMemSize(p:pointer;Size:ptrint):ptrint;
  70. begin
  71. if size<=0 then
  72. begin
  73. if size<0 then
  74. runerror(204);
  75. exit;
  76. end;
  77. if (p <> nil) then
  78. begin
  79. if (size <> pptrint(p-sizeof(ptrint))^) then
  80. runerror(204);
  81. end;
  82. CFreeMemSize:=CFreeMem(P);
  83. end;
  84. Function CAllocMem(Size : ptrint) : Pointer;
  85. begin
  86. CAllocMem:=calloc(Size+sizeof(ptrint),1);
  87. if (CAllocMem <> nil) then
  88. begin
  89. pptrint(CAllocMem)^ := size;
  90. inc(CAllocMem,sizeof(ptrint));
  91. end;
  92. end;
  93. Function CReAllocMem (var p:pointer;Size:ptrint):Pointer;
  94. begin
  95. if size=0 then
  96. begin
  97. if p<>nil then
  98. begin
  99. dec(p,sizeof(ptrint));
  100. free(p);
  101. p:=nil;
  102. end;
  103. end
  104. else
  105. begin
  106. inc(size,sizeof(ptrint));
  107. if p=nil then
  108. p:=malloc(Size)
  109. else
  110. begin
  111. dec(p,sizeof(ptrint));
  112. p:=realloc(p,size);
  113. end;
  114. if (p <> nil) then
  115. begin
  116. pptrint(p)^ := size-sizeof(ptrint);
  117. inc(p,sizeof(ptrint));
  118. end;
  119. end;
  120. CReAllocMem:=p;
  121. end;
  122. Function CMemSize (p:pointer): ptrint;
  123. begin
  124. CMemSize:=pptrint(p-sizeof(ptrint))^;
  125. end;
  126. {$ifdef HASGETFPCHEAPSTATUS}
  127. function CGetHeapStatus:THeapStatus;
  128. var res: THeapStatus;
  129. begin
  130. fillchar(res,sizeof(res),0);
  131. CGetHeapStatus:=res;
  132. end;
  133. function CGetFPCHeapStatus:TFPCHeapStatus;
  134. begin
  135. fillchar(CGetFPCHeapStatus,sizeof(CGetFPCHeapStatus),0);
  136. end;
  137. {$else HASGETFPCHEAPSTATUS}
  138. Procedure CGetHeapStatus(var status:THeapStatus);
  139. begin
  140. fillchar(status,sizeof(status),0);
  141. end;
  142. {$endif HASGETFPCHEAPSTATUS}
  143. Const
  144. CMemoryManager : TMemoryManager =
  145. (
  146. NeedLock : false;
  147. GetMem : @CGetmem;
  148. FreeMem : @CFreeMem;
  149. FreememSize : @CFreememSize;
  150. AllocMem : @CAllocMem;
  151. ReallocMem : @CReAllocMem;
  152. MemSize : @CMemSize;
  153. GetHeapStatus : @CGetHeapStatus;
  154. {$ifdef HASGETFPCHEAPSTATUS}
  155. GetFPCHeapStatus: @CGetFPCHeapStatus;
  156. {$endif HASGETFPCHEAPSTATUS}
  157. );
  158. Var
  159. OldMemoryManager : TMemoryManager;
  160. Initialization
  161. GetMemoryManager (OldMemoryManager);
  162. SetMemoryManager (CmemoryManager);
  163. Finalization
  164. SetMemoryManager (OldMemoryManager);
  165. end.