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
- BufferModeSettings.cs
- XmlDataSourceView.cs
- ProtocolsConfigurationEntry.cs
- TextElement.cs
- CompModSwitches.cs
- ListItemParagraph.cs
- ByteAnimationBase.cs
- TaiwanLunisolarCalendar.cs
- ListViewAutomationPeer.cs
- BinaryFormatterWriter.cs
- EdmType.cs
- NameValueConfigurationCollection.cs
- LogExtentCollection.cs
- XmlCompatibilityReader.cs
- ViewgenGatekeeper.cs
- NativeConfigurationLoader.cs
- TailCallAnalyzer.cs
- Grid.cs
- PlainXmlWriter.cs
- ScriptingJsonSerializationSection.cs
- WeakRefEnumerator.cs
- CacheSection.cs
- SqlTriggerAttribute.cs
- IEnumerable.cs
- HttpRuntimeSection.cs
- ForeignKeyConstraint.cs
- IndexedGlyphRun.cs
- CatalogPartChrome.cs
- SafeLibraryHandle.cs
- _TransmitFileOverlappedAsyncResult.cs
- SimpleHandlerFactory.cs
- NotImplementedException.cs
- UnaryExpression.cs
- PropertyFilter.cs
- NativeMethods.cs
- InvokePattern.cs
- KeyManager.cs
- _LocalDataStore.cs
- WebPartMenu.cs
- SystemParameters.cs
- BamlCollectionHolder.cs
- MsmqChannelFactory.cs
- Visitor.cs
- CodePageUtils.cs
- ChildChangedEventArgs.cs
- WebContext.cs
- MobileControlPersister.cs
- ISO2022Encoding.cs
- HyperLink.cs
- EntitySqlQueryCacheEntry.cs
- WindowsTab.cs
- CellPartitioner.cs
- FixedSOMTable.cs
- ToolStripItemRenderEventArgs.cs
- XmlWhitespace.cs
- WebRequestModuleElementCollection.cs
- InstancePersistence.cs
- AttributeUsageAttribute.cs
- InvalidProgramException.cs
- Style.cs
- NativeMethods.cs
- SchemaEntity.cs
- HitTestDrawingContextWalker.cs
- GroupBoxRenderer.cs
- BinaryParser.cs
- SchemaImporterExtensionElement.cs
- DataControlReference.cs
- Msec.cs
- FirewallWrapper.cs
- TrackingMemoryStreamFactory.cs
- ErrorTableItemStyle.cs
- SessionEndingEventArgs.cs
- DelegateTypeInfo.cs
- SerializerWriterEventHandlers.cs
- GB18030Encoding.cs
- Misc.cs
- FormatVersion.cs
- RectAnimationUsingKeyFrames.cs
- ContainerTracking.cs
- unsafeIndexingFilterStream.cs
- RemotingSurrogateSelector.cs
- InvalidDataException.cs
- DefaultClaimSet.cs
- BooleanConverter.cs
- CommonDialog.cs
- XmlComment.cs
- MissingFieldException.cs
- RowToParametersTransformer.cs
- TextBlockAutomationPeer.cs
- OrderPreservingPipeliningMergeHelper.cs
- XPathAxisIterator.cs
- AdjustableArrowCap.cs
- SemanticAnalyzer.cs
- Fault.cs
- PropertyGroupDescription.cs
- LingerOption.cs
- TextTrailingWordEllipsis.cs
- DrawingImage.cs
- UnionCqlBlock.cs
- ProfilePropertyNameValidator.cs