cmem.pp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999 by Michael Van Canneyt, member of the
  5. Free Pascal development team
  6. Implements a memory manager that uses the C memory management.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. unit cmem;
  14. interface
  15. Const
  16. {$ifndef ver1_0}
  17. {$if defined(win32)}
  18. LibName = 'msvcrt';
  19. {$elseif defined(netware)}
  20. LibName = 'clib';
  21. {$elseif defined(netwlibc)}
  22. LibName = 'libc';
  23. {$elseif defined(macos)}
  24. LibName = 'StdCLib';
  25. {$else}
  26. LibName = 'c';
  27. {$endif}
  28. {$else}
  29. {$ifndef win32}
  30. {$ifdef netware}
  31. LibName = 'clib';
  32. {$else}
  33. {$ifdef netwlibc}
  34. LibName = 'libc';
  35. {$else}
  36. {$ifdef macos}
  37. LibName = 'StdCLib';
  38. {$else}
  39. LibName = 'c';
  40. {$endif macos}
  41. {$endif netwlibc}
  42. {$endif}
  43. {$else}
  44. LibName = 'msvcrt';
  45. {$endif}
  46. {$endif}
  47. Function Malloc (Size : ptrint) : Pointer; {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'malloc';
  48. Procedure Free (P : pointer); {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'free';
  49. function ReAlloc (P : Pointer; Size : ptrint) : pointer; {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'realloc';
  50. Function CAlloc (unitSize,UnitCount : ptrint) : pointer; {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'calloc';
  51. implementation
  52. type
  53. pptrint = ^ptrint;
  54. Function CGetMem (Size : ptrint) : Pointer;
  55. begin
  56. CGetMem:=Malloc(Size+sizeof(ptrint));
  57. if (CGetMem <> nil) then
  58. begin
  59. pptrint(CGetMem)^ := size;
  60. inc(CGetMem,sizeof(ptrint));
  61. end;
  62. end;
  63. Function CFreeMem (P : pointer) : ptrint;
  64. begin
  65. if (p <> nil) then
  66. dec(p,sizeof(ptrint));
  67. Free(P);
  68. CFreeMem:=0;
  69. end;
  70. Function CFreeMemSize(p:pointer;Size:ptrint):ptrint;
  71. begin
  72. if size<=0 then
  73. begin
  74. if size<0 then
  75. runerror(204);
  76. exit;
  77. end;
  78. if (p <> nil) then
  79. begin
  80. if (size <> pptrint(p-sizeof(ptrint))^) then
  81. runerror(204);
  82. end;
  83. CFreeMemSize:=CFreeMem(P);
  84. end;
  85. Function CAllocMem(Size : ptrint) : Pointer;
  86. begin
  87. CAllocMem:=calloc(Size+sizeof(ptrint),1);
  88. if (CAllocMem <> nil) then
  89. begin
  90. pptrint(CAllocMem)^ := size;
  91. inc(CAllocMem,sizeof(ptrint));
  92. end;
  93. end;
  94. Function CReAllocMem (var p:pointer;Size:ptrint):Pointer;
  95. begin
  96. if size=0 then
  97. begin
  98. if p<>nil then
  99. begin
  100. dec(p,sizeof(ptrint));
  101. free(p);
  102. p:=nil;
  103. end;
  104. end
  105. else
  106. begin
  107. inc(size,sizeof(ptrint));
  108. if p=nil then
  109. p:=malloc(Size)
  110. else
  111. begin
  112. dec(p,sizeof(ptrint));
  113. p:=realloc(p,size);
  114. end;
  115. if (p <> nil) then
  116. begin
  117. pptrint(p)^ := size-sizeof(ptrint);
  118. inc(p,sizeof(ptrint));
  119. end;
  120. end;
  121. CReAllocMem:=p;
  122. end;
  123. Function CMemSize (p:pointer): ptrint;
  124. begin
  125. CMemSize:=pptrint(p-sizeof(ptrint))^;
  126. end;
  127. Procedure CGetHeapStatus(var status:THeapStatus);
  128. begin
  129. fillchar(status,sizeof(status),0);
  130. end;
  131. Const
  132. CMemoryManager : TMemoryManager =
  133. (
  134. NeedLock : false;
  135. GetMem : @CGetmem;
  136. FreeMem : @CFreeMem;
  137. FreememSize : @CFreememSize;
  138. AllocMem : @CAllocMem;
  139. ReallocMem : @CReAllocMem;
  140. MemSize : @CMemSize;
  141. GetHeapStatus : @CGetHeapStatus;
  142. );
  143. Var
  144. OldMemoryManager : TMemoryManager;
  145. Initialization
  146. GetMemoryManager (OldMemoryManager);
  147. SetMemoryManager (CmemoryManager);
  148. Finalization
  149. SetMemoryManager (OldMemoryManager);
  150. end.
  151. {
  152. $Log$
  153. Revision 1.11 2004-11-22 19:34:58 peter
  154. * GetHeapStatus added, removed MaxAvail,MemAvail,HeapSize
  155. Revision 1.10 2004/11/21 21:14:14 peter
  156. * Freemem(p,0) does nothing
  157. Revision 1.9 2004/09/19 19:04:11 olle
  158. * added $if defined(..) for 2.0.
  159. Revision 1.8 2004/09/19 08:16:03 olle
  160. * reverted to $ifdef style, so 1.0.x can eat it.
  161. Revision 1.7 2004/09/18 08:40:26 olle
  162. + added support for macos
  163. Revision 1.6 2004/09/15 20:37:42 armin
  164. * add support for netware libc
  165. Revision 1.5 2004/05/05 13:00:43 jonas
  166. * fixed reallocmem (bug noted by Vincent Snijders)
  167. Revision 1.4 2004/03/23 22:35:20 peter
  168. * dec ptr before free in reallocmem
  169. Revision 1.3 2004/03/17 12:50:53 michael
  170. * Fixed: Macavail -> CMaxAvail (from Marc Weustinc)
  171. Revision 1.2 2004/03/16 15:25:16 peter
  172. * adaption to compile with 1.0.x in new rtl
  173. Revision 1.1 2004/03/15 21:48:26 peter
  174. * cmem moved to rtl
  175. * longint replaced with ptrint in heapmanagers
  176. Revision 1.9 2004/03/12 13:08:08 jonas
  177. + added memsize() support (needed to use cmem with the compiler)
  178. Revision 1.8 2003/03/17 15:40:05 armin
  179. + LibName for netware
  180. Revision 1.7 2002/11/01 17:56:39 peter
  181. * needlock field added for 1.1
  182. Revision 1.6 2002/09/08 15:43:47 michael
  183. + Fixed calling conventions
  184. Revision 1.5 2002/09/07 15:42:54 peter
  185. * old logs removed and tabs fixed
  186. Revision 1.4 2002/07/01 16:24:04 peter
  187. * updates for 1.0 compiler
  188. Revision 1.3 2002/06/13 05:01:44 michael
  189. + Added windows msvcrt support
  190. Revision 1.2 2002/06/13 04:54:47 michael
  191. + Fixed parameter type mismatch
  192. Revision 1.1 2002/01/29 17:54:59 peter
  193. * splitted to base and extra
  194. }