Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Core / CSharp / System / Windows / Media / SafeMILHandle.cs / 1 / 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
- MemoryRecordBuffer.cs
- WebServiceAttribute.cs
- CrossContextChannel.cs
- HostSecurityManager.cs
- ContractMapping.cs
- RightsManagementPermission.cs
- HttpWriter.cs
- QualifiedCellIdBoolean.cs
- MessageHeaderT.cs
- SQLSingle.cs
- BehaviorEditorPart.cs
- ConditionalAttribute.cs
- PersonalizationStateInfoCollection.cs
- PackagingUtilities.cs
- AnnotationHighlightLayer.cs
- RijndaelManagedTransform.cs
- ToolStripHighContrastRenderer.cs
- SortableBindingList.cs
- Context.cs
- DataMisalignedException.cs
- EntityClientCacheKey.cs
- DecimalConverter.cs
- TriState.cs
- AddInContractAttribute.cs
- AnnouncementInnerClientCD1.cs
- ColorAnimationUsingKeyFrames.cs
- DBSchemaTable.cs
- TaskSchedulerException.cs
- ToolStripDropDownButton.cs
- PerformanceCountersElement.cs
- WebPartDisplayModeEventArgs.cs
- Page.cs
- HttpCachePolicy.cs
- DependencyProperty.cs
- NeedSkipTokenVisitor.cs
- dsa.cs
- RecordsAffectedEventArgs.cs
- ZipIOEndOfCentralDirectoryBlock.cs
- ParameterModifier.cs
- RootDesignerSerializerAttribute.cs
- UIPropertyMetadata.cs
- shaper.cs
- XmlSchemaComplexContentExtension.cs
- MachineKey.cs
- FlowDocumentPaginator.cs
- TypedTableBase.cs
- InheritanceContextHelper.cs
- XamlLoadErrorInfo.cs
- LocatorBase.cs
- HtmlForm.cs
- DetailsViewCommandEventArgs.cs
- XomlCompilerParameters.cs
- ErrorTolerantObjectWriter.cs
- MetaChildrenColumn.cs
- FixedSOMPageConstructor.cs
- EdmProviderManifest.cs
- GeometryValueSerializer.cs
- MenuStrip.cs
- ServiceDefaults.cs
- NetworkAddressChange.cs
- DbParameterHelper.cs
- NetCodeGroup.cs
- DeploymentSectionCache.cs
- WebBrowserUriTypeConverter.cs
- ScriptReferenceBase.cs
- SrgsElement.cs
- ColumnResizeUndoUnit.cs
- InputGestureCollection.cs
- HatchBrush.cs
- Marshal.cs
- XmlChoiceIdentifierAttribute.cs
- FragmentQuery.cs
- ProviderCollection.cs
- DisplayNameAttribute.cs
- MatrixStack.cs
- JsonClassDataContract.cs
- ContentType.cs
- TrustManagerMoreInformation.cs
- DBCommandBuilder.cs
- RegexRunnerFactory.cs
- DataServiceResponse.cs
- SequenceDesigner.cs
- DefaultValueAttribute.cs
- BrowserTree.cs
- QilList.cs
- CategoryAttribute.cs
- HtmlLink.cs
- HelpInfo.cs
- SqlProviderUtilities.cs
- DispatcherExceptionEventArgs.cs
- mansign.cs
- NetWebProxyFinder.cs
- ConstraintStruct.cs
- DataGridPagerStyle.cs
- ListViewContainer.cs
- ObjectView.cs
- HtmlToClrEventProxy.cs
- _FixedSizeReader.cs
- HttpConfigurationSystem.cs
- Transform.cs