Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / untmp / whidbey / QFE / 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- MultiplexingFormatMapping.cs
- ProtocolsConfigurationHandler.cs
- KeyboardEventArgs.cs
- ConnectionStringsSection.cs
- XAMLParseException.cs
- List.cs
- StrongName.cs
- NameValuePair.cs
- GenericArgumentsUpdater.cs
- OLEDB_Util.cs
- RenamedEventArgs.cs
- RowVisual.cs
- TableLayoutCellPaintEventArgs.cs
- LayoutExceptionEventArgs.cs
- PropertyValueChangedEvent.cs
- EFTableProvider.cs
- FilteredDataSetHelper.cs
- LocatorPart.cs
- ButtonBase.cs
- EdmMember.cs
- MediaPlayer.cs
- ActivityMetadata.cs
- FormsAuthenticationUserCollection.cs
- TextModifier.cs
- DescendantQuery.cs
- WindowsListViewGroupHelper.cs
- DataFieldEditor.cs
- TextTreeText.cs
- EntitySqlException.cs
- RuntimeConfigurationRecord.cs
- DelimitedListTraceListener.cs
- DropTarget.cs
- WorkflowRuntime.cs
- XmlSortKeyAccumulator.cs
- EventEntry.cs
- TextSearch.cs
- ResourceDefaultValueAttribute.cs
- SoundPlayer.cs
- EpmCustomContentWriterNodeData.cs
- Pair.cs
- DesignerView.xaml.cs
- DbDataRecord.cs
- Decoder.cs
- RequestResizeEvent.cs
- RequiredFieldValidator.cs
- DocumentApplicationJournalEntry.cs
- GenerateHelper.cs
- GB18030Encoding.cs
- diagnosticsswitches.cs
- AnimationTimeline.cs
- OutArgument.cs
- InputEventArgs.cs
- AsyncContentLoadedEventArgs.cs
- TrackingConditionCollection.cs
- PasswordDeriveBytes.cs
- ConnectionProviderAttribute.cs
- WebControlToolBoxItem.cs
- HtmlHead.cs
- CommandPlan.cs
- BackgroundFormatInfo.cs
- SystemTcpStatistics.cs
- CertificateReferenceElement.cs
- DataKey.cs
- SqlInternalConnectionTds.cs
- XmlSchemaAnnotation.cs
- ClientBuildManagerCallback.cs
- ToolStripDropDown.cs
- HtmlInputPassword.cs
- StyleXamlParser.cs
- TrackPointCollection.cs
- PropertyGrid.cs
- IdentityVerifier.cs
- NonBatchDirectoryCompiler.cs
- BasicDesignerLoader.cs
- MemberInfoSerializationHolder.cs
- ConnectionsZone.cs
- AssemblyCache.cs
- XPathAncestorQuery.cs
- TimerTable.cs
- xsdvalidator.cs
- Operators.cs
- WindowsListViewSubItem.cs
- WindowAutomationPeer.cs
- QueryOperationResponseOfT.cs
- GridViewDeletedEventArgs.cs
- ServerProtocol.cs
- LinqDataView.cs
- ContentFilePart.cs
- TreeViewItem.cs
- DataBoundControl.cs
- DbProviderManifest.cs
- AutomationPeer.cs
- SafeNativeMethods.cs
- WindowsFormsHelpers.cs
- x509store.cs
- OneWayElement.cs
- TrackBarRenderer.cs
- WindowsGraphicsCacheManager.cs
- SqlCacheDependencySection.cs
- ToolStripHighContrastRenderer.cs