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
- DbMetaDataColumnNames.cs
- CustomAttributeBuilder.cs
- SpellCheck.cs
- PersonalizationState.cs
- WeakReferenceList.cs
- _AuthenticationState.cs
- SchemaAttDef.cs
- ComponentResourceManager.cs
- RSAOAEPKeyExchangeFormatter.cs
- Int32AnimationUsingKeyFrames.cs
- DefaultWorkflowSchedulerService.cs
- ActiveXHelper.cs
- DataGridViewBand.cs
- BitmapEffect.cs
- DATA_BLOB.cs
- XmlNamespaceMappingCollection.cs
- RotateTransform3D.cs
- HMACSHA512.cs
- RecognizedWordUnit.cs
- x509utils.cs
- CompilationLock.cs
- DataGridColumn.cs
- WebPart.cs
- ServicePrincipalNameElement.cs
- MultipartIdentifier.cs
- RegexReplacement.cs
- WindowsFormsSectionHandler.cs
- Variant.cs
- ConfigurationSectionGroup.cs
- Trigger.cs
- SizeF.cs
- SchemeSettingElementCollection.cs
- OdbcRowUpdatingEvent.cs
- MemberDescriptor.cs
- CryptoApi.cs
- DebugViewWriter.cs
- XmlSchemaImporter.cs
- StateMachineSubscription.cs
- PassportIdentity.cs
- InputMethodStateTypeInfo.cs
- InkCanvasAutomationPeer.cs
- CompatibleIComparer.cs
- StatusBarPanelClickEvent.cs
- DispatcherObject.cs
- TypedDataSourceCodeGenerator.cs
- RegexCompilationInfo.cs
- FontNameConverter.cs
- FixedStringLookup.cs
- DigitShape.cs
- BufferedReceiveElement.cs
- printdlgexmarshaler.cs
- WebProxyScriptElement.cs
- FilterException.cs
- SQLBytes.cs
- PathSegment.cs
- DetailsViewRow.cs
- GcHandle.cs
- MouseActionConverter.cs
- TextBlock.cs
- TypedElement.cs
- DragDrop.cs
- ScrollData.cs
- TreeNodeEventArgs.cs
- XmlTextAttribute.cs
- DesignerSerializerAttribute.cs
- DataGridRelationshipRow.cs
- EntityExpressionVisitor.cs
- ValidationErrorCollection.cs
- PageParser.cs
- Choices.cs
- TogglePatternIdentifiers.cs
- DataGridViewTopLeftHeaderCell.cs
- HandlerBase.cs
- EntityDataSourceWizardForm.cs
- BoolExpr.cs
- TailPinnedEventArgs.cs
- RunInstallerAttribute.cs
- DataObjectPastingEventArgs.cs
- PathTooLongException.cs
- TextRangeEditTables.cs
- LogSwitch.cs
- ParameterToken.cs
- CursorConverter.cs
- RichTextBoxAutomationPeer.cs
- InvalidOperationException.cs
- QilInvoke.cs
- BulletedList.cs
- ListManagerBindingsCollection.cs
- DigitShape.cs
- Error.cs
- MsmqBindingElementBase.cs
- QueryTask.cs
- StaticTextPointer.cs
- AutoGeneratedField.cs
- SiteMapNode.cs
- CommentEmitter.cs
- MulticastOption.cs
- ArrayEditor.cs
- DesignerOptionService.cs
- BitmapEffectvisualstate.cs