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
- IBuiltInEvidence.cs
- OracleFactory.cs
- FuncCompletionCallbackWrapper.cs
- QueryExpr.cs
- ValidatorCompatibilityHelper.cs
- SQLStringStorage.cs
- RangeContentEnumerator.cs
- XmlWhitespace.cs
- Misc.cs
- ChtmlPageAdapter.cs
- streamingZipPartStream.cs
- ProfileModule.cs
- DocumentSequence.cs
- CodeIdentifier.cs
- EventManager.cs
- BamlTreeMap.cs
- AutoFocusStyle.xaml.cs
- dbenumerator.cs
- MailAddressCollection.cs
- MissingManifestResourceException.cs
- ServerIdentity.cs
- ControlBuilderAttribute.cs
- XPathParser.cs
- SqlStatistics.cs
- TextTreeTextNode.cs
- DataControlFieldCell.cs
- QueryActivatableWorkflowsCommand.cs
- ToolStripDropDown.cs
- RectAnimationClockResource.cs
- MessageQueueConverter.cs
- SignatureHelper.cs
- ReadOnlyHierarchicalDataSource.cs
- RepeatBehaviorConverter.cs
- TargetConverter.cs
- DataGridViewRowErrorTextNeededEventArgs.cs
- HttpProfileGroupBase.cs
- FixedPage.cs
- HistoryEventArgs.cs
- CatalogZoneDesigner.cs
- ProvidePropertyAttribute.cs
- IpcPort.cs
- ObjectSet.cs
- StringResourceManager.cs
- ReferenceEqualityComparer.cs
- TreeNodeStyleCollection.cs
- NamespaceInfo.cs
- SymbolType.cs
- DocumentViewer.cs
- ProxyWebPart.cs
- WindowsSlider.cs
- UnsafeNativeMethods.cs
- WindowsFormsHostPropertyMap.cs
- SamlAssertion.cs
- TextParaLineResult.cs
- EventLog.cs
- ConfigurationPermission.cs
- StylusPointDescription.cs
- ContractListAdapter.cs
- GeneralTransform3DGroup.cs
- KerberosRequestorSecurityToken.cs
- TemplateColumn.cs
- RemotingServices.cs
- PersonalizationProviderHelper.cs
- TagPrefixCollection.cs
- Sequence.cs
- OpenTypeCommon.cs
- MergeFilterQuery.cs
- HtmlControlPersistable.cs
- CommentEmitter.cs
- DataGridLinkButton.cs
- DataGridViewSelectedRowCollection.cs
- LinqDataSource.cs
- DisableDpiAwarenessAttribute.cs
- Expressions.cs
- OwnerDrawPropertyBag.cs
- SafeFindHandle.cs
- StylusTip.cs
- WindowsAuthenticationEventArgs.cs
- FtpWebResponse.cs
- EventMappingSettingsCollection.cs
- RelatedEnd.cs
- ImageDrawing.cs
- WebReferencesBuildProvider.cs
- BindStream.cs
- InputLanguageManager.cs
- HijriCalendar.cs
- CodeDelegateInvokeExpression.cs
- FrugalList.cs
- SchemaElementDecl.cs
- MaterialCollection.cs
- DataServiceContext.cs
- Normalization.cs
- AbsoluteQuery.cs
- DesignTimeHTMLTextWriter.cs
- HttpStaticObjectsCollectionBase.cs
- RichTextBoxConstants.cs
- ColorTransform.cs
- DeflateInput.cs
- WindowsComboBox.cs
- SqlSelectClauseBuilder.cs