5 // Zoltan Varga (vargaz@gmail.com)
7 // Copyright (C) 2009, Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections.Generic;
33 using Microsoft.Win32.SafeHandles;
34 using System.Runtime.ConstrainedExecution;
36 namespace System.Runtime.InteropServices
38 public abstract class SafeBuffer : SafeHandleZeroOrMinusOneIsInvalid, IDisposable {
40 unsafe byte *last_byte;
43 protected SafeBuffer (bool ownsHandle) : base (ownsHandle)
47 [CLSCompliant (false)]
48 public void Initialize (ulong numBytes)
51 throw new ArgumentOutOfRangeException ("numBytes");
54 byte_length = numBytes;
56 last_byte = (byte *) (((byte *) handle) + numBytes);
60 [CLSCompliant (false)]
61 public void Initialize (uint numElements, uint sizeOfEachElement)
63 Initialize (numElements * sizeOfEachElement);
66 [CLSCompliant (false)]
67 public void Initialize<T> (uint numElements) where T : struct
69 Initialize (numElements, (uint)Marshal.SizeOf (typeof (T)));
72 [CLSCompliant (false)]
73 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
74 public unsafe void AcquirePointer (ref byte* pointer) {
76 throw new InvalidOperationException ();
79 DangerousAddRef (ref success);
81 pointer = (byte*)handle;
84 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
85 public void ReleasePointer () {
87 throw new InvalidOperationException ();
91 [CLSCompliant (false)]
92 public ulong ByteLength {
98 [CLSCompliant (false)]
99 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
100 public T Read<T> (ulong byteOffset) where T : struct
103 throw new InvalidOperationException ();
106 byte *source = (((byte *) handle) + byteOffset);
107 if (source >= last_byte || source + Marshal.SizeOf (typeof (T)) > last_byte){
108 throw new ArgumentException ("byteOffset");
111 return (T) Marshal.PtrToStructure ((IntPtr) source, typeof (T));
115 [CLSCompliant (false)]
116 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
117 public void ReadArray<T> (ulong byteOffset, T[] array, int index, int count) where T : struct {
119 throw new InvalidOperationException ();
122 int size = Marshal.SizeOf (typeof (T)) * count;
123 byte *source = (((byte *) handle) + byteOffset);
124 if (source >= last_byte || source + size > last_byte)
125 throw new ArgumentException ("byteOffset");
127 Marshal.copy_from_unmanaged ((IntPtr) source, index, array, count);
131 [CLSCompliant (false)]
132 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
133 public void Write<T> (ulong byteOffset, T value) where T : struct {
135 throw new InvalidOperationException ();
138 byte *target = (((byte *) handle) + byteOffset);
139 if (target >= last_byte || target + Marshal.SizeOf (typeof (T)) > last_byte)
140 throw new ArgumentException ("byteOffset");
142 Marshal.StructureToPtr (value, (IntPtr) target, false);
146 [CLSCompliant (false)]
147 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
148 public void WriteArray<T> (ulong byteOffset, T[] array, int index, int count) where T : struct
151 throw new InvalidOperationException ();
154 byte *target = ((byte *) handle) + byteOffset;
155 int size = Marshal.SizeOf (typeof (T)) * count;
156 if (target >= last_byte || target + size > last_byte)
157 throw new ArgumentException ("would overrite");
159 Marshal.copy_to_unmanaged (array, index, (IntPtr) target, count);