简体   繁体   English

64位内存分配

[英]64bit Memory allocation

I've been asked to create a Delphi compatible dll in C++ to do simple 64bit memory management. 我被要求用C ++创建一个Delphi兼容的dll来进行简单的64位内存管理。

The background is that the system in Delphi needs to allocate a lots of chunks of memory that would go well outside 32bit addressable space. 背景是Delphi中的系统需要分配大量内存,这些内存在32位可寻址空间之外。 The Delphi developer explained to me that he could not allocate memory with the Delphi commands available to him. Delphi开发人员向我解释说,他无法使用他可用的Delphi命令分配内存。 He says that he can hold a 64bit address, so he just wants to call a function I provide to allocate the memory and return a 64bit pointer to him. 他说他可以保存64位地址,所以他只想调用我提供的函数来分配内存并向他返回64位指针。 Then another function to free up the memory later. 然后是另一个功能,以便以后释放内存。

Now, I only have VS 2008 at my disposal so firstly I'm not even sure I can create a Delphi compatible dll in the first place. 现在,我只有VS 2008供我使用,所以首先我甚至不确定我是否可以创建一个Delphi兼容的dll。

Any Delphi experts care to help me out. 任何Delphi专家都会帮助我。 Maybe there is a way to achieve what he requires without re-inventing the wheel. 也许有一种方法可以实现他所需要的而无需重新发明轮子。 Other developers must have come across this before in Delphi. 其他开发人员必须在Delphi之前遇到过这个问题。

All comments appreciated. 所有评论赞赏。

Only 64 bit processes can address 64 bit memory. 只有64位进程可以处理64位内存。 A 64 bit process can only load 64 bit dlls and 32 bits processes can only load 32 bits dlls. 64位进程只能加载64位dll,32位进程只能加载32位dll。 Delphi's compiler can only make 32 bits binaries. Delphi的编译器只能生成32位二进制文​​件。

So a 32 bits Delphi exe can not load your 64 bit c++ dll. 所以32位Delphi exe无法加载你的64位c ++ dll。 It could load a 32 bit c++ dll, but then that dll wouldn't be able to address the 64 bit memory space. 它可以加载一个32位的c ++ dll,但那个dll将无法处理64位的内存空间。 You are kind of stuck with this solution. 你有点坚持这个解决方案。

Delphi could, with the right compiler options and Windows switches address 3GB of memory without problems. 使用正确的编译器选项和Windows开关,Delphi可以毫无问题地处理3GB内存。 Even more memory could be accessed by a 32 bits process if it uses Physical Address Extension . 如果使用物理地址扩展,则32位进程可以访问更多内存。 It then needs to swap memory pages in and out of the 32 bits memory through the use of Address Windowing Extensions . 然后,它需要通过使用Address Windowing Extensions将存储器页面交换进出32位存储器。

Delphi pointers are 32-bit. Delphi指针是32位的。 Period. 期。 Your Delphi developer may be able to 'store' the 64-bit values you want to return to him, but he can't access the memory that they point to, so it's pretty futile. 您的Delphi开发人员可能能够“存储”您想要返回给他的64位值,但他无法访问他们指向的内存,因此这非常徒劳。

Previously, I'd written:- 以前,我写过: -

A 64-bit version of Delphi is on Codegear/Embarcadero's road map for "middle of 2009". 64位版本的Delphi位于Codegear / Embarcadero的 “2009年中期” 路线图中。 Product quality seems to be (at last!) taking precedence over hitting ship dates exactly, so don't hold your breath... 产品质量似乎(最后!)优先于完全击中船舶日期,所以不要屏住呼吸......

But, in August 2010, Embarcadero published a new roadmap here . 但是,2010年8月,Embarcadero 在这里发布了一个新的路线图 This doesn't give specific dates, but mentions a 64-bit Compiler Preview , with Projected Availability, 1st Half of 2011 . 这没有给出具体日期,但提到了一个64位编译器预览版 ,其中包括2011年上半年的预计可用性

您可以看看Free Pascal,因为它包含64位版本,并且主要是Delphi兼容语法。

In order to allocate memory shared by multiple process, you should use a memory mapped file. 为了分配多个进程共享的内存,您应该使用内存映射文件。

The code available at http://www.delphifaq.com/faq/delphi_windows_API/f348.shtml can be used to communicate between a 32 bit and a 64 bit process. http://www.delphifaq.com/faq/delphi_windows_API/f348.shtml上提供的代码可用于在32位和64位进程之间进行通信。

Here are the steps: 以下是步骤:

  • Create a memory mapped file, either on disk, either on memory; 在磁盘上创建内存映射文件,在内存中;
  • Create a mutex to notify file change; 创建互斥锁以通知文件更改;
  • One end write some data to the memory mapped file; 一端将一些数据写入内存映射文件;
  • Then it flags the mutex; 然后它标记互斥锁;
  • Other end receive the mutex notification; 另一端收到互斥通知;
  • Then it reads the data from the memory mapped file. 然后它从内存映射文件中读取数据。

It's up to you to create a custom binary layout in the memory mapped file, in order to share any data. 您可以在内存映射文件中创建自定义二进制布局,以便共享任何数据。

By design, memory mapped files are fast (it's a kernel-level / x86 CPU feature), and can handle huge memory (up to 1 GB for a 32 bit process, from my experiment). 根据设计,内存映射文件很快(它是内核级/ x86 CPU功能),并且可以处理大量内存(对于32位进程,从我的实验中可以高达1 GB)。

This kind of communication is used by http://cc.embarcadero.com/Author/802978 to call any 64 bit dll from a 32 bit Delphi program. http://cc.embarcadero.com/Author/802978使用这种通信从32位Delphi程序调用任何64位dll。

You might also want to add a way to pin and unpin that 64-bit pointer to a 32-bit memory address. 您可能还想添加一种方法来固定和取消固定该64位指针到32位内存地址。 Since this is Delphi, I'm pretty sure it's Windows specific, so you might as well use Address Windowing Extensions . 由于这是Delphi,我很确定它是特定于Windows的,所以你不妨使用Address Windowing Extensions That way, you can support allocating, freeing, and pinning and unpinning memory to a 32-bit address range and still take advantage of a 64-bit memory allocation space. 这样,您可以支持分配,释放,固定和取消固定内存到32位地址范围,并仍然利用64位内存分配空间。 Assuming that the user will actually commit the memory such that it fits in the 32-bit virtual address space. 假设用户实际上将提交内存,使其适合32位虚拟地址空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM