在 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, FileMode mode, System.String mapName, 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,0,int pad)
f.Close()
最佳答案
查看 CreateFromFile
的 .NET 实现没有实现该功能。除了参数检查之外,据我所知,它是围绕 Windows API 调用的一个纤薄包装器。
出于这个原因,创建更大文件的能力更像是 .NET 领域的巧合,因此如果底层操作系统不允许类似的功能,Mono 删除该能力也就不足为奇了。
更简单地说,不太可能,因为 .NET 版本在技术上也不会扩展文件,Windows API 会。
关于f# - MemoryMappedFiles.MemoryMappedFile.CreateFromFile 不会在 linux/mono 下扩展文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9895384/