MemoryMappedFile.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // MemoryMappedFile.cs
  3. //
  4. // Authors:
  5. // Zoltan Varga ([email protected])
  6. //
  7. // Copyright (C) 2009, Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_4_0
  29. using System;
  30. using System.IO;
  31. using System.Collections.Generic;
  32. using Microsoft.Win32.SafeHandles;
  33. using Mono.Unix.Native;
  34. namespace System.IO.MemoryMappedFiles
  35. {
  36. public class MemoryMappedFile : IDisposable {
  37. FileStream stream;
  38. [MonoTODO]
  39. public static MemoryMappedFile CreateFromFile (FileStream fileStream) {
  40. if (fileStream == null)
  41. throw new ArgumentNullException ("fileStream");
  42. return new MemoryMappedFile () { stream = fileStream };
  43. }
  44. [MonoTODO]
  45. public static MemoryMappedFile CreateFromFile (FileStream fileStream, string mapName) {
  46. throw new NotImplementedException ();
  47. }
  48. [MonoTODO]
  49. public static MemoryMappedFile CreateFromFile (FileStream fileStream, string mapName, long capacity) {
  50. throw new NotImplementedException ();
  51. }
  52. [MonoTODO]
  53. public static MemoryMappedFile CreateFromFile (FileStream fileStream, string mapName, long capacity, MemoryMappedFileAccess access) {
  54. throw new NotImplementedException ();
  55. }
  56. /*
  57. [MonoTODO]
  58. public static MemoryMappedFile CreateFromFile (FileStream fileStream, string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability inheritability, bool leaveOpen) {
  59. throw new NotImplementedException ();
  60. }
  61. */
  62. [MonoTODO]
  63. public static MemoryMappedFile CreateNew (string mapName, long capacity) {
  64. throw new NotImplementedException ();
  65. }
  66. [MonoTODO]
  67. public static MemoryMappedFile CreateNew (string mapName, long capacity, MemoryMappedFileAccess access) {
  68. throw new NotImplementedException ();
  69. }
  70. /*
  71. [MonoTODO]
  72. public static MemoryMappedFile CreateNew (string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability handleInheritability) {
  73. throw new NotImplementedException ();
  74. }
  75. */
  76. [MonoTODO]
  77. public static MemoryMappedFile CreateOrOpen (string mapName, long capacity) {
  78. throw new NotImplementedException ();
  79. }
  80. [MonoTODO]
  81. public static MemoryMappedFile CreateOrOpen (string mapName, long capacity, MemoryMappedFileAccess access) {
  82. throw new NotImplementedException ();
  83. }
  84. /*
  85. [MonoTODO]
  86. public static MemoryMappedFile CreateOrOpen (string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability handleInheritability) {
  87. throw new NotImplementedException ();
  88. }
  89. */
  90. public MemoryMappedViewStream CreateViewStream () {
  91. return CreateViewStream (0, 0);
  92. }
  93. public MemoryMappedViewStream CreateViewStream (long offset, long size) {
  94. return CreateViewStream (offset, size, MemoryMappedFileAccess.ReadWrite);
  95. }
  96. [MonoTODO]
  97. public MemoryMappedViewStream CreateViewStream (long offset, long size, MemoryMappedFileAccess access) {
  98. return new MemoryMappedViewStream (stream, offset, size, access);
  99. }
  100. public MemoryMappedViewAccessor CreateViewAccessor () {
  101. return CreateViewAccessor (0, 0);
  102. }
  103. public MemoryMappedViewAccessor CreateViewAccessor (long offset, long size) {
  104. return CreateViewAccessor (offset, size, MemoryMappedFileAccess.ReadWrite);
  105. }
  106. [MonoTODO]
  107. public MemoryMappedViewAccessor CreateViewAccessor (long offset, long size, MemoryMappedFileAccess access) {
  108. return new MemoryMappedViewAccessor (stream, offset, size, access);
  109. }
  110. MemoryMappedFile () {
  111. }
  112. [MonoTODO]
  113. public void Dispose () {
  114. }
  115. [MonoTODO]
  116. public SafeMemoryMappedFileHandle SafeMemoryMappedFileHandle {
  117. get {
  118. throw new NotImplementedException ();
  119. }
  120. }
  121. static int pagesize;
  122. internal static unsafe void MapPosix (FileStream file, long offset, long size, MemoryMappedFileAccess access, out IntPtr map_addr, out int offset_diff, out ulong map_size) {
  123. if (pagesize == 0)
  124. pagesize = Syscall.getpagesize ();
  125. long fsize = file.Length;
  126. if (size == 0 || size > fsize)
  127. size = fsize;
  128. // Align offset
  129. long real_offset = offset & ~(pagesize - 1);
  130. offset_diff = (int)(offset - real_offset);
  131. // FIXME: Need to determine the unix fd for the file, Handle is only
  132. // equal to it by accident
  133. map_size = (ulong)size;
  134. map_addr = Syscall.mmap (IntPtr.Zero, map_size, MmapProts.PROT_READ, MmapFlags.MAP_SHARED, (int)file.Handle, real_offset);
  135. if (map_addr == (IntPtr)(-1))
  136. throw new IOException ("mmap failed for " + file + "(" + offset + ", " + size + ")");
  137. }
  138. internal static void UnmapPosix (IntPtr map_addr, ulong map_size) {
  139. int err = Syscall.munmap (map_addr, map_size);
  140. if (err != 0)
  141. /* This shouldn't happen */
  142. throw new IOException ("munmap failed for address " + map_addr + ", size=" + map_size);
  143. }
  144. }
  145. }
  146. #endif