cmem.pp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. free(p);
  73. p:=nil;
  74. end;
  75. end
  76. else
  77. begin
  78. inc(size,sizeof(ptrint));
  79. if p=nil then
  80. p:=calloc(Size,1)
  81. else
  82. begin
  83. dec(p,sizeof(ptrint));
  84. p:=realloc(p,size);
  85. end;
  86. if (p <> nil) then
  87. begin
  88. pptrint(p)^ := size-sizeof(ptrint);
  89. inc(p,sizeof(ptrint));
  90. end;
  91. end;
  92. CReAllocMem:=p;
  93. end;
  94. Function CMemSize (p:pointer): ptrint;
  95. begin
  96. CMemSize:=pptrint(p-sizeof(ptrint))^;
  97. end;
  98. Function CMemAvail : ptrint;
  99. begin
  100. CMemAvail:=0;
  101. end;
  102. Function CMaxAvail: ptrint;
  103. begin
  104. CMaxAvail:=0;
  105. end;
  106. Function CHeapSize : ptrint;
  107. begin
  108. CHeapSize:=0;
  109. end;
  110. Const
  111. CMemoryManager : TMemoryManager =
  112. (
  113. NeedLock : false;
  114. GetMem : @CGetmem;
  115. FreeMem : @CFreeMem;
  116. FreememSize : @CFreememSize;
  117. AllocMem : @CAllocMem;
  118. ReallocMem : @CReAllocMem;
  119. MemSize : @CMemSize;
  120. MemAvail : @CMemAvail;
  121. MaxAvail : @CMaxAvail;
  122. HeapSize : @CHeapSize;
  123. );
  124. Var
  125. OldMemoryManager : TMemoryManager;
  126. Initialization
  127. GetMemoryManager (OldMemoryManager);
  128. SetMemoryManager (CmemoryManager);
  129. Finalization
  130. SetMemoryManager (OldMemoryManager);
  131. end.
  132. {
  133. $Log$
  134. Revision 1.3 2004-03-17 12:50:53 michael
  135. * Fixed: Macavail -> CMaxAvail (from Marc Weustinc)
  136. Revision 1.2 2004/03/16 15:25:16 peter
  137. * adaption to compile with 1.0.x in new rtl
  138. Revision 1.1 2004/03/15 21:48:26 peter
  139. * cmem moved to rtl
  140. * longint replaced with ptrint in heapmanagers
  141. Revision 1.9 2004/03/12 13:08:08 jonas
  142. + added memsize() support (needed to use cmem with the compiler)
  143. Revision 1.8 2003/03/17 15:40:05 armin
  144. + LibName for netware
  145. Revision 1.7 2002/11/01 17:56:39 peter
  146. * needlock field added for 1.1
  147. Revision 1.6 2002/09/08 15:43:47 michael
  148. + Fixed calling conventions
  149. Revision 1.5 2002/09/07 15:42:54 peter
  150. * old logs removed and tabs fixed
  151. Revision 1.4 2002/07/01 16:24:04 peter
  152. * updates for 1.0 compiler
  153. Revision 1.3 2002/06/13 05:01:44 michael
  154. + Added windows msvcrt support
  155. Revision 1.2 2002/06/13 04:54:47 michael
  156. + Fixed parameter type mismatch
  157. Revision 1.1 2002/01/29 17:54:59 peter
  158. * splitted to base and extra
  159. }