Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / IO / PinnedBufferMemoryStream.cs / 1305376 / 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; using System.Diagnostics.Contracts; namespace System.IO { internal sealed unsafe class PinnedBufferMemoryStream : UnmanagedMemoryStream { private byte[] _array; private GCHandle _pinningHandle; // The new inheritance model requires a Critical default ctor since base (UnmanagedMemoryStream) has one [System.Security.SecurityCritical] private PinnedBufferMemoryStream():base(){} [System.Security.SecurityCritical] // auto-generated internal PinnedBufferMemoryStream(byte[] array) { Contract.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); } [System.Security.SecuritySafeCritical] // auto-generated 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; using System.Diagnostics.Contracts; namespace System.IO { internal sealed unsafe class PinnedBufferMemoryStream : UnmanagedMemoryStream { private byte[] _array; private GCHandle _pinningHandle; // The new inheritance model requires a Critical default ctor since base (UnmanagedMemoryStream) has one [System.Security.SecurityCritical] private PinnedBufferMemoryStream():base(){} [System.Security.SecurityCritical] // auto-generated internal PinnedBufferMemoryStream(byte[] array) { Contract.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); } [System.Security.SecuritySafeCritical] // auto-generated 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
- OleDbErrorCollection.cs
- ViewBase.cs
- EntityDataSourceEntityTypeFilterConverter.cs
- WmfPlaceableFileHeader.cs
- Exceptions.cs
- AppDomainProtocolHandler.cs
- PropertyCollection.cs
- DodSequenceMerge.cs
- TrustManagerPromptUI.cs
- SocketElement.cs
- DictionaryChange.cs
- HtmlShim.cs
- StringHelper.cs
- SchemaElementLookUpTableEnumerator.cs
- UnsafeNativeMethods.cs
- DocumentApplication.cs
- ListViewItemSelectionChangedEvent.cs
- MachineKeySection.cs
- ThrowHelper.cs
- PreProcessor.cs
- AsnEncodedData.cs
- BindingListCollectionView.cs
- TextEditorMouse.cs
- InputMethodStateTypeInfo.cs
- BuiltInExpr.cs
- SecurityRuntime.cs
- ReadOnlyHierarchicalDataSource.cs
- MaskDescriptor.cs
- Double.cs
- SmtpCommands.cs
- WebPartDisplayModeCollection.cs
- WebPartZone.cs
- SymDocumentType.cs
- TypedReference.cs
- HttpCapabilitiesEvaluator.cs
- ProtocolsConfigurationEntry.cs
- TypeNameConverter.cs
- Point4D.cs
- ServiceDefaults.cs
- ImpersonationContext.cs
- QuaternionValueSerializer.cs
- TextChangedEventArgs.cs
- WmpBitmapDecoder.cs
- DbParameterCollectionHelper.cs
- ExpressionConverter.cs
- TagNameToTypeMapper.cs
- XamlTypeMapperSchemaContext.cs
- EffectiveValueEntry.cs
- PackagingUtilities.cs
- ScriptResourceAttribute.cs
- TimelineGroup.cs
- SqlDataSourceCommandEventArgs.cs
- RegexMatchCollection.cs
- ByteRangeDownloader.cs
- DatagridviewDisplayedBandsData.cs
- GenericUriParser.cs
- TextModifier.cs
- RefType.cs
- DispatcherExceptionFilterEventArgs.cs
- ItemList.cs
- SectionUpdates.cs
- ISAPIWorkerRequest.cs
- ToolStripCustomTypeDescriptor.cs
- RegistryKey.cs
- EntityUtil.cs
- RegexReplacement.cs
- SatelliteContractVersionAttribute.cs
- DataGridRow.cs
- NativeMethods.cs
- SelectedCellsCollection.cs
- HttpCacheVary.cs
- PropertySourceInfo.cs
- HttpFormatExtensions.cs
- ColumnMapTranslator.cs
- ComponentResourceKeyConverter.cs
- ToolStripDesignerAvailabilityAttribute.cs
- TraversalRequest.cs
- PriorityBindingExpression.cs
- HotSpotCollection.cs
- SafeCryptContextHandle.cs
- QilBinary.cs
- WebControlAdapter.cs
- HeaderPanel.cs
- UIElementParaClient.cs
- WindowsPen.cs
- XmlDocumentSchema.cs
- GradientStop.cs
- FeatureManager.cs
- TabControlEvent.cs
- FunctionCommandText.cs
- CachedCompositeFamily.cs
- CollectionChangeEventArgs.cs
- OdbcConnectionOpen.cs
- ISAPIRuntime.cs
- ExpressionPrefixAttribute.cs
- FixedFlowMap.cs
- MiniCustomAttributeInfo.cs
- X509UI.cs
- InkCanvasInnerCanvas.cs
- InvalidProgramException.cs