lac

package module
v0.0.0-...-7bb5cce Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DisableAllLac = false

	// our memory is much cheaper than systems,
	// so we can be more aggressive than `append`.
	SliceExtendRatio = 2.5

	BugfixClearPointerInMem = true
	BugfixCorruptOtherMem   = true

	ZeroMemOnFree = false
)

Functions

func Append

func Append[T any](ac *Allocator, s []T, elems ...T) []T

func Attach

func Attach[T any](ac *Allocator, ptr T) T

Attach mark ptr as external pointer and will keep ptr alive during GC, otherwise the ptr from heap may be GCed and cause a dangled pointer, no panic will report by the runtime. So make sure to mark objects from native heap as external pointers by using this function. External pointers will be checked in debug mode. Can attach Lac objects as well without any side effects.

NOTE: you must attach the external ptr **before** assigning it to the lac-allocated object, this is to ensure the external ptr is always reachable for the GC. usage: ```go

obj := lac.New[ObjType](ac)
obj.Field = lac.Attach(ac, externalPtr)

```

func New

func New[T any](ac *Allocator) (r *T)

func NewEnum

func NewEnum[T any](ac *Allocator, e T) *T

func NewFrom

func NewFrom[T any](ac *Allocator, src *T) *T

NewFrom copy the src object from heap to lac thus slower than New due to the heap malloc of src. **Prefer using New for better performance**. It is useful for old-code migration using struct literal syntax:

obj := lac.NewFrom(ac, &SomeData{
	Field1: Value1,
	Field2: Value2,
})

This is a bit clearer than the following `new` syntax:

obj := lac.New[SomeData](ac)
obj.Field1 = Value1
obj.Field2 = Value2

func NewMap

func NewMap[K comparable, V any](ac *Allocator, cap int) map[K]V

func NewSlice

func NewSlice[T any](ac *Allocator, len, cap int) (r []T)

NewSlice does not zero the slice automatically, this is OK with most cases and can improve the performance. zero it yourself for your need.

Types

type Allocator

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

func (*Allocator) Bool

func (ac *Allocator) Bool(v bool) (r *bool)

func (*Allocator) CheckExternalPointers

func (ac *Allocator) CheckExternalPointers()

CheckExternalPointers is useful for if you want to check external pointers but don't want to invalidate pointers. e.g. using lac as memory allocator for config data globally.

func (*Allocator) DecRef

func (ac *Allocator) DecRef()

DecRef will put the ac back into Pool if ref count reduced to zero. If one DecRef call is missed causes the Lac not go back to Pool, it will be recycled by GC later. If more DecRef calls are called cause the ref cnt reduced to negative, panic in debug mode.

func (*Allocator) Float32

func (ac *Allocator) Float32(v float32) (r *float32)

func (*Allocator) Float64

func (ac *Allocator) Float64(v float64) (r *float64)

func (*Allocator) IncRef

func (ac *Allocator) IncRef()

IncRef should be called before and outside the new goroutine, never be in the new goroutine, otherwise the execution of new goroutine may be delayed after the caller quit, which may cause a UseAfterFree error. e.g.

ac.IncRef() // <<<- Correct usage. should be called before and outside the new goroutine.
go func() {

	// ac.IncRef() <<<- !!!!!!! Incorrect usage !!!!!!!!!!!!

	defer ac.DecRef()
	....
}()

if IncRef is not call correctly the Lac will be recycled ahead of time, in debug mode your Lac allocated objects become corrupted and panic occurs when using them. UseAfterFree can also be caught by the validity check in release mode.

This appointment also ensure the single-threaded version will never run in parallel with the multi-threaded version.

func (*Allocator) Int

func (ac *Allocator) Int(v int) (r *int)

func (*Allocator) Int32

func (ac *Allocator) Int32(v int32) (r *int32)

func (*Allocator) Int64

func (ac *Allocator) Int64(v int64) (r *int64)

func (*Allocator) NewString

func (ac *Allocator) NewString(v string) string

func (*Allocator) Release

func (ac *Allocator) Release()

func (*Allocator) String

func (ac *Allocator) String(v string) (r *string)

func (*Allocator) Uint32

func (ac *Allocator) Uint32(v uint32) (r *uint32)

func (*Allocator) Uint64

func (ac *Allocator) Uint64(v uint64) (r *uint64)

type AllocatorPool

type AllocatorPool struct {
	Logger
	Pool[*Allocator]

	MaxLac int

	Name string

	Stats struct {
		TotalCreatedAc atomic.Int64
		ChunksUsed     atomic.Int64
		ChunksMiss     atomic.Int64
		AllocBytes     atomic.Int64
	}
	// contains filtered or unexported fields
}

func NewAllocatorPool

func NewAllocatorPool(name string, logger Logger, lacCap, chunkSz, defaultChunks, chunksCap int) *AllocatorPool

func (*AllocatorPool) DebugCheck

func (p *AllocatorPool) DebugCheck()

DebugCheck check if all items from pool are all returned to pool. useful for leak-checking.

func (*AllocatorPool) DumpStats

func (p *AllocatorPool) DumpStats(reset bool) string

func (*AllocatorPool) EnableDebugMode

func (p *AllocatorPool) EnableDebugMode(v bool)

func (*AllocatorPool) Get

func (p *AllocatorPool) Get() *Allocator

type ChunkPool

type ChunkPool struct {
	Pool[*sliceHeader]

	ChunkSize int
	Stats     struct {
		TotalCreated atomic.Int64
	}
}

type Logger

type Logger interface {
	Errorf(format string, args ...interface{})
}

type Pool

type Pool[T any] struct {
	Logger

	New func() T

	Cap int

	// the max count of call to New function.
	MaxNew int
	Name   string

	CheckDuplication bool
	// require CheckDuplication=true
	// check duplicated put.
	Equal func(a, b T) bool
	// contains filtered or unexported fields
}

func (*Pool[T]) Clear

func (p *Pool[T]) Clear()

func (*Pool[T]) DebugCheck

func (p *Pool[T]) DebugCheck()

func (*Pool[T]) Get

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

func (*Pool[T]) Put

func (p *Pool[T]) Put(v T) bool

func (*Pool[T]) Reserve

func (p *Pool[T]) Reserve(cnt int)

Jump to

Keyboard shortcuts

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