cmem.pp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999 by Michael Van Canneyt, member of the
  4. Free Pascal development team
  5. Implements a memory manager that uses the C memory management.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$IFNDEF FPC_DOTTEDUNITS}
  13. unit cmem;
  14. {$ENDIF FPC_DOTTEDUNITS}
  15. interface
  16. Const
  17. {$if defined(go32v2) or defined(wii)}
  18. {$define USE_STATIC_LIBC}
  19. {$endif}
  20. {$if defined(win32)}
  21. LibName = 'msvcrt';
  22. {$elseif defined(win64)}
  23. LibName = 'msvcrt';
  24. {$elseif defined(wince)}
  25. LibName = 'coredll';
  26. {$elseif defined(netware)}
  27. LibName = 'clib';
  28. {$elseif defined(netwlibc)}
  29. LibName = 'libc';
  30. {$elseif defined(macos)}
  31. LibName = 'StdCLib';
  32. {$elseif defined(beos)}
  33. LibName = 'root';
  34. {$else}
  35. LibName = 'c';
  36. {$endif}
  37. {$ifdef USE_STATIC_LIBC}
  38. {$linklib c}
  39. Function malloc (Size : ptruint) : Pointer;cdecl; external;
  40. Procedure free (P : pointer); cdecl; external;
  41. function realloc (P : Pointer; Size : ptruint) : pointer;cdecl; external;
  42. Function calloc (unitSize,UnitCount : ptruint) : pointer;cdecl; external;
  43. {$else not USE_STATIC_LIBC}
  44. Function Malloc (Size : ptruint) : Pointer; cdecl; external LibName name 'malloc';
  45. Procedure Free (P : pointer); cdecl; external LibName name 'free';
  46. function ReAlloc (P : Pointer; Size : ptruint) : pointer; cdecl; external LibName name 'realloc';
  47. Function CAlloc (unitSize,UnitCount : ptruint) : pointer; cdecl; external LibName name 'calloc';
  48. {$endif not USE_STATIC_LIBC}
  49. implementation
  50. {$macro on}
  51. const
  52. { poor man's max function for constants:
  53. headersize = max(sizeof(ptruint),FPC_STACKALIGNMENT); }
  54. headersize = ord(sizeof(ptruint)>FPC_STACKALIGNMENT)*sizeof(ptruint)+ord(sizeof(ptruint)<=FPC_STACKALIGNMENT)*FPC_STACKALIGNMENT;
  55. {$macro off}
  56. Function CGetMem (Size : ptruint) : Pointer;
  57. begin
  58. CGetMem:=Malloc(Size+headersize);
  59. if (CGetMem <> nil) then
  60. begin
  61. Pptruint(CGetMem)^ := size;
  62. inc(CGetMem,headersize);
  63. end;
  64. end;
  65. Function CFreeMem (P : pointer) : ptruint;
  66. begin
  67. if (p <> nil) then
  68. dec(p,headersize);
  69. Free(P);
  70. CFreeMem:=0;
  71. end;
  72. Function CFreeMemSize(p:pointer;Size:ptruint):ptruint;
  73. begin
  74. if size<=0 then
  75. exit;
  76. if (p <> nil) then
  77. begin
  78. if (size <> Pptruint(p-headersize)^) then
  79. runerror(204);
  80. end;
  81. CFreeMemSize:=CFreeMem(P);
  82. end;
  83. Function CAllocMem(Size : ptruint) : Pointer;
  84. begin
  85. CAllocMem:=calloc(Size+headersize,1);
  86. if (CAllocMem <> nil) then
  87. begin
  88. Pptruint(CAllocMem)^ := size;
  89. inc(CAllocMem,headersize);
  90. end;
  91. end;
  92. Function CReAllocMem (var p:pointer;Size:ptruint):Pointer;
  93. begin
  94. if size=0 then
  95. begin
  96. if p<>nil then
  97. begin
  98. dec(p,headersize);
  99. free(p);
  100. p:=nil;
  101. end;
  102. end
  103. else
  104. begin
  105. inc(size,headersize);
  106. if p=nil then
  107. p:=malloc(Size)
  108. else
  109. begin
  110. dec(p,headersize);
  111. p:=realloc(p,size);
  112. end;
  113. if (p <> nil) then
  114. begin
  115. Pptruint(p)^ := size-headersize;
  116. inc(p,headersize);
  117. end;
  118. end;
  119. CReAllocMem:=p;
  120. end;
  121. Function CMemSize (p:pointer): ptruint;
  122. begin
  123. CMemSize:=Pptruint(p-headersize)^;
  124. end;
  125. function CGetHeapStatus:THeapStatus;
  126. var res: THeapStatus;
  127. begin
  128. fillchar(res,sizeof(res),0);
  129. CGetHeapStatus:=res;
  130. end;
  131. function CGetFPCHeapStatus:TFPCHeapStatus;
  132. begin
  133. fillchar(CGetFPCHeapStatus,sizeof(CGetFPCHeapStatus),0);
  134. end;
  135. Const
  136. CMemoryManager : TMemoryManager =
  137. (
  138. NeedLock : false;
  139. GetMem : @CGetmem;
  140. FreeMem : @CFreeMem;
  141. FreememSize : @CFreememSize;
  142. AllocMem : @CAllocMem;
  143. ReallocMem : @CReAllocMem;
  144. MemSize : @CMemSize;
  145. InitThread : nil;
  146. DoneThread : nil;
  147. RelocateHeap : nil;
  148. GetHeapStatus : @CGetHeapStatus;
  149. GetFPCHeapStatus: @CGetFPCHeapStatus;
  150. );
  151. Var
  152. OldMemoryManager : TMemoryManager;
  153. Initialization
  154. GetMemoryManager (OldMemoryManager);
  155. SetMemoryManager (CmemoryManager);
  156. Finalization
  157. SetMemoryManager (OldMemoryManager);
  158. end.