Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / 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); } } }
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Expressions.cs
- LayoutInformation.cs
- ADMembershipProvider.cs
- ConfigurationLockCollection.cs
- TiffBitmapDecoder.cs
- SourceElementsCollection.cs
- MailMessageEventArgs.cs
- ADMembershipUser.cs
- GiveFeedbackEventArgs.cs
- WebBrowserDocumentCompletedEventHandler.cs
- WebBrowser.cs
- QueryStack.cs
- EncodingFallbackAwareXmlTextWriter.cs
- _AutoWebProxyScriptEngine.cs
- SafeArrayRankMismatchException.cs
- TcpServerChannel.cs
- StoreAnnotationsMap.cs
- DataServiceHostWrapper.cs
- TextTreeUndo.cs
- IntSecurity.cs
- _TimerThread.cs
- TextStore.cs
- TypeSystem.cs
- CursorInteropHelper.cs
- DefaultSection.cs
- CheckBoxPopupAdapter.cs
- KeyValueConfigurationCollection.cs
- CodeMethodReturnStatement.cs
- PeerApplicationLaunchInfo.cs
- ConfigurationValues.cs
- XmlQualifiedName.cs
- WorkflowMarkupSerializationProvider.cs
- SchemaInfo.cs
- TogglePatternIdentifiers.cs
- LabelEditEvent.cs
- TextComposition.cs
- ListItemParagraph.cs
- JavaScriptObjectDeserializer.cs
- DbConnectionOptions.cs
- CachedTypeface.cs
- PropertyGridView.cs
- ThemeDictionaryExtension.cs
- ClipboardProcessor.cs
- ToolStripLabel.cs
- GenerateHelper.cs
- Span.cs
- Validator.cs
- ChangePassword.cs
- Authorization.cs
- PointConverter.cs
- OdbcDataAdapter.cs
- WindowsMenu.cs
- CollectionAdapters.cs
- IisTraceListener.cs
- SoapHelper.cs
- ExitEventArgs.cs
- DataGridCommandEventArgs.cs
- SqlDuplicator.cs
- Image.cs
- EdgeModeValidation.cs
- DataQuery.cs
- ToolStripContainerActionList.cs
- XmlTextReader.cs
- Base64Stream.cs
- DurableEnlistmentState.cs
- ContentControl.cs
- SystemIPGlobalStatistics.cs
- SqlBulkCopyColumnMappingCollection.cs
- SingleAnimationBase.cs
- Bitmap.cs
- objectquery_tresulttype.cs
- InputProcessorProfilesLoader.cs
- Utils.cs
- SlotInfo.cs
- BinaryReader.cs
- TypePresenter.xaml.cs
- SerializationSectionGroup.cs
- bindurihelper.cs
- DESCryptoServiceProvider.cs
- TiffBitmapEncoder.cs
- SQLByteStorage.cs
- StringUtil.cs
- IISMapPath.cs
- DESCryptoServiceProvider.cs
- XmlRawWriter.cs
- _TimerThread.cs
- Compiler.cs
- SafeNativeMethods.cs
- DocumentSchemaValidator.cs
- EntityUtil.cs
- WrapperEqualityComparer.cs
- EmptyEnumerable.cs
- PeerDuplexChannelListener.cs
- PeerNode.cs
- UnionExpr.cs
- SectionUpdates.cs
- TreeNodeBinding.cs
- RuntimeConfigurationRecord.cs
- Rect3D.cs
- SizeFConverter.cs