@@ -314,21 +314,26 @@ type ByteMemory with
314314
315315 member x.AsReadOnly () = ReadOnlyByteMemory x
316316
317+ static member Empty = ByteArrayMemory([||], 0 , 0 ) :> ByteMemory
318+
317319 static member CreateMemoryMappedFile ( bytes : ReadOnlyByteMemory ) =
318- if Utils.runningOnMono
319- then
320- // mono's MemoryMappedFile implementation throws with null `mapName`, so we use byte arrays instead: https://github.com/mono/mono/issues/10245
321- ByteArrayMemory.FromArray ( bytes.ToArray()) :> ByteMemory
320+ let length = int64 bytes.Length
321+ if length = 0 L then
322+ ByteMemory.Empty
322323 else
323- let length = int64 bytes.Length
324- let mmf =
325- let mmf = MemoryMappedFile.CreateNew( null , length, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, HandleInheritability.None)
326- use stream = mmf.CreateViewStream( 0 L, length, MemoryMappedFileAccess.ReadWrite)
327- bytes.CopyTo stream
328- mmf
329-
330- let accessor = mmf.CreateViewAccessor( 0 L, length, MemoryMappedFileAccess.ReadWrite)
331- RawByteMemory.FromUnsafePointer( accessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), int length, ( mmf, accessor))
324+ if Utils.runningOnMono
325+ then
326+ // mono's MemoryMappedFile implementation throws with null `mapName`, so we use byte arrays instead: https://github.com/mono/mono/issues/10245
327+ ByteArrayMemory.FromArray ( bytes.ToArray()) :> ByteMemory
328+ else
329+ let mmf =
330+ let mmf = MemoryMappedFile.CreateNew( null , length, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, HandleInheritability.None)
331+ use stream = mmf.CreateViewStream( 0 L, length, MemoryMappedFileAccess.ReadWrite)
332+ bytes.CopyTo stream
333+ mmf
334+
335+ let accessor = mmf.CreateViewAccessor( 0 L, length, MemoryMappedFileAccess.ReadWrite)
336+ RawByteMemory.FromUnsafePointer( accessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), int length, ( mmf, accessor))
332337
333338 static member FromFile ( path , access , ? canShadowCopy : bool ) =
334339 let canShadowCopy = defaultArg canShadowCopy false
0 commit comments