• delphi内存映射(跨平台)


    delphi内存映射(跨平台)

    /// <author>cxg 2020-12-9</author>
    
    unit yn.map;
    
    interface
    
    uses
      SysUtils,
      {$IFDEF posix}
      Posix.SysMman
      {$ENDIF}
      {$IFDEF mswindows}
      Windows
      {$ENDIF}
      ;
    
    type
      PShareMem = ^TShareMem;
    
      TShareMem = record
        size: Cardinal;
        data: Pointer;
      end;
    
    type
      TWriteRead = procedure (data: pointer) of object;
    
      TMemMap = class
      private
        FShareMemName: string;
        FFileHandle: THandle;
        p: PShareMem;
        FWrite: TWriteRead;
        FRead: TWriteRead;
      public
        constructor Create(const ShareMemName: string; data: Pointer; const dataLen: Cardinal); overload;
        destructor Destroy; override;
      public
        function createFileMap: Boolean;
        function map: Boolean;
        function unMap: Boolean;
        function openFileMap: Boolean;
        function closeFileMap: Boolean;
      public
        procedure write;
        procedure read;
      public
        property OnWrite: TWriteRead read FWrite write FWrite;
        property OnRead: TWriteRead read FRead write FRead;
      end;
    
    implementation
    
    { TMemMap }
    
    function TMemMap.closeFileMap: Boolean;
    begin
      {$IFDEF mswindows}
      Result := CloseHandle(FFileHandle);
      {$ENDIF}
    end;
    
    constructor TMemMap.Create(const ShareMemName: string; data: Pointer; const dataLen: Cardinal);
    begin
      FFileHandle := 0;
      FShareMemName := ShareMemName;
      New(p);
      p^.data := data;
      p^.size := dataLen;
    end;
    
    function TMemMap.createFileMap: Boolean;
    begin
      {$IFDEF mswindows}
      FFileHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, p^.size, PChar(FShareMemName));
      Result := FFileHandle <> 0;
      {$ENDIF}
    end;
    
    destructor TMemMap.Destroy;
    begin
      self.closeFileMap;
      Dispose(p^.data);
      Dispose(p);
      inherited;
    end;
    
    function TMemMap.map: Boolean;
    begin
      {$IFDEF mswindows}
      p^.data := MapViewOfFile(FFileHandle, FILE_MAP_ALL_ACCESS, 0, 0, p^.size);
      Result := p <> nil;
      {$ELSE}
      mmap(p^.data, p^.size, PROT_READ or PROT_WRITE, MAP_SHARED, 0, 0);
      {$ENDIF}
    end;
    
    function TMemMap.openFileMap: Boolean;
    begin
      {$IFDEF mswindows}
      FFileHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, false, PChar(FShareMemName));
      Result := FFileHandle <> 0;
      {$ENDIF}
    end;
    
    procedure TMemMap.read;
    begin
      openFileMap;
      self.map;
      if Assigned(FRead) then
        FRead(p^.data);
      Self.unMap;
    end;
    
    function TMemMap.unMap: Boolean;
    begin
      {$IFDEF mswindows}
      Result := UnmapViewOfFile(p^.data);
      {$ELSE}
      munmap(p^.data, p^.size);
      {$ENDIF}
    end;
    
    procedure TMemMap.write;
    begin
      if FFileHandle = 0 then
        self.createFileMap;
      map;
      if Assigned(FWrite) then
        FWrite(p^.data);
      Self.unMap;
    end;
    
    end.
    

      

      

  • 相关阅读:
    macOS 系统下载地址
    docker基本使用-nginx
    adb命令安装apk
    docker基本使用-常用命令
    docker基本使用-安装
    Vue技术点整理-vue.config.js
    flutter环境部署
    Android webview 问题记录
    Node 使用webpack编写简单的前端应用
    前端api管理工具YApi
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/14108131.html
Copyright © 2020-2023  润新知