MemoryMappedFile.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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 System.Runtime.InteropServices;
  34. #if !MOBILE
  35. using Mono.Unix.Native;
  36. using Mono.Unix;
  37. #else
  38. using System.Runtime.CompilerServices;
  39. #endif
  40. namespace System.IO.MemoryMappedFiles
  41. {
  42. #if !MOBILE
  43. internal static class MemoryMapImpl {
  44. //
  45. // Turns the FileMode into the first half of open(2) flags
  46. //
  47. static OpenFlags ToUnixMode (FileMode mode)
  48. {
  49. switch (mode){
  50. case FileMode.CreateNew:
  51. return OpenFlags.O_CREAT | OpenFlags.O_EXCL;
  52. case FileMode.Create:
  53. return OpenFlags.O_CREAT | OpenFlags.O_TRUNC;
  54. case FileMode.OpenOrCreate:
  55. return OpenFlags.O_CREAT;
  56. case FileMode.Truncate:
  57. return OpenFlags.O_TRUNC;
  58. case FileMode.Append:
  59. return OpenFlags.O_APPEND;
  60. default:
  61. case FileMode.Open:
  62. return 0;
  63. }
  64. }
  65. //
  66. // Turns the MemoryMappedFileAccess into the second half of open(2) flags
  67. //
  68. static OpenFlags ToUnixMode (MemoryMappedFileAccess access)
  69. {
  70. switch (access){
  71. case MemoryMappedFileAccess.CopyOnWrite:
  72. case MemoryMappedFileAccess.ReadWriteExecute:
  73. case MemoryMappedFileAccess.ReadWrite:
  74. return OpenFlags.O_RDWR;
  75. case MemoryMappedFileAccess.Write:
  76. return OpenFlags.O_WRONLY;
  77. case MemoryMappedFileAccess.ReadExecute:
  78. case MemoryMappedFileAccess.Read:
  79. default:
  80. return OpenFlags.O_RDONLY;
  81. }
  82. }
  83. static MmapProts ToUnixProts (MemoryMappedFileAccess access)
  84. {
  85. switch (access){
  86. case MemoryMappedFileAccess.ReadWrite:
  87. return MmapProts.PROT_WRITE | MmapProts.PROT_READ;
  88. case MemoryMappedFileAccess.Write:
  89. return MmapProts.PROT_WRITE;
  90. case MemoryMappedFileAccess.CopyOnWrite:
  91. return MmapProts.PROT_WRITE | MmapProts.PROT_READ;
  92. case MemoryMappedFileAccess.ReadExecute:
  93. return MmapProts.PROT_EXEC;
  94. case MemoryMappedFileAccess.ReadWriteExecute:
  95. return MmapProts.PROT_WRITE | MmapProts.PROT_READ | MmapProts.PROT_EXEC;
  96. case MemoryMappedFileAccess.Read:
  97. default:
  98. return MmapProts.PROT_READ;
  99. }
  100. }
  101. internal static int Open (string path, FileMode mode, ref long capacity, MemoryMappedFileAccess access)
  102. {
  103. if (MonoUtil.IsUnix){
  104. Stat buf;
  105. if (Syscall.stat (path, out buf) == -1)
  106. UnixMarshal.ThrowExceptionForLastError ();
  107. if (capacity == 0) {
  108. // Special files such as FIFOs, sockets, and devices can
  109. // have a size of 0. Specifying a capacity for these
  110. // also makes little sense, so don't do the check if the
  111. // file is one of these.
  112. if (buf.st_size == 0 &&
  113. (buf.st_mode & (FilePermissions.S_IFCHR |
  114. FilePermissions.S_IFBLK |
  115. FilePermissions.S_IFIFO |
  116. FilePermissions.S_IFSOCK)) == 0) {
  117. throw new ArgumentException ("A positive capacity must be specified for a Memory Mapped File backed by an empty file.");
  118. }
  119. capacity = buf.st_size;
  120. } else if (capacity < buf.st_size) {
  121. throw new ArgumentException ("The capacity may not be smaller than the file size.");
  122. }
  123. int fd = Syscall.open (path, ToUnixMode (mode) | ToUnixMode (access), FilePermissions.DEFFILEMODE);
  124. if (fd == -1)
  125. UnixMarshal.ThrowExceptionForLastError ();
  126. return fd;
  127. }
  128. throw new NotImplementedException ();
  129. }
  130. internal static void CloseFD (int fd) {
  131. Syscall.close (fd);
  132. }
  133. internal static void Flush (int fd) {
  134. if (MonoUtil.IsUnix)
  135. Syscall.fsync (fd);
  136. else
  137. throw new NotImplementedException ("Not implemented on Windows");
  138. }
  139. static int pagesize;
  140. internal static unsafe void Map (int file_handle, long offset, ref long size, MemoryMappedFileAccess access, out IntPtr map_addr, out int offset_diff)
  141. {
  142. if (!MonoUtil.IsUnix)
  143. throw new NotImplementedException ("Not implemented on windows.");
  144. if (pagesize == 0)
  145. pagesize = Syscall.getpagesize ();
  146. Stat buf;
  147. Syscall.fstat (file_handle, out buf);
  148. long fsize = buf.st_size;
  149. if (size == 0 || size > fsize)
  150. size = fsize;
  151. // Align offset
  152. long real_offset = offset & ~(pagesize - 1);
  153. offset_diff = (int)(offset - real_offset);
  154. // FIXME: Need to determine the unix fd for the file, Handle is only
  155. // equal to it by accident
  156. //
  157. // The new API no longer uses FileStream everywhere, but exposes instead
  158. // the filename (with one exception), we could move this API to use
  159. // file descriptors instead of the FileStream plus its Handle.
  160. //
  161. map_addr = Syscall.mmap (IntPtr.Zero, (ulong) size,
  162. ToUnixProts (access),
  163. access == MemoryMappedFileAccess.CopyOnWrite ? MmapFlags.MAP_PRIVATE : MmapFlags.MAP_SHARED,
  164. file_handle, real_offset);
  165. if (map_addr == (IntPtr)(-1))
  166. throw new IOException ("mmap failed for fd#" + file_handle + "(" + offset + ", " + size + ")");
  167. }
  168. internal static bool Unmap (IntPtr map_addr, ulong map_size)
  169. {
  170. if (!MonoUtil.IsUnix)
  171. return false;
  172. return Syscall.munmap (map_addr, map_size) == 0;
  173. }
  174. static void ConfigureUnixFD (IntPtr handle, HandleInheritability h)
  175. {
  176. // TODO: Mono.Posix is lacking O_CLOEXEC definitions for fcntl.
  177. }
  178. [DllImport("kernel32", SetLastError = true)]
  179. static extern bool SetHandleInformation (IntPtr hObject, int dwMask, int dwFlags);
  180. static void ConfigureWindowsFD (IntPtr handle, HandleInheritability h)
  181. {
  182. SetHandleInformation (handle, 1 /* FLAG_INHERIT */, h == HandleInheritability.None ? 0 : 1);
  183. }
  184. internal static void ConfigureFD (IntPtr handle, HandleInheritability inheritability)
  185. {
  186. if (MonoUtil.IsUnix)
  187. ConfigureUnixFD (handle, inheritability);
  188. else
  189. ConfigureWindowsFD (handle, inheritability);
  190. }
  191. }
  192. #else
  193. internal static class MemoryMapImpl {
  194. [DllImport ("libc")]
  195. static extern int fsync (int fd);
  196. [DllImport ("libc")]
  197. static extern int close (int fd);
  198. [DllImport ("libc")]
  199. static extern int fcntl (int fd, int cmd, int arg0);
  200. //XXX check if android off_t is 64bits or not. on iOS / darwin it is.
  201. [DllImport ("libc")]
  202. static extern IntPtr mmap (IntPtr addr, IntPtr len, int prot, int flags, int fd, long offset);
  203. [DllImport ("libc")]
  204. static extern int munmap (IntPtr addr, IntPtr size);
  205. [DllImport ("libc", SetLastError=true)]
  206. static extern int open (string path, int flags, int access);
  207. #if MONODROID
  208. [DllImport ("__Internal")]
  209. static extern int monodroid_getpagesize ();
  210. static int getpagesize ()
  211. {
  212. return monodroid_getpagesize ();
  213. }
  214. #else
  215. [DllImport ("libc")]
  216. static extern int getpagesize ();
  217. #endif
  218. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  219. static extern long mono_filesize_from_path (string str);
  220. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  221. static extern long mono_filesize_from_fd (int fd);
  222. //Values valid on iOS/OSX and android ndk r6
  223. const int F_GETFD = 1;
  224. const int F_SETFD = 2;
  225. const int FD_CLOEXEC = 1;
  226. const int DEFFILEMODE = 0x666;
  227. const int O_RDONLY = 0x0;
  228. const int O_WRONLY = 0x1;
  229. const int O_RDWR = 0x2;
  230. const int PROT_READ = 0x1;
  231. const int PROT_WRITE = 0x2;
  232. const int PROT_EXEC = 0x4;
  233. const int MAP_PRIVATE = 0x2;
  234. const int MAP_SHARED = 0x1;
  235. const int EINVAL = 22;
  236. #if MONODROID
  237. const int O_CREAT = 0x040;
  238. const int O_TRUNC = 0x080;
  239. const int O_EXCL = 0x200;
  240. const int ENAMETOOLONG = 63;
  241. #else
  242. /* MONOTOUCH */
  243. const int O_CREAT = 0x0200;
  244. const int O_TRUNC = 0x0400;
  245. const int O_EXCL = 0x0800;
  246. const int ENAMETOOLONG = 36;
  247. #endif
  248. static int ToUnixMode (FileMode mode)
  249. {
  250. switch (mode) {
  251. case FileMode.CreateNew:
  252. return O_CREAT | O_EXCL;
  253. case FileMode.Create:
  254. return O_CREAT | O_TRUNC;
  255. case FileMode.OpenOrCreate:
  256. return O_CREAT;
  257. case FileMode.Truncate:
  258. return O_TRUNC;
  259. default:
  260. case FileMode.Open:
  261. return 0;
  262. }
  263. }
  264. //
  265. // Turns the MemoryMappedFileAccess into the second half of open(2) flags
  266. //
  267. static int ToUnixMode (MemoryMappedFileAccess access)
  268. {
  269. switch (access) {
  270. case MemoryMappedFileAccess.CopyOnWrite:
  271. case MemoryMappedFileAccess.ReadWriteExecute:
  272. case MemoryMappedFileAccess.ReadWrite:
  273. return O_RDWR;
  274. case MemoryMappedFileAccess.Write:
  275. return O_WRONLY;
  276. case MemoryMappedFileAccess.ReadExecute:
  277. case MemoryMappedFileAccess.Read:
  278. default:
  279. return O_RDONLY;
  280. }
  281. }
  282. static int ToUnixProts (MemoryMappedFileAccess access)
  283. {
  284. switch (access){
  285. case MemoryMappedFileAccess.ReadWrite:
  286. return PROT_WRITE | PROT_READ;
  287. case MemoryMappedFileAccess.Write:
  288. return PROT_WRITE;
  289. case MemoryMappedFileAccess.CopyOnWrite:
  290. return PROT_WRITE | PROT_READ;
  291. case MemoryMappedFileAccess.ReadExecute:
  292. return PROT_EXEC;
  293. case MemoryMappedFileAccess.ReadWriteExecute:
  294. return PROT_WRITE | PROT_READ | PROT_EXEC;
  295. case MemoryMappedFileAccess.Read:
  296. default:
  297. return PROT_READ;
  298. }
  299. }
  300. static void ThrowErrorFromErrno (int errno)
  301. {
  302. switch (errno) {
  303. case EINVAL: throw new ArgumentException ();
  304. case ENAMETOOLONG: throw new PathTooLongException ();
  305. default: throw new IOException ("Failed with errno " + errno);
  306. }
  307. }
  308. internal static int Open (string path, FileMode mode, long capacity, MemoryMappedFileAccess access)
  309. {
  310. long file_size = mono_filesize_from_path (path);
  311. if (file_size < 0)
  312. throw new FileNotFoundException (path);
  313. if (capacity > file_size)
  314. throw new ArgumentException ("capacity");
  315. int fd = open (path, ToUnixMode (mode) | ToUnixMode (access), DEFFILEMODE);
  316. if (fd == -1)
  317. ThrowErrorFromErrno (Marshal.GetLastWin32Error ());
  318. return fd;
  319. }
  320. internal static void CloseFD (int fd)
  321. {
  322. close (fd);
  323. }
  324. internal static void Flush (int fd)
  325. {
  326. fsync (fd);
  327. }
  328. internal static bool Unmap (IntPtr map_addr, ulong map_size)
  329. {
  330. return munmap (map_addr, (IntPtr)map_size) == 0;
  331. }
  332. static int pagesize;
  333. internal static unsafe void Map (int file_handle, long offset, ref long size, MemoryMappedFileAccess access, out IntPtr map_addr, out int offset_diff)
  334. {
  335. if (pagesize == 0)
  336. pagesize = getpagesize ();
  337. long fsize = mono_filesize_from_fd (file_handle);
  338. if (fsize < 0)
  339. throw new FileNotFoundException ();
  340. if (size == 0 || size > fsize)
  341. size = fsize;
  342. // Align offset
  343. long real_offset = offset & ~(pagesize - 1);
  344. offset_diff = (int)(offset - real_offset);
  345. map_addr = mmap (IntPtr.Zero, (IntPtr) size,
  346. ToUnixProts (access),
  347. access == MemoryMappedFileAccess.CopyOnWrite ? MAP_PRIVATE : MAP_SHARED,
  348. file_handle, real_offset);
  349. if (map_addr == (IntPtr)(-1))
  350. throw new IOException ("mmap failed for fd#" + file_handle + "(" + offset + ", " + size + ")");
  351. }
  352. internal static void ConfigureFD (IntPtr handle, HandleInheritability inheritability)
  353. {
  354. int fd = (int)handle;
  355. int flags = fcntl (fd, F_GETFD, 0);
  356. if (inheritability == HandleInheritability.None)
  357. flags &= ~FD_CLOEXEC;
  358. else
  359. flags |= FD_CLOEXEC;
  360. fcntl (fd, F_SETFD, flags);
  361. }
  362. }
  363. #endif
  364. public class MemoryMappedFile : IDisposable {
  365. MemoryMappedFileAccess fileAccess;
  366. string name;
  367. long fileCapacity;
  368. //
  369. // We allow the use of either the FileStream/keepOpen combo
  370. // or a Unix file descriptor. This way we avoid the dependency on
  371. // Mono's io-layer having the Unix file descriptors mapped to
  372. // the same io-layer handle
  373. //
  374. FileStream stream;
  375. bool keepOpen;
  376. int unix_fd;
  377. public static MemoryMappedFile CreateFromFile (string path)
  378. {
  379. return CreateFromFile (path, FileMode.Open, null, 0, MemoryMappedFileAccess.ReadWrite);
  380. }
  381. public static MemoryMappedFile CreateFromFile (string path, FileMode mode)
  382. {
  383. return CreateFromFile (path, mode, null, 0, MemoryMappedFileAccess.ReadWrite);
  384. }
  385. public static MemoryMappedFile CreateFromFile (string path, FileMode mode, string mapName)
  386. {
  387. return CreateFromFile (path, mode, mapName, 0, MemoryMappedFileAccess.ReadWrite);
  388. }
  389. public static MemoryMappedFile CreateFromFile (string path, FileMode mode, string mapName, long capacity)
  390. {
  391. return CreateFromFile (path, mode, mapName, capacity, MemoryMappedFileAccess.ReadWrite);
  392. }
  393. public static MemoryMappedFile CreateFromFile (string path, FileMode mode, string mapName, long capacity, MemoryMappedFileAccess access)
  394. {
  395. if (path == null)
  396. throw new ArgumentNullException ("path");
  397. if (path.Length == 0)
  398. throw new ArgumentException ("path");
  399. if (mapName != null && mapName.Length == 0)
  400. throw new ArgumentException ("mapName");
  401. if (mode == FileMode.Append)
  402. throw new ArgumentException ("mode");
  403. if (capacity < 0)
  404. throw new ArgumentOutOfRangeException ("capacity");
  405. int fd = MemoryMapImpl.Open (path, mode, ref capacity, access);
  406. return new MemoryMappedFile () {
  407. unix_fd = fd,
  408. fileAccess = access,
  409. name = mapName,
  410. fileCapacity = capacity
  411. };
  412. }
  413. #if MOBILE
  414. public static MemoryMappedFile CreateFromFile (FileStream fileStream, string mapName, long capacity, MemoryMappedFileAccess access,
  415. HandleInheritability inheritability,
  416. bool leaveOpen)
  417. #else
  418. [MonoLimitation ("memoryMappedFileSecurity is currently ignored")]
  419. public static MemoryMappedFile CreateFromFile (FileStream fileStream, string mapName, long capacity, MemoryMappedFileAccess access,
  420. MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability inheritability,
  421. bool leaveOpen)
  422. #endif
  423. {
  424. if (fileStream == null)
  425. throw new ArgumentNullException ("fileStream");
  426. if (mapName != null && mapName.Length == 0)
  427. throw new ArgumentException ("mapName");
  428. if ((!MonoUtil.IsUnix && capacity == 0 && fileStream.Length == 0) || (capacity > fileStream.Length))
  429. throw new ArgumentException ("capacity");
  430. MemoryMapImpl.ConfigureFD (fileStream.Handle, inheritability);
  431. return new MemoryMappedFile () {
  432. stream = fileStream,
  433. fileAccess = access,
  434. name = mapName,
  435. fileCapacity = capacity,
  436. keepOpen = leaveOpen
  437. };
  438. }
  439. [MonoLimitation ("CreateNew requires that mapName be a file name on Unix")]
  440. public static MemoryMappedFile CreateNew (string mapName, long capacity)
  441. {
  442. #if MOBILE
  443. return CreateNew (mapName, capacity, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.DelayAllocatePages, 0);
  444. #else
  445. return CreateNew (mapName, capacity, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.DelayAllocatePages, null, 0);
  446. #endif
  447. }
  448. [MonoLimitation ("CreateNew requires that mapName be a file name on Unix")]
  449. public static MemoryMappedFile CreateNew (string mapName, long capacity, MemoryMappedFileAccess access)
  450. {
  451. #if MOBILE
  452. return CreateNew (mapName, capacity, access, MemoryMappedFileOptions.DelayAllocatePages, 0);
  453. #else
  454. return CreateNew (mapName, capacity, access, MemoryMappedFileOptions.DelayAllocatePages, null, 0);
  455. #endif
  456. }
  457. #if MOBILE
  458. public static MemoryMappedFile CreateNew (string mapName, long capacity, MemoryMappedFileAccess access,
  459. MemoryMappedFileOptions options,
  460. HandleInheritability handleInheritability)
  461. #else
  462. [MonoLimitation ("CreateNew requires that mapName be a file name on Unix; options and memoryMappedFileSecurity are ignored")]
  463. public static MemoryMappedFile CreateNew (string mapName, long capacity, MemoryMappedFileAccess access,
  464. MemoryMappedFileOptions options, MemoryMappedFileSecurity memoryMappedFileSecurity,
  465. HandleInheritability inheritability)
  466. #endif
  467. {
  468. return CreateFromFile (mapName, FileMode.CreateNew, mapName, capacity, access);
  469. }
  470. [MonoLimitation ("CreateOrOpen requires that mapName be a file name on Unix")]
  471. public static MemoryMappedFile CreateOrOpen (string mapName, long capacity)
  472. {
  473. return CreateOrOpen (mapName, capacity, MemoryMappedFileAccess.ReadWrite);
  474. }
  475. [MonoLimitation ("CreateOrOpen requires that mapName be a file name on Unix")]
  476. public static MemoryMappedFile CreateOrOpen (string mapName, long capacity, MemoryMappedFileAccess access)
  477. {
  478. return CreateFromFile (mapName, FileMode.OpenOrCreate, mapName, capacity, access);
  479. }
  480. [MonoTODO]
  481. #if MOBILE
  482. public static MemoryMappedFile CreateOrOpen (string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, HandleInheritability inheritability)
  483. #else
  484. public static MemoryMappedFile CreateOrOpen (string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability inheritability)
  485. #endif
  486. {
  487. throw new NotImplementedException ();
  488. }
  489. [MonoTODO]
  490. public static MemoryMappedFile OpenExisting (string mapName)
  491. {
  492. throw new NotImplementedException ();
  493. }
  494. [MonoTODO]
  495. public static MemoryMappedFile OpenExisting (string mapName, MemoryMappedFileRights desiredAccessRights)
  496. {
  497. throw new NotImplementedException ();
  498. }
  499. [MonoTODO]
  500. public static MemoryMappedFile OpenExisting (string mapName, MemoryMappedFileRights desiredAccessRights, HandleInheritability inheritability)
  501. {
  502. throw new NotImplementedException ();
  503. }
  504. public MemoryMappedViewStream CreateViewStream ()
  505. {
  506. return CreateViewStream (0, 0);
  507. }
  508. public MemoryMappedViewStream CreateViewStream (long offset, long size)
  509. {
  510. return CreateViewStream (offset, size, MemoryMappedFileAccess.ReadWrite);
  511. }
  512. public MemoryMappedViewStream CreateViewStream (long offset, long size, MemoryMappedFileAccess access)
  513. {
  514. return new MemoryMappedViewStream (stream != null ? (int)stream.Handle : unix_fd, offset, size, access);
  515. }
  516. public MemoryMappedViewAccessor CreateViewAccessor ()
  517. {
  518. return CreateViewAccessor (0, 0);
  519. }
  520. public MemoryMappedViewAccessor CreateViewAccessor (long offset, long size)
  521. {
  522. return CreateViewAccessor (offset, size, MemoryMappedFileAccess.ReadWrite);
  523. }
  524. public MemoryMappedViewAccessor CreateViewAccessor (long offset, long size, MemoryMappedFileAccess access)
  525. {
  526. int file_handle = stream != null ? (int) stream.Handle : unix_fd;
  527. return new MemoryMappedViewAccessor (file_handle, offset, size, access);
  528. }
  529. MemoryMappedFile ()
  530. {
  531. }
  532. public void Dispose ()
  533. {
  534. Dispose (true);
  535. }
  536. protected virtual void Dispose (bool disposing)
  537. {
  538. if (disposing){
  539. if (stream != null){
  540. if (keepOpen == false)
  541. stream.Close ();
  542. unix_fd = -1;
  543. stream = null;
  544. }
  545. if (unix_fd != -1) {
  546. MemoryMapImpl.CloseFD (unix_fd);
  547. unix_fd = -1;
  548. }
  549. }
  550. }
  551. #if !MOBILE
  552. [MonoTODO]
  553. public MemoryMappedFileSecurity GetAccessControl ()
  554. {
  555. throw new NotImplementedException ();
  556. }
  557. [MonoTODO]
  558. public void SetAccessControl (MemoryMappedFileSecurity memoryMappedFileSecurity)
  559. {
  560. throw new NotImplementedException ();
  561. }
  562. #endif
  563. [MonoTODO]
  564. public SafeMemoryMappedFileHandle SafeMemoryMappedFileHandle {
  565. get {
  566. throw new NotImplementedException ();
  567. }
  568. }
  569. }
  570. }
  571. #endif