Merge pull request #1851 from esdrubal/mmf
authorMarek Safar <marek.safar@gmail.com>
Wed, 3 Jun 2015 16:07:39 +0000 (18:07 +0200)
committerMarek Safar <marek.safar@gmail.com>
Wed, 3 Jun 2015 16:07:39 +0000 (18:07 +0200)
MemoryMappedFile improvements

mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedViewAccessor.cs
mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedViewStream.cs
mcs/class/System.Core/Test/System.IO.MemoryMappedFiles/MemoryMappedFileTest.cs
mcs/class/corlib/System.IO/UnmanagedMemoryAccessor.cs [deleted file]
mcs/class/corlib/System.Runtime.InteropServices/Marshal.cs
mcs/class/corlib/corlib.dll.sources

index 783aa9142fbbc18bd8d8adc57259b592eea12cec..8bb8fc31b4bc02b206608b19910c91832463365c 100644 (file)
@@ -37,12 +37,19 @@ namespace System.IO.MemoryMappedFiles
        public sealed class MemoryMappedViewAccessor : UnmanagedMemoryAccessor, IDisposable {
                IntPtr mmap_handle;
                SafeMemoryMappedViewHandle safe_handle;
+               long pointerOffset;
 
                internal MemoryMappedViewAccessor (IntPtr handle, long offset, long size, MemoryMappedFileAccess access)
                {
+                       pointerOffset = offset;
                        Create (handle, offset, size, access);
                }
 
+               public long PointerOffset
+               {
+                       get { return pointerOffset; }
+               }
+
                static FileAccess ToFileAccess (MemoryMappedFileAccess access)
                {
                        switch (access){
index 58f933ae7921fdaea243b1a09a993d0dde7db64e..6fbaca605f900dd66241e5ed1a6cdbbad3d3f933 100644 (file)
@@ -36,12 +36,19 @@ namespace System.IO.MemoryMappedFiles
        public sealed class MemoryMappedViewStream : UnmanagedMemoryStream {
                IntPtr mmap_handle;
                object monitor;
-               
+               long pointerOffset;
+
                internal MemoryMappedViewStream (IntPtr handle, long offset, long size, MemoryMappedFileAccess access) {
+                       pointerOffset = offset;
                        monitor = new Object ();
                        CreateStream (handle, offset, size, access);
                }
 
+               public long PointerOffset
+               {
+                       get { return pointerOffset; }
+               }
+
                public SafeMemoryMappedViewHandle SafeMemoryMappedViewHandle { 
                        get {
                                throw new NotImplementedException ();
index f64d0c54477b7dd9e8281190050979a0aeb5bdb4..c07666a29c6d97de26b9037c2e5d60e95c43f584 100644 (file)
@@ -214,6 +214,39 @@ namespace MonoTests.System.IO.MemoryMappedFiles {
                        }
                }
 
+               [Test]
+               public unsafe void ViewAccessorReadArrayWithOffset () {
+                       var file = MemoryMappedFile.CreateFromFile (fname, FileMode.Open);
+                       var offset = 3;
+                       var expected = "lo!";
+
+                       using (var v = file.CreateViewAccessor (offset, expected.Length)) {
+                               Assert.AreEqual (offset, v.PointerOffset);
+
+                               var a = new byte [expected.Length];
+                               var n = v.ReadArray (0, a, 0, expected.Length);
+                               Assert.AreEqual (expected.Length, n);
+                               var s = new string (Array.ConvertAll (a, b => (char)b));
+                               Assert.AreEqual (expected, s);
+                       }
+               }
+
+               [Test]
+               public unsafe void ViewStreamReadWithOffset () {
+                       var file = MemoryMappedFile.CreateFromFile (fname, FileMode.Open);
+                       var offset = 3;
+                       var expected = "lo!";
+
+                       using (var v = file.CreateViewStream (offset, expected.Length)) {
+                               Assert.AreEqual (offset, v.PointerOffset);
+
+                               var a = new byte [expected.Length];
+                               var n = v.Read (a, 0, expected.Length);
+                               Assert.AreEqual (expected.Length, n);
+                               var s = new string (Array.ConvertAll (a, b => (char)b));
+                               Assert.AreEqual (expected, s);
+                       }
+               }
 
                [Test]
                public void NamedMappingToInvalidFile ()
diff --git a/mcs/class/corlib/System.IO/UnmanagedMemoryAccessor.cs b/mcs/class/corlib/System.IO/UnmanagedMemoryAccessor.cs
deleted file mode 100644 (file)
index feb7b4a..0000000
+++ /dev/null
@@ -1,477 +0,0 @@
-//
-// System.IO.UnmanagedMemoryAccessor.cs
-//
-// Author:
-//  Zoltan Varga (vargaz@gmail.com)
-//  Marek Safar (marek.safar@gmail.com)
-//
-// Copyright (C) 2009 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-
-using System;
-using System.Runtime.InteropServices;
-using System.Security.Permissions;
-
-namespace System.IO
-{
-       [MonoTODO ("Offset is ignored")]
-       public class UnmanagedMemoryAccessor : IDisposable {
-               SafeBuffer buffer;
-#pragma warning disable 414
-               long offset;
-#pragma warning restore
-               long capacity;
-               bool canwrite, canread;
-
-               protected UnmanagedMemoryAccessor ()
-               {
-               }
-
-               public UnmanagedMemoryAccessor (SafeBuffer buffer, long offset, long capacity)
-               {
-                       Initialize (buffer, offset, capacity, FileAccess.ReadWrite);
-               }
-
-               public UnmanagedMemoryAccessor (SafeBuffer buffer, long offset, long capacity, FileAccess access)
-               {
-                       Initialize (buffer, offset, capacity, access);
-               }
-
-               [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
-               protected void Initialize(SafeBuffer buffer, long offset, long capacity, FileAccess access)
-               {
-                       if (buffer == null)
-                               throw new ArgumentNullException ("buffer");
-                       if (offset < 0)
-                               throw new ArgumentOutOfRangeException ("offset");
-                       if (capacity < 0)
-                               throw new ArgumentOutOfRangeException ("capacity");
-
-                       if (access == FileAccess.Read || access == FileAccess.ReadWrite)
-                               canread = true;
-                       if (access == FileAccess.Write || access == FileAccess.ReadWrite)
-                               canwrite = true;
-
-                       if (this.buffer != null)
-                               Dispose (true);
-                                        
-                       this.buffer = buffer;
-                       this.offset = offset;
-                       this.capacity = capacity;
-               }
-               
-               public void Dispose ()
-               {
-                       Dispose (true);
-                       GC.SuppressFinalize (this);
-               }
-
-               protected virtual void Dispose (bool disposing)
-               {
-                       if (buffer != null){
-                               if (disposing){
-                                       buffer.Dispose ();
-                               }
-                       }
-                       buffer = null;
-               }
-
-               public byte ReadByte (long position) 
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       return buffer.Read<byte> ((ulong) position);
-               }
-
-               public bool ReadBoolean (long position) 
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       return buffer.Read<bool> ((ulong) position);
-               }
-
-               public char ReadChar (long position) 
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       return buffer.Read<char> ((ulong) position);
-               }
-               
-               public decimal ReadDecimal (long position) 
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       return buffer.Read<decimal> ((ulong) position);
-               }
-               
-               public double ReadDouble (long position) 
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       return buffer.Read<double> ((ulong) position);
-               }
-
-               public short ReadInt16 (long position) 
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       return buffer.Read<short> ((ulong) position);
-               }
-               
-               public int ReadInt32 (long position) 
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       return buffer.Read<int> ((ulong) position);
-               }
-               
-               public long ReadInt64 (long position) 
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       return buffer.Read<long> ((ulong) position);
-               }
-               
-               [CLSCompliant (false)]
-               public sbyte ReadSByte (long position) 
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       return buffer.Read<sbyte> ((ulong) position);
-               }
-               
-               public float ReadSingle (long position) 
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       return buffer.Read<float> ((ulong) position);
-               }
-               
-               [CLSCompliant (false)]
-               public ushort ReadUInt16 (long position) 
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       return buffer.Read<ushort> ((ulong) position);
-               }
-
-               [CLSCompliant (false)]
-               public uint ReadUInt32 (long position) 
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       return buffer.Read<uint> ((ulong) position);
-               }
-
-               [CLSCompliant (false)]
-               public ulong ReadUInt64 (long position) 
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       return buffer.Read<ulong> ((ulong) position);
-               }
-
-               public void Read<T> (long position, out T structure) where T : struct
-               {
-                       if (!canread)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       structure = buffer.Read<T> ((ulong) position);
-               }
-
-               public int ReadArray<T> (long position, T [] array, int offset, int count) where T : struct
-               {
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       long left = capacity - position;
-                       var slots = Math.Min (count, (int)(left / Marshal.SizeOf (typeof (T))));
-
-                       buffer.ReadArray ((ulong) position, array, offset, slots);
-                       return slots;
-               }
-               
-               public void Write (long position, bool value) 
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write ((ulong)position, value);
-               }
-
-               public void Write (long position, byte value) 
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write ((ulong)position, value);
-               }
-
-               public void Write (long position, char value) 
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write ((ulong)position, value);
-               }
-
-               public void Write (long position, decimal value) 
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write ((ulong)position, value);
-               }
-               
-               public void Write (long position, double value) 
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write ((ulong)position, value);
-               }
-
-               public void Write (long position, short value) 
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write ((ulong)position, value);
-               }
-
-               public void Write (long position, int value) 
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write ((ulong)position, value);
-               }
-
-               public void Write (long position, long value) 
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write ((ulong)position, value);
-               }
-
-               [CLSCompliant (false)]
-               public void Write (long position, sbyte value) 
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write ((ulong)position, value);
-               }
-
-               public void Write (long position, float value) 
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write ((ulong)position, value);
-               }
-
-               [CLSCompliant (false)]
-               public void Write (long position, ushort value) 
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write ((ulong)position, value);
-               }
-
-               [CLSCompliant (false)]
-               public void Write (long position, uint value) 
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write ((ulong)position, value);
-               }
-
-               [CLSCompliant (false)]
-               public void Write (long position, ulong value) 
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write ((ulong)position, value);
-               }
-
-               public void Write<T> (long position, ref T structure) where T : struct
-               {
-                       if (!canwrite)
-                               throw new NotSupportedException ();
-                       if (buffer == null)
-                               throw new ObjectDisposedException ("buffer");
-                       if (position < 0)
-                               throw new ArgumentOutOfRangeException ();
-                       
-                       buffer.Write<T> ((ulong)position, structure);
-               }
-
-               public void WriteArray<T> (long position, T [] array, int offset, int count) where T : struct 
-               {
-                       buffer.WriteArray ((ulong)position, array, offset, count);
-               }
-       
-               public bool CanRead {
-                       get { return canread; }
-               }
-
-               public bool CanWrite {
-                       get { return canwrite; }
-               }
-
-               public long Capacity {
-                       get { return capacity; }
-               }
-               
-               protected bool IsOpen {
-                       get { return buffer != null; }
-               }
-       }
-}
-
index 44c9ec8a72670db2e0354972d4c05aad60f47d88..3f8fc5e932b80b9e37aa20ee4d3c3056b00e501d 100644 (file)
@@ -979,6 +979,21 @@ namespace System.Runtime.InteropServices
                        return SizeOf (structure.GetType ());
                }
 
+               internal static uint SizeOfType (Type type)
+               {
+                       return (uint) SizeOf (type);
+               }
+
+               internal static uint AlignedSizeOf<T> () where T : struct
+               {
+                       uint size = SizeOfTypei (typeof (T));
+                       if (size == 1 || size == 2)
+                               return size;
+                       if (IntPtr.Size == 8 && size == 4)
+                               return size;
+                       return (size + 3) & (~((uint)3));
+               }
+
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr StringToBSTR (string s);
 
index 3e183e7c4a9b7c27b3ce53f08672a1e7b378ca91..1b02a58884552a064a351aa33d386fc1fc65b195 100644 (file)
@@ -214,7 +214,6 @@ System.IO/SearchPattern.cs
 System.IO/SeekOrigin.cs
 System.IO/UnexceptionalStreamReader.cs
 System.IO/UnexceptionalStreamWriter.cs
-System.IO/UnmanagedMemoryAccessor.cs
 System.IO.IsolatedStorage/INormalizeForIsolatedStorage.cs
 System.IO.IsolatedStorage/IsolatedStorage.cs
 System.IO.IsolatedStorage/IsolatedStorageException.cs
@@ -997,6 +996,7 @@ ReferenceSources/SecurityContext.cs
 ../../../external/referencesource/mscorlib/system/int64.cs
 ../../../external/referencesource/mscorlib/system/iobservable.cs
 ../../../external/referencesource/mscorlib/system/iobserver.cs
+../../../external/referencesource/mscorlib/system/io/unmanagedmemoryaccessor.cs
 ../../../external/referencesource/mscorlib/system/iprogress.cs
 ../../../external/referencesource/mscorlib/system/iserviceobjectprovider.cs
 ../../../external/referencesource/mscorlib/system/invalidtimezoneexception.cs