cmem.pp 3.7 KB

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