Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / 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
- RequestStatusBarUpdateEventArgs.cs
- TagMapInfo.cs
- ScriptControl.cs
- IsolatedStoragePermission.cs
- HashHelper.cs
- ReadWriteSpinLock.cs
- SqlReferenceCollection.cs
- Renderer.cs
- ContentElement.cs
- ClientProxyGenerator.cs
- XmlTextReaderImplHelpers.cs
- RenamedEventArgs.cs
- ClientSettings.cs
- AppSettingsSection.cs
- WebContext.cs
- pingexception.cs
- QueryResults.cs
- COAUTHINFO.cs
- Image.cs
- SeverityFilter.cs
- HtmlMobileTextWriter.cs
- SessionEndingEventArgs.cs
- EditCommandColumn.cs
- GrammarBuilderBase.cs
- ColorAnimationBase.cs
- ComponentResourceKeyConverter.cs
- Globals.cs
- ExcCanonicalXml.cs
- DataGridTablesFactory.cs
- BlockExpression.cs
- DbgCompiler.cs
- TrustLevelCollection.cs
- ResourceAssociationTypeEnd.cs
- DragStartedEventArgs.cs
- SafeIUnknown.cs
- TemplateBindingExpressionConverter.cs
- KoreanLunisolarCalendar.cs
- AutomationPattern.cs
- TypefaceMetricsCache.cs
- InputLanguageManager.cs
- PublisherIdentityPermission.cs
- ScriptControlManager.cs
- DataServiceClientException.cs
- BindingSource.cs
- SQLBytesStorage.cs
- CheckBoxBaseAdapter.cs
- Metadata.cs
- SynchronousChannelMergeEnumerator.cs
- WindowsComboBox.cs
- ControlBuilder.cs
- WeakHashtable.cs
- SubqueryTrackingVisitor.cs
- ZipFileInfo.cs
- TraceData.cs
- Token.cs
- GZipDecoder.cs
- basecomparevalidator.cs
- CompilationRelaxations.cs
- PocoEntityKeyStrategy.cs
- MasterPageCodeDomTreeGenerator.cs
- DefaultWorkflowTransactionService.cs
- LinearGradientBrush.cs
- ToolStripItemEventArgs.cs
- Page.cs
- WindowsSecurityTokenAuthenticator.cs
- LocalizationComments.cs
- HwndSubclass.cs
- CallbackHandler.cs
- ContractValidationHelper.cs
- Triplet.cs
- FileDialog.cs
- ConfigurationPropertyCollection.cs
- KeyInfo.cs
- DataGridRelationshipRow.cs
- IndexOutOfRangeException.cs
- TemplateModeChangedEventArgs.cs
- NativeObjectSecurity.cs
- Setter.cs
- BulletedListDesigner.cs
- WrapPanel.cs
- TrackingMemoryStream.cs
- MailMessage.cs
- MailMessage.cs
- SortDescriptionCollection.cs
- WmlFormAdapter.cs
- SspiSafeHandles.cs
- UIElementCollection.cs
- DataGridViewButtonCell.cs
- SchemaTableOptionalColumn.cs
- CompilerScope.cs
- AppDomainUnloadedException.cs
- ConnectionInterfaceCollection.cs
- Certificate.cs
- CodeCastExpression.cs
- _TLSstream.cs
- Repeater.cs
- DataGridViewCheckBoxCell.cs
- FileDialogPermission.cs
- URIFormatException.cs
- InputProviderSite.cs