MemoryMappedView.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // MemoryMappedView.cs
  3. //
  4. // Authors:
  5. // Marcos Henrich ([email protected])
  6. //
  7. // Copyright (C) 2015, Xamarin, Inc
  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. using System;
  29. using System.IO;
  30. using System.Diagnostics;
  31. using System.Collections.Generic;
  32. using Microsoft.Win32.SafeHandles;
  33. namespace System.IO.MemoryMappedFiles
  34. {
  35. internal class MemoryMappedView : IDisposable {
  36. private SafeMemoryMappedViewHandle m_viewHandle;
  37. private Int64 m_pointerOffset;
  38. private Int64 m_size;
  39. private MemoryMappedFileAccess m_access;
  40. [System.Security.SecurityCritical]
  41. private unsafe MemoryMappedView(SafeMemoryMappedViewHandle viewHandle, Int64 pointerOffset,
  42. Int64 size, MemoryMappedFileAccess access) {
  43. m_viewHandle = viewHandle;
  44. m_pointerOffset = pointerOffset;
  45. m_size = size;
  46. m_access = access;
  47. }
  48. internal SafeMemoryMappedViewHandle ViewHandle {
  49. [System.Security.SecurityCritical]
  50. get {
  51. return m_viewHandle;
  52. }
  53. }
  54. internal Int64 PointerOffset {
  55. get {
  56. return m_pointerOffset;
  57. }
  58. }
  59. internal Int64 Size {
  60. get {
  61. return m_size;
  62. }
  63. }
  64. internal MemoryMappedFileAccess Access {
  65. get {
  66. return m_access;
  67. }
  68. }
  69. internal unsafe static MemoryMappedView Create (IntPtr handle, long offset, long size, MemoryMappedFileAccess access)
  70. {
  71. IntPtr base_address;
  72. IntPtr mmap_handle;
  73. MemoryMapImpl.Map (handle, offset, ref size, access, out mmap_handle, out base_address);
  74. var safe_handle = new SafeMemoryMappedViewHandle (mmap_handle, base_address, size);
  75. // MemoryMapImpl.Map returns a base_address to the offset so MemoryMappedView is initiated
  76. // no offset.
  77. return new MemoryMappedView (safe_handle, 0, size, access);
  78. }
  79. public void Flush (IntPtr capacity)
  80. {
  81. MemoryMapImpl.Flush (m_viewHandle.DangerousGetHandle ());
  82. }
  83. protected virtual void Dispose (bool disposing)
  84. {
  85. if (m_viewHandle != null && !m_viewHandle.IsClosed) {
  86. m_viewHandle.Dispose ();
  87. }
  88. }
  89. public void Dispose ()
  90. {
  91. Dispose (true);
  92. GC.SuppressFinalize (this);
  93. }
  94. internal bool IsClosed {
  95. get {
  96. return (m_viewHandle == null || m_viewHandle.IsClosed);
  97. }
  98. }
  99. }
  100. }