本文整理汇总了C#中MemoryMappedFileAccess类的典型用法代码示例。如果您正苦于以下问题:C# MemoryMappedFileAccess类的具体用法?C# MemoryMappedFileAccess怎么用?C# MemoryMappedFileAccess使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MemoryMappedFileAccess类属于命名空间,在下文中一共展示了MemoryMappedFileAccess类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MemoryMappedViewAccessor
internal MemoryMappedViewAccessor (FileStream file, long offset, long size, MemoryMappedFileAccess access) {
monitor = new Object ();
if (Environment.OSVersion.Platform < PlatformID.Unix)
throw new NotImplementedException ("Not implemented on windows.");
else
CreatePosix (file, offset, size, access);
}
示例2: CreateCore
private static SafeMemoryMappedFileHandle CreateCore(
SafeFileHandle fileHandle, string mapName, HandleInheritability inheritability,
MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity)
{
Interop.SECURITY_ATTRIBUTES secAttrs = GetSecAttrs(inheritability);
// split the long into two ints
int capacityLow = unchecked((int)(capacity & 0x00000000FFFFFFFFL));
int capacityHigh = unchecked((int)(capacity >> 32));
SafeMemoryMappedFileHandle handle = fileHandle != null ?
Interop.mincore.CreateFileMapping(fileHandle, ref secAttrs, GetPageAccess(access) | (int)options, capacityHigh, capacityLow, mapName) :
Interop.mincore.CreateFileMapping(Interop.INVALID_HANDLE_VALUE, ref secAttrs, GetPageAccess(access) | (int)options, capacityHigh, capacityLow, mapName);
int errorCode = Marshal.GetLastWin32Error();
if (!handle.IsInvalid)
{
if (errorCode == Interop.ERROR_ALREADY_EXISTS)
{
handle.Dispose();
throw Win32Marshal.GetExceptionForWin32Error(errorCode);
}
}
else if (handle.IsInvalid)
{
throw Win32Marshal.GetExceptionForWin32Error(errorCode);
}
return handle;
}
示例3: ValidateMemoryMappedFile
/// <summary>Performs basic verification on a map.</summary>
/// <param name="mmf">The map.</param>
/// <param name="expectedCapacity">The capacity that was specified to create the map.</param>
/// <param name="expectedAccess">The access specified to create the map.</param>
/// <param name="expectedInheritability">The inheritability specified to create the map.</param>
protected static void ValidateMemoryMappedFile(MemoryMappedFile mmf,
long expectedCapacity,
MemoryMappedFileAccess expectedAccess = MemoryMappedFileAccess.ReadWrite,
HandleInheritability expectedInheritability = HandleInheritability.None)
{
// Validate that we got a MemoryMappedFile object and that its handle is valid
Assert.NotNull(mmf);
Assert.NotNull(mmf.SafeMemoryMappedFileHandle);
Assert.Same(mmf.SafeMemoryMappedFileHandle, mmf.SafeMemoryMappedFileHandle);
Assert.False(mmf.SafeMemoryMappedFileHandle.IsClosed);
Assert.False(mmf.SafeMemoryMappedFileHandle.IsInvalid);
AssertInheritability(mmf.SafeMemoryMappedFileHandle, expectedInheritability);
// Create and validate one or more views from the map
if (IsReadable(expectedAccess) && IsWritable(expectedAccess))
{
CreateAndValidateViews(mmf, expectedCapacity, MemoryMappedFileAccess.Read);
CreateAndValidateViews(mmf, expectedCapacity, MemoryMappedFileAccess.Write);
CreateAndValidateViews(mmf, expectedCapacity, MemoryMappedFileAccess.ReadWrite);
}
else if (IsWritable(expectedAccess))
{
CreateAndValidateViews(mmf, expectedCapacity, MemoryMappedFileAccess.Write);
}
else if (IsReadable(expectedAccess))
{
CreateAndValidateViews(mmf, expectedCapacity, MemoryMappedFileAccess.Read);
}
else
{
Assert.Throws<UnauthorizedAccessException>(() => mmf.CreateViewAccessor(0, expectedCapacity, MemoryMappedFileAccess.Read));
Assert.Throws<UnauthorizedAccessException>(() => mmf.CreateViewAccessor(0, expectedCapacity, MemoryMappedFileAccess.Write));
Assert.Throws<UnauthorizedAccessException>(() => mmf.CreateViewAccessor(0, expectedCapacity, MemoryMappedFileAccess.ReadWrite));
}
}
示例4: CreateViewStream
public Stream CreateViewStream(long offset, long size, MemoryMappedFileAccess access)
{
IDisposable context = Acquire();
DisposableStream result = new DisposableStream(_mmf.CreateViewStream(offset, size, access));
result.AfterDispose.Add(context);
return result;
}
示例5: MemoryMappedView
private MemoryMappedView(SafeMemoryMappedViewHandle viewHandle, long pointerOffset, long size, MemoryMappedFileAccess access)
{
this.m_viewHandle = viewHandle;
this.m_pointerOffset = pointerOffset;
this.m_size = size;
this.m_access = access;
}
示例6: CreateCore
private static SafeMemoryMappedFileHandle CreateCore(
FileStream fileStream, string mapName, HandleInheritability inheritability,
MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity)
{
SafeFileHandle fileHandle = fileStream != null ? fileStream.SafeFileHandle : null;
Interop.Kernel32.SECURITY_ATTRIBUTES secAttrs = GetSecAttrs(inheritability);
SafeMemoryMappedFileHandle handle = fileHandle != null ?
Interop.CreateFileMapping(fileHandle, ref secAttrs, GetPageAccess(access) | (int)options, capacity, mapName) :
Interop.CreateFileMapping(INVALID_HANDLE_VALUE, ref secAttrs, GetPageAccess(access) | (int)options, capacity, mapName);
int errorCode = Marshal.GetLastWin32Error();
if (!handle.IsInvalid)
{
if (errorCode == Interop.Errors.ERROR_ALREADY_EXISTS)
{
handle.Dispose();
throw Win32Marshal.GetExceptionForWin32Error(errorCode);
}
}
else // handle.IsInvalid
{
handle.Dispose();
throw Win32Marshal.GetExceptionForWin32Error(errorCode);
}
return handle;
}
示例7: CreateCore
private static unsafe SafeMemoryMappedFileHandle CreateCore(
FileStream fileStream, string mapName,
HandleInheritability inheritability, MemoryMappedFileAccess access,
MemoryMappedFileOptions options, long capacity)
{
if (mapName != null)
{
// Named maps are not supported in our Unix implementation. We could support named maps on Linux using
// shared memory segments (shmget/shmat/shmdt/shmctl/etc.), but that doesn't work on OSX by default due
// to very low default limits on OSX for the size of such objects; it also doesn't support behaviors
// like copy-on-write or the ability to control handle inheritability, and reliably cleaning them up
// relies on some non-conforming behaviors around shared memory IDs remaining valid even after they've
// been marked for deletion (IPC_RMID). We could also support named maps using the current implementation
// by not unlinking after creating the backing store, but then the backing stores would remain around
// and accessible even after process exit, with no good way to appropriately clean them up.
// (File-backed maps may still be used for cross-process communication.)
throw CreateNamedMapsNotSupportedException();
}
bool ownsFileStream = false;
if (fileStream != null)
{
// This map is backed by a file. Make sure the file's size is increased to be
// at least as big as the requested capacity of the map.
if (fileStream.Length < capacity)
{
try
{
fileStream.SetLength(capacity);
}
catch (ArgumentException exc)
{
// If the capacity is too large, we'll get an ArgumentException from SetLength,
// but on Windows this same condition is represented by an IOException.
throw new IOException(exc.Message, exc);
}
}
}
else
{
// This map is backed by memory-only. With files, multiple views over the same map
// will end up being able to share data through the same file-based backing store;
// for anonymous maps, we need a similar backing store, or else multiple views would logically
// each be their own map and wouldn't share any data. To achieve this, we create a backing object
// (either memory or on disk, depending on the system) and use its file descriptor as the file handle.
// However, we only do this when the permission is more than read-only. We can't change the size
// of an object that has read-only permissions, but we also don't need to worry about sharing
// views over a read-only, anonymous, memory-backed map, because the data will never change, so all views
// will always see zero and can't change that. In that case, we just use the built-in anonymous support of
// the map by leaving fileStream as null.
Interop.libc.MemoryMappedProtections protections = MemoryMappedView.GetProtections(access, forVerification: false);
if ((protections & Interop.libc.MemoryMappedProtections.PROT_WRITE) != 0 && capacity > 0)
{
ownsFileStream = true;
fileStream = CreateSharedBackingObject(protections, capacity);
}
}
return new SafeMemoryMappedFileHandle(fileStream, ownsFileStream, inheritability, access, options, capacity);
}
示例8: CreateStreamPosix
unsafe void CreateStreamPosix (FileStream file, long offset, long size, MemoryMappedFileAccess access) {
long fsize = file.Length;
if (size == 0 || size > fsize)
size = fsize;
int offset_diff;
MemoryMappedFile.MapPosix (file, offset, size, access, out mmap_addr, out offset_diff, out mmap_size);
FileAccess faccess;
switch (access) {
case MemoryMappedFileAccess.ReadWrite:
faccess = FileAccess.ReadWrite;
break;
case MemoryMappedFileAccess.Read:
faccess = FileAccess.Read;
break;
case MemoryMappedFileAccess.Write:
faccess = FileAccess.Write;
break;
default:
throw new NotImplementedException ("access mode " + access + " not supported.");
}
Initialize ((byte*)mmap_addr + offset_diff, size, size, faccess);
}
示例9: MemoryMappedViewStream
internal MemoryMappedViewStream (int fd, long offset, long size, MemoryMappedFileAccess access) {
this.fd = fd;
monitor = new Object ();
if (MonoUtil.IsUnix)
CreateStreamPosix (fd, offset, size, access);
else
throw new NotImplementedException ("Not implemented on windows.");
}
示例10: MemoryMappedFileCommunicator
public MemoryMappedFileCommunicator(MemoryMappedFile rpMemoryMappedFile, long rpOffset, long rpSize, MemoryMappedFileAccess rpAccess)
{
r_MemoryMappedFile = rpMemoryMappedFile;
r_ViewAccessor = rpMemoryMappedFile.CreateViewAccessor(rpOffset, rpSize, rpAccess);
ReadPosition = -1;
r_WritePosition = -1;
}
示例11: MemoryMappedViewAccessor
internal MemoryMappedViewAccessor (int file_handle, long offset, long size, MemoryMappedFileAccess access)
{
this.file_handle = file_handle;
if (MonoUtil.IsUnix)
CreatePosix (offset, size, access);
else
throw new NotImplementedException ("Not implemented on windows.");
}
示例12: MemoryMappedView
private unsafe MemoryMappedView(SafeMemoryMappedViewHandle viewHandle, Int64 pointerOffset,
Int64 size, MemoryMappedFileAccess access) {
m_viewHandle = viewHandle;
m_pointerOffset = pointerOffset;
m_size = size;
m_access = access;
}
示例13: MemoryMappedView
private unsafe MemoryMappedView(SafeMemoryMappedViewHandle viewHandle, long pointerOffset,
long size, MemoryMappedFileAccess access)
{
_viewHandle = viewHandle;
_pointerOffset = pointerOffset;
_size = size;
_access = access;
}
示例14: ValidAccessLevelCombinations
public void ValidAccessLevelCombinations(MemoryMappedFileAccess mapAccess, MemoryMappedFileAccess viewAccess)
{
const int Capacity = 4096;
using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, Capacity, mapAccess))
using (MemoryMappedViewStream s = mmf.CreateViewStream(0, Capacity, viewAccess))
{
ValidateMemoryMappedViewStream(s, Capacity, viewAccess);
}
}
示例15: CreateOrOpenCore
private static SafeMemoryMappedFileHandle CreateOrOpenCore(
string mapName,
HandleInheritability inheritability, MemoryMappedFileAccess access,
MemoryMappedFileOptions options, long capacity)
{
// Since we don't support mapName != null, CreateOrOpenCore can't
// be used to Open an existing map, and thus is identical to CreateCore.
return CreateCore(null, mapName, inheritability, access, options, capacity);
}