Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / ManagedLibraries / Remoting / Channels / CORE / ByteBufferPool.cs / 1305376 / ByteBufferPool.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== //========================================================================== // File: ByteBufferPool.cs // // Summary: Stream used for reading from a socket by remoting channels. // //========================================================================= using System; using System.Threading; namespace System.IO { internal interface IByteBufferPool { byte[] GetBuffer(); void ReturnBuffer(byte[] buffer); } // This isn't actually a buffer pool. It always creates a new byte buffer. internal class ByteBufferAllocator : IByteBufferPool { private int _bufferSize; public ByteBufferAllocator(int bufferSize) { _bufferSize = bufferSize; } public byte[] GetBuffer() { return new byte[_bufferSize]; } public void ReturnBuffer(byte[] buffer) { } } // ByteBufferAllocator internal class ByteBufferPool : IByteBufferPool { private byte[][] _bufferPool = null; private int _current; // -1 for none private int _last; private int _max; // maximum number of buffers to pool private int _bufferSize; private Object _controlCookie = "cookie object"; public ByteBufferPool(int maxBuffers, int bufferSize) { _max = maxBuffers; _bufferPool = new byte[_max][]; _bufferSize = bufferSize; _current = -1; _last = -1; } // ByteBufferPool public byte[] GetBuffer() { Object cookie = null; try { // If a ThreadAbortException gets thrown after the exchange, // but before the result is assigned to cookie, then the // control cookie is lost forever. However, the buffer pool // will still function normally and return everybody a new // buffer each time (that isn't very likely to happen, // so we don't really care). cookie = Interlocked.Exchange(ref _controlCookie, null); if (cookie != null) { // we have the control cookie, so take a buffer if (_current == -1) { _controlCookie = cookie; // no pooled buffers available return new byte[_bufferSize]; } else { // grab next available buffer byte[] buffer = _bufferPool[_current]; _bufferPool[_current] = null; // update "current" index if (_current == _last) { // this is the last free buffer _current = -1; } else { _current = (_current + 1) % _max; } _controlCookie = cookie; return buffer; } } else { // we don't have the control cookie, so just create a new buffer since // there will probably be a lot of contention anyway. return new byte[_bufferSize]; } } catch (ThreadAbortException) { if (cookie != null) { // This should be rare, so just reset // everything to the initial state. _current = -1; _last = -1; // restore cookie _controlCookie = cookie; } throw; } } // GetBuffer public void ReturnBuffer(byte[] buffer) { if (buffer == null) throw new ArgumentNullException("buffer"); // The purpose of the buffer pool is to try to reduce the // amount of garbage generated, so it doesn't matter if // the buffer gets tossed out. Since we don't want to // take the perf hit of taking a lock, we only return // the buffer if we can grab the control cookie. Object cookie = null; try { // If a ThreadAbortException gets thrown after the exchange, // but before the result is assigned to cookie, then the // control cookie is lost forever. However, the buffer pool // will still function normally and return everybody a new // buffer each time (that isn't very likely to happen, // so we don't really care). cookie = Interlocked.Exchange(ref _controlCookie, null); if (cookie != null) { if (_current == -1) { _bufferPool[0] = buffer; _current = 0; _last = 0; } else { int newLast = (_last + 1) % _max; if (newLast != _current) { // the pool isn't full so store this buffer _last = newLast; _bufferPool[_last] = buffer; } } _controlCookie = cookie; } } catch (ThreadAbortException) { if (cookie != null) { // This should be rare, so just reset // everything to the initial state. _current = -1; _last = -1; // restore cookie _controlCookie = cookie; } throw; } } // ReturnBuffer } // ByteBufferPool } // namespace System.IO // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== //========================================================================== // File: ByteBufferPool.cs // // Summary: Stream used for reading from a socket by remoting channels. // //========================================================================= using System; using System.Threading; namespace System.IO { internal interface IByteBufferPool { byte[] GetBuffer(); void ReturnBuffer(byte[] buffer); } // This isn't actually a buffer pool. It always creates a new byte buffer. internal class ByteBufferAllocator : IByteBufferPool { private int _bufferSize; public ByteBufferAllocator(int bufferSize) { _bufferSize = bufferSize; } public byte[] GetBuffer() { return new byte[_bufferSize]; } public void ReturnBuffer(byte[] buffer) { } } // ByteBufferAllocator internal class ByteBufferPool : IByteBufferPool { private byte[][] _bufferPool = null; private int _current; // -1 for none private int _last; private int _max; // maximum number of buffers to pool private int _bufferSize; private Object _controlCookie = "cookie object"; public ByteBufferPool(int maxBuffers, int bufferSize) { _max = maxBuffers; _bufferPool = new byte[_max][]; _bufferSize = bufferSize; _current = -1; _last = -1; } // ByteBufferPool public byte[] GetBuffer() { Object cookie = null; try { // If a ThreadAbortException gets thrown after the exchange, // but before the result is assigned to cookie, then the // control cookie is lost forever. However, the buffer pool // will still function normally and return everybody a new // buffer each time (that isn't very likely to happen, // so we don't really care). cookie = Interlocked.Exchange(ref _controlCookie, null); if (cookie != null) { // we have the control cookie, so take a buffer if (_current == -1) { _controlCookie = cookie; // no pooled buffers available return new byte[_bufferSize]; } else { // grab next available buffer byte[] buffer = _bufferPool[_current]; _bufferPool[_current] = null; // update "current" index if (_current == _last) { // this is the last free buffer _current = -1; } else { _current = (_current + 1) % _max; } _controlCookie = cookie; return buffer; } } else { // we don't have the control cookie, so just create a new buffer since // there will probably be a lot of contention anyway. return new byte[_bufferSize]; } } catch (ThreadAbortException) { if (cookie != null) { // This should be rare, so just reset // everything to the initial state. _current = -1; _last = -1; // restore cookie _controlCookie = cookie; } throw; } } // GetBuffer public void ReturnBuffer(byte[] buffer) { if (buffer == null) throw new ArgumentNullException("buffer"); // The purpose of the buffer pool is to try to reduce the // amount of garbage generated, so it doesn't matter if // the buffer gets tossed out. Since we don't want to // take the perf hit of taking a lock, we only return // the buffer if we can grab the control cookie. Object cookie = null; try { // If a ThreadAbortException gets thrown after the exchange, // but before the result is assigned to cookie, then the // control cookie is lost forever. However, the buffer pool // will still function normally and return everybody a new // buffer each time (that isn't very likely to happen, // so we don't really care). cookie = Interlocked.Exchange(ref _controlCookie, null); if (cookie != null) { if (_current == -1) { _bufferPool[0] = buffer; _current = 0; _last = 0; } else { int newLast = (_last + 1) % _max; if (newLast != _current) { // the pool isn't full so store this buffer _last = newLast; _bufferPool[_last] = buffer; } } _controlCookie = cookie; } } catch (ThreadAbortException) { if (cookie != null) { // This should be rare, so just reset // everything to the initial state. _current = -1; _last = -1; // restore cookie _controlCookie = cookie; } throw; } } // ReturnBuffer } // ByteBufferPool } // namespace System.IO // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- MetadataArtifactLoaderFile.cs
- ProfileSettingsCollection.cs
- TextDecoration.cs
- XmlSchemaAnnotated.cs
- XmlNodeList.cs
- DesignerForm.cs
- oledbmetadatacolumnnames.cs
- InputLanguageSource.cs
- ExpressionValueEditor.cs
- ConstraintStruct.cs
- GridView.cs
- GroupBoxRenderer.cs
- SequentialUshortCollection.cs
- SimpleBitVector32.cs
- FontWeight.cs
- HandlerBase.cs
- Point.cs
- ButtonChrome.cs
- OdbcHandle.cs
- TimeoutValidationAttribute.cs
- QilFactory.cs
- XamlWriterExtensions.cs
- COM2Properties.cs
- PieceNameHelper.cs
- ExecutionContext.cs
- PaperSize.cs
- EllipseGeometry.cs
- UserInitiatedNavigationPermission.cs
- ElementNotEnabledException.cs
- HtmlInputText.cs
- ObjectListShowCommandsEventArgs.cs
- Utils.cs
- RegexMatch.cs
- WebPartVerb.cs
- ReliableSession.cs
- DictionaryKeyPropertyAttribute.cs
- NonSerializedAttribute.cs
- CreateDataSourceDialog.cs
- QilReference.cs
- DbParameterCollectionHelper.cs
- XmlSchemaGroup.cs
- ConfigurationSection.cs
- SerializableAttribute.cs
- DbReferenceCollection.cs
- __Error.cs
- Stylesheet.cs
- DateTimeConverter2.cs
- MruCache.cs
- CqlLexerHelpers.cs
- StyleXamlParser.cs
- TemplatePropertyEntry.cs
- DataBindingHandlerAttribute.cs
- OleDbCommand.cs
- StructuralCache.cs
- Figure.cs
- EntityClassGenerator.cs
- ToolStripManager.cs
- ObjectFullSpanRewriter.cs
- _IPv6Address.cs
- LinkArea.cs
- Comparer.cs
- GlobalizationSection.cs
- PropertyItemInternal.cs
- ClientProxyGenerator.cs
- ConcurrencyBehavior.cs
- RootProfilePropertySettingsCollection.cs
- PartitionResolver.cs
- Schema.cs
- DataGridViewTextBoxColumn.cs
- UserCancellationException.cs
- WorkflowServiceBuildProvider.cs
- NetworkCredential.cs
- ListViewDesigner.cs
- FixUpCollection.cs
- TiffBitmapDecoder.cs
- DoubleAnimationUsingPath.cs
- InheritanceRules.cs
- XmlSignatureManifest.cs
- BinaryMethodMessage.cs
- TransformerInfoCollection.cs
- SizeAnimation.cs
- XPathNodeIterator.cs
- DataPagerCommandEventArgs.cs
- QueryTaskGroupState.cs
- ProcessStartInfo.cs
- ValidationHelper.cs
- ScriptingWebServicesSectionGroup.cs
- ToolStripDropDownClosingEventArgs.cs
- PropertyExpression.cs
- SimpleTypeResolver.cs
- WizardSideBarListControlItemEventArgs.cs
- MemberCollection.cs
- TagPrefixAttribute.cs
- SettingsBindableAttribute.cs
- SecurityManager.cs
- loginstatus.cs
- LinqTreeNodeEvaluator.cs
- Pts.cs
- CodePageEncoding.cs
- EdmTypeAttribute.cs