Code:
/ FX-1434 / FX-1434 / 1.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
- ModelServiceImpl.cs
- ArraySortHelper.cs
- FindSimilarActivitiesVerb.cs
- CutCopyPasteHelper.cs
- InitializerFacet.cs
- InvalidContentTypeException.cs
- printdlgexmarshaler.cs
- LayoutSettings.cs
- WebPartVerb.cs
- hresults.cs
- AnonymousIdentificationSection.cs
- DataKey.cs
- HostingEnvironment.cs
- WindowHelperService.cs
- IIS7UserPrincipal.cs
- ArrayWithOffset.cs
- HotSpot.cs
- DTCTransactionManager.cs
- GridItem.cs
- TransformCryptoHandle.cs
- ConfigXmlWhitespace.cs
- NamespaceExpr.cs
- BrushValueSerializer.cs
- HeaderUtility.cs
- EventHandlers.cs
- ProfilePropertySettingsCollection.cs
- BufferedStream.cs
- ContentValidator.cs
- CfgArc.cs
- StrokeIntersection.cs
- CodeNamespaceImport.cs
- SessionStateItemCollection.cs
- Rfc4050KeyFormatter.cs
- RijndaelManagedTransform.cs
- ScriptIgnoreAttribute.cs
- ZipIOLocalFileHeader.cs
- EditorAttribute.cs
- DataRowExtensions.cs
- MachineKeySection.cs
- DbModificationClause.cs
- FieldNameLookup.cs
- DispatcherEventArgs.cs
- RegexTree.cs
- ValueType.cs
- WebPartsPersonalizationAuthorization.cs
- XPathExpr.cs
- TraceContextRecord.cs
- WebPartAddingEventArgs.cs
- DateTimeSerializationSection.cs
- XmlMembersMapping.cs
- SQLDecimal.cs
- CorrelationManager.cs
- HttpCookiesSection.cs
- SoapConverter.cs
- ConvertEvent.cs
- TimeManager.cs
- AnonymousIdentificationModule.cs
- ApplicationId.cs
- ValidationEventArgs.cs
- MessageLoggingFilterTraceRecord.cs
- XmlNodeChangedEventManager.cs
- ContentPlaceHolderDesigner.cs
- versioninfo.cs
- ProxyFragment.cs
- Sql8ConformanceChecker.cs
- ServiceDescriptionData.cs
- AmbientLight.cs
- FlowLayoutSettings.cs
- GCHandleCookieTable.cs
- MdImport.cs
- XPathSingletonIterator.cs
- XmlWrappingReader.cs
- WrapPanel.cs
- LazyLoadBehavior.cs
- WorkflowPageSetupDialog.cs
- DetailsViewRowCollection.cs
- SubMenuStyleCollectionEditor.cs
- HierarchicalDataSourceIDConverter.cs
- ReachDocumentReferenceSerializerAsync.cs
- TableChangeProcessor.cs
- _Win32.cs
- Application.cs
- WebPartEventArgs.cs
- User.cs
- RuleSettings.cs
- ReadOnlyDictionary.cs
- DockEditor.cs
- SqlVersion.cs
- VarRefManager.cs
- ContentDisposition.cs
- Int16.cs
- OperatingSystem.cs
- AlternateView.cs
- SmtpSection.cs
- HtmlAnchor.cs
- Clock.cs
- TargetConverter.cs
- BindingNavigatorDesigner.cs
- TextInfo.cs
- TypeBuilderInstantiation.cs