cmem.pp 4.4 KB

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