-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryMappedFile.cs
More file actions
301 lines (254 loc) · 10.4 KB
/
Copy pathMemoryMappedFile.cs
File metadata and controls
301 lines (254 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//
// MemoryMappedFile.cs
//
// Implementation of a library to use Win32 Memory Mapped
// Files from within .NET applications
//
// COPYRIGHT (C) 2001, Tomas Restrepo (tomasr@mvps.org)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace Winterdom.IO.FileMap
{
/// <summary>
/// Specifies access for the mapped file.
/// These correspond to the FILE_MAP_XXX
/// constants used by MapViewOfFile[Ex]()
/// </summary>
public enum MapAccess
{
FileMapCopy = 0x0001,
FileMapWrite = 0x0002,
FileMapRead = 0x0004,
FileMapAllAccess = 0x001f,
}
/// <summary>Wrapper class around the Win32 MMF APIs</summary>
/// <remarks>
/// Allows you to easily use memory mapped files on
/// .NET applications.
/// Currently, not all functionality provided by
/// the Win32 system is avaliable. Things that are not
/// supported include:
/// <list>
/// <item>You can't specify security descriptors</item>
/// <item>You can't build the memory mapped file
/// on top of a System.IO.File already opened</item>
/// </list>
/// The class is currently MarshalByRefObject, but I would
/// be careful about possible interactions!
/// </remarks>
public class MemoryMappedFile : MarshalByRefObject, IDisposable
{
//! handle to MemoryMappedFile object
private IntPtr _hMap = IntPtr.Zero;
#region Constants
private const int GENERIC_READ = unchecked((int)0x80000000);
private const int GENERIC_WRITE = unchecked((int)0x40000000);
private const int OPEN_ALWAYS = 4;
private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
private static readonly IntPtr NULL_HANDLE = IntPtr.Zero;
#endregion // Constants
public bool IsOpen {
get { return (_hMap != NULL_HANDLE); }
}
/// <summary>
/// Default constructor
/// </summary>
private MemoryMappedFile()
{
}
/// <summary>
/// Finalizer
/// </summary>
~MemoryMappedFile()
{
Dispose(false);
}
#region Create Overloads
/// <summary>
/// Create an unnamed map object with no file backing
/// </summary>
/// <param name="protection">desired access to the
/// mapping object</param>
/// <param name="maxSize">maximum size of filemap object</param>
/// <param name="name">name of file mapping object</param>
/// <returns>The memory mapped file instance</returns>
public static MemoryMappedFile
Create(MapProtection protection, long maxSize, string name)
{
return Create ( null, protection, maxSize, name );
}
/// <summary>
/// Create an named map object with no file backing
/// </summary>
/// <param name="protection">desired access to the
/// mapping object</param>
/// <param name="maxSize">maximum size of filemap object</param>
/// <returns>The memory mapped file instance</returns>
public static MemoryMappedFile
Create(MapProtection protection, long maxSize)
{
return Create ( null, protection, maxSize, null );
}
/// <summary>
/// Create an unnamed map object with a maximum size
/// equal to that of the file
/// </summary>
/// <param name="fileName">name of backing file</param>
/// <param name="protection">desired access to the
/// mapping object</param>
/// <returns>The memory mapped file instance</returns>
public static MemoryMappedFile
Create(string fileName, MapProtection protection)
{
return Create ( fileName, protection, -1, null );
}
/// <summary>
/// Create an unnamed map object
/// </summary>
/// <param name="fileName">name of backing file</param>
/// <param name="protection">desired access to the
/// mapping object</param>
/// <param name="maxSize">maximum size of filemap
/// object, or -1 for size of file</param>
/// <returns>The memory mapped file instance</returns>
public static MemoryMappedFile
Create ( string fileName, MapProtection protection,
long maxSize )
{
return Create ( fileName, protection, maxSize, null );
}
/// <summary>
/// Create a named map object
/// </summary>
/// <param name="fileName">name of backing file, or null
/// for a pagefile-backed map</param>
/// <param name="protection">desired access to the mapping
/// object</param>
/// <param name="maxSize">maximum size of filemap object, or 0
/// for size of file</param>
/// <param name="name">name of file mapping object</param>
/// <returns>The memory mapped file instance</returns>
public static MemoryMappedFile
Create ( string fileName, MapProtection protection,
long maxSize, String name )
{
MemoryMappedFile map = new MemoryMappedFile();
// open file first
IntPtr hFile = INVALID_HANDLE_VALUE;
if ( fileName != null )
{
// determine file access needed
// we'll always need generic read access
int desiredAccess = GENERIC_READ;
if ( (protection == MapProtection.PageReadWrite) ||
(protection == MapProtection.PageWriteCopy) )
{
desiredAccess |= GENERIC_WRITE;
}
// open or create the file
// if it doesn't exist, it gets created
hFile = Win32MapApis.CreateFile (
fileName, desiredAccess, 0,
IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero
);
if ( hFile == INVALID_HANDLE_VALUE )
throw new FileMapIOException ( Marshal.GetHRForLastWin32Error() );
// FIX: Is support neede for zero-length files!?!
}
map._hMap = Win32MapApis.CreateFileMapping (
hFile, IntPtr.Zero, (int)protection,
(int)((maxSize >> 32) & 0xFFFFFFFF),
(int)(maxSize & 0xFFFFFFFF), name
);
// close file handle, we don't need it
if ( hFile != INVALID_HANDLE_VALUE ) Win32MapApis.CloseHandle(hFile);
if ( map._hMap == NULL_HANDLE )
throw new FileMapIOException ( Marshal.GetHRForLastWin32Error() );
return map;
}
#endregion // Create Overloads
/// <summary>
/// Open an existing named File Mapping object
/// </summary>
/// <param name="access">desired access to the map</param>
/// <param name="name">name of object</param>
/// <returns>The memory mapped file instance</returns>
public static MemoryMappedFile Open ( MapAccess access, String name )
{
MemoryMappedFile map = new MemoryMappedFile();
map._hMap = Win32MapApis.OpenFileMapping ( (int)access, false, name );
if ( map._hMap == NULL_HANDLE )
throw new FileMapIOException ( Marshal.GetHRForLastWin32Error() );
return map;
}
/// <summary>
/// Close this File Mapping object
/// From here on, You can't do anything with it
/// but the open views remain valid.
/// </summary>
public void Close()
{
Dispose(true);
}
/// <summary>
/// Map a view of the file mapping object
/// This returns a stream, giving you easy access to the memory,
/// as you can use StreamReaders and StreamWriters on top of it
/// </summary>
/// <param name="access">desired access to the view</param>
/// <param name="offset">offset of the file mapping object to
/// start view at</param>
/// <param name="size">size of the view</param>
public Stream MapView ( MapAccess access, long offset, int size )
{
if ( !IsOpen )
throw new ObjectDisposedException("MMF already closed");
IntPtr baseAddress = IntPtr.Zero;
baseAddress = Win32MapApis.MapViewOfFile (
_hMap, (int)access,
(int)((offset >> 32) & 0xFFFFFFFF),
(int)(offset & 0xFFFFFFFF), size
);
if ( baseAddress == IntPtr.Zero )
throw new FileMapIOException ( Marshal.GetHRForLastWin32Error() );
// Find out what MapProtection to use
// based on the MapAccess flags...
MapProtection protection;
if ( access == MapAccess.FileMapRead )
protection = MapProtection.PageReadOnly;
else
protection = MapProtection.PageReadWrite;
return new MapViewStream ( baseAddress, size, protection );
}
#region IDisposable implementation
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if ( IsOpen )
Win32MapApis.CloseHandle ( _hMap );
_hMap = NULL_HANDLE;
if ( disposing )
GC.SuppressFinalize(this);
}
#endregion // IDisposable implementation
} // class MemoryMappedFile
} // namespace Winterdom.IO.FileMap