Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / 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); } } }
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ValidationHelpers.cs
- ToolStripItem.cs
- WindowCollection.cs
- InternalControlCollection.cs
- LocalizabilityAttribute.cs
- MsmqBindingBase.cs
- _SslStream.cs
- _LocalDataStore.cs
- ApplicationSecurityManager.cs
- ScrollableControl.cs
- ToolStripDesignerAvailabilityAttribute.cs
- WMIInterop.cs
- RequestCacheEntry.cs
- DefaultEventAttribute.cs
- BlobPersonalizationState.cs
- TdsParserStaticMethods.cs
- FileRecordSequenceHelper.cs
- JulianCalendar.cs
- CompensatableTransactionScopeActivityDesigner.cs
- ComponentFactoryHelpers.cs
- TreeWalker.cs
- LineBreak.cs
- SafeFileHandle.cs
- ScriptControlManager.cs
- ListViewPagedDataSource.cs
- DataServiceProviderWrapper.cs
- PeerChannelListener.cs
- BaseDataBoundControl.cs
- DesignerVerbCollection.cs
- XPathArrayIterator.cs
- DeclaredTypeValidator.cs
- MediaElementAutomationPeer.cs
- TimerElapsedEvenArgs.cs
- ListViewItemSelectionChangedEvent.cs
- Size3D.cs
- WebException.cs
- ActionFrame.cs
- SafeSecurityHandles.cs
- MemberAssignmentAnalysis.cs
- DataPagerFieldCommandEventArgs.cs
- ProcessHostConfigUtils.cs
- _ListenerAsyncResult.cs
- Touch.cs
- OptimizedTemplateContentHelper.cs
- AttachedProperty.cs
- MulticastOption.cs
- RegexMatchCollection.cs
- ProgressBarHighlightConverter.cs
- DataGridColumnHeader.cs
- TypeDefinition.cs
- SecurityChannelListener.cs
- CngAlgorithm.cs
- DateTime.cs
- LineServices.cs
- HostingPreferredMapPath.cs
- HeaderedContentControl.cs
- FtpWebRequest.cs
- DesignerWebPartChrome.cs
- UpDownBase.cs
- MetafileHeaderWmf.cs
- AppDomainInfo.cs
- DrawingContextDrawingContextWalker.cs
- AccessDataSourceView.cs
- ClientUtils.cs
- SoapUnknownHeader.cs
- OptimalTextSource.cs
- PageCache.cs
- RequestTimeoutManager.cs
- DefaultProxySection.cs
- wgx_sdk_version.cs
- XmlNodeList.cs
- QueryOptionExpression.cs
- DoubleAnimationClockResource.cs
- StylusDevice.cs
- x509utils.cs
- Calendar.cs
- StructuredType.cs
- ListComponentEditor.cs
- HwndHost.cs
- HashHelper.cs
- AncillaryOps.cs
- ObjectPropertyMapping.cs
- Sentence.cs
- MarkupCompilePass1.cs
- EventLogTraceListener.cs
- UserControl.cs
- ConvertersCollection.cs
- CollectionCodeDomSerializer.cs
- IsolationInterop.cs
- webbrowsersite.cs
- UnsignedPublishLicense.cs
- TouchEventArgs.cs
- Focus.cs
- PermissionSetTriple.cs
- EdmType.cs
- ResourceSetExpression.cs
- HtmlElementErrorEventArgs.cs
- DoubleLink.cs
- DropDownButton.cs
- XmlEncoding.cs