cmem.pp 3.8 KB

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