cmem.pp 4.4 KB

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