public override byte[] TakeBuffer(int bufferSize) { Fx.Assert(bufferSize >= 0, "caller must ensure a non-negative argument"); BufferPool bufferPool = FindPool(bufferSize); if (bufferPool != null) { byte[] buffer = bufferPool.Take(); if (buffer != null) { bufferPool.DecrementCount(); return(buffer); } if (bufferPool.Peak == bufferPool.Limit) { bufferPool.Misses++; if (++totalMisses >= maxMissesBeforeTuning) { TuneQuotas(); } } return(InternalBufferManager.AllocateByteArray(bufferPool.BufferSize)); } else { return(InternalBufferManager.AllocateByteArray(bufferSize)); } }
public void Validate(bool write, int dataSize) { bool valid = false; if (write) { if (this.Size < dataSize && this.autoGrow) { if (this.references != 1) { throw new InvalidOperationException("Cannot grow the current buffer because it has more than one references"); } int newSize = Math.Max(this.Capacity * 2, this.Capacity + dataSize); ManagedBuffer newBuffer; if (this.bufferManager != null) { newBuffer = ByteBuffer.AllocateBuffer(newSize, this.bufferManager); } else { newBuffer = new ManagedBuffer(new byte[newSize], null); } System.Buffer.BlockCopy(this.buffer, this.start, newBuffer.Buffer, 0, this.Capacity); int consumed = this.read - this.start; int written = this.write - this.start; this.start = 0; this.read = consumed; this.write = written; this.end = newSize; if (this.bufferManager != null) { this.bufferManager.ReturnBuffer(this.buffer); } this.buffer = newBuffer.Buffer; this.bufferManager = newBuffer.BufferManager; } valid = this.Size >= dataSize; } else { valid = this.Length >= dataSize; } if (!valid) { throw new AmqpException(AmqpErrorCode.DecodeError, AmqpResources.GetString(AmqpResources.AmqpInsufficientBufferSize, dataSize, write ? this.Size : this.Length)); } }
ByteBuffer(byte[] buffer, int offset, int count, int size, bool autoGrow, InternalBufferManager bufferManager) { Fx.Assert(buffer != null, "buffer cannot be null"); this.buffer = buffer; this.start = offset; this.read = offset; this.write = offset + count; this.end = offset + size; this.autoGrow = autoGrow; this.bufferManager = bufferManager; this.references = 1; }
public static void InitBufferManagers() { if (TransportBufferManager == null) { lock (syncRoot) { if (TransportBufferManager == null) { TransportBufferManager = InternalBufferManager.Create(48 * 1024 * 1024, AmqpConstants.TransportBufferSize, true); } } } }
// attempts to allocate using the supplied buffer manager, falls back to the default buffer manager on failure static ManagedBuffer AllocateBuffer(int size, InternalBufferManager bufferManager) { if (bufferManager != null) { byte[] buffer = bufferManager.TakeBuffer(size); if (buffer != null) { return(new ManagedBuffer(buffer, bufferManager)); } } return(new ManagedBuffer(BufferManager.TakeBuffer(size), BufferManager)); }
/// <summary> /// Initializes the pool of buffers used for transport read and write. /// </summary> /// <param name="bufferSize">The buffer size in bytes.</param> /// <param name="maxCount">Number of buffers to allocate.</param> /// <remarks>This method allocates the buffers. It should /// be called as early as possible when the process starts, so that /// when a buffer is pinned for pending I/O, it doesn't cause significant /// heap fragmentation.</remarks> public static void InitTransportBufferManager(int bufferSize, int maxCount) { if (TransportBufferManager == null) { lock (syncRoot) { if (TransportBufferManager == null) { TransportBufferManager = InternalBufferManager.CreatePreallocatedBufferManager(bufferSize, maxCount); } } } }
ByteBuffer(byte[] buffer, int offset, int count, int size, bool autoGrow, InternalBufferManager bufferManager) { Fx.Assert(buffer != null, "buffer cannot be null"); this.buffer = buffer; this.start = offset; this.read = offset; this.write = offset + count; this.end = offset + size; this.autoGrow = autoGrow; this.bufferManager = bufferManager; this.references = 1; #if DEBUG if (AmqpTrace.AmqpDebug && bufferManager != null) { this.stack = new StackTrace(); } #endif }
public ManagedBuffer(byte[] buffer, InternalBufferManager bufferManager) { this.Buffer = buffer; this.BufferManager = bufferManager; }
// attempts to allocate using the supplied buffer manager, falls back to the default buffer manager on failure static ManagedBuffer AllocateBuffer(int size, InternalBufferManager bufferManager) { if (bufferManager != null) { byte[] buffer = bufferManager.TakeBuffer(size); if (buffer != null) { return new ManagedBuffer(buffer, bufferManager); } } return new ManagedBuffer(BufferManager.TakeBuffer(size), BufferManager); }
public override byte[] TakeBuffer(int bufferSize) { return(InternalBufferManager.AllocateByteArray(bufferSize)); }