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
- SignatureHelper.cs
- GridViewRowEventArgs.cs
- SocketStream.cs
- RawStylusInputCustomData.cs
- EpmTargetPathSegment.cs
- UriTemplateVariableQueryValue.cs
- ListManagerBindingsCollection.cs
- translator.cs
- XmlSchemaExternal.cs
- CqlParserHelpers.cs
- Identity.cs
- BitmapEffect.cs
- ErrorWrapper.cs
- GroupBox.cs
- PtsPage.cs
- EdmItemCollection.OcAssemblyCache.cs
- Serializer.cs
- EventProvider.cs
- TextSelection.cs
- GridEntry.cs
- SafeBitVector32.cs
- TableRowGroup.cs
- DefaultSerializationProviderAttribute.cs
- HttpConfigurationContext.cs
- StringUtil.cs
- SafeEventLogWriteHandle.cs
- VBCodeProvider.cs
- AssemblyResourceLoader.cs
- ValidationErrorInfo.cs
- SymmetricCryptoHandle.cs
- CurrentChangingEventArgs.cs
- MimePart.cs
- EmissiveMaterial.cs
- ProviderBase.cs
- InternalBase.cs
- DBCommand.cs
- DataGridHeaderBorder.cs
- VersionValidator.cs
- Adorner.cs
- ResourceReader.cs
- EntityWithKeyStrategy.cs
- XmlSchemaSimpleType.cs
- DecoderBestFitFallback.cs
- LongTypeConverter.cs
- BridgeDataReader.cs
- WhitespaceSignificantCollectionAttribute.cs
- DocumentXPathNavigator.cs
- IisTraceWebEventProvider.cs
- InvalidComObjectException.cs
- RegexTypeEditor.cs
- CodeDomDesignerLoader.cs
- TextProperties.cs
- webclient.cs
- StaticContext.cs
- OleDbSchemaGuid.cs
- Resources.Designer.cs
- MultiView.cs
- RelationshipEndMember.cs
- ClientBuildManager.cs
- DiscoveryOperationContextExtension.cs
- HostUtils.cs
- ProcessModelSection.cs
- EmptyReadOnlyDictionaryInternal.cs
- TypeBuilderInstantiation.cs
- QilIterator.cs
- KeyProperty.cs
- RectangleHotSpot.cs
- RadialGradientBrush.cs
- ColumnBinding.cs
- ColumnMapProcessor.cs
- ContentWrapperAttribute.cs
- ObjectDataSource.cs
- SqlProcedureAttribute.cs
- PaintEvent.cs
- RMPublishingDialog.cs
- ResponseBodyWriter.cs
- WebPartCancelEventArgs.cs
- LineProperties.cs
- Accessors.cs
- XPathEmptyIterator.cs
- LeaseManager.cs
- Unit.cs
- ZoomingMessageFilter.cs
- SafeFileMappingHandle.cs
- EventRecordWrittenEventArgs.cs
- XmlNotation.cs
- UserMapPath.cs
- RequestUriProcessor.cs
- PasswordRecovery.cs
- StateManagedCollection.cs
- AuthenticatingEventArgs.cs
- WebPartConnection.cs
- WebServiceTypeData.cs
- SystemBrushes.cs
- ListViewGroupItemCollection.cs
- GlyphInfoList.cs
- WorkflowOperationInvoker.cs
- EventMappingSettings.cs
- BlurEffect.cs
- counter.cs