-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.go
55 lines (47 loc) · 1.01 KB
/
page.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
package pool
import "sync/atomic"
type page struct {
containerSize uint64
containers []*container
}
func newPage(containerSize uint64) *page {
return &page{containerSize: containerSize}
}
func (p *page) addContainer() []byte {
container := newContainer(p.containerSize)
p.containers = append(p.containers, container)
return container.allocate(p.containerSize)
}
func (p *page) get(size uint64) []byte {
for _, container := range p.containers {
if container.inUse() {
continue
}
buffer := container.bytes[:size]
newPointer := pointerOf(buffer)
if atomic.CompareAndSwapUintptr(&container.pointer, 0, newPointer) {
return buffer
}
}
return nil
}
func (p *page) freeContainer(buffer []byte) bool {
for _, container := range p.containers {
if container.free(buffer) {
return true
}
}
return false
}
func (p *page) usageEntry() Entry {
used := 0
for _, container := range p.containers {
if container.inUse() {
used++
}
}
return Entry{
Used: used,
Count: len(p.containers),
}
}