chronos/bipbuffer

  Source   Edit

This module implements Bip Buffer (bi-partite circular buffer) by Simone Cooke.

The Bip-Buffer is like a circular buffer, but slightly different. Instead of keeping one head and tail pointer to the data in the buffer, it maintains two revolving regions, allowing for fast data access without having to worry about wrapping at the end of the buffer. Buffer allocations are always maintained as contiguous blocks, allowing the buffer to be used in a highly efficient manner with API calls, and also reducing the amount of copying which needs to be performed to put data into the buffer. Finally, a two-phase allocation system allows the user to pessimistically reserve an area of buffer space, and then trim back the buffer to commit to only the space which was used.

https://www.codeproject.com/Articles/3479/The-Bip-Buffer-The-Circular-Buffer-with-a-Twist

Types

BipBuffer = object
  a, b, r: BipPos
  data: seq[byte]
  Source   Edit

Procs

func availSpace(bp: BipBuffer): Natural {....raises: [], tags: [].}
Returns amount of space available for reserve in buffer bp.   Source   Edit
proc commit(bp: var BipBuffer; size: Natural) {....raises: [], tags: [].}
Updates structure's pointers when new data inserted into buffer.   Source   Edit
proc consume(bp: var BipBuffer; size: Natural) {....raises: [], tags: [].}
The procedure removes/frees size bytes from the buffer bp.   Source   Edit
proc init(t: typedesc[BipBuffer]; size: int): BipBuffer {....raises: [].}
Creates new Bip Buffer with size size.   Source   Edit
func len(bp: BipBuffer): Natural {....raises: [], tags: [].}
  Source   Edit
proc reserve(bp: var BipBuffer; size: Natural = 0): tuple[data: ptr byte,
    size: Natural] {....raises: [], tags: [].}

Reserve size bytes in buffer.

If size == 0 (default) reserve all available space from buffer.

If there is not enough space in buffer for resevation - error will be returned.

Returns current reserved range as pointer of type pt and size of type st.

  Source   Edit

Iterators

iterator items(bp: BipBuffer): byte {....raises: [], tags: [].}
Iterates over all the bytes in the buffer.   Source   Edit
iterator regions(bp: var BipBuffer): tuple[data: ptr byte, size: Natural] {.
    ...raises: [], tags: [].}
Iterates over all the regions (a and b) in the buffer.   Source   Edit