-
Notifications
You must be signed in to change notification settings - Fork 2
/
memory.go
68 lines (58 loc) · 1.01 KB
/
memory.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
package dilithium
import (
"sync"
"sync/atomic"
)
type Buffer struct {
Data []byte
Size uint32
Used uint32
refs int32
pool *Pool
}
func NewBuffer(pool *Pool) *Buffer {
return &Buffer{
Data: make([]byte, pool.bufSize),
Size: pool.bufSize,
Used: 0,
refs: 0,
pool: pool,
}
}
func (buf *Buffer) Ref() {
atomic.AddInt32(&buf.refs, 1)
}
func (buf *Buffer) Unref() {
if atomic.AddInt32(&buf.refs, -1) < 1 {
buf.Used = 0
//buf.pool.Put(buf)
}
}
type Pool struct {
id string
bufSize uint32
store *sync.Pool
ii InstrumentInstance
}
func NewPool(id string, bufSize uint32, ii InstrumentInstance) *Pool {
pool := &Pool{
id: id,
bufSize: bufSize,
store: new(sync.Pool),
ii: ii,
}
pool.store.New = pool.allocate
return pool
}
func (pool *Pool) Get() *Buffer {
buf := pool.store.Get().(*Buffer)
buf.Ref()
return buf
}
func (pool *Pool) Put(buf *Buffer) {
pool.store.Put(buf)
}
func (pool *Pool) allocate() interface{} {
pool.ii.Allocate(pool.id)
return NewBuffer(pool)
}