cmem.pp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 win32}
  17. {$ifdef netware}
  18. LibName = 'clib';
  19. {$else}
  20. {$ifdef netwlibc}
  21. LibName = 'libc';
  22. {$else}
  23. {$ifdef macos}
  24. LibName = 'StdCLib';
  25. {$else}
  26. LibName = 'c';
  27. {$endif macos}
  28. {$endif netwlibc}
  29. {$endif}
  30. {$else}
  31. LibName = 'msvcrt';
  32. {$endif}
  33. Function Malloc (Size : ptrint) : Pointer; {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'malloc';
  34. Procedure Free (P : pointer); {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'free';
  35. function ReAlloc (P : Pointer; Size : ptrint) : pointer; {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'realloc';
  36. Function CAlloc (unitSize,UnitCount : ptrint) : pointer; {$ifdef win32}stdcall{$else}cdecl{$endif}; external LibName name 'calloc';
  37. implementation
  38. type
  39. pptrint = ^ptrint;
  40. Function CGetMem (Size : ptrint) : Pointer;
  41. begin
  42. CGetMem:=Malloc(Size+sizeof(ptrint));
  43. if (CGetMem <> nil) then
  44. begin
  45. pptrint(CGetMem)^ := size;
  46. inc(CGetMem,sizeof(ptrint));
  47. end;
  48. end;
  49. Function CFreeMem (P : pointer) : ptrint;
  50. begin
  51. if (p <> nil) then
  52. dec(p,sizeof(ptrint));
  53. Free(P);
  54. CFreeMem:=0;
  55. end;
  56. Function CFreeMemSize(p:pointer;Size:ptrint):ptrint;
  57. begin
  58. if (p <> nil) then
  59. begin
  60. if (size <> pptrint(p-sizeof(ptrint))^) then
  61. runerror(204);
  62. end;
  63. CFreeMemSize:=CFreeMem(P);
  64. end;
  65. Function CAllocMem(Size : ptrint) : Pointer;
  66. begin
  67. CAllocMem:=calloc(Size+sizeof(ptrint),1);
  68. if (CAllocMem <> nil) then
  69. begin
  70. pptrint(CAllocMem)^ := size;
  71. inc(CAllocMem,sizeof(ptrint));
  72. end;
  73. end;
  74. Function CReAllocMem (var p:pointer;Size:ptrint):Pointer;
  75. begin
  76. if size=0 then
  77. begin
  78. if p<>nil then
  79. begin
  80. dec(p,sizeof(ptrint));
  81. free(p);
  82. p:=nil;
  83. end;
  84. end
  85. else
  86. begin
  87. inc(size,sizeof(ptrint));
  88. if p=nil then
  89. p:=malloc(Size)
  90. else
  91. begin
  92. dec(p,sizeof(ptrint));
  93. p:=realloc(p,size);
  94. end;
  95. if (p <> nil) then
  96. begin
  97. pptrint(p)^ := size-sizeof(ptrint);
  98. inc(p,sizeof(ptrint));
  99. end;
  100. end;
  101. CReAllocMem:=p;
  102. end;
  103. Function CMemSize (p:pointer): ptrint;
  104. begin
  105. CMemSize:=pptrint(p-sizeof(ptrint))^;
  106. end;
  107. Function CMemAvail : ptrint;
  108. begin
  109. CMemAvail:=0;
  110. end;
  111. Function CMaxAvail: ptrint;
  112. begin
  113. CMaxAvail:=0;
  114. end;
  115. Function CHeapSize : ptrint;
  116. begin
  117. CHeapSize:=0;
  118. end;
  119. Const
  120. CMemoryManager : TMemoryManager =
  121. (
  122. NeedLock : false;
  123. GetMem : @CGetmem;
  124. FreeMem : @CFreeMem;
  125. FreememSize : @CFreememSize;
  126. AllocMem : @CAllocMem;
  127. ReallocMem : @CReAllocMem;
  128. MemSize : @CMemSize;
  129. MemAvail : @CMemAvail;
  130. MaxAvail : @CMaxAvail;
  131. HeapSize : @CHeapSize;
  132. );
  133. Var
  134. OldMemoryManager : TMemoryManager;
  135. Initialization
  136. GetMemoryManager (OldMemoryManager);
  137. SetMemoryManager (CmemoryManager);
  138. Finalization
  139. SetMemoryManager (OldMemoryManager);
  140. end.
  141. {
  142. $Log$
  143. Revision 1.8 2004-09-19 08:16:03 olle
  144. * reverted to $ifdef style, so 1.0.x can eat it.
  145. Revision 1.7 2004/09/18 08:40:26 olle
  146. + added support for macos
  147. Revision 1.6 2004/09/15 20:37:42 armin
  148. * add support for netware libc
  149. Revision 1.5 2004/05/05 13:00:43 jonas
  150. * fixed reallocmem (bug noted by Vincent Snijders)
  151. Revision 1.4 2004/03/23 22:35:20 peter
  152. * dec ptr before free in reallocmem
  153. Revision 1.3 2004/03/17 12:50:53 michael
  154. * Fixed: Macavail -> CMaxAvail (from Marc Weustinc)
  155. Revision 1.2 2004/03/16 15:25:16 peter
  156. * adaption to compile with 1.0.x in new rtl
  157. Revision 1.1 2004/03/15 21:48:26 peter
  158. * cmem moved to rtl
  159. * longint replaced with ptrint in heapmanagers
  160. Revision 1.9 2004/03/12 13:08:08 jonas
  161. + added memsize() support (needed to use cmem with the compiler)
  162. Revision 1.8 2003/03/17 15:40:05 armin
  163. + LibName for netware
  164. Revision 1.7 2002/11/01 17:56:39 peter
  165. * needlock field added for 1.1
  166. Revision 1.6 2002/09/08 15:43:47 michael
  167. + Fixed calling conventions
  168. Revision 1.5 2002/09/07 15:42:54 peter
  169. * old logs removed and tabs fixed
  170. Revision 1.4 2002/07/01 16:24:04 peter
  171. * updates for 1.0 compiler
  172. Revision 1.3 2002/06/13 05:01:44 michael
  173. + Added windows msvcrt support
  174. Revision 1.2 2002/06/13 04:54:47 michael
  175. + Fixed parameter type mismatch
  176. Revision 1.1 2002/01/29 17:54:59 peter
  177. * splitted to base and extra
  178. }