case 1:
return new ArgumentException ("A positive capacity must be specified for a Memory Mapped File backed by an empty file.");
case 2:
- return new ArgumentOutOfRangeException ("The capacity may not be smaller than the file size.");
+ return new ArgumentOutOfRangeException ("capacity", "The capacity may not be smaller than the file size.");
case 3:
return new FileNotFoundException (path);
case 4:
return new ArgumentException ("Invalid FileMode value.");
case 9:
return new IOException ("Could not map file");
+ case 10:
+ return new UnauthorizedAccessException ("Access to the path is denied.");
+ case 11:
+ return new ArgumentOutOfRangeException ("capacity", "The capacity cannot be greater than the size of the system's logical address space.");
default:
return new IOException ("Failed with unknown error code " + error);
}
public class MemoryMappedFile : IDisposable {
- MemoryMappedFileAccess fileAccess;
- string name;
- long fileCapacity;
+ // MemoryMappedFileAccess fileAccess;
+ // string name;
+ // long fileCapacity;
//
// We allow the use of either the FileStream/keepOpen combo
if (mode == FileMode.Append)
throw new ArgumentException ("mode");
- IntPtr handle = MemoryMapImpl.OpenFile (path, mode, null, out capacity, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.DelayAllocatePages);
+ IntPtr handle = MemoryMapImpl.OpenFile (path, mode, null, out capacity, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None);
return new MemoryMappedFile () {
handle = handle,
- fileAccess = MemoryMappedFileAccess.ReadWrite,
- fileCapacity = capacity
+ // fileAccess = MemoryMappedFileAccess.ReadWrite,
+ // fileCapacity = capacity
};
}
if (capacity < 0)
throw new ArgumentOutOfRangeException ("capacity");
- IntPtr handle = MemoryMapImpl.OpenFile (path, mode, mapName, out capacity, access, MemoryMappedFileOptions.DelayAllocatePages);
+ IntPtr handle = MemoryMapImpl.OpenFile (path, mode, mapName, out capacity, access, MemoryMappedFileOptions.None);
return new MemoryMappedFile () {
handle = handle,
- fileAccess = access,
- name = mapName,
- fileCapacity = capacity
+ // fileAccess = access,
+ // name = mapName,
+ // fileCapacity = capacity
};
}
+ public static MemoryMappedFile CreateFromFile (FileStream fileStream, string mapName, long capacity, MemoryMappedFileAccess access,
+ HandleInheritability inheritability,
+ bool leaveOpen)
+ {
+ if (fileStream == null)
+ throw new ArgumentNullException ("fileStream");
+ if (mapName != null && mapName.Length == 0)
+ throw new ArgumentException ("mapName");
+ if ((!MonoUtil.IsUnix && capacity == 0 && fileStream.Length == 0) || (capacity > fileStream.Length))
+ throw new ArgumentException ("capacity");
+
+ IntPtr handle = MemoryMapImpl.OpenHandle (fileStream.SafeFileHandle.DangerousGetHandle (), mapName, out capacity, access, MemoryMappedFileOptions.None);
+
+ MemoryMapImpl.ConfigureHandleInheritability (handle, inheritability);
+
+ return new MemoryMappedFile () {
+ handle = handle,
+ // fileAccess = access,
+ // name = mapName,
+ // fileCapacity = capacity,
+
+ stream = fileStream,
+ keepOpen = leaveOpen
+ };
+ }
[MonoLimitation ("memoryMappedFileSecurity is currently ignored")]
public static MemoryMappedFile CreateFromFile (FileStream fileStream, string mapName, long capacity, MemoryMappedFileAccess access,
if ((!MonoUtil.IsUnix && capacity == 0 && fileStream.Length == 0) || (capacity > fileStream.Length))
throw new ArgumentException ("capacity");
- IntPtr handle = MemoryMapImpl.OpenHandle (fileStream.Handle, mapName, out capacity, access, MemoryMappedFileOptions.DelayAllocatePages);
+ IntPtr handle = MemoryMapImpl.OpenHandle (fileStream.SafeFileHandle.DangerousGetHandle (), mapName, out capacity, access, MemoryMappedFileOptions.None);
MemoryMapImpl.ConfigureHandleInheritability (handle, inheritability);
return new MemoryMappedFile () {
handle = handle,
- fileAccess = access,
- name = mapName,
- fileCapacity = capacity,
+ // fileAccess = access,
+ // name = mapName,
+ // fileCapacity = capacity,
stream = fileStream,
keepOpen = leaveOpen
return new MemoryMappedFile () {
handle = handle,
- fileAccess = access,
- name = mapName,
- fileCapacity = capacity
+ // fileAccess = access,
+ // name = mapName,
+ // fileCapacity = capacity
};
}
[MonoLimitation ("Named mappings scope is process local")]
public static MemoryMappedFile CreateNew (string mapName, long capacity)
{
- return CreateNew (mapName, capacity, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.DelayAllocatePages, null, HandleInheritability.None);
+ return CreateNew (mapName, capacity, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, null, HandleInheritability.None);
}
[MonoLimitation ("Named mappings scope is process local")]
public static MemoryMappedFile CreateNew (string mapName, long capacity, MemoryMappedFileAccess access)
{
- return CreateNew (mapName, capacity, access, MemoryMappedFileOptions.DelayAllocatePages, null, HandleInheritability.None);
+ return CreateNew (mapName, capacity, access, MemoryMappedFileOptions.None, null, HandleInheritability.None);
+ }
+
+ [MonoLimitation ("Named mappings scope is process local; options is ignored")]
+ public static MemoryMappedFile CreateNew (string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, HandleInheritability inheritability)
+ {
+ return CreateNew (mapName, capacity, access, options, null, inheritability);
}
[MonoLimitation ("Named mappings scope is process local; options and memoryMappedFileSecurity are ignored")]
[MonoLimitation ("Named mappings scope is process local")]
public static MemoryMappedFile CreateOrOpen (string mapName, long capacity, MemoryMappedFileAccess access)
{
- return CreateOrOpen (mapName, capacity, access, MemoryMappedFileOptions.DelayAllocatePages, null, HandleInheritability.None);
+ return CreateOrOpen (mapName, capacity, access, MemoryMappedFileOptions.None, null, HandleInheritability.None);
+ }
+
+ [MonoLimitation ("Named mappings scope is process local")]
+ public static MemoryMappedFile CreateOrOpen (string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, HandleInheritability inheritability)
+ {
+ return CreateOrOpen (mapName, capacity, access, options, null, inheritability);
}
[MonoLimitation ("Named mappings scope is process local")]
public MemoryMappedViewStream CreateViewStream (long offset, long size, MemoryMappedFileAccess access)
{
- return new MemoryMappedViewStream (handle, offset, size, access);
+ var view = MemoryMappedView.Create (handle, offset, size, access);
+ return new MemoryMappedViewStream (view);
}
public MemoryMappedViewAccessor CreateViewAccessor ()
public MemoryMappedViewAccessor CreateViewAccessor (long offset, long size, MemoryMappedFileAccess access)
{
- return new MemoryMappedViewAccessor (handle, offset, size, access);
+ var view = MemoryMappedView.Create (handle, offset, size, access);
+ return new MemoryMappedViewAccessor (view);
}
MemoryMappedFile ()
throw new NotImplementedException ();
}
}
+
+ // This converts a MemoryMappedFileAccess to a FileAccess. MemoryMappedViewStream and
+ // MemoryMappedViewAccessor subclass UnmanagedMemoryStream and UnmanagedMemoryAccessor, which both use
+ // FileAccess to determine whether they are writable and/or readable.
+ internal static FileAccess GetFileAccess (MemoryMappedFileAccess access) {
+
+ if (access == MemoryMappedFileAccess.Read) {
+ return FileAccess.Read;
+ }
+ if (access == MemoryMappedFileAccess.Write) {
+ return FileAccess.Write;
+ }
+ else if (access == MemoryMappedFileAccess.ReadWrite) {
+ return FileAccess.ReadWrite;
+ }
+ else if (access == MemoryMappedFileAccess.CopyOnWrite) {
+ return FileAccess.ReadWrite;
+ }
+ else if (access == MemoryMappedFileAccess.ReadExecute) {
+ return FileAccess.Read;
+ }
+ else if (access == MemoryMappedFileAccess.ReadWriteExecute) {
+ return FileAccess.ReadWrite;
+ }
+
+ // If we reached here, access was invalid.
+ throw new ArgumentOutOfRangeException ("access");
+ }
}
}