Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / 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
- BindMarkupExtensionSerializer.cs
- ManagementPath.cs
- HMACSHA512.cs
- TraceListener.cs
- Inline.cs
- MobileControl.cs
- ObjectItemCollection.cs
- HorizontalAlignConverter.cs
- BaseDataList.cs
- safemediahandle.cs
- DbParameterCollection.cs
- SystemException.cs
- StreamAsIStream.cs
- StrongNameMembershipCondition.cs
- ActivityInstanceReference.cs
- SqlDataRecord.cs
- ProviderConnectionPointCollection.cs
- SystemUnicastIPAddressInformation.cs
- HttpGetProtocolImporter.cs
- WebMessageFormatHelper.cs
- AssemblyFilter.cs
- PointLight.cs
- XmlSerializerAssemblyAttribute.cs
- RsaSecurityToken.cs
- Geometry3D.cs
- clipboard.cs
- ManipulationInertiaStartingEventArgs.cs
- ToolStripDropTargetManager.cs
- CodeTypeDelegate.cs
- PromptStyle.cs
- SimpleType.cs
- PasswordTextContainer.cs
- DoubleAnimationUsingPath.cs
- SerialStream.cs
- MSAAWinEventWrap.cs
- XPathBuilder.cs
- UserControlBuildProvider.cs
- TraceSource.cs
- ConfigurationConverterBase.cs
- Crc32Helper.cs
- HandledMouseEvent.cs
- GlobalizationSection.cs
- AstNode.cs
- ScrollViewerAutomationPeer.cs
- WindowsIdentity.cs
- DataList.cs
- CaseStatementSlot.cs
- CryptoConfig.cs
- SelectionProviderWrapper.cs
- IPAddress.cs
- ObjectDataProvider.cs
- ToolStripPanelCell.cs
- HitTestParameters3D.cs
- ActiveXSite.cs
- Setter.cs
- ResourceSetExpression.cs
- Relationship.cs
- Int32Animation.cs
- ArithmeticException.cs
- FontFamilyConverter.cs
- Random.cs
- TextBoxAutomationPeer.cs
- DataSourceControl.cs
- LOSFormatter.cs
- PropertyToken.cs
- InkPresenterAutomationPeer.cs
- SelectionService.cs
- VsPropertyGrid.cs
- HostedTcpTransportManager.cs
- SettingsSavedEventArgs.cs
- ModelItemCollection.cs
- ThousandthOfEmRealPoints.cs
- SecurityException.cs
- TerminatorSinks.cs
- Encoder.cs
- MouseActionConverter.cs
- StreamGeometryContext.cs
- DataGridColumnHeader.cs
- XmlWrappingReader.cs
- WCFBuildProvider.cs
- MetadataExporter.cs
- XmlBoundElement.cs
- ISAPIApplicationHost.cs
- TagPrefixCollection.cs
- TreeNode.cs
- EpmCustomContentDeSerializer.cs
- FileLogRecordStream.cs
- ProcessModelInfo.cs
- HttpWriter.cs
- ControlDesigner.cs
- SafeCertificateContext.cs
- DataRow.cs
- Stylus.cs
- PointHitTestParameters.cs
- PartialTrustVisibleAssemblyCollection.cs
- SecurityContext.cs
- AdapterDictionary.cs
- _Win32.cs
- MutableAssemblyCacheEntry.cs
- CustomWebEventKey.cs