MemoryMappedFile support on Windows
[mono.git] / mcs / class / System.Core / System.IO.MemoryMappedFiles / MemoryMappedFile.cs
index 5bba4d9209b21555441b2a1620e3efbd62864ca5..7a3cce164f61e2feffc9b46fcd751d1c75626e91 100644 (file)
@@ -70,7 +70,7 @@ namespace System.IO.MemoryMappedFiles
                        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:
@@ -85,6 +85,10 @@ namespace System.IO.MemoryMappedFiles
                                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);
                        }
@@ -111,9 +115,9 @@ namespace System.IO.MemoryMappedFiles
 
 
        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
@@ -140,12 +144,12 @@ namespace System.IO.MemoryMappedFiles
                        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
                        };
                }
 
@@ -172,16 +176,41 @@ namespace System.IO.MemoryMappedFiles
                        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,
@@ -195,15 +224,15 @@ namespace System.IO.MemoryMappedFiles
                        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
@@ -224,22 +253,28 @@ namespace System.IO.MemoryMappedFiles
                        
                        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")]
@@ -259,7 +294,13 @@ namespace System.IO.MemoryMappedFiles
                [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")]