cmem.pp 3.7 KB

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