Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / 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
- TriggerBase.cs
- FrameworkContentElementAutomationPeer.cs
- OpenTypeLayout.cs
- PageRequestManager.cs
- Processor.cs
- DecoderFallbackWithFailureFlag.cs
- CodeTryCatchFinallyStatement.cs
- ClosableStream.cs
- ServerProtocol.cs
- JavaScriptString.cs
- PanelDesigner.cs
- Missing.cs
- wgx_render.cs
- prefixendpointaddressmessagefiltertable.cs
- RawKeyboardInputReport.cs
- ParallelLoopState.cs
- CardSpaceSelector.cs
- DbModificationClause.cs
- SaveFileDialog.cs
- Propagator.cs
- GlyphRun.cs
- SqlGatherProducedAliases.cs
- CompiledXpathExpr.cs
- SecurityState.cs
- InitializingNewItemEventArgs.cs
- SafeNativeMethods.cs
- Module.cs
- WebPartEditorApplyVerb.cs
- StickyNoteContentControl.cs
- PenThreadWorker.cs
- CancellationTokenRegistration.cs
- FileAuthorizationModule.cs
- PixelFormat.cs
- KeyboardEventArgs.cs
- BindingCollection.cs
- MainMenu.cs
- DataControlFieldCollection.cs
- HtmlInputFile.cs
- DbDataSourceEnumerator.cs
- ExpandedProjectionNode.cs
- XmlSchemaSimpleContentExtension.cs
- PreviewPrintController.cs
- CodeComment.cs
- VectorCollection.cs
- InputBuffer.cs
- ComponentResourceManager.cs
- QilFunction.cs
- GenericsNotImplementedException.cs
- XmlAnyElementAttributes.cs
- ApplicationDirectory.cs
- JoinGraph.cs
- ValidationResult.cs
- LicenseProviderAttribute.cs
- CodeParameterDeclarationExpression.cs
- AssertFilter.cs
- PropertyConverter.cs
- WebServiceResponse.cs
- Lasso.cs
- ApplicationInfo.cs
- XmlEncoding.cs
- DataViewManager.cs
- HttpHeaderCollection.cs
- Trace.cs
- Point.cs
- ConfigurationElementCollection.cs
- KeySplineConverter.cs
- FindCriteria.cs
- ActivationServices.cs
- DataBindEngine.cs
- TrackBarRenderer.cs
- SqlCacheDependencyDatabase.cs
- ProxyHelper.cs
- PersonalizationStateInfo.cs
- DocumentOrderQuery.cs
- SqlFileStream.cs
- ClientRoleProvider.cs
- MsmqIntegrationAppDomainProtocolHandler.cs
- GreenMethods.cs
- HtmlInputFile.cs
- SelectionManager.cs
- BaseValidator.cs
- SQLUtility.cs
- ListViewSortEventArgs.cs
- ClientSettingsSection.cs
- NativeMethods.cs
- BrowsableAttribute.cs
- FixedDocumentSequencePaginator.cs
- DbBuffer.cs
- UpDownBaseDesigner.cs
- RemoteHelper.cs
- SetterBaseCollection.cs
- DataGridTextBoxColumn.cs
- RepeaterItemEventArgs.cs
- CultureSpecificStringDictionary.cs
- CodeTypeMemberCollection.cs
- BaseValidator.cs
- TableHeaderCell.cs
- XPathMultyIterator.cs
- SQLDecimal.cs
- CodeCatchClause.cs