Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Core / CSharp / System / Windows / Media / SafeMILHandle.cs / 2 / SafeMILHandle.cs
//---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // // Description: // A safe way to deal with unmanaged MIL interface pointers. //--------------------------------------------------------------------------- using System; using System.IO; using System.Security; using System.Security.Permissions; using System.Collections; using System.Reflection; using MS.Internal; using MS.Win32; using System.Diagnostics; using System.Windows.Media; using System.Runtime; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using Microsoft.Win32.SafeHandles; using Microsoft.Internal; using UnsafeNativeMethods=MS.Win32.PresentationCore.UnsafeNativeMethods; namespace System.Windows.Media { internal class SafeMILHandle : SafeHandleZeroOrMinusOneIsInvalid { ////// Use this constructor if the handle isn't ready yet and later /// set the handle with SetHandle. SafeMILHandle owns the release /// of the handle. /// ////// Critical: This derives from a class that has a link demand and inheritance demand /// TreatAsSafe: Ok to call constructor /// [SecurityCritical,SecurityTreatAsSafe] internal SafeMILHandle() : base(true) { } ////// Use this constructor if the handle exists at construction time. /// SafeMILHandle owns the release of the parameter. /// ////// Critical: This code calls UpdateEstimatedSize. /// It is used to keep memory around /// [SecurityCritical] internal SafeMILHandle(IntPtr handle, long estimatedSize) : base(true) { SetHandle(handle); // // Hint the GC at the size of the unmanaged memory associated with // this object. We release pressure in the finalizer. // UpdateEstimatedSize(estimatedSize); } ////// Change our size to the new size specified /// ////// Critical: This code calls into AddMemoryPressure and RemoveMemoryPressure /// both of which have link demands. It is used to keep memory around /// [SecurityCritical] internal void UpdateEstimatedSize(long estimatedSize) { if (_gcPressure > 0) { MemoryPressure.Remove(_gcPressure); } _gcPressure = estimatedSize; if (_gcPressure > 0) { MemoryPressure.Add(_gcPressure); } } ////// Critical - calls unmanaged code, not treat as safe because you must /// validate that handle is a valid COM object. /// [SecurityCritical] protected override bool ReleaseHandle() { UnsafeNativeMethods.MILUnknown.ReleaseInterface(ref handle); // // We've released the unmangaed memory, so remove associated // GC pressure. // UpdateEstimatedSize(0); return true; } // // Estimated size in bytes of the unmanaged memory we are holding onto // private long _gcPressure; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // // Description: // A safe way to deal with unmanaged MIL interface pointers. //--------------------------------------------------------------------------- using System; using System.IO; using System.Security; using System.Security.Permissions; using System.Collections; using System.Reflection; using MS.Internal; using MS.Win32; using System.Diagnostics; using System.Windows.Media; using System.Runtime; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using Microsoft.Win32.SafeHandles; using Microsoft.Internal; using UnsafeNativeMethods=MS.Win32.PresentationCore.UnsafeNativeMethods; namespace System.Windows.Media { internal class SafeMILHandle : SafeHandleZeroOrMinusOneIsInvalid { ////// Use this constructor if the handle isn't ready yet and later /// set the handle with SetHandle. SafeMILHandle owns the release /// of the handle. /// ////// Critical: This derives from a class that has a link demand and inheritance demand /// TreatAsSafe: Ok to call constructor /// [SecurityCritical,SecurityTreatAsSafe] internal SafeMILHandle() : base(true) { } ////// Use this constructor if the handle exists at construction time. /// SafeMILHandle owns the release of the parameter. /// ////// Critical: This code calls UpdateEstimatedSize. /// It is used to keep memory around /// [SecurityCritical] internal SafeMILHandle(IntPtr handle, long estimatedSize) : base(true) { SetHandle(handle); // // Hint the GC at the size of the unmanaged memory associated with // this object. We release pressure in the finalizer. // UpdateEstimatedSize(estimatedSize); } ////// Change our size to the new size specified /// ////// Critical: This code calls into AddMemoryPressure and RemoveMemoryPressure /// both of which have link demands. It is used to keep memory around /// [SecurityCritical] internal void UpdateEstimatedSize(long estimatedSize) { if (_gcPressure > 0) { MemoryPressure.Remove(_gcPressure); } _gcPressure = estimatedSize; if (_gcPressure > 0) { MemoryPressure.Add(_gcPressure); } } ////// Critical - calls unmanaged code, not treat as safe because you must /// validate that handle is a valid COM object. /// [SecurityCritical] protected override bool ReleaseHandle() { UnsafeNativeMethods.MILUnknown.ReleaseInterface(ref handle); // // We've released the unmangaed memory, so remove associated // GC pressure. // UpdateEstimatedSize(0); return true; } // // Estimated size in bytes of the unmanaged memory we are holding onto // private long _gcPressure; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- FontStretchConverter.cs
- ElasticEase.cs
- BinaryReader.cs
- SpellerInterop.cs
- ToolStripDesignerAvailabilityAttribute.cs
- FrameworkContentElementAutomationPeer.cs
- Variant.cs
- DBConnectionString.cs
- TableDesigner.cs
- SQLByteStorage.cs
- Int16KeyFrameCollection.cs
- XamlPoint3DCollectionSerializer.cs
- filewebrequest.cs
- DataObject.cs
- HttpCapabilitiesBase.cs
- SystemFonts.cs
- TypeUtil.cs
- Int32.cs
- PointLight.cs
- DataGridAutoGeneratingColumnEventArgs.cs
- SerializationStore.cs
- TriggerAction.cs
- Registry.cs
- DiagnosticsConfigurationHandler.cs
- ConfigurationManagerHelperFactory.cs
- Marshal.cs
- BehaviorEditorPart.cs
- TimelineGroup.cs
- EnumBuilder.cs
- DataGridLinkButton.cs
- UTF7Encoding.cs
- Span.cs
- HandlerBase.cs
- MouseGestureValueSerializer.cs
- ProcessStartInfo.cs
- HybridDictionary.cs
- ModifierKeysValueSerializer.cs
- DataGridCellAutomationPeer.cs
- LabelLiteral.cs
- dataprotectionpermission.cs
- UrlPath.cs
- EntityDataSourceQueryBuilder.cs
- MergeFilterQuery.cs
- BamlRecords.cs
- InitialServerConnectionReader.cs
- SystemGatewayIPAddressInformation.cs
- ControlPersister.cs
- RectangleGeometry.cs
- ConfigUtil.cs
- DrawingGroup.cs
- TransactionFlowElement.cs
- XmlEventCache.cs
- DataRecordObjectView.cs
- TimeIntervalCollection.cs
- MediaScriptCommandRoutedEventArgs.cs
- ReferenceAssemblyAttribute.cs
- HttpProfileBase.cs
- ContentType.cs
- UniformGrid.cs
- odbcmetadatacolumnnames.cs
- Bits.cs
- EventEntry.cs
- UshortList2.cs
- TraceHwndHost.cs
- WorkerRequest.cs
- OleDbParameterCollection.cs
- Certificate.cs
- securestring.cs
- DiscoveryDocumentSerializer.cs
- MultiBindingExpression.cs
- WaitHandleCannotBeOpenedException.cs
- RichTextBoxConstants.cs
- Nullable.cs
- InputLanguageCollection.cs
- CredentialCache.cs
- MergePropertyDescriptor.cs
- AssemblyCollection.cs
- ContainerUIElement3D.cs
- PropertyInformation.cs
- IODescriptionAttribute.cs
- XmlSchemaDatatype.cs
- WebPartConnectionsConfigureVerb.cs
- SqlFileStream.cs
- MinimizableAttributeTypeConverter.cs
- BinaryConverter.cs
- SqlCharStream.cs
- MdiWindowListStrip.cs
- SrgsText.cs
- DataGridColumn.cs
- GenericTypeParameterBuilder.cs
- WebPartDisplayModeCollection.cs
- AdornerLayer.cs
- KeyConstraint.cs
- ConsoleEntryPoint.cs
- EntityException.cs
- StateElement.cs
- EnumBuilder.cs
- PrintingPermission.cs
- XmlValidatingReader.cs
- VerificationAttribute.cs