使用页面文件支持的共享内存映射文件的 Delphi 6 代码是否正确?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论