Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
MemoryMappedFile.cs
Go to the documentation of this file.
5
7
9{
11
12 private readonly bool _leaveOpen;
13
14 private readonly FileStream _fileStream;
15
17
23
24 private MemoryMappedFile(SafeMemoryMappedFileHandle handle, FileStream fileStream, bool leaveOpen)
25 {
27 _fileStream = fileStream;
28 _leaveOpen = leaveOpen;
29 }
30
31 [SupportedOSPlatform("windows")]
32 public static MemoryMappedFile OpenExisting(string mapName)
33 {
34 return OpenExisting(mapName, MemoryMappedFileRights.ReadWrite, HandleInheritability.None);
35 }
36
37 [SupportedOSPlatform("windows")]
38 public static MemoryMappedFile OpenExisting(string mapName, MemoryMappedFileRights desiredAccessRights)
39 {
40 return OpenExisting(mapName, desiredAccessRights, HandleInheritability.None);
41 }
42
43 [SupportedOSPlatform("windows")]
44 public static MemoryMappedFile OpenExisting(string mapName, MemoryMappedFileRights desiredAccessRights, HandleInheritability inheritability)
45 {
46 if (mapName == null)
47 {
49 }
50 if (mapName.Length == 0)
51 {
53 }
54 if (inheritability < HandleInheritability.None || inheritability > HandleInheritability.Inheritable)
55 {
56 throw new ArgumentOutOfRangeException("inheritability");
57 }
58 if (((uint)desiredAccessRights & 0xFEF0FFF0u) != 0)
59 {
60 throw new ArgumentOutOfRangeException("desiredAccessRights");
61 }
62 SafeMemoryMappedFileHandle handle = OpenCore(mapName, inheritability, desiredAccessRights, createOrOpen: false);
63 return new MemoryMappedFile(handle);
64 }
65
66 public static MemoryMappedFile CreateFromFile(string path)
67 {
68 return CreateFromFile(path, FileMode.Open, null, 0L, MemoryMappedFileAccess.ReadWrite);
69 }
70
71 public static MemoryMappedFile CreateFromFile(string path, FileMode mode)
72 {
73 return CreateFromFile(path, mode, null, 0L, MemoryMappedFileAccess.ReadWrite);
74 }
75
76 public static MemoryMappedFile CreateFromFile(string path, FileMode mode, string? mapName)
77 {
78 return CreateFromFile(path, mode, mapName, 0L, MemoryMappedFileAccess.ReadWrite);
79 }
80
81 public static MemoryMappedFile CreateFromFile(string path, FileMode mode, string? mapName, long capacity)
82 {
83 return CreateFromFile(path, mode, mapName, capacity, MemoryMappedFileAccess.ReadWrite);
84 }
85
86 public static MemoryMappedFile CreateFromFile(string path, FileMode mode, string? mapName, long capacity, MemoryMappedFileAccess access)
87 {
88 if (path == null)
89 {
90 throw new ArgumentNullException("path");
91 }
92 if (mapName != null && mapName.Length == 0)
93 {
95 }
96 if (capacity < 0)
97 {
99 }
100 if (access < MemoryMappedFileAccess.ReadWrite || access > MemoryMappedFileAccess.ReadWriteExecute)
101 {
102 throw new ArgumentOutOfRangeException("access");
103 }
104 switch (mode)
105 {
106 case FileMode.Append:
108 case FileMode.Truncate:
110 default:
111 {
112 if (access == MemoryMappedFileAccess.Write)
113 {
115 }
116 bool existed = File.Exists(path);
117 FileStream fileStream = new FileStream(path, mode, GetFileAccess(access), FileShare.Read, 4096, FileOptions.None);
118 if (capacity == 0L && fileStream.Length == 0L)
119 {
120 CleanupFile(fileStream, existed, path);
122 }
123 if (capacity == 0L)
124 {
125 capacity = fileStream.Length;
126 }
127 SafeMemoryMappedFileHandle safeMemoryMappedFileHandle = null;
128 try
129 {
130 safeMemoryMappedFileHandle = CreateCore(fileStream, mapName, HandleInheritability.None, access, MemoryMappedFileOptions.None, capacity);
131 }
132 catch
133 {
134 CleanupFile(fileStream, existed, path);
135 throw;
136 }
137 return new MemoryMappedFile(safeMemoryMappedFileHandle, fileStream, leaveOpen: false);
138 }
139 }
140 }
141
142 public static MemoryMappedFile CreateFromFile(FileStream fileStream, string? mapName, long capacity, MemoryMappedFileAccess access, HandleInheritability inheritability, bool leaveOpen)
143 {
144 if (fileStream == null)
145 {
147 }
148 if (mapName != null && mapName.Length == 0)
149 {
151 }
152 if (capacity < 0)
153 {
155 }
156 if (capacity == 0L && fileStream.Length == 0L)
157 {
159 }
160 switch (access)
161 {
162 default:
163 throw new ArgumentOutOfRangeException("access");
164 case MemoryMappedFileAccess.Write:
166 case MemoryMappedFileAccess.ReadWrite:
167 case MemoryMappedFileAccess.Read:
168 case MemoryMappedFileAccess.CopyOnWrite:
169 case MemoryMappedFileAccess.ReadExecute:
170 case MemoryMappedFileAccess.ReadWriteExecute:
171 {
172 if (inheritability < HandleInheritability.None || inheritability > HandleInheritability.Inheritable)
173 {
174 throw new ArgumentOutOfRangeException("inheritability");
175 }
176 fileStream.Flush();
177 if (capacity == 0L)
178 {
179 capacity = fileStream.Length;
180 }
181 SafeMemoryMappedFileHandle handle = CreateCore(fileStream, mapName, inheritability, access, MemoryMappedFileOptions.None, capacity);
182 return new MemoryMappedFile(handle, fileStream, leaveOpen);
183 }
184 }
185 }
186
187 public static MemoryMappedFile CreateNew(string? mapName, long capacity)
188 {
190 }
191
192 public static MemoryMappedFile CreateNew(string? mapName, long capacity, MemoryMappedFileAccess access)
193 {
194 return CreateNew(mapName, capacity, access, MemoryMappedFileOptions.None, HandleInheritability.None);
195 }
196
198 {
199 if (mapName != null && mapName.Length == 0)
200 {
202 }
203 if (capacity <= 0)
204 {
206 }
207 if (IntPtr.Size == 4 && capacity > uint.MaxValue)
208 {
210 }
211 switch (access)
212 {
213 default:
214 throw new ArgumentOutOfRangeException("access");
215 case MemoryMappedFileAccess.Write:
217 case MemoryMappedFileAccess.ReadWrite:
218 case MemoryMappedFileAccess.Read:
219 case MemoryMappedFileAccess.CopyOnWrite:
220 case MemoryMappedFileAccess.ReadExecute:
221 case MemoryMappedFileAccess.ReadWriteExecute:
222 {
223 if (((uint)options & 0xFBFFFFFFu) != 0)
224 {
225 throw new ArgumentOutOfRangeException("options");
226 }
227 if (inheritability < HandleInheritability.None || inheritability > HandleInheritability.Inheritable)
228 {
229 throw new ArgumentOutOfRangeException("inheritability");
230 }
231 SafeMemoryMappedFileHandle handle = CreateCore(null, mapName, inheritability, access, options, capacity);
232 return new MemoryMappedFile(handle);
233 }
234 }
235 }
236
237 [SupportedOSPlatform("windows")]
238 public static MemoryMappedFile CreateOrOpen(string mapName, long capacity)
239 {
241 }
242
243 [SupportedOSPlatform("windows")]
244 public static MemoryMappedFile CreateOrOpen(string mapName, long capacity, MemoryMappedFileAccess access)
245 {
246 return CreateOrOpen(mapName, capacity, access, MemoryMappedFileOptions.None, HandleInheritability.None);
247 }
248
249 [SupportedOSPlatform("windows")]
251 {
252 if (mapName == null)
253 {
255 }
256 if (mapName.Length == 0)
257 {
259 }
260 if (capacity <= 0)
261 {
263 }
264 if (IntPtr.Size == 4 && capacity > uint.MaxValue)
265 {
267 }
268 if (access < MemoryMappedFileAccess.ReadWrite || access > MemoryMappedFileAccess.ReadWriteExecute)
269 {
270 throw new ArgumentOutOfRangeException("access");
271 }
272 if (((uint)options & 0xFBFFFFFFu) != 0)
273 {
274 throw new ArgumentOutOfRangeException("options");
275 }
276 if (inheritability < HandleInheritability.None || inheritability > HandleInheritability.Inheritable)
277 {
278 throw new ArgumentOutOfRangeException("inheritability");
279 }
280 SafeMemoryMappedFileHandle handle = ((access != MemoryMappedFileAccess.Write) ? CreateOrOpenCore(mapName, inheritability, access, options, capacity) : OpenCore(mapName, inheritability, access, createOrOpen: true));
281 return new MemoryMappedFile(handle);
282 }
283
285 {
286 return CreateViewStream(0L, 0L, MemoryMappedFileAccess.ReadWrite);
287 }
288
290 {
291 return CreateViewStream(offset, size, MemoryMappedFileAccess.ReadWrite);
292 }
293
295 {
296 if (offset < 0)
297 {
299 }
300 if (size < 0)
301 {
303 }
304 if (access < MemoryMappedFileAccess.ReadWrite || access > MemoryMappedFileAccess.ReadWriteExecute)
305 {
306 throw new ArgumentOutOfRangeException("access");
307 }
308 if (IntPtr.Size == 4 && size > uint.MaxValue)
309 {
311 }
313 return new MemoryMappedViewStream(view);
314 }
315
320
322 {
323 return CreateViewAccessor(offset, size, MemoryMappedFileAccess.ReadWrite);
324 }
325
327 {
328 if (offset < 0)
329 {
331 }
332 if (size < 0)
333 {
335 }
336 if (access < MemoryMappedFileAccess.ReadWrite || access > MemoryMappedFileAccess.ReadWriteExecute)
337 {
338 throw new ArgumentOutOfRangeException("access");
339 }
340 if (IntPtr.Size == 4 && size > uint.MaxValue)
341 {
343 }
345 return new MemoryMappedViewAccessor(view);
346 }
347
348 public void Dispose()
349 {
350 Dispose(disposing: true);
351 GC.SuppressFinalize(this);
352 }
353
354 protected virtual void Dispose(bool disposing)
355 {
356 try
357 {
358 if (!_handle.IsClosed)
359 {
361 }
362 }
363 finally
364 {
365 if (_fileStream != null && !_leaveOpen)
366 {
368 }
369 }
370 }
371
373 {
374 switch (access)
375 {
376 case MemoryMappedFileAccess.Read:
377 case MemoryMappedFileAccess.ReadExecute:
378 return FileAccess.Read;
379 case MemoryMappedFileAccess.ReadWrite:
380 case MemoryMappedFileAccess.CopyOnWrite:
381 case MemoryMappedFileAccess.ReadWriteExecute:
382 return FileAccess.ReadWrite;
383 default:
384 return FileAccess.Write;
385 }
386 }
387
388 private static void CleanupFile(FileStream fileStream, bool existed, string path)
389 {
390 fileStream.Dispose();
391 if (!existed)
392 {
393 File.Delete(path);
394 }
395 }
396
397 private static void VerifyMemoryMappedFileAccess(MemoryMappedFileAccess access, long capacity, FileStream fileStream)
398 {
399 if (access == MemoryMappedFileAccess.Read && capacity > fileStream.Length)
400 {
402 }
403 if (fileStream.Length > capacity)
404 {
406 }
407 }
408
410 {
411 SafeFileHandle safeFileHandle = fileStream?.SafeFileHandle;
412 global::Interop.Kernel32.SECURITY_ATTRIBUTES securityAttributes = GetSecAttrs(inheritability);
413 if (fileStream != null)
414 {
415 VerifyMemoryMappedFileAccess(access, capacity, fileStream);
416 }
417 SafeMemoryMappedFileHandle safeMemoryMappedFileHandle = ((safeFileHandle != null) ? global::Interop.CreateFileMapping(safeFileHandle, ref securityAttributes, GetPageAccess(access) | (int)options, capacity, mapName) : global::Interop.CreateFileMapping(new IntPtr(-1), ref securityAttributes, GetPageAccess(access) | (int)options, capacity, mapName));
418 int lastPInvokeError = Marshal.GetLastPInvokeError();
419 if (!safeMemoryMappedFileHandle.IsInvalid)
420 {
421 if (lastPInvokeError == 183)
422 {
423 safeMemoryMappedFileHandle.Dispose();
424 throw System.IO.Win32Marshal.GetExceptionForWin32Error(lastPInvokeError);
425 }
426 return safeMemoryMappedFileHandle;
427 }
428 safeMemoryMappedFileHandle.Dispose();
429 throw System.IO.Win32Marshal.GetExceptionForWin32Error(lastPInvokeError);
430 }
431
432 private static SafeMemoryMappedFileHandle OpenCore(string mapName, HandleInheritability inheritability, MemoryMappedFileAccess access, bool createOrOpen)
433 {
434 return OpenCore(mapName, inheritability, GetFileMapAccess(access), createOrOpen);
435 }
436
437 private static SafeMemoryMappedFileHandle OpenCore(string mapName, HandleInheritability inheritability, MemoryMappedFileRights rights, bool createOrOpen)
438 {
439 return OpenCore(mapName, inheritability, GetFileMapAccess(rights), createOrOpen);
440 }
441
443 {
444 SafeMemoryMappedFileHandle safeMemoryMappedFileHandle = null;
445 global::Interop.Kernel32.SECURITY_ATTRIBUTES securityAttributes = GetSecAttrs(inheritability);
446 int num = 14;
447 int num2 = 0;
448 while (num > 0)
449 {
450 safeMemoryMappedFileHandle = global::Interop.CreateFileMapping(new IntPtr(-1), ref securityAttributes, GetPageAccess(access) | (int)options, capacity, mapName);
451 if (!safeMemoryMappedFileHandle.IsInvalid)
452 {
453 break;
454 }
455 safeMemoryMappedFileHandle.Dispose();
456 int lastPInvokeError = Marshal.GetLastPInvokeError();
457 if (lastPInvokeError != 5)
458 {
459 throw System.IO.Win32Marshal.GetExceptionForWin32Error(lastPInvokeError);
460 }
461 safeMemoryMappedFileHandle = global::Interop.OpenFileMapping(GetFileMapAccess(access), (inheritability & HandleInheritability.Inheritable) != 0, mapName);
462 if (!safeMemoryMappedFileHandle.IsInvalid)
463 {
464 break;
465 }
466 safeMemoryMappedFileHandle.Dispose();
467 int lastPInvokeError2 = Marshal.GetLastPInvokeError();
468 if (lastPInvokeError2 != 2)
469 {
470 throw System.IO.Win32Marshal.GetExceptionForWin32Error(lastPInvokeError2);
471 }
472 num--;
473 if (num2 == 0)
474 {
475 num2 = 10;
476 continue;
477 }
478 Thread.Sleep(num2);
479 num2 *= 2;
480 }
481 if (safeMemoryMappedFileHandle == null || safeMemoryMappedFileHandle.IsInvalid)
482 {
484 }
485 return safeMemoryMappedFileHandle;
486 }
487
488 private static int GetFileMapAccess(MemoryMappedFileRights rights)
489 {
490 return (int)rights;
491 }
492
493 internal static int GetFileMapAccess(MemoryMappedFileAccess access)
494 {
495 return access switch
496 {
497 MemoryMappedFileAccess.Read => 4,
498 MemoryMappedFileAccess.Write => 2,
499 MemoryMappedFileAccess.ReadWrite => 6,
500 MemoryMappedFileAccess.CopyOnWrite => 1,
501 MemoryMappedFileAccess.ReadExecute => 36,
502 _ => 38,
503 };
504 }
505
506 internal static int GetPageAccess(MemoryMappedFileAccess access)
507 {
508 return access switch
509 {
510 MemoryMappedFileAccess.Read => 2,
511 MemoryMappedFileAccess.ReadWrite => 4,
512 MemoryMappedFileAccess.CopyOnWrite => 8,
513 MemoryMappedFileAccess.ReadExecute => 32,
514 _ => 64,
515 };
516 }
517
518 private static SafeMemoryMappedFileHandle OpenCore(string mapName, HandleInheritability inheritability, int desiredAccessRights, bool createOrOpen)
519 {
520 SafeMemoryMappedFileHandle safeMemoryMappedFileHandle = global::Interop.OpenFileMapping(desiredAccessRights, (inheritability & HandleInheritability.Inheritable) != 0, mapName);
521 int lastWin32Error = Marshal.GetLastWin32Error();
522 if (safeMemoryMappedFileHandle.IsInvalid)
523 {
524 safeMemoryMappedFileHandle.Dispose();
525 if (createOrOpen && lastWin32Error == 2)
526 {
528 }
529 throw System.IO.Win32Marshal.GetExceptionForWin32Error(lastWin32Error);
530 }
531 return safeMemoryMappedFileHandle;
532 }
533
534 private unsafe static global::Interop.Kernel32.SECURITY_ATTRIBUTES GetSecAttrs(HandleInheritability inheritability)
535 {
536 global::Interop.Kernel32.SECURITY_ATTRIBUTES result = default(global::Interop.Kernel32.SECURITY_ATTRIBUTES);
537 if ((inheritability & HandleInheritability.Inheritable) != 0)
538 {
539 result = default(global::Interop.Kernel32.SECURITY_ATTRIBUTES);
540 result.nLength = (uint)sizeof(global::Interop.Kernel32.SECURITY_ATTRIBUTES);
541 result.bInheritHandle = global::Interop.BOOL.TRUE;
542 }
543 return result;
544 }
545}
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
override long Length
Definition FileStream.cs:28
virtual SafeFileHandle SafeFileHandle
Definition FileStream.cs:21
override void Flush()
override void Dispose(bool disposing)
static bool Exists([NotNullWhen(true)] string? path)
Definition File.cs:97
static void Delete(string path)
Definition File.cs:88
static MemoryMappedFile CreateFromFile(string path, FileMode mode)
static SafeMemoryMappedFileHandle CreateOrOpenCore(string mapName, HandleInheritability inheritability, MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity)
static SafeMemoryMappedFileHandle OpenCore(string mapName, HandleInheritability inheritability, MemoryMappedFileAccess access, bool createOrOpen)
static MemoryMappedFile OpenExisting(string mapName, MemoryMappedFileRights desiredAccessRights)
static void VerifyMemoryMappedFileAccess(MemoryMappedFileAccess access, long capacity, FileStream fileStream)
MemoryMappedViewAccessor CreateViewAccessor(long offset, long size, MemoryMappedFileAccess access)
MemoryMappedViewAccessor CreateViewAccessor(long offset, long size)
static MemoryMappedFile OpenExisting(string mapName)
static int GetFileMapAccess(MemoryMappedFileRights rights)
static MemoryMappedFile CreateOrOpen(string mapName, long capacity, MemoryMappedFileAccess access)
static MemoryMappedFile CreateOrOpen(string mapName, long capacity)
static MemoryMappedFile CreateOrOpen(string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, HandleInheritability inheritability)
static MemoryMappedFile OpenExisting(string mapName, MemoryMappedFileRights desiredAccessRights, HandleInheritability inheritability)
static SafeMemoryMappedFileHandle OpenCore(string mapName, HandleInheritability inheritability, int desiredAccessRights, bool createOrOpen)
static MemoryMappedFile CreateFromFile(FileStream fileStream, string? mapName, long capacity, MemoryMappedFileAccess access, HandleInheritability inheritability, bool leaveOpen)
static MemoryMappedFile CreateNew(string? mapName, long capacity, MemoryMappedFileAccess access)
MemoryMappedViewStream CreateViewStream(long offset, long size)
static unsafe global::Interop.Kernel32.SECURITY_ATTRIBUTES GetSecAttrs(HandleInheritability inheritability)
MemoryMappedFile(SafeMemoryMappedFileHandle handle)
static int GetFileMapAccess(MemoryMappedFileAccess access)
static void CleanupFile(FileStream fileStream, bool existed, string path)
static SafeMemoryMappedFileHandle CreateCore(FileStream fileStream, string mapName, HandleInheritability inheritability, MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity)
static MemoryMappedFile CreateNew(string? mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, HandleInheritability inheritability)
static FileAccess GetFileAccess(MemoryMappedFileAccess access)
MemoryMappedFile(SafeMemoryMappedFileHandle handle, FileStream fileStream, bool leaveOpen)
static MemoryMappedFile CreateFromFile(string path, FileMode mode, string? mapName, long capacity, MemoryMappedFileAccess access)
static int GetPageAccess(MemoryMappedFileAccess access)
MemoryMappedViewStream CreateViewStream(long offset, long size, MemoryMappedFileAccess access)
readonly SafeMemoryMappedFileHandle _handle
static SafeMemoryMappedFileHandle OpenCore(string mapName, HandleInheritability inheritability, MemoryMappedFileRights rights, bool createOrOpen)
static MemoryMappedFile CreateNew(string? mapName, long capacity)
static MemoryMappedFile CreateFromFile(string path, FileMode mode, string? mapName)
static MemoryMappedFile CreateFromFile(string path, FileMode mode, string? mapName, long capacity)
static MemoryMappedFile CreateFromFile(string path)
static MemoryMappedView CreateView(SafeMemoryMappedFileHandle memMappedFileHandle, MemoryMappedFileAccess access, long offset, long size)
static Exception GetExceptionForWin32Error(int errorCode, string path="")
static string InvalidOperation_CantCreateFileMapping
Definition SR.cs:66
static string Argument_NewMMFWriteAccessNotAllowed
Definition SR.cs:42
static string Argument_ReadAccessWithLargeCapacity
Definition SR.cs:44
static string ArgumentNull_FileStream
Definition SR.cs:52
static string ArgumentOutOfRange_PositiveOrDefaultSizeRequired
Definition SR.cs:60
static string Argument_NewMMFTruncateModeNotAllowed
Definition SR.cs:48
static string Argument_MapNameEmptyString
Definition SR.cs:38
static string ArgumentOutOfRange_PositiveOrDefaultCapacityRequired
Definition SR.cs:58
static string ArgumentOutOfRange_CapacityLargerThanLogicalAddressSpaceNotAllowed
Definition SR.cs:54
static string ArgumentNull_MapName
Definition SR.cs:50
static string Argument_NewMMFAppendModeNotAllowed
Definition SR.cs:46
static string ArgumentOutOfRange_CapacityGEFileSizeRequired
Definition SR.cs:62
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
static string ArgumentOutOfRange_NeedPositiveNumber
Definition SR.cs:56
static string Argument_EmptyFile
Definition SR.cs:40
Definition SR.cs:7
static void Sleep(int millisecondsTimeout)
Definition Thread.cs:658
static int Size
Definition IntPtr.cs:21