rbuf

package
Version: v0.2.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2021 License: MIT, MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PointerRingBuf

type PointerRingBuf struct {
	A        []interface{}
	N        int // MaxView, the total size of A, whether or not in use.
	Beg      int // start of in-use data in A
	Readable int // number of pointers available in A (in use)
}

PointerRingBuf:

a fixed-size circular ring buffer of interface{}

func NewPointerRingBuf

func NewPointerRingBuf(sliceN int) *PointerRingBuf

constructor. NewPointerRingBuf will allocate internally a slice of size sliceN

func (*PointerRingBuf) Adopt

func (b *PointerRingBuf) Adopt(me []interface{})

Adopt(): non-standard.

For efficiency's sake, (possibly) take ownership of already allocated slice offered in me.

If me is large we will adopt it, and we will potentially then write to the me buffer. If we already have a bigger buffer, copy me into the existing buffer instead.

func (*PointerRingBuf) Advance

func (b *PointerRingBuf) Advance(n int)

Advance(): non-standard, but better than Next(), because we don't have to unwrap our buffer and pay the cpu time for the copy that unwrapping may need. Useful in conjuction/after ReadWithoutAdvance() above.

func (*PointerRingBuf) Push

func (b *PointerRingBuf) Push(p []interface{}) (n int, err error)

Push writes len(p) pointers from p to the ring. It returns the number of elements written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. Push must return a non-nil error if it returns n < len(p).

func (*PointerRingBuf) PushAndMaybeOverwriteOldestData

func (b *PointerRingBuf) PushAndMaybeOverwriteOldestData(p []interface{}) (n int, err error)

PushAndMaybeOverwriteOldestData always consumes the full slice p, even if that means blowing away the oldest unread pointers in the ring to make room. In reality, only the last min(len(p),b.N) bytes of p will end up being written to the ring.

This allows the ring to act as a record of the most recent b.N bytes of data -- a kind of temporal LRU cache, so the speak. The linux kernel's dmesg ring buffer is similar.

func (*PointerRingBuf) ReadPtrs

func (b *PointerRingBuf) ReadPtrs(p []interface{}) (n int, err error)

ReadPtrs():

from bytes.Buffer.Read(): Read reads the next len(p) interface{} pointers from the buffer or until the buffer is drained. The return value n is the number of bytes read. If the buffer has no data to return, err is io.EOF (unless len(p) is zero); otherwise it is nil.

func (*PointerRingBuf) ReadWithoutAdvance

func (b *PointerRingBuf) ReadWithoutAdvance(p []interface{}) (n int, err error)

ReadWithoutAdvance(): if you want to Read the data and leave it in the buffer, so as to peek ahead for example.

func (*PointerRingBuf) Reset

func (b *PointerRingBuf) Reset()

Reset quickly forgets any data stored in the ring buffer. The data is still there, but the ring buffer will ignore it and overwrite those buffers as new data comes in.

func (*PointerRingBuf) TwoContig

func (b *PointerRingBuf) TwoContig() (first []interface{}, second []interface{})

TwoContig returns all readable pointers, but in two separate slices, to avoid copying. The two slices are from the same buffer, but are not contiguous. Either or both may be empty slices.

func (*PointerRingBuf) WritePtrs

func (b *PointerRingBuf) WritePtrs(p []interface{}) (n int, err error)

WritePtrs writes len(p) interface{} values from p to the underlying data stream. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. Write must return a non-nil error if it returns n < len(p).

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL