簡體   English   中英

文件上的內存映射到期了嗎?

[英]Memory mapping on file expires?

我有一個文件,使用內存映射在該文件上創建巨大的數組(可能不適合物理內存)。 內存上映射了兩個區域-數組計數(4個字節)和一些正在文件中移動的窗口以訪問數組的不同元素。

除非有問題,否則一切正常。 經過多次使用數組計數的操作(有時進行數百萬次操作)后,當我嘗試通過其內存地址讀取或寫入此計數時,得到系統消息“訪問被拒絕”,此地址自創建以來從未改變。

似乎此映射頁面以某種方式過期了...

type
      // view info
      TViewInfo = record
        ptr: pointer;                      // pointer to fist byte in view
        offset: longword;                  // offset of our data inside the view
        addr: PPointer;                    // pointer to variable pointer (ptr + offset)
      end;

      TSizeRec = packed record
      case integer of
        0: (full: int64);
        1: (lo, hi: longword);
      end;


    function TFileMappedArray.CreateView(offset: int64; size: longword; var p: pointer): TViewInfo;
    var
      offs: TSizeRec;
      fsize: int64;
    begin
      p := nil;
      result.addr := @p;

      // view must start on the mem_granularity*N offset
      // so we need to adjust our numbers
      result.offset := offset mod fMemGranularity;
      offs.full := (offset div fMemGranularity)*fMemGranularity;
      size := size + result.offset;

      fsize := int64(fMaxNumOfItems)*fItemSize + sizeof(longword);
      if (offs.full  fsize) then
        size := fsize - offs.full;

      result.ptr := MapViewOfFile(fMappingHandle, FILE_MAP_WRITE, offs.hi, offs.lo, size);

      p := pointer(longword(result.ptr) + result.offset);
    end;


    var
      fNumberOfItems: PLongword;   // our counter
      fNumberView: TViewInfo;      // our view


    // create view of first 4 bytes in file
    fNumberView := CreateView(0, 4, pointer(fNumberOfItems));


    // get count
    array count := fNuberOfItems^;

    // set count
    fNumberOfItems^ := new count;

獲取或設置計數有時,隨機且很少會產生錯誤

創建內存映射后釋放文件句柄時出錯。 隨時間沒有過期。 但是,只要存在內存映射,就應該保持文件句柄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM