linkedlistbuffer

package module
v0.0.0-...-36c6c4e Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2023 License: MIT Imports: 8 Imported by: 0

README

This repository implements a linked list buffer that divides memory into many default 4KB-sized blocks, which are stored in a sync.Pool. When used, the blocks obtained from sync.Pool are linked together through a linked list.

In addition, the repository provides methods related to NewBytesBuffer(), which will take out a total of 5MB blocks (*BytesBuffer) from a default 5GB(max) memory pool. The default memory size of each BytesBuffer is 5MB(max), which implements io.ReaderWriter.

Example

package main

import (
	"fmt"
	"log"

	"github.com/zjj/linkedlistbuffer"
)

func main() {
	buf := linkedlistbuffer.NewBytesBuffer()
	c, err := buf.Write([]byte("hello!"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("wrote count:", c)

	outBuf := make([]byte, 5)
	c, err = buf.Read(outBuf)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("read count: %d\n", c)
	fmt.Printf("read content: %s\n", string(outBuf))

	c, err = buf.Read(outBuf)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("read count: %d\n", c)
	fmt.Printf("read content: %s\n", string(outBuf))
	buf.Free() // actually, GC will run on the buf to relase all if you forget to call Free
}
License

https://github.com/zjj/linkedlistbuffer/blob/master/LICENSE

Documentation

Index

Constants

View Source
const (
	UnLimitedLinkedListLength int = 0
	UnLimitedBufferPoolSize   int = 0
	DefaultBufferSize         int = 1024 * 4
)

Variables

View Source
var ErrBufferPoolFull = errors.New("buffer pool is full")
View Source
var ErrBufferTooSmall = errors.New("buffer is too small")
View Source
var ErrLinkedListEmpty = errors.New("linked list is empty")
View Source
var ErrLinkedListFull = errors.New("linked list is full")

LinkedList

View Source
var ErrLinkedListMaxLengthTooSmall = errors.New("linked list max length is too small")

Functions

This section is empty.

Types

type Buffer

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

type BufferPool

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

func NewBufferPool

func NewBufferPool(maxPoolSize int) *BufferPool

func (*BufferPool) Get

func (bp *BufferPool) Get(ctx context.Context) (*Buffer, error)

func (*BufferPool) Put

func (bp *BufferPool) Put(buf *Buffer)

func (*BufferPool) TotalSize

func (bp *BufferPool) TotalSize() int

func (*BufferPool) WithBufferSize

func (bp *BufferPool) WithBufferSize(size int) *BufferPool

WithBufferSize set the buffer size of the buffer pool, it can only be called before the buffer pool is used.

type BytesBuffer

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

func NewBytesBuffer

func NewBytesBuffer() *BytesBuffer

func (*BytesBuffer) Free

func (b *BytesBuffer) Free()

Free is not nessary to call gc will free the memory

func (*BytesBuffer) Read

func (b *BytesBuffer) Read(p []byte) (n int, err error)

func (*BytesBuffer) ReadAll

func (b *BytesBuffer) ReadAll() ([]byte, error)

func (*BytesBuffer) Write

func (b *BytesBuffer) Write(p []byte) (n int, err error)

type LinkedList

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

func NewLinkedListIO

func NewLinkedListIO(maxLength int) *LinkedList

func (*LinkedList) Free

func (l *LinkedList) Free()

Free all buffers in the list it's not big deal if you forget to call this function gc will do it for you

func (*LinkedList) Pop

func (l *LinkedList) Pop() (*Buffer, error)

func (*LinkedList) Push

func (l *LinkedList) Push(b *Buffer) bool

func (*LinkedList) Read

func (l *LinkedList) Read(p []byte) (n int, err error)

func (*LinkedList) ReadAll

func (l *LinkedList) ReadAll() ([]byte, error)

func (*LinkedList) Size

func (l *LinkedList) Size() int

func (*LinkedList) WithBufferPool

func (l *LinkedList) WithBufferPool(bp *BufferPool) *LinkedList

func (*LinkedList) Write

func (l *LinkedList) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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