Documentation
¶
Index ¶
- Variables
- func Append[T any](ac *Allocator, s []T, elems ...T) []T
- func Attach[T any](ac *Allocator, ptr T) T
- func New[T any](ac *Allocator) (r *T)
- func NewEnum[T any](ac *Allocator, e T) *T
- func NewFrom[T any](ac *Allocator, src *T) *T
- func NewMap[K comparable, V any](ac *Allocator, cap int) map[K]V
- func NewSlice[T any](ac *Allocator, len, cap int) (r []T)
- type Allocator
- func (ac *Allocator) Bool(v bool) (r *bool)
- func (ac *Allocator) CheckExternalPointers()
- func (ac *Allocator) DecRef()
- func (ac *Allocator) Float32(v float32) (r *float32)
- func (ac *Allocator) Float64(v float64) (r *float64)
- func (ac *Allocator) IncRef()
- func (ac *Allocator) Int(v int) (r *int)
- func (ac *Allocator) Int32(v int32) (r *int32)
- func (ac *Allocator) Int64(v int64) (r *int64)
- func (ac *Allocator) NewString(v string) string
- func (ac *Allocator) Release()
- func (ac *Allocator) String(v string) (r *string)
- func (ac *Allocator) Uint32(v uint32) (r *uint32)
- func (ac *Allocator) Uint64(v uint64) (r *uint64)
- type AllocatorPool
- type ChunkPool
- type Logger
- type Pool
Constants ¶
This section is empty.
Variables ¶
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 Attach ¶
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 NewFrom ¶
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
Types ¶
type Allocator ¶
type Allocator struct {
// contains filtered or unexported fields
}
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) 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.
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 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]) DebugCheck ¶
func (p *Pool[T]) DebugCheck()