加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

f# – MemoryMappedFiles.MemoryMappedFile.CreateFromFile不会

发布时间:2020-12-14 01:57:57 所属栏目:Linux 来源:网络整理
导读:在 windows下,当容量参数(最后一个参数)设置为比底层文件大时,此F#代码将文件从12个字节扩展到655346个字节.这似乎是扩展内存映射文件最干净的方法.在mono / linux下,它抛出一个ArgumentException:capacity,除非该文件与映射的容量一样长.是否有一种干净的
在 windows下,当容量参数(最后一个参数)设置为比底层文件大时,此F#代码将文件从12个字节扩展到655346个字节.这似乎是扩展内存映射文件最干净的方法.在mono / linux下,它抛出一个ArgumentException:capacity,除非该文件与映射的容量一样长.是否有一种干净的方式来获取单声道扩展文件,或者我必须先预先扩展文件才能映射?

let Main () =
    let path = "parts.pash"
    let l = 65536L
    let mm =    MemoryMappedFiles.MemoryMappedFile.CreateFromFile(path,FileMode.OpenOrCreate,"pashmap",l)

    ()

Main()

错误信息

Unhandled Exception: System.ArgumentException: capacity at
System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile
(System.String path,FileMode mode,System.String mapName,Int64
capacity,MemoryMappedFileAccess access) [0x00000] in :0 at
System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile
(System.String path,Int64
capacity) [0x00000] in :0 at Program.Main ()
[0x00000] in :0 at
.$Program.main@ () [0x00000] in :0

单声道版本:

[daz@clowder pash]$mono --version
Mono JIT compiler version 2.10.1 (tarball Mon Apr  4 10:40:52 PDT 2011)
Copyright (C) 2002-2011 Novell,Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  x86
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported,not enabled.
        GC:            Included Boehm (with typed GC and Parallel Mark)

编辑:似乎内存映射的不同底层行为在API中公开,因此您需要将文件自己扩展到正确的长度以进行平台中立

let f = File.Open(path,FileMode.Append,FileAccess.Write)
let pad = l- FileInfo(path).Length
let padding = Array.create (int32 pad) 0uy
f.Write(padding,int pad)
f.Close()

解决方法

查看CreateFromFile的.NET实现,没有该功能的实现.除了参数检查之外,从我可以看出,它是Windows API调用的一个纤薄的包装器.

由于这个原因,创建一个更大的文件的能力更像是.NET领域的巧合,因此如果底层操作系统不允许类似的功能,Mono就会删除这种能力也就不足为奇了.

更简单,不太可能,因为.NET版本在技术上也没有扩展文件,Windows API也是如此.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读