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
- ContainerSelectorBehavior.cs
- SerializationInfoEnumerator.cs
- TypeLoadException.cs
- ToolstripProfessionalRenderer.cs
- MILUtilities.cs
- TableLayoutSettingsTypeConverter.cs
- TypeSystem.cs
- Vector3DValueSerializer.cs
- TextBoxBase.cs
- ImageIndexConverter.cs
- NegatedCellConstant.cs
- TextTabProperties.cs
- PhysicalOps.cs
- DictionaryBase.cs
- TouchPoint.cs
- FormatterConverter.cs
- TdsParserHelperClasses.cs
- StrokeCollection2.cs
- SkewTransform.cs
- SqlTypeConverter.cs
- ExecutionTracker.cs
- PointCollectionValueSerializer.cs
- X509SecurityToken.cs
- WebConfigurationFileMap.cs
- CustomAttributeSerializer.cs
- HuffmanTree.cs
- returneventsaver.cs
- TabControlEvent.cs
- SynchronizedInputAdaptor.cs
- PointLight.cs
- Enum.cs
- AbandonedMutexException.cs
- FilterableAttribute.cs
- ConfigurationManagerHelperFactory.cs
- XsltQilFactory.cs
- DurableOperationContext.cs
- ServerValidateEventArgs.cs
- InternalSafeNativeMethods.cs
- FocusWithinProperty.cs
- CompilerParameters.cs
- WebPartHeaderCloseVerb.cs
- BamlRecordReader.cs
- Rect3D.cs
- EdmSchemaAttribute.cs
- ButtonRenderer.cs
- XpsThumbnail.cs
- ObjectPropertyMapping.cs
- QilReplaceVisitor.cs
- FloaterParaClient.cs
- Image.cs
- RuleAttributes.cs
- LoadMessageLogger.cs
- GenerateTemporaryTargetAssembly.cs
- MulticastDelegate.cs
- VisualTarget.cs
- InitializerFacet.cs
- Stream.cs
- UpdateDelegates.Generated.cs
- XmlDataDocument.cs
- EntityStoreSchemaGenerator.cs
- OracleTimeSpan.cs
- HtmlPanelAdapter.cs
- AdornerLayer.cs
- SiteMapSection.cs
- ApplicationServiceHelper.cs
- TripleDESCryptoServiceProvider.cs
- ContentPresenter.cs
- ConfigsHelper.cs
- XmlStreamStore.cs
- EtwTrace.cs
- FilteredAttributeCollection.cs
- ObjectSecurity.cs
- DescendentsWalkerBase.cs
- BamlRecordReader.cs
- VirtualDirectoryMapping.cs
- IPeerNeighbor.cs
- RuntimeEnvironment.cs
- ContentPresenter.cs
- BinaryFormatterWriter.cs
- IsolatedStorageFilePermission.cs
- WindowsSolidBrush.cs
- Attribute.cs
- WorkflowQueuingService.cs
- DateTimePicker.cs
- XmlSchemaAttribute.cs
- XmlToDatasetMap.cs
- CodeDesigner.cs
- DateTimeFormat.cs
- DoubleCollectionValueSerializer.cs
- DocumentXPathNavigator.cs
- SqlMetaData.cs
- EnumBuilder.cs
- ItemCheckedEvent.cs
- ScriptControl.cs
- RouteItem.cs
- InputProviderSite.cs
- WrappedReader.cs
- EventSetter.cs
- CommandEventArgs.cs
- ColorConverter.cs