Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / 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
- ImpersonateTokenRef.cs
- TextTreePropertyUndoUnit.cs
- XmlElement.cs
- XmlAnyElementAttributes.cs
- InvalidDocumentContentsException.cs
- BasicCellRelation.cs
- GacUtil.cs
- XmlAnyElementAttribute.cs
- SmtpLoginAuthenticationModule.cs
- WebPartEventArgs.cs
- EntityDataSourceWrapperCollection.cs
- CodeTypeReferenceExpression.cs
- ToolStripSplitButton.cs
- NavigationPropertyEmitter.cs
- PaperSource.cs
- Model3DGroup.cs
- WizardDesigner.cs
- EntityDesignerUtils.cs
- Encoder.cs
- FontSizeConverter.cs
- StreamGeometryContext.cs
- DataTemplateSelector.cs
- behaviorssection.cs
- DataListComponentEditor.cs
- PkcsMisc.cs
- LinkLabelLinkClickedEvent.cs
- ColumnTypeConverter.cs
- ControlSerializer.cs
- DataGridColumnsPage.cs
- MailHeaderInfo.cs
- NamedPipeConnectionPoolSettingsElement.cs
- ProviderUtil.cs
- Parameter.cs
- BinaryFormatterWriter.cs
- HyperLink.cs
- RegexTree.cs
- JsonReader.cs
- BooleanStorage.cs
- Message.cs
- TextElementEnumerator.cs
- Region.cs
- JobPageOrder.cs
- MatrixAnimationBase.cs
- StatusBarItem.cs
- KeyBinding.cs
- Rule.cs
- PrintingPermissionAttribute.cs
- StrongNameHelpers.cs
- AppManager.cs
- WindowShowOrOpenTracker.cs
- StdValidatorsAndConverters.cs
- PhonemeEventArgs.cs
- XmlArrayItemAttributes.cs
- DataControlCommands.cs
- TransformerTypeCollection.cs
- TreeWalker.cs
- FormatConvertedBitmap.cs
- ToolbarAUtomationPeer.cs
- DateTimeSerializationSection.cs
- CompilerError.cs
- CompilerHelpers.cs
- MediaPlayer.cs
- SingletonInstanceContextProvider.cs
- XmlJsonWriter.cs
- XmlEntityReference.cs
- CharacterHit.cs
- XmlILConstructAnalyzer.cs
- TextTreeExtractElementUndoUnit.cs
- SmiEventSink.cs
- NamespaceCollection.cs
- CounterSample.cs
- ContextStack.cs
- QilLoop.cs
- WebServiceClientProxyGenerator.cs
- SqlBooleanizer.cs
- TileModeValidation.cs
- StrokeNodeOperations2.cs
- MouseEventArgs.cs
- _LocalDataStoreMgr.cs
- DataGridTextBoxColumn.cs
- StrokeFIndices.cs
- IdentityNotMappedException.cs
- WebPartMenuStyle.cs
- DataServiceHostFactory.cs
- AnnotationObservableCollection.cs
- SmiEventSink_Default.cs
- SafeRightsManagementHandle.cs
- CurrentChangingEventArgs.cs
- DataRecordInternal.cs
- MultiTrigger.cs
- Encoder.cs
- CodeSubDirectory.cs
- HwndPanningFeedback.cs
- sqlstateclientmanager.cs
- WebConfigurationFileMap.cs
- Point3D.cs
- HandoffBehavior.cs
- DecimalAnimationBase.cs
- MetadataProperty.cs
- GcSettings.cs