Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / untmp / whidbey / QFE / ndp / clr / src / BCL / System / Runtime / InteropServices / SafePointer.cs / 1 / SafePointer.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== /*============================================================ ** ** Purpose: Unsafe code that uses pointers should use ** SafePointer to fix subtle lifetime problems with the ** underlying resource. ** ===========================================================*/ // Design points: // *) Avoid handle-recycling problems (including ones triggered via // resurrection attacks) for all accesses via pointers. This requires tying // together the lifetime of the unmanaged resource with the code that reads // from that resource, in a package that uses synchronization to enforce // the correct semantics during finalization. We're using SafeHandle's // ref count as a gate on whether the pointer can be dereferenced because that // controls the lifetime of the resource. // // *) Keep the penalties for using this class small, both in terms of space // and time. Having multiple threads reading from a memory mapped file // will already require 2 additional interlocked operations. If we add in // a "current position" concept, that requires additional space in memory and // synchronization. Since the position in memory is often (but not always) // something that can be stored on the stack, we can save some memory by // excluding it from this object. However, avoiding the need for // synchronization is a more significant win. This design allows multiple // threads to read and write memory simultaneously without locks (as long as // you don't write to a region of memory that overlaps with what another // thread is accessing). // // *) Space-wise, we use the following memory, including SafeHandle's fields: // Object Header MT* handle int bool bool <2 pad bytes> length // On 32 bit platforms: 24 bytes. On 64 bit platforms: 40 bytes. // (We can safe 4 bytes on x86 only by shrinking SafeHandle) // // *) Wrapping a SafeHandle would have been a nice solution, but without an // ordering between // Implementation notes: // *) The Initialize method must be called before you use any instance of // a SafePointer. To avoid races when storing SafePointers in statics, // you either need to take a lock when publishing the SafePointer, or you // need to create a local, initialize the SafePointer, then assign to the // static variable (perhaps using Interlocked.CompareExchange). Of course, // assignments in a static class constructor are under a lock implicitly. using System; using System.Security.Permissions; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Runtime.Versioning; using Microsoft.Win32.SafeHandles; namespace System.Runtime.InteropServices { [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)] internal abstract unsafe class SafePointer : SafeHandleZeroOrMinusOneIsInvalid { // Steal UIntPtr.MaxValue as our uninitialized value. private static readonly UIntPtr Uninitialized = (UIntPtr.Size == 4) ? ((UIntPtr) UInt32.MaxValue) : ((UIntPtr) UInt64.MaxValue); private UIntPtr _numBytes; protected SafePointer(bool ownsHandle) : base(ownsHandle) { _numBytes = Uninitialized; } ////// Specifies the size of the region of memory, in bytes. Must be /// called before using the SafePointer. /// /// Number of valid bytes in memory. //[CLSCompliant(false)] public void Initialize(ulong numBytes) { if (numBytes < 0) throw new ArgumentOutOfRangeException("numBytes", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (IntPtr.Size == 4 && numBytes > UInt32.MaxValue) throw new ArgumentOutOfRangeException("numBytes", Environment.GetResourceString("ArgumentOutOfRange_AddressSpace")); if (numBytes >= (ulong) Uninitialized) throw new ArgumentOutOfRangeException("numBytes", Environment.GetResourceString("ArgumentOutOfRange_UIntPtrMax-1")); _numBytes = (UIntPtr) numBytes; } ////// Specifies the the size of the region in memory, as the number of /// elements in an array. Must be called before using the SafePointer. /// //[CLSCompliant(false)] public void Initialize(uint numElements, uint sizeOfEachElement) { if (numElements < 0) throw new ArgumentOutOfRangeException("numElements", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (sizeOfEachElement < 0) throw new ArgumentOutOfRangeException("sizeOfEachElement", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (IntPtr.Size == 4 && numElements * sizeOfEachElement > UInt32.MaxValue) throw new ArgumentOutOfRangeException("numBytes", Environment.GetResourceString("ArgumentOutOfRange_AddressSpace")); if (numElements * sizeOfEachElement >= (ulong)Uninitialized) throw new ArgumentOutOfRangeException("numElements", Environment.GetResourceString("ArgumentOutOfRange_UIntPtrMax-1")); _numBytes = checked((UIntPtr) (numElements * sizeOfEachElement)); } ////// Specifies the the size of the region in memory, as the number of /// elements in an array. Must be called before using the SafePointer. /// //[CLSCompliant(false)] public void Initialize(uint numElements) where T : struct { Initialize(numElements, SizeOf ()); } /// /// Returns the size of an instance of a value type. /// ///Provide a value type to figure out its size ///The size of T in bytes. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] public static uint SizeOf() where T : struct { return SizeOfType(typeof(T)); } // Callers should ensure that they [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public void AcquirePointer(ref byte* pointer) { if (_numBytes == Uninitialized) throw NotInitialized(); pointer = null; RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { bool junk = false; DangerousAddRef(ref junk); pointer = (byte*)handle; } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] public void ReleasePointer() { if (_numBytes == Uninitialized) throw NotInitialized(); DangerousRelease(); } /// /// Read a value type from memory at the given offset. This is /// equivalent to: return *(T*)(bytePtr + byteOffset); /// ///The value type to read /// Where to start reading from memory. You /// may have to [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public T Read(uint byteOffset) where T : struct { if (_numBytes == Uninitialized) throw NotInitialized(); uint sizeofT = SizeOf (); byte* ptr = (byte*)handle + byteOffset; SpaceCheck(ptr, sizeofT); // return *(T*) (_ptr + byteOffset); T value; bool mustCallRelease = false; RuntimeHelpers.PrepareConstrainedRegions(); try { DangerousAddRef(ref mustCallRelease); GenericPtrToStructure (ptr, out value, sizeofT); } finally { if (mustCallRelease) DangerousRelease(); } return value; } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public void ReadArray (uint byteOffset, T[] array, int index, int count) where T : struct { if (array == null) throw new ArgumentNullException("array", Environment.GetResourceString("ArgumentNull_Buffer")); if (index < 0) throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (count < 0) throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (array.Length - index < count) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); if (_numBytes == Uninitialized) throw NotInitialized(); uint sizeofT = SizeOf (); byte* ptr = (byte*)handle + byteOffset; SpaceCheck(ptr, checked((ulong) (sizeofT * count))); bool mustCallRelease = false; RuntimeHelpers.PrepareConstrainedRegions(); try { DangerousAddRef(ref mustCallRelease); for (int i = 0; i < count; i++) unsafe { GenericPtrToStructure (ptr + sizeofT * i, out array[i + count], sizeofT); } } finally { if (mustCallRelease) DangerousRelease(); } } /// /// Write a value type to memory at the given offset. This is /// equivalent to: *(T*)(bytePtr + byteOffset) = value; /// ///The type of the value type to write to memory. /// The location in memory to write to. You /// may have to [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public void Write(uint byteOffset, T value) where T : struct { if (_numBytes == Uninitialized) throw NotInitialized(); uint sizeofT = SizeOf (); byte* ptr = (byte*)handle + byteOffset; SpaceCheck(ptr, sizeofT); // *((T*) (_ptr + byteOffset)) = value; bool mustCallRelease = false; RuntimeHelpers.PrepareConstrainedRegions(); try { DangerousAddRef(ref mustCallRelease); GenericStructureToPtr(ref value, ptr, sizeofT); } finally { if (mustCallRelease) DangerousRelease(); } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public void WriteArray (uint byteOffset, T[] array, int index, int count) where T : struct { if (array == null) throw new ArgumentNullException("array", Environment.GetResourceString("ArgumentNull_Buffer")); if (index < 0) throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (count < 0) throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (array.Length - index < count) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); if (_numBytes == Uninitialized) throw NotInitialized(); uint sizeofT = SizeOf (); byte* ptr = (byte*)handle + byteOffset; SpaceCheck(ptr, checked((ulong)(sizeofT * count))); bool mustCallRelease = false; RuntimeHelpers.PrepareConstrainedRegions(); try { DangerousAddRef(ref mustCallRelease); for (int i = 0; i < count; i++) unsafe { GenericStructureToPtr(ref array[i + count], ptr + sizeofT * i, sizeofT); } } finally { if (mustCallRelease) DangerousRelease(); } } /// /// Returns the number of bytes in the memory region. /// public ulong ByteLength { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] get { if (_numBytes == Uninitialized) throw NotInitialized(); return (ulong) _numBytes; } } /* No indexer. The perf would be misleadingly bad. People should use * AcquirePointer and ReleasePointer instead. */ [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] private void SpaceCheck(byte* ptr, ulong sizeInBytes) { if ((ulong)(ptr - (byte*) handle) > ((ulong)_numBytes) - sizeInBytes) NotEnoughRoom(); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] private static void NotEnoughRoom() { throw new ArgumentException(Environment.GetResourceString("Arg_BufferTooSmall")); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] private static InvalidOperationException NotInitialized() { BCLDebug.Assert(false, "Unintialized SafePointer! Someone needs to call Initialize before using this instance!"); return new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustCallInitialize")); } // FCALL limitations mean we can't have generic FCALL methods. However, we can pass // TypedReferences to FCALL methods. private static void GenericPtrToStructure(byte* ptr, out T structure, uint sizeofT) where T : struct { structure = default(T); // Dummy assignment to silence the compiler PtrToStructureNative(ptr, __makeref(structure), sizeofT); } [MethodImpl(MethodImplOptions.InternalCall)] [ResourceExposure(ResourceScope.None)] private static extern void PtrToStructureNative(byte* ptr, /*out T*/ TypedReference structure, uint sizeofT); private static void GenericStructureToPtr (ref T structure, byte* ptr, uint sizeofT) where T : struct { StructureToPtrNative(__makeref(structure), ptr, sizeofT); } [MethodImpl(MethodImplOptions.InternalCall)] [ResourceExposure(ResourceScope.None)] private static extern void StructureToPtrNative(/*ref T*/ TypedReference structure, byte* ptr, uint sizeofT); // Type must be a value type with no object reference fields. We only // assertt this, due to the lack of a suitable generic constraint. [MethodImpl(MethodImplOptions.InternalCall)] [ResourceExposure(ResourceScope.None)] private static extern uint SizeOfType(Type type); } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SqlMetaData.cs
- path.cs
- PeerEndPoint.cs
- NotImplementedException.cs
- TextParagraphCache.cs
- BookmarkOptionsHelper.cs
- PropertiesTab.cs
- ScalarType.cs
- FixedSOMTableCell.cs
- RepeaterCommandEventArgs.cs
- DataRowCollection.cs
- WebConfigurationHost.cs
- QueueProcessor.cs
- ZipArchive.cs
- X509ServiceCertificateAuthentication.cs
- DataBindingHandlerAttribute.cs
- ThreadAttributes.cs
- ComplusTypeValidator.cs
- CollectionViewProxy.cs
- _SslSessionsCache.cs
- X509AsymmetricSecurityKey.cs
- isolationinterop.cs
- ComponentConverter.cs
- RowCache.cs
- ReaderContextStackData.cs
- TTSEngineTypes.cs
- TextRenderer.cs
- SqlDataSourceCustomCommandEditor.cs
- ZipIOExtraFieldElement.cs
- LinkLabelLinkClickedEvent.cs
- OutputCacheModule.cs
- CookieHandler.cs
- SplineKeyFrames.cs
- ResourcePropertyMemberCodeDomSerializer.cs
- VisualTransition.cs
- UTF8Encoding.cs
- AttributeCollection.cs
- StructuredCompositeActivityDesigner.cs
- ButtonBaseAutomationPeer.cs
- SafeFileMappingHandle.cs
- DeferredReference.cs
- RegexTree.cs
- Point3DAnimation.cs
- VectorAnimation.cs
- ReturnValue.cs
- LocationSectionRecord.cs
- ManagedFilter.cs
- XmlToDatasetMap.cs
- BaseValidator.cs
- DiscoveryInnerClientAdhocCD1.cs
- SubordinateTransaction.cs
- PolyLineSegment.cs
- ControlDesigner.cs
- NetCodeGroup.cs
- IdleTimeoutMonitor.cs
- DataObjectPastingEventArgs.cs
- basevalidator.cs
- SplitContainer.cs
- MarkupCompilePass1.cs
- RtfFormatStack.cs
- UIElementAutomationPeer.cs
- TypeConverterHelper.cs
- SqlDataSourceCommandEventArgs.cs
- ReferenceConverter.cs
- EdmProviderManifest.cs
- DataGridLengthConverter.cs
- MultiSelectRootGridEntry.cs
- CursorInteropHelper.cs
- OdbcErrorCollection.cs
- OperationAbortedException.cs
- SecurityPermission.cs
- TextBox.cs
- Point4D.cs
- EventManager.cs
- CustomAttributeSerializer.cs
- XamlPoint3DCollectionSerializer.cs
- DataBindingHandlerAttribute.cs
- NavigationHelper.cs
- AsyncResult.cs
- FileNameEditor.cs
- DBSqlParserTableCollection.cs
- DecodeHelper.cs
- TypeHelper.cs
- ToolStripPanel.cs
- CalculatedColumn.cs
- ObfuscateAssemblyAttribute.cs
- ObjectListCommandCollection.cs
- AuthorizationRuleCollection.cs
- BufferBuilder.cs
- ServiceModelConfigurationSectionGroup.cs
- SQLDecimal.cs
- SettingsPropertyIsReadOnlyException.cs
- DataControlFieldHeaderCell.cs
- ControlCollection.cs
- ContentPresenter.cs
- GlyphRunDrawing.cs
- ItemsChangedEventArgs.cs
- CacheHelper.cs
- ParserStreamGeometryContext.cs
- ObjectPropertyMapping.cs