package mem_ring import ( "sync" "sync/atomic" ) // Note: T should be Copy type Item[T any] struct { next uint data T } type Slab[T any] struct { data []Item[T] next uint } type LockedSlab[T any] struct { slab Slab[T] lock sync.Mutex } type MultiSlab[T any] struct { slabs []LockedSlab[T] index uint32 exp uint mask uint } func NewSlab[T any]() *Slab[T] { return &Slab[T]{ data: make([]Item[T], 0, 1), next: 0, } } func NewLockedSlab[T any]() *LockedSlab[T] { return &LockedSlab[T]{ slab: *NewSlab[T](), lock: sync.Mutex{}, } } func NewMultiSlab[T any](opts ...MultiSlabOption) *MultiSlab[T] { opt := MultiSlabOpt{ SlabSizeExp: 4, } for _, op := range opts { op.apply(&opt) } size := 1 << opt.SlabSizeExp slabs := make([]LockedSlab[T], size) for i := 0; i < size; i++ { slabs[i] = *NewLockedSlab[T]() } return &MultiSlab[T]{ slabs: slabs, exp: opt.SlabSizeExp, mask: 1<> s.exp return s.slabs[slab_idx].Pop(inner_idx) }