pool

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

View Source
const (
	DefaultMaxBits = 18 // 支持到256K
	DefaultMaxSize = 1 << DefaultMaxBits
)

Variables

This section is empty.

Functions

func WithAutoClean

func WithAutoClean(interval time.Duration) func(*Allocator)

可选: 设置自动清理时间

func WithZeroOnPut

func WithZeroOnPut(zero bool) func(*Allocator)

可选:设置回收时是否清零

Types

type Allocator

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

func NewAllocator

func NewAllocator(opts ...func(*Allocator)) *Allocator

func (*Allocator) CurrentBytes

func (alloc *Allocator) CurrentBytes() int64

统计当前内存占用(单位:字节)

func (*Allocator) Get

func (alloc *Allocator) Get(size int) *[]byte

Get 返回一个合适大小的 []byte 指针

func (*Allocator) GetBytes

func (alloc *Allocator) GetBytes(size int) []byte

GetBytes 直接返回 []byte,简化调用

func (*Allocator) Put

func (alloc *Allocator) Put(p *[]byte) error

Put 回收 []byte 指针到池

func (*Allocator) StartAutoClean

func (alloc *Allocator) StartAutoClean(interval time.Duration)

func (*Allocator) StopAutoClean

func (alloc *Allocator) StopAutoClean()

type PointerWithReset

type PointerWithReset[T any] interface {
	*T

	Reset()
}

type Pool

type Pool[T any, P PointerWithReset[T]] struct {
	New func() P
	// contains filtered or unexported fields
}

func NewPool

func NewPool[T any, P PointerWithReset[T]](new func() P) *Pool[T, P]

func (*Pool[T, P]) Get

func (p *Pool[T, P]) Get() P

func (*Pool[T, P]) Put

func (p *Pool[T, P]) Put(value P)

Jump to

Keyboard shortcuts

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