Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / xsp / System / Web / BufferAllocator.cs / 1 / BufferAllocator.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- /* * Buffer Allocators with recycling * * Copyright (c) 1999 Microsoft Corporation */ namespace System.Web { using System.Collections; using System.IO; using System.Globalization; using System.Web.Util; ////////////////////////////////////////////////////////////////////////////// // Generic buffer recycling /* * Base class for allocator doing buffer recycling */ internal abstract class BufferAllocator { private int _maxFree; private int _numFree; private Stack _buffers; private static int s_ProcsFudgeFactor; static BufferAllocator() { s_ProcsFudgeFactor = SystemInfo.GetNumProcessCPUs(); if (s_ProcsFudgeFactor < 1) s_ProcsFudgeFactor = 1; if (s_ProcsFudgeFactor > 4) s_ProcsFudgeFactor = 4; } internal BufferAllocator(int maxFree) { _buffers = new Stack(); _numFree = 0; _maxFree = maxFree * s_ProcsFudgeFactor; } internal /*public*/ Object GetBuffer() { Object buffer = null; if (_numFree > 0) { lock(this) { if (_numFree > 0) { buffer = _buffers.Pop(); _numFree--; } } } if (buffer == null) buffer = AllocBuffer(); return buffer; } internal void ReuseBuffer(Object buffer) { if (_numFree < _maxFree) { lock(this) { if (_numFree < _maxFree) { _buffers.Push(buffer); _numFree++; } } } } /* * To be implemented by a derived class */ abstract protected Object AllocBuffer(); } /* * Concrete allocator class for ubyte[] buffer recycling */ internal class UbyteBufferAllocator : BufferAllocator { private int _bufferSize; internal UbyteBufferAllocator(int bufferSize, int maxFree) : base(maxFree) { _bufferSize = bufferSize; } protected override Object AllocBuffer() { return new byte[_bufferSize]; } } /* * Concrete allocator class for char[] buffer recycling */ internal class CharBufferAllocator : BufferAllocator { private int _bufferSize; internal CharBufferAllocator(int bufferSize, int maxFree) : base(maxFree) { _bufferSize = bufferSize; } protected override Object AllocBuffer() { return new char[_bufferSize]; } } /* * Concrete allocator class for int[] buffer recycling */ internal class IntegerArrayAllocator : BufferAllocator { private int _arraySize; internal IntegerArrayAllocator(int arraySize, int maxFree) : base(maxFree) { _arraySize = arraySize; } protected override Object AllocBuffer() { return new int[_arraySize]; } } /* * Concrete allocator class for IntPtr[] buffer recycling */ internal class IntPtrArrayAllocator : BufferAllocator { private int _arraySize; internal IntPtrArrayAllocator(int arraySize, int maxFree) : base(maxFree) { _arraySize = arraySize; } protected override Object AllocBuffer() { return new IntPtr[_arraySize]; } } }
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PointCollection.cs
- AnonymousIdentificationModule.cs
- ProviderConnectionPoint.cs
- MenuStrip.cs
- SelectionList.cs
- RenderOptions.cs
- ListViewEditEventArgs.cs
- mediapermission.cs
- CqlErrorHelper.cs
- TdsParserSafeHandles.cs
- DescendantOverDescendantQuery.cs
- HyperLink.cs
- PointLight.cs
- HiddenField.cs
- Mapping.cs
- XslAst.cs
- SmiEventSink_Default.cs
- GeneralTransform3DGroup.cs
- StateDesigner.Layouts.cs
- GridViewColumnCollection.cs
- Sql8ExpressionRewriter.cs
- UnsafeNativeMethodsCLR.cs
- InvalidDataContractException.cs
- AssignDesigner.xaml.cs
- ElasticEase.cs
- HierarchicalDataSourceControl.cs
- Cell.cs
- XsdValidatingReader.cs
- ListView.cs
- QueryableFilterRepeater.cs
- TextSelectionProcessor.cs
- TargetInvocationException.cs
- ImageBrush.cs
- WSTrust.cs
- Vector3D.cs
- DataGridItem.cs
- ImageKeyConverter.cs
- MediaTimeline.cs
- UnaryNode.cs
- SmtpReplyReaderFactory.cs
- LicFileLicenseProvider.cs
- CharacterString.cs
- ExtendedProtectionPolicy.cs
- ETagAttribute.cs
- _ConnectStream.cs
- MostlySingletonList.cs
- MenuEventArgs.cs
- _UncName.cs
- MessageUtil.cs
- ReturnEventArgs.cs
- TextEditorSelection.cs
- CommonBehaviorsSection.cs
- ImageDrawing.cs
- TagNameToTypeMapper.cs
- HttpGetProtocolReflector.cs
- XmlSchemaAnnotation.cs
- TraceListener.cs
- NameSpaceExtractor.cs
- LongMinMaxAggregationOperator.cs
- HttpListener.cs
- ListenerSingletonConnectionReader.cs
- ViewEventArgs.cs
- ScrollBarRenderer.cs
- ListBindableAttribute.cs
- TraceProvider.cs
- ProcessHostConfigUtils.cs
- UnsafePeerToPeerMethods.cs
- PageStatePersister.cs
- HotCommands.cs
- UpdateCommand.cs
- ConnectionOrientedTransportBindingElement.cs
- Label.cs
- RowsCopiedEventArgs.cs
- FilterQueryOptionExpression.cs
- ComponentCommands.cs
- ToolStripDropDownMenu.cs
- ListViewItemEventArgs.cs
- ProvidePropertyAttribute.cs
- CompatibleIComparer.cs
- HealthMonitoringSection.cs
- JpegBitmapDecoder.cs
- ErrorStyle.cs
- SQLDateTime.cs
- URLIdentityPermission.cs
- DnsPermission.cs
- Point.cs
- FunctionDescription.cs
- InheritedPropertyChangedEventArgs.cs
- PresentationSource.cs
- SAPIEngineTypes.cs
- ADRoleFactory.cs
- DataServiceResponse.cs
- Menu.cs
- Authorization.cs
- WindowsListView.cs
- ISAPIRuntime.cs
- AssemblyName.cs
- ParserExtension.cs
- OleDbStruct.cs
- ClonableStack.cs