ringbuf

package
v0.0.0-...-052ef2a Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const CacheLinePad = unsafe.Sizeof(cpu.CacheLinePad{})

Variables

View Source
var (
	ErrFull      = errors.New("full")
	ErrEmptyData = errors.New("empty data")
	ErrTooBig    = errors.New("too big")
)
View Source
var (
	ErrTooManyDataToWrite = errors.New("too many data to write")
	ErrIsFull             = errors.New("ringbuffer is full")
	ErrIsEmpty            = errors.New("ringbuffer is empty")
	ErrAccuqireLock       = errors.New("no lock to accquire")
)

Functions

This section is empty.

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer implements a circular buffer.

func New

func New(capacity int64) *Buffer

New returns the RingBuffer object

func (*Buffer) Read

func (b *Buffer) Read(buf []byte) (int, error)

func (*Buffer) Write

func (b *Buffer) Write(buf []byte) (int, error)

type RingBuffer

type RingBuffer struct {
	// contains filtered or unexported fields
}

RingBuffer is a circular buffer that implement io.ReaderWriter interface.

func NewRingBuffer

func NewRingBuffer(size int) *RingBuffer

NewRingBuffer returns a new RingBuffer whose buffer has the given size.

func (*RingBuffer) Bytes

func (r *RingBuffer) Bytes() []byte

Bytes returns all available read bytes. It does not move the read pointer and only copy the available data.

func (*RingBuffer) Capacity

func (r *RingBuffer) Capacity() int

Capacity returns the size of the underlying buffer.

func (*RingBuffer) Free

func (r *RingBuffer) Free() int

Free returns the length of available bytes to write.

func (*RingBuffer) IsEmpty

func (r *RingBuffer) IsEmpty() bool

IsEmpty returns this ringbuffer is empty.

func (*RingBuffer) IsFull

func (r *RingBuffer) IsFull() bool

IsFull returns this ringbuffer is full.

func (*RingBuffer) Length

func (r *RingBuffer) Length() int

Length return the length of available read bytes.

func (*RingBuffer) Read

func (r *RingBuffer) Read(p []byte) (n int, err error)

Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Even if Read returns n < len(p), it may use all of p as scratch space during the call. If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more. When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. Callers should always process the n > 0 bytes returned before considering the error err. Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF behaviors.

func (*RingBuffer) ReadByte

func (r *RingBuffer) ReadByte() (b byte, err error)

ReadByte reads and returns the next byte from the input or ErrIsEmpty.

func (*RingBuffer) Reset

func (r *RingBuffer) Reset()

Reset the read pointer and writer pointer to zero.

func (*RingBuffer) TryRead

func (r *RingBuffer) TryRead(p []byte) (n int, err error)

TryRead read up to len(p) bytes into p like Read but it is not blocking. If it has not succeeded to accquire the lock, it return 0 as n and ErrAccuqireLock.

func (*RingBuffer) TryWrite

func (r *RingBuffer) TryWrite(p []byte) (n int, err error)

TryWrite writes len(p) bytes from p to the underlying buf like Write, but it is not blocking. If it has not succeeded to accquire the lock, it return 0 as n and ErrAccuqireLock.

func (*RingBuffer) TryWriteByte

func (r *RingBuffer) TryWriteByte(c byte) error

TryWriteByte writes one byte into buffer without blocking. If it has not succeeded to accquire the lock, it return ErrAccuqireLock.

func (*RingBuffer) Write

func (r *RingBuffer) Write(p []byte) (n int, err error)

Write writes len(p) bytes from p to the underlying buf. 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 returns a non-nil error if it returns n < len(p). Write must not modify the slice data, even temporarily.

func (*RingBuffer) WriteByte

func (r *RingBuffer) WriteByte(c byte) error

WriteByte writes one byte into buffer, and returns ErrIsFull if buffer is full.

func (*RingBuffer) WriteString

func (r *RingBuffer) WriteString(s string) (n int, err error)

WriteString writes the contents of the string s to buffer, which accepts a slice of bytes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL