cmem.pp 4.1 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. 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. function CGetHeapStatus:THeapStatus;
  128. var res: THeapStatus;
  129. begin
  130. fillchar(res,sizeof(res),0);
  131. CGetHeapStatus:=res;
  132. end;
  133. function CGetFPCHeapStatus:TFPCHeapStatus;
  134. begin
  135. fillchar(CGetFPCHeapStatus,sizeof(CGetFPCHeapStatus),0);
  136. end;
  137. Const
  138. CMemoryManager : TMemoryManager =
  139. (
  140. NeedLock : false;
  141. GetMem : @CGetmem;
  142. FreeMem : @CFreeMem;
  143. FreememSize : @CFreememSize;
  144. AllocMem : @CAllocMem;
  145. ReallocMem : @CReAllocMem;
  146. MemSize : @CMemSize;
  147. GetHeapStatus : @CGetHeapStatus;
  148. GetFPCHeapStatus: @CGetFPCHeapStatus;
  149. );
  150. Var
  151. OldMemoryManager : TMemoryManager;
  152. Initialization
  153. GetMemoryManager (OldMemoryManager);
  154. SetMemoryManager (CmemoryManager);
  155. Finalization
  156. SetMemoryManager (OldMemoryManager);
  157. end.
  158. {
  159. $Log$
  160. Revision 1.13 2005-02-28 15:38:38 marco
  161. * getFPCheapstatus (no, FPC HEAP, not FP CHEAP!)
  162. Revision 1.12 2005/02/14 17:13:22 peter
  163. * truncate log
  164. }