Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / clr / src / BCL / System / IO / PinnedBufferMemoryStream.cs / 1 / PinnedBufferMemoryStream.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== /*============================================================ ** ** Class: PinnedBufferMemoryStream ** ** ** Purpose: Pins a byte[], exposing it as an unmanaged memory ** stream. Used in ResourceReader for corner cases. ** ** ===========================================================*/ using System; using System.Runtime.InteropServices; namespace System.IO { internal sealed unsafe class PinnedBufferMemoryStream : UnmanagedMemoryStream { private byte[] _array; private GCHandle _pinningHandle; internal PinnedBufferMemoryStream(byte[] array) { BCLDebug.Assert(array != null, "Array can't be null"); int len = array.Length; // Handle 0 length byte arrays specially. if (len == 0) { array = new byte[1]; len = 0; } _array = array; _pinningHandle = new GCHandle(array, GCHandleType.Pinned); // Now the byte[] is pinned for the lifetime of this instance. // But I also need to get a pointer to that block of memory... fixed(byte* ptr = _array) Initialize(ptr, len, len, FileAccess.Read, true); } ~PinnedBufferMemoryStream() { Dispose(false); } protected override void Dispose(bool disposing) { if (_isOpen) { _pinningHandle.Free(); _isOpen = false; } #if _DEBUG // To help track down lifetime issues on checked builds, force //a full GC here. if (disposing) { GC.Collect(); GC.WaitForPendingFinalizers(); } #endif base.Dispose(disposing); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== /*============================================================ ** ** Class: PinnedBufferMemoryStream ** ** ** Purpose: Pins a byte[], exposing it as an unmanaged memory ** stream. Used in ResourceReader for corner cases. ** ** ===========================================================*/ using System; using System.Runtime.InteropServices; namespace System.IO { internal sealed unsafe class PinnedBufferMemoryStream : UnmanagedMemoryStream { private byte[] _array; private GCHandle _pinningHandle; internal PinnedBufferMemoryStream(byte[] array) { BCLDebug.Assert(array != null, "Array can't be null"); int len = array.Length; // Handle 0 length byte arrays specially. if (len == 0) { array = new byte[1]; len = 0; } _array = array; _pinningHandle = new GCHandle(array, GCHandleType.Pinned); // Now the byte[] is pinned for the lifetime of this instance. // But I also need to get a pointer to that block of memory... fixed(byte* ptr = _array) Initialize(ptr, len, len, FileAccess.Read, true); } ~PinnedBufferMemoryStream() { Dispose(false); } protected override void Dispose(bool disposing) { if (_isOpen) { _pinningHandle.Free(); _isOpen = false; } #if _DEBUG // To help track down lifetime issues on checked builds, force //a full GC here. if (disposing) { GC.Collect(); GC.WaitForPendingFinalizers(); } #endif base.Dispose(disposing); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ScaleTransform.cs
- CompModHelpers.cs
- TargetFrameworkUtil.cs
- TreeWalker.cs
- RequestCacheEntry.cs
- DynamicValueConverter.cs
- XmlSchemaSubstitutionGroup.cs
- SchemaNamespaceManager.cs
- TextEncodedRawTextWriter.cs
- BlockCollection.cs
- ConsumerConnectionPointCollection.cs
- JavaScriptString.cs
- CodeArrayIndexerExpression.cs
- AdornerDecorator.cs
- ZipIOCentralDirectoryBlock.cs
- FormClosedEvent.cs
- _DigestClient.cs
- TableLayoutStyleCollection.cs
- TimeSpanSecondsConverter.cs
- ApplicationSecurityInfo.cs
- GeneralTransformCollection.cs
- XmlReaderSettings.cs
- CopyNamespacesAction.cs
- Freezable.cs
- XPathNavigator.cs
- UnhandledExceptionEventArgs.cs
- UnsafeNativeMethods.cs
- Point4DConverter.cs
- ObjectViewListener.cs
- GridViewUpdateEventArgs.cs
- PartialCachingAttribute.cs
- HMACSHA384.cs
- AssertValidation.cs
- DataGridDesigner.cs
- ClientUrlResolverWrapper.cs
- Win32NamedPipes.cs
- EventMetadata.cs
- RequestSecurityTokenForRemoteTokenFactory.cs
- Scene3D.cs
- BamlStream.cs
- ColorConvertedBitmapExtension.cs
- ColorTransform.cs
- InternalException.cs
- NavigationHelper.cs
- ChangeTracker.cs
- XmlArrayItemAttributes.cs
- XpsS0ValidatingLoader.cs
- ValidatingPropertiesEventArgs.cs
- ToolboxItem.cs
- ImageListStreamer.cs
- CustomAssemblyResolver.cs
- Keyboard.cs
- DataGridTableStyleMappingNameEditor.cs
- OletxEnlistment.cs
- HandlerWithFactory.cs
- FileRegion.cs
- XPathExpr.cs
- AssemblyCollection.cs
- RenderData.cs
- DoubleCollectionConverter.cs
- DataServiceHost.cs
- basecomparevalidator.cs
- AutomationProperties.cs
- XPathNodeIterator.cs
- BufferedWebEventProvider.cs
- EntityReference.cs
- WindowsButton.cs
- CacheDependency.cs
- HwndAppCommandInputProvider.cs
- PropertyValueUIItem.cs
- CommandLineParser.cs
- OleDbSchemaGuid.cs
- PermissionToken.cs
- EncryptedKeyIdentifierClause.cs
- UnsafeNativeMethods.cs
- FrameworkElementAutomationPeer.cs
- XmlSerializerOperationFormatter.cs
- ConfigXmlComment.cs
- AncillaryOps.cs
- PropertyTab.cs
- WebServiceData.cs
- IPEndPointCollection.cs
- Int32Collection.cs
- PageCatalogPartDesigner.cs
- Point4D.cs
- NavigationPropertySingletonExpression.cs
- CellTreeNode.cs
- VectorCollectionConverter.cs
- WebPartExportVerb.cs
- InputReferenceExpression.cs
- XmlUtf8RawTextWriter.cs
- DbMetaDataColumnNames.cs
- RawStylusInputCustomData.cs
- TemplateBaseAction.cs
- DataSourceXmlSerializer.cs
- DbDataAdapter.cs
- GetCryptoTransformRequest.cs
- Polyline.cs
- XmlMemberMapping.cs
- UxThemeWrapper.cs