Struct RingBuffer

A RingBuffer uses the underlying memory management system to avoid any copying of data (unless expanding).

struct RingBuffer(T, ulong floorSize = 8192)
  
if (isPowerOf2(T.sizeof));

It works by using the OS's mechanisms that map memory (mmap or VirtualAlloc) to map the same region to 2 consecutive addresses. This allows one to use a buffer simply as an array, even when the data wraps around the end of the buffer.

Like AllocatedBuffer, the growth is limited to doubling, but this has an extra restriction that the buffer must be a multiple of the page size. Note that this does NOT add any memory to the GC, so do not store GC pointers in this buffer.

Unlike AllocatedBuffer, this buffer is NOT copyable, so it must be refcounted if you are to pass it around. See rbufd which does this automatically for you. The reason for this is that it must unmap the memory on destruction.

Note that this buffer is not @safe, since it is possible on reallocation to have dangling pointers (if anything keeps a reference to the original memory).

Methods

NameDescription
avail ()
capacity ()
extend (request) Add more data to the window of currently valid data. To avoid expensive reallocation, use avail to tune this call.
releaseBack (elements) Give bytes back to the buffer from the back of the buffer. These bytes can be removed in this operation or further operations and should no longer be used.
releaseFront (elements) Give bytes back to the buffer from the front of the buffer. These bytes can be removed in this operation or further operations and should no longer be used.
window () The window of currently valid data.

Parameters

NameDescription
T The type of the elements the buffer will use. Must be sized as a power of 2.
floorSize The size that can be freely allocated before growth is restricted to 2x. Note that the OS imposes a floor size of one page in addition to this.