cmem.pp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. Function CGetMem (Size : ptruint) : Pointer;
  49. begin
  50. CGetMem:=Malloc(Size+sizeof(ptruint));
  51. if (CGetMem <> nil) then
  52. begin
  53. Pptruint(CGetMem)^ := size;
  54. inc(CGetMem,sizeof(ptruint));
  55. end;
  56. end;
  57. Function CFreeMem (P : pointer) : ptruint;
  58. begin
  59. if (p <> nil) then
  60. dec(p,sizeof(ptruint));
  61. Free(P);
  62. CFreeMem:=0;
  63. end;
  64. Function CFreeMemSize(p:pointer;Size:ptruint):ptruint;
  65. begin
  66. if size<=0 then
  67. exit;
  68. if (p <> nil) then
  69. begin
  70. if (size <> Pptruint(p-sizeof(ptruint))^) then
  71. runerror(204);
  72. end;
  73. CFreeMemSize:=CFreeMem(P);
  74. end;
  75. Function CAllocMem(Size : ptruint) : Pointer;
  76. begin
  77. CAllocMem:=calloc(Size+sizeof(ptruint),1);
  78. if (CAllocMem <> nil) then
  79. begin
  80. Pptruint(CAllocMem)^ := size;
  81. inc(CAllocMem,sizeof(ptruint));
  82. end;
  83. end;
  84. Function CReAllocMem (var p:pointer;Size:ptruint):Pointer;
  85. begin
  86. if size=0 then
  87. begin
  88. if p<>nil then
  89. begin
  90. dec(p,sizeof(ptruint));
  91. free(p);
  92. p:=nil;
  93. end;
  94. end
  95. else
  96. begin
  97. inc(size,sizeof(ptruint));
  98. if p=nil then
  99. p:=malloc(Size)
  100. else
  101. begin
  102. dec(p,sizeof(ptruint));
  103. p:=realloc(p,size);
  104. end;
  105. if (p <> nil) then
  106. begin
  107. Pptruint(p)^ := size-sizeof(ptruint);
  108. inc(p,sizeof(ptruint));
  109. end;
  110. end;
  111. CReAllocMem:=p;
  112. end;
  113. Function CMemSize (p:pointer): ptruint;
  114. begin
  115. CMemSize:=Pptruint(p-sizeof(ptruint))^;
  116. end;
  117. function CGetHeapStatus:THeapStatus;
  118. var res: THeapStatus;
  119. begin
  120. fillchar(res,sizeof(res),0);
  121. CGetHeapStatus:=res;
  122. end;
  123. function CGetFPCHeapStatus:TFPCHeapStatus;
  124. begin
  125. fillchar(CGetFPCHeapStatus,sizeof(CGetFPCHeapStatus),0);
  126. end;
  127. Const
  128. CMemoryManager : TMemoryManager =
  129. (
  130. NeedLock : false;
  131. GetMem : @CGetmem;
  132. FreeMem : @CFreeMem;
  133. FreememSize : @CFreememSize;
  134. AllocMem : @CAllocMem;
  135. ReallocMem : @CReAllocMem;
  136. MemSize : @CMemSize;
  137. InitThread : nil;
  138. DoneThread : nil;
  139. RelocateHeap : nil;
  140. GetHeapStatus : @CGetHeapStatus;
  141. GetFPCHeapStatus: @CGetFPCHeapStatus;
  142. );
  143. Var
  144. OldMemoryManager : TMemoryManager;
  145. Initialization
  146. GetMemoryManager (OldMemoryManager);
  147. SetMemoryManager (CmemoryManager);
  148. Finalization
  149. SetMemoryManager (OldMemoryManager);
  150. end.