/
pools.go
128 lines (101 loc) · 2.83 KB
/
pools.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package core
import (
"errors"
"sync"
"unsafe"
"github.com/bits-and-blooms/bitset"
"github.com/inoxlang/inox/internal/utils"
)
var (
ErrFullPool = errors.New("pool is full")
ErrInvalidPoolConfig = errors.New("provided pool configuration is invalid")
ErrNotOwnedPoolItem = errors.New("passed pool item is not owned by current pool")
ErrDoublePoolItemRelease = errors.New("pool item is already released")
)
// ArrayPool is a pool providing slices of fixed length, the returned slices ("arrays") should not be modified (expect setting elements).
type ArrayPool[T any] struct {
data []T
bitset *bitset.BitSet
lock sync.Mutex
arrayLen int
arrayCount int
elemSize int
releaseElem func(*T)
}
func NewArrayPool[T any](
byteSize int,
arrayLen int,
//called on all elements of arrays released with ReleaseArray, note that the passed value can be zero
releaseElem func(*T),
) (*ArrayPool[T], error) {
if arrayLen <= 0 || byteSize <= 0 {
return nil, ErrInvalidPoolConfig
}
elemSize := int(utils.GetByteSize[T]())
data := make([]T, byteSize/elemSize)
arrayCount := len(data) / arrayLen
if arrayCount == 0 {
return nil, ErrInvalidPoolConfig
}
return &ArrayPool[T]{
data: data,
bitset: bitset.New(uint(arrayCount)),
arrayLen: arrayLen,
arrayCount: arrayCount,
elemSize: elemSize,
releaseElem: releaseElem,
}, nil
}
func (p *ArrayPool[T]) TotalArrayCount() int {
return p.arrayCount
}
func (p *ArrayPool[T]) AvailableArrayCount() int {
p.lock.Lock()
defer p.lock.Unlock()
return int(p.bitset.Len() - p.bitset.Count())
}
func (p *ArrayPool[T]) InUseArrayCount() int {
p.lock.Lock()
defer p.lock.Unlock()
return int(p.bitset.Count())
}
func (p *ArrayPool[T]) IsFull() bool {
return p.AvailableArrayCount() == 0
}
func (p *ArrayPool[T]) IsEmpty() bool {
return p.AvailableArrayCount() == p.arrayCount
}
// GetArray returns a slice that should not be modified (expect setting elements).
func (p *ArrayPool[T]) GetArray() ([]T, error) {
p.lock.Lock()
defer p.lock.Unlock()
arrayLen := p.arrayLen
i, avail := p.bitset.NextClear(0)
if !avail {
return nil, ErrFullPool
}
p.bitset.Set(i)
array := p.data[int(i)*arrayLen : int(i+1)*arrayLen]
return array[0:len(array):len(array)], nil
}
func (p *ArrayPool[T]) ReleaseArray(s []T) error {
p.lock.Lock()
defer p.lock.Unlock()
for i := range s {
p.releaseElem(&s[i])
}
arrayDataPtr := uintptr(unsafe.Pointer(unsafe.SliceData(s)))
dataPtr := uintptr(unsafe.Pointer(unsafe.SliceData(p.data)))
if arrayDataPtr < dataPtr {
return ErrNotOwnedPoolItem
}
arrayIndex := uint((arrayDataPtr - dataPtr) / uintptr(p.elemSize) / uintptr(p.arrayLen))
if arrayIndex >= uint(p.arrayCount) {
return ErrNotOwnedPoolItem
}
if !p.bitset.Test(arrayIndex) {
return ErrDoublePoolItemRelease
}
p.bitset.Clear(arrayIndex)
return nil
}