private PinnedBuffer CheckOutBuffer(long size) { var buffer = FirstAvailableBuffer(size); if (buffer == null) { buffer = new PinnedBuffer(size); _buffers.Add(buffer); } buffer.Reserve(); //todo: improve encapsulation here Log.Debug(Name + " Buffer of size " + size + " checked out - there are now " + Count(b => b.Length == size) + " buffers of that size, " + Count(b => b.Length == size && b.Available) + " available."); return(buffer); }
/// <summary> /// Finds the first available buffer or returns null. Calls to linq /// like FirstOrDefault are not thread safe and blow up if the underlying /// iterator changes. /// </summary> private PinnedBuffer FirstAvailableBuffer(long size) { PinnedBuffer buffer = null; // This implementation assumes that we add to _buffers, but don't remove for (int i = 0; i < _buffers.Count; i++) { var b = _buffers[i]; if (b != null && b.Length == size && b.Available) { buffer = b; break; } } return(buffer); }
public unsafe WrappedSquareImage(BufferManager manager, PinnedBuffer buffer, int dimension, int bytesPerPixel) : base(manager, buffer) { switch (dimension) { case 128: InnerImage = new TwoByte128X128Image(buffer.Buffer); break; case 256: InnerImage = new TwoByte256X256Image(buffer.Buffer); break; case 512: InnerImage = new TwoByte512X512Image(buffer.Buffer); break; case 1024: { if (bytesPerPixel == 2) { InnerImage = new TwoByte1024X1024Image(buffer.Buffer); } else if (bytesPerPixel == 3) { InnerImage = new ThreeByte1024X1024Image(buffer.Buffer); } else { throw new Exception($"Invalid bytes per pixel requested for new WrappedSquareImage: {bytesPerPixel}"); } break; } default: throw new Exception($"Invalid image size requested for new WrappedSquareImage: {dimension}"); } ImagePixelHeight = dimension; ImagePixelWidth = dimension; BytesPerPixel = bytesPerPixel; }
public WrappedBuffer(BufferManager manager, PinnedBuffer buffer) { _manager = manager; _buffer = buffer; }
internal void Return(PinnedBuffer buffer) { buffer.Release(); //todo: improve encapsulation here Log.Debug(Name + " Buffer returned."); }
public unsafe WrappedImage(BufferManager manager, PinnedBuffer buffer) : base(manager, buffer) { Frame = new T(); Frame.SetBuffer(buffer.Buffer, (int)buffer.Length); }