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
- TrustSection.cs
- EUCJPEncoding.cs
- ChangeInterceptorAttribute.cs
- MemoryFailPoint.cs
- SqlNotificationEventArgs.cs
- SkinBuilder.cs
- WebBrowserPermission.cs
- NotCondition.cs
- NegatedConstant.cs
- MetadataWorkspace.cs
- StrongNameUtility.cs
- XpsSerializationException.cs
- DocumentGrid.cs
- UTF32Encoding.cs
- MultiTouchSystemGestureLogic.cs
- DataSourceConverter.cs
- BasePattern.cs
- WebRequestModuleElementCollection.cs
- DocumentOrderQuery.cs
- TransportReplyChannelAcceptor.cs
- Activity.cs
- ClaimComparer.cs
- SamlAuthenticationClaimResource.cs
- RequestFactory.cs
- WindowsHyperlink.cs
- CodeMemberField.cs
- NonceToken.cs
- Journal.cs
- propertytag.cs
- TreeView.cs
- NullableIntAverageAggregationOperator.cs
- ColumnWidthChangedEvent.cs
- SiteMapProvider.cs
- AssemblyBuilder.cs
- XmlQualifiedName.cs
- TrackingRecord.cs
- FullTextBreakpoint.cs
- BackgroundFormatInfo.cs
- BaseServiceProvider.cs
- ILGenerator.cs
- TransferRequestHandler.cs
- Compiler.cs
- InvalidCastException.cs
- CodeAccessSecurityEngine.cs
- InternalsVisibleToAttribute.cs
- DoWorkEventArgs.cs
- Currency.cs
- UnsafeNativeMethods.cs
- MemberDomainMap.cs
- SrgsElementFactoryCompiler.cs
- OraclePermissionAttribute.cs
- IsolatedStoragePermission.cs
- EdmComplexTypeAttribute.cs
- BinaryConverter.cs
- ExtentKey.cs
- Action.cs
- MultitargetingHelpers.cs
- EdmProperty.cs
- CompilationRelaxations.cs
- AssemblyHash.cs
- LabelEditEvent.cs
- Rect3DConverter.cs
- ADMembershipUser.cs
- SizeAnimation.cs
- XamlTemplateSerializer.cs
- RegexRunnerFactory.cs
- CompilerLocalReference.cs
- ImageListStreamer.cs
- SiteMapHierarchicalDataSourceView.cs
- MimeParameter.cs
- querybuilder.cs
- FragmentQueryKB.cs
- DragStartedEventArgs.cs
- StatusStrip.cs
- CommandEventArgs.cs
- FormatterServices.cs
- UncommonField.cs
- VisualStyleElement.cs
- GeneratedCodeAttribute.cs
- RuleSetDialog.Designer.cs
- XmlSerializer.cs
- SerializationSectionGroup.cs
- DBSqlParser.cs
- CompilerResults.cs
- OrderedDictionaryStateHelper.cs
- AssociationTypeEmitter.cs
- WorkflowHostingEndpoint.cs
- ClipboardProcessor.cs
- LinkLabelLinkClickedEvent.cs
- SmiContextFactory.cs
- QuinticEase.cs
- Baml2006ReaderContext.cs
- XmlEncoding.cs
- cookie.cs
- DataGridCommandEventArgs.cs
- CorePropertiesFilter.cs
- ScrollData.cs
- FixUpCollection.cs
- DLinqAssociationProvider.cs
- ProjectedSlot.cs