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