使用页面文件支持的共享内存映射文件的 Delphi 6 代码是否正确?

发布于 2024-12-25 07:33:55 字数 2225 浏览 1 评论 0原文

我有一个 Delphi 6 应用程序和一个 DLL,它们共享内存映射文件以在它们之间传输数据。我知道软页面错误是内存映射文件的正常副作用,但我得到的结果比我想象的要多得多(任务管理器中的高 PF Delta 值约为每秒 2000 个)。因此,我发布了创建内存映射文件、写入该文件并从中读取的代码部分,以查看是否有人可以看到我的方法中的一些缺陷。下面是代码摘录。请注意,我使用的所需文件大小为 1MB:

// Uses pagefile.sys
procedure initFile(
                theFilename: string;
                theDesiredFilesize: integer;
                out theViewHandle: THandle;
                out theBaseAddressPtr: Pointer);
var
    MaximumSizeLow, MaximumSizeHigh: Cardinal;
begin
    I64ToCardinals(theDesiredFilesize, MaximumSizeLow, MaximumSizeHigh);

    theViewHandle :=

        CreateFileMapping(
                    INVALID_HANDLE_VALUE,
                    nil,
                    PAGE_READWRITE or SEC_COMMIT,
                    MaximumSizeHigh,
                    MaximumSizeLow,
                    PChar(theFilename));

    if theViewHandle = 0 then
        RaiseLastOSError;

    theBaseAddressPtr := MapViewOfFile (theViewHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
end;

procedure TShareMem.doSaveBuffer(const theSourceBufPtr: Pointer; numBytes: integer);
var
    newSize: Int64;
begin
    if not Assigned(theSourceBufPtr) then
        raise Exception.Create('(doSaveBuffer) The source buffer pointer is unassigned.');

    newSize := numBytes;

    Move(theSourceBufPtr^, FDataBufferPtr^, newSize);

    // Increment the write count.
    Inc(FDataBufferHeader.writeCount);

    // Update the variable that lets others know the actual size of the
    //  object/data we just stored.
    FDataBufferHeader.sizeInUse := numBytes;

    // Update the header file.
    updateHeaderFile;
end;

procedure TShareMem.loadStream(theDestStream: TMemoryStream);
var
    theSize: Int64;
begin
    if not Assigned(theDestStream) then
        raise Exception.Create('(loadStream) The destination stream is unassigned.');

    updateHeader;

    // Rewind the destination stream.
    theDestStream.Position := 0;

    theSize := FDataBufferHeader.sizeInUse;
    theDestStream.Size := theSize;

    // Read data from the memory mapped data buffer into the stream.
    // theDestStream.WriteBuffer(FDataBufferPtr^, FDataBufferHeader.size);
    Move(FDataBufferPtr^, theDestStream.Memory^, theSize);
end;

I have a Delphi 6 application and also a DLL that share a memory mapped file to transfer data between them. I know soft page faults are a normal side effect of memory mapped files, but I am getting a lot more than I think should (high PF Delta values in the Task Manager of about 2000 per second). Therefore I am posting the parts of my code that create the memory mapped file, write to it, and read from it to see if anyone can see some flaw in my approach. Here are the code excerpts below. Note, I am using a desired file size of 1MB:

// Uses pagefile.sys
procedure initFile(
                theFilename: string;
                theDesiredFilesize: integer;
                out theViewHandle: THandle;
                out theBaseAddressPtr: Pointer);
var
    MaximumSizeLow, MaximumSizeHigh: Cardinal;
begin
    I64ToCardinals(theDesiredFilesize, MaximumSizeLow, MaximumSizeHigh);

    theViewHandle :=

        CreateFileMapping(
                    INVALID_HANDLE_VALUE,
                    nil,
                    PAGE_READWRITE or SEC_COMMIT,
                    MaximumSizeHigh,
                    MaximumSizeLow,
                    PChar(theFilename));

    if theViewHandle = 0 then
        RaiseLastOSError;

    theBaseAddressPtr := MapViewOfFile (theViewHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
end;

procedure TShareMem.doSaveBuffer(const theSourceBufPtr: Pointer; numBytes: integer);
var
    newSize: Int64;
begin
    if not Assigned(theSourceBufPtr) then
        raise Exception.Create('(doSaveBuffer) The source buffer pointer is unassigned.');

    newSize := numBytes;

    Move(theSourceBufPtr^, FDataBufferPtr^, newSize);

    // Increment the write count.
    Inc(FDataBufferHeader.writeCount);

    // Update the variable that lets others know the actual size of the
    //  object/data we just stored.
    FDataBufferHeader.sizeInUse := numBytes;

    // Update the header file.
    updateHeaderFile;
end;

procedure TShareMem.loadStream(theDestStream: TMemoryStream);
var
    theSize: Int64;
begin
    if not Assigned(theDestStream) then
        raise Exception.Create('(loadStream) The destination stream is unassigned.');

    updateHeader;

    // Rewind the destination stream.
    theDestStream.Position := 0;

    theSize := FDataBufferHeader.sizeInUse;
    theDestStream.Size := theSize;

    // Read data from the memory mapped data buffer into the stream.
    // theDestStream.WriteBuffer(FDataBufferPtr^, FDataBufferHeader.size);
    Move(FDataBufferPtr^, theDestStream.Memory^, theSize);
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文