cmem.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. unit cmem;
  13. interface
  14. Const
  15. {$if defined(go32v2) or defined(wii)}
  16. {$define USE_STATIC_LIBC}
  17. {$endif}
  18. {$if defined(win32)}
  19. LibName = 'msvcrt';
  20. {$elseif defined(win64)}
  21. LibName = 'msvcrt';
  22. {$elseif defined(wince)}
  23. LibName = 'coredll';
  24. {$elseif defined(netware)}
  25. LibName = 'clib';
  26. {$elseif defined(netwlibc)}
  27. LibName = 'libc';
  28. {$elseif defined(macos)}
  29. LibName = 'StdCLib';
  30. {$elseif defined(beos)}
  31. LibName = 'root';
  32. {$else}
  33. LibName = 'c';
  34. {$endif}
  35. {$ifdef USE_STATIC_LIBC}
  36. {$linklib c}
  37. Function malloc (Size : ptruint) : Pointer;cdecl; external;
  38. Procedure free (P : pointer); cdecl; external;
  39. function realloc (P : Pointer; Size : ptruint) : pointer;cdecl; external;
  40. Function calloc (unitSize,UnitCount : ptruint) : pointer;cdecl; external;
  41. {$else not USE_STATIC_LIBC}
  42. Function Malloc (Size : ptruint) : Pointer; cdecl; external LibName name 'malloc';
  43. Procedure Free (P : pointer); cdecl; external LibName name 'free';
  44. function ReAlloc (P : Pointer; Size : ptruint) : pointer; cdecl; external LibName name 'realloc';
  45. Function CAlloc (unitSize,UnitCount : ptruint) : pointer; cdecl; external LibName name 'calloc';
  46. {$endif not USE_STATIC_LIBC}
  47. implementation
  48. {$macro on}
  49. const
  50. { poor man's max function for constants:
  51. headersize = max(sizeof(ptruint),FPC_STACKALIGNMENT); }
  52. headersize = ord(sizeof(ptruint)>FPC_STACKALIGNMENT)*sizeof(ptruint)+ord(sizeof(ptruint)<=FPC_STACKALIGNMENT)*FPC_STACKALIGNMENT;
  53. {$macros off}
  54. Function CGetMem (Size : ptruint) : Pointer;
  55. begin
  56. CGetMem:=Malloc(Size+headersize);
  57. if (CGetMem <> nil) then
  58. begin
  59. Pptruint(CGetMem)^ := size;
  60. inc(CGetMem,headersize);
  61. end;
  62. end;
  63. Function CFreeMem (P : pointer) : ptruint;
  64. begin
  65. if (p <> nil) then
  66. dec(p,headersize);
  67. Free(P);
  68. CFreeMem:=0;
  69. end;
  70. Function CFreeMemSize(p:pointer;Size:ptruint):ptruint;
  71. begin
  72. if size<=0 then
  73. exit;
  74. if (p <> nil) then
  75. begin
  76. if (size <> Pptruint(p-headersize)^) then
  77. runerror(204);
  78. end;
  79. CFreeMemSize:=CFreeMem(P);
  80. end;
  81. Function CAllocMem(Size : ptruint) : Pointer;
  82. begin
  83. CAllocMem:=calloc(Size+headersize,1);
  84. if (CAllocMem <> nil) then
  85. begin
  86. Pptruint(CAllocMem)^ := size;
  87. inc(CAllocMem,headersize);
  88. end;
  89. end;
  90. Function CReAllocMem (var p:pointer;Size:ptruint):Pointer;
  91. begin
  92. if size=0 then
  93. begin
  94. if p<>nil then
  95. begin
  96. dec(p,headersize);
  97. free(p);
  98. p:=nil;
  99. end;
  100. end
  101. else
  102. begin
  103. inc(size,headersize);
  104. if p=nil then
  105. p:=malloc(Size)
  106. else
  107. begin
  108. dec(p,headersize);
  109. p:=realloc(p,size);
  110. end;
  111. if (p <> nil) then
  112. begin
  113. Pptruint(p)^ := size-headersize;
  114. inc(p,headersize);
  115. end;
  116. end;
  117. CReAllocMem:=p;
  118. end;
  119. Function CMemSize (p:pointer): ptruint;
  120. begin
  121. CMemSize:=Pptruint(p-headersize)^;
  122. end;
  123. function CGetHeapStatus:THeapStatus;
  124. var res: THeapStatus;
  125. begin
  126. fillchar(res,sizeof(res),0);
  127. CGetHeapStatus:=res;
  128. end;
  129. function CGetFPCHeapStatus:TFPCHeapStatus;
  130. begin
  131. fillchar(CGetFPCHeapStatus,sizeof(CGetFPCHeapStatus),0);
  132. end;
  133. Const
  134. CMemoryManager : TMemoryManager =
  135. (
  136. NeedLock : false;
  137. GetMem : @CGetmem;
  138. FreeMem : @CFreeMem;
  139. FreememSize : @CFreememSize;
  140. AllocMem : @CAllocMem;
  141. ReallocMem : @CReAllocMem;
  142. MemSize : @CMemSize;
  143. InitThread : nil;
  144. DoneThread : nil;
  145. RelocateHeap : nil;
  146. GetHeapStatus : @CGetHeapStatus;
  147. GetFPCHeapStatus: @CGetFPCHeapStatus;
  148. );
  149. Var
  150. OldMemoryManager : TMemoryManager;
  151. Initialization
  152. GetMemoryManager (OldMemoryManager);
  153. SetMemoryManager (CmemoryManager);
  154. Finalization
  155. SetMemoryManager (OldMemoryManager);
  156. end.