cmem.pp 3.7 KB

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