1 using Microsoft.Win32.SafeHandles;
10 [__DynamicallyInvokable]
11 public abstract class SafeBuffer : SafeHandleZeroOrMinusOneIsInvalid
21 [__DynamicallyInvokable]
24 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
25 [__DynamicallyInvokable]
28 if (_numBytes == Uninitialized)
30 throw NotInitialized();
32 return (ulong)_numBytes;
39 [__DynamicallyInvokable]
43 _numBytes = Uninitialized;
52 [__DynamicallyInvokable]
59 if (
IntPtr.
Size == 4 && numBytes > uint.MaxValue)
63 if (numBytes >= (ulong)Uninitialized)
78 [__DynamicallyInvokable]
79 public void Initialize(uint numElements, uint sizeOfEachElement)
85 if (sizeOfEachElement < 0)
89 if (
IntPtr.
Size == 4 && numElements * sizeOfEachElement > uint.MaxValue)
93 if (numElements * sizeOfEachElement >= (ulong)Uninitialized)
97 _numBytes = (
UIntPtr)checked(numElements * sizeOfEachElement);
106 [CLSCompliant(
false)]
107 [__DynamicallyInvokable]
116 [CLSCompliant(
false)]
117 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail)]
120 if (_numBytes == Uninitialized)
122 throw NotInitialized();
131 bool success =
false;
132 DangerousAddRef(ref success);
133 pointer = (
byte*)(
void*)handle;
139 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
140 [__DynamicallyInvokable]
143 if (_numBytes == Uninitialized)
145 throw NotInitialized();
155 [CLSCompliant(
false)]
156 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail)]
157 [__DynamicallyInvokable]
158 public unsafe
T Read<T>(ulong byteOffset) where
T :
struct 160 if (_numBytes == Uninitialized)
162 throw NotInitialized();
164 uint num =
Marshal.SizeOfType(typeof(
T));
165 byte* ptr = (
byte*)(
void*)handle + byteOffset;
166 SpaceCheck(ptr, num);
167 bool success =
false;
171 DangerousAddRef(ref success);
172 GenericPtrToStructure(ptr, out
T structure, num);
197 [CLSCompliant(
false)]
198 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail)]
199 [__DynamicallyInvokable]
200 public unsafe
void ReadArray<T>(ulong byteOffset, T[] array,
int index,
int count) where
T :
struct 214 if (array.Length - index < count)
218 if (_numBytes == Uninitialized)
220 throw NotInitialized();
222 uint sizeofT =
Marshal.SizeOfType(typeof(
T));
223 uint num =
Marshal.AlignedSizeOf<
T>();
224 byte* ptr = (
byte*)(
void*)handle + byteOffset;
228 SpaceCheck(ptr, (ulong)(unchecked((
long)num) * unchecked((
long)count)));
234 DangerousAddRef(ref success);
235 for (
int i = 0; i < count; i++)
237 GenericPtrToStructure(ptr + num * i, out array[i + index], sizeofT);
254 [CLSCompliant(
false)]
255 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail)]
256 [__DynamicallyInvokable]
257 public unsafe
void Write<T>(ulong byteOffset, T value) where
T :
struct 259 if (_numBytes == Uninitialized)
261 throw NotInitialized();
263 uint num =
Marshal.SizeOfType(typeof(
T));
264 byte* ptr = (
byte*)(
void*)handle + byteOffset;
265 SpaceCheck(ptr, num);
266 bool success =
false;
270 DangerousAddRef(ref success);
271 GenericStructureToPtr(ref value, ptr, num);
294 [CLSCompliant(
false)]
295 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail)]
296 [__DynamicallyInvokable]
297 public unsafe
void WriteArray<T>(ulong byteOffset, T[] array,
int index,
int count) where
T :
struct 311 if (array.Length - index < count)
315 if (_numBytes == Uninitialized)
317 throw NotInitialized();
319 uint sizeofT =
Marshal.SizeOfType(typeof(
T));
320 uint num =
Marshal.AlignedSizeOf<
T>();
321 byte* ptr = (
byte*)(
void*)handle + byteOffset;
325 SpaceCheck(ptr, (ulong)(unchecked((
long)num) * unchecked((
long)count)));
331 DangerousAddRef(ref success);
332 for (
int i = 0; i < count; i++)
334 GenericStructureToPtr(ref array[i + index], ptr + num * i, sizeofT);
346 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
347 private unsafe
void SpaceCheck(
byte* ptr, ulong sizeInBytes)
349 if ((ulong)_numBytes < sizeInBytes)
353 if ((ulong)(ptr - (
byte*)(
void*)handle) > (ulong)_numBytes - sizeInBytes)
359 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
360 private static void NotEnoughRoom()
362 throw new ArgumentException(Environment.GetResourceString(
"Arg_BufferTooSmall"));
365 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
366 private static InvalidOperationException NotInitialized()
368 return new InvalidOperationException(Environment.GetResourceString(
"InvalidOperation_MustCallInitialize"));
371 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
372 internal unsafe
static void GenericPtrToStructure<T>(
byte* ptr, out T structure, uint sizeofT) where
T :
struct 374 structure =
default(T);
375 PtrToStructureNative(ptr, __makeref(structure), sizeofT);
379 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
380 private unsafe
static extern void PtrToStructureNative(
byte* ptr, TypedReference structure, uint sizeofT);
382 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
383 internal unsafe
static void GenericStructureToPtr<T>(ref T structure,
byte* ptr, uint sizeofT) where
T :
struct 385 StructureToPtrNative(__makeref(structure), ptr, sizeofT);
389 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
390 private unsafe
static extern void StructureToPtrNative(TypedReference structure,
byte* ptr, uint sizeofT);
unsafe void ReadArray< T >(ulong byteOffset, T[] array, int index, int count)
Reads the specified number of value types from memory starting at the offset, and writes them into an...
A platform-specific type that is used to represent a pointer or a handle.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
SafeBuffer(bool ownsHandle)
Creates a new instance of the T:System.Runtime.InteropServices.SafeBuffer class, and specifies whethe...
void ReleasePointer()
Releases a pointer that was obtained by the M:System.Runtime.InteropServices.SafeBuffer....
void Initialize(ulong numBytes)
Defines the allocation size of the memory region in bytes. You must call this method before you use t...
The exception that is thrown when the value of an argument is outside the allowable range of values a...
unsafe void Write< T >(ulong byteOffset, T value)
Writes a value type to memory at the given location.
ulong ByteLength
Gets the size of the buffer, in bytes.
Cer
Specifies a method's behavior when called within a constrained execution region.
void Initialize< T >(uint numElements)
Defines the allocation size of the memory region by specifying the number of value types....
Provides information about, and means to manipulate, the current environment and platform....
static int Size
Gets the size of this instance.
unsafe void AcquirePointer(ref byte *pointer)
Obtains a pointer from a T:System.Runtime.InteropServices.SafeBuffer object for a block of memory.
Provides a controlled memory buffer that can be used for reading and writing. Attempts to access memo...
A platform-specific type that is used to represent a pointer or a handle.
static void PrepareConstrainedRegions()
Designates a body of code as a constrained execution region (CER).
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
MethodImplOptions
Defines the details of how a method is implemented.
The exception that is thrown when one of the arguments provided to a method is not valid.
static int Size
Gets the size of this instance.
void Initialize(uint numElements, uint sizeOfEachElement)
Specifies the allocation size of the memory buffer by using the specified number of elements and elem...
Consistency
Specifies a reliability contract.
unsafe void WriteArray< T >(ulong byteOffset, T[] array, int index, int count)
Writes the specified number of value types to a memory location by reading bytes starting from the sp...
unsafe T Read< T >(ulong byteOffset)
Reads a value type from memory at the specified offset.
Provides a set of static methods and properties that provide support for compilers....