MemoryMappedFile.cs 19 KB

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