【发布时间】:2014-07-25 12:01:00
【问题描述】:
如何在 Delphi 中使用Hoard Memory Manager?我正在寻找FastMM 的替代品,这对于严肃的多线程服务器应用程序来说是没有希望的。我查看了ScaleMM2,但它在 64 位中不稳定。
如何静态链接Hoard Memory Manager。因为它带有一个OBJ 文件。
【问题讨论】:
标签: delphi delphi-xe6
如何在 Delphi 中使用Hoard Memory Manager?我正在寻找FastMM 的替代品,这对于严肃的多线程服务器应用程序来说是没有希望的。我查看了ScaleMM2,但它在 64 位中不稳定。
如何静态链接Hoard Memory Manager。因为它带有一个OBJ 文件。
【问题讨论】:
标签: delphi delphi-xe6
正如大卫所说,您需要一个 .dll 版本。如果您不能自己构建它,您可以找到预构建的版本。仅快速搜索turned up an older version, though。
André Mussche modified the FastCode MM Challenge 将在 2011 年添加 Hoard。如果您 browse the source code,您将看到如何使用它的示例,包括 winhoard.dll 的另一个预编译副本。但是,performance 和 memory usage 在这些测试中表现不佳。
【讨论】:
http://scalemm.googlecode.com/svn/trunk/Challenge/HoardMM.pas,它有这个声明msvcrtDLL = 'libhoard.dll',这让我发笑。事实上,这段代码和这段代码 (scalemm.googlecode.com/svn/trunk/Challenge/msvcrtMM.pas) 似乎是由我编写的!
您不能真正希望静态链接 Hoard,因为它是用 C++ 实现的,因此需要 C++ 运行时。您不能期望在您的 Delphi 程序中放置 C++ 运行时。
因此,您需要在 DLL 中构建 Hoard 内存管理器,例如使用 Visual Studio。该 DLL 必须导出 malloc、free 和 realloc。使用 .def 文件执行此操作。然后创建一个简单的 Delphi 单元,它链接到 DLL 并安装基于这些导入函数的内存管理器。使用我对other question 的回答来指导您。与往常一样,使内存管理器单元成为 .dpr 文件使用列表中的第一个单元。
请确保您遵守许可。如果我没记错的话,你需要支付商业许可证,或者根据 GPL 许可你的软件。
FWIW,我在msvcrt.dll 中使用来自系统 C 运行时的malloc,它的扩展性比 FastMM 好。
【讨论】:
extern "C" 来实现。这具有不使用 C++ 名称修饰的副作用。例如,查看libhoard.cpp 的结尾。将它与 Delphi 链接并不一定那么容易,因为您还必须提供指向 C 和 C++ 标准库的链接。 C 和 C++ 编译器会自动执行此操作,但 Delphi 不会。
由于大家FAILED提供了详细的解决方案。这是一个实际可行的解决方案。
1:打开libhoard.cpp
2:在代码中添加以下几行。
extern "C"{
__declspec(dllexport) void* scalable_malloc (size_t size)
{
return malloc(size);
}
__declspec(dllexport) void* scalable_realloc (void* ptr, size_t size)
{
return realloc(ptr, size);
}
__declspec(dllexport) void scalable_free (void* ptr)
{
free(ptr);
}
}
3:使用nmake windows编译
4:使用它
unit hoard_mm;
interface
implementation
type
size_t = Cardinal;
const
hoardDLL = 'libhoard.dll';
function scalable_malloc(Size: size_t): Pointer; cdecl; external hoardDLL;
function scalable_realloc(P: Pointer; Size: size_t): Pointer; cdecl; external hoardDLL;
procedure scalable_free(P: Pointer); cdecl; external hoardDLL;
function GetMem(Size: NativeInt): Pointer; inline;
begin
Result := scalable_malloc(size);
end;
function FreeMem(P: Pointer): Integer; inline;
begin
scalable_free(P);
Result := 0;
end;
function ReallocMem(P: Pointer; Size: NativeInt): Pointer; inline;
begin
Result := scalable_realloc(P, Size);
end;
function AllocMem(Size: NativeInt): Pointer; inline;
begin
Result := GetMem(Size);
if Assigned(Result) then
FillChar(Result^, Size, 0);
end;
function RegisterUnregisterExpectedMemoryLeak(P: Pointer): Boolean; inline;
begin
Result := False;
end;
const
MemoryManager: TMemoryManagerEx = (
GetMem: GetMem;
FreeMem: FreeMem;
ReallocMem: ReallocMem;
AllocMem: AllocMem;
RegisterExpectedMemoryLeak: RegisterUnregisterExpectedMemoryLeak;
UnregisterExpectedMemoryLeak: RegisterUnregisterExpectedMemoryLeak
);
initialization
SetMemoryManager(MemoryManager);
end.
【讨论】: