Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Activation / ServiceMemoryGates.cs / 1 / ServiceMemoryGates.cs
//---------------------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------------------- namespace System.ServiceModel.Activation { using System.Collections.Generic; using System.ServiceModel.Channels; using System.Runtime.InteropServices; using System.ComponentModel; using System.Threading; using System.Runtime.CompilerServices; using System.Security; static class ServiceMemoryGates { ////// Critical - uses SecurityCritical helper methods to check memory status /// - allocates unmanaged resources, can only be called with admin-specified value /// [SecurityCritical] internal static void Check(int minFreeMemoryPercentage) { // Boundary check percentage, if out of bounds Gate is turned off. // 0 is defined as disabled. Configuration defines 99 as max allowed so we disable // if we receive something out of range if (minFreeMemoryPercentage < 1 || minFreeMemoryPercentage > 99) { return; } UnsafeNativeMethods.MEMORYSTATUSEX memoryStatus = new UnsafeNativeMethods.MEMORYSTATUSEX(); GetGlobalMemoryStatus(ref memoryStatus); ulong threshold = memoryStatus.ullTotalPageFile / 100 * (ulong)minFreeMemoryPercentage; if (memoryStatus.ullAvailPageFile < threshold) { // Yield this thread -- this will also ensure that we have a full time slice when we // do our alloc/free Thread.Sleep(0); // Commit 1.5 times of the threshold. This should force the page file to grow, if // possible. (GlobalMemoryStatusEx only returns information about the current pagefile, // not about the maximum pagefile.) uint sizeToAlloc = 0; if (threshold < (ulong)int.MaxValue) { sizeToAlloc = (uint)threshold / 2 * 3; } if (sizeToAlloc > 0) { ForcePageFileToGrow(sizeToAlloc); GetGlobalMemoryStatus(ref memoryStatus); threshold = memoryStatus.ullTotalPageFile / 100 * (ulong)minFreeMemoryPercentage; } if (memoryStatus.ullAvailPageFile < threshold) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InsufficientMemoryException( SR.GetString(SR.Hosting_MemoryGatesCheckFailed, memoryStatus.ullAvailPageFile, minFreeMemoryPercentage))); } } } ////// Critical - Uses UnsafeNativeMethods, caller must guard parameter /// [SecurityCritical] static void ForcePageFileToGrow(uint sizeToAlloc) { RuntimeHelpers.PrepareConstrainedRegions(); try {} finally { // We don't care if this actually succeeds or not, we are just trying to grow the page // file. #pragma warning suppress 56523 // [....], we don't care whether this actually succeeds IntPtr ptr = UnsafeNativeMethods.VirtualAlloc(IntPtr.Zero, (UIntPtr)sizeToAlloc, UnsafeNativeMethods.MEM_COMMIT, (uint)UnsafeNativeMethods.PAGE_READWRITE); if (ptr != IntPtr.Zero) { #pragma warning suppress 56523 // [....], we don't care whether this actually succeeds UnsafeNativeMethods.VirtualFree(ptr, (UIntPtr)sizeToAlloc, UnsafeNativeMethods.MEM_DECOMMIT); } } } ////// Critical - Uses UnsafeNativeMethods to fetch memory status structure, caller must not leak it /// [SecurityCritical] static void GetGlobalMemoryStatus(ref UnsafeNativeMethods.MEMORYSTATUSEX memoryStatus) { memoryStatus.dwLength = (uint)Marshal.SizeOf(typeof(UnsafeNativeMethods.MEMORYSTATUSEX)); if (!UnsafeNativeMethods.GlobalMemoryStatusEx(ref memoryStatus)) { int error = Marshal.GetLastWin32Error(); // Treat as the worst case. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new InvalidOperationException(SR.GetString(SR.Hosting_GetGlobalMemoryFailed), new Win32Exception(error))); } } } } // 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
- ProvideValueServiceProvider.cs
- FactoryMaker.cs
- _TLSstream.cs
- LongValidatorAttribute.cs
- XmlSecureResolver.cs
- Lock.cs
- EventlogProvider.cs
- DataRecordObjectView.cs
- VerificationException.cs
- CollectionContainer.cs
- Panel.cs
- ZoneMembershipCondition.cs
- Style.cs
- MatrixCamera.cs
- MatrixCamera.cs
- sqlinternaltransaction.cs
- ExtensionDataReader.cs
- SwitchCase.cs
- CodeAccessSecurityEngine.cs
- SelectionEditor.cs
- CellTreeNodeVisitors.cs
- AdRotator.cs
- ColumnClickEvent.cs
- PersonalizationState.cs
- CompiledIdentityConstraint.cs
- assemblycache.cs
- WebEvents.cs
- Internal.cs
- Range.cs
- XmlStreamStore.cs
- DetailsViewDeletedEventArgs.cs
- ADConnectionHelper.cs
- DataPagerFieldItem.cs
- AdornedElementPlaceholder.cs
- NodeCounter.cs
- XmlAttributeCollection.cs
- Win32Exception.cs
- MouseDevice.cs
- DependencyStoreSurrogate.cs
- SplitterCancelEvent.cs
- HashLookup.cs
- FileChangeNotifier.cs
- EdmSchemaError.cs
- SqlProvider.cs
- RegexRunner.cs
- ProgressBar.cs
- DescendantOverDescendantQuery.cs
- Size3D.cs
- ServiceRoute.cs
- QilLoop.cs
- SamlAuthorizationDecisionClaimResource.cs
- SelectionList.cs
- ResourceAssociationSet.cs
- TemplateKey.cs
- XmlQueryRuntime.cs
- SerializationSectionGroup.cs
- Convert.cs
- HttpResponseInternalWrapper.cs
- XmlAutoDetectWriter.cs
- ValueSerializer.cs
- DefaultValueAttribute.cs
- NonBatchDirectoryCompiler.cs
- CultureTable.cs
- DBSchemaTable.cs
- SqlDependencyListener.cs
- OptionalColumn.cs
- WebPartRestoreVerb.cs
- SequentialUshortCollection.cs
- RuntimeConfig.cs
- WebCategoryAttribute.cs
- WhitespaceRuleLookup.cs
- ClientScriptManagerWrapper.cs
- ParameterToken.cs
- PermissionRequestEvidence.cs
- ExpandedWrapper.cs
- BindUriHelper.cs
- MethodBody.cs
- AuthenticationConfig.cs
- OdbcConnectionString.cs
- InfoCardRSAOAEPKeyExchangeFormatter.cs
- GifBitmapEncoder.cs
- PenContext.cs
- DataGridViewRowHeightInfoPushedEventArgs.cs
- LinkArea.cs
- ECDsa.cs
- ServiceHostFactory.cs
- ToolStripSplitStackLayout.cs
- X509CertificateCollection.cs
- XXXOnTypeBuilderInstantiation.cs
- LogSwitch.cs
- ResponseStream.cs
- XPathAncestorQuery.cs
- WindowInteropHelper.cs
- UserPreferenceChangingEventArgs.cs
- CodeMemberMethod.cs
- NeutralResourcesLanguageAttribute.cs
- ErrorStyle.cs
- ListItem.cs
- FileStream.cs
- BulletedList.cs