/// <summary>Initializes the memory-mapped file handle.</summary> /// <param name="fileStream">The underlying file stream; may be null.</param> /// <param name="ownsFileStream">Whether this SafeHandle is responsible for Disposing the fileStream.</param> /// <param name="inheritability">The inheritability of the memory-mapped file.</param> /// <param name="access">The access for the memory-mapped file.</param> /// <param name="options">The options for the memory-mapped file.</param> /// <param name="capacity">The capacity of the memory-mapped file.</param> internal SafeMemoryMappedFileHandle( FileStream?fileStream, bool ownsFileStream, HandleInheritability inheritability, MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity) : base(ownsHandle: true) { Debug.Assert(!ownsFileStream || fileStream != null, "We can only own a FileStream we're actually given."); // Store the arguments. We'll actually open the map when the view is created. _fileStream = fileStream; _ownsFileStream = ownsFileStream; _inheritability = inheritability; _access = access; _options = options; _capacity = capacity; IntPtr handlePtr; if (fileStream != null) { bool ignored = false; SafeFileHandle handle = fileStream.SafeFileHandle; handle.DangerousAddRef(ref ignored); _fileStreamHandle = handle; handlePtr = handle.DangerousGetHandle(); } else { handlePtr = IntPtr.MaxValue; } SetHandle(handlePtr); }