-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.go
183 lines (152 loc) · 3.68 KB
/
dir.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package bakemono
import (
"bytes"
"encoding/binary"
"fmt"
)
// Dir is index unit in cache vol. Inspired by Traffic Server.
// use raw array to reduce memory allocation, especially for bare metal > 100TB.
// raw data format
type Dir struct {
/*
unsigned int offset : 24; // (0,1:0-7)
unsigned int bigInternal : 2; // (1:8-9)
unsigned int sizeInternal : 6; // (1:10-15)
unsigned int tag : 12; // (2:0-11)
unsigned int phase : 1; // (2:12)
unsigned int head : 1; // (2:13)
unsigned int pinned : 1; // (2:14)
unsigned int token : 1; // (2:15)
unsigned int next : 16; // (3)
unsigned int offset_high : 16;
if unused, raw[2] is `prev`, represents previous dir in freelist.
approx_size = sectorSize(512) * (2**3big) * sizeInternal
*/
raw [5]uint16
// TODO: data range guard for every field
}
func (d *Dir) clear() {
d.raw[0] = 0
d.raw[1] = 0
d.raw[2] = 0
d.raw[3] = 0
d.raw[4] = 0
}
func (d *Dir) MarshalBinary() ([]byte, error) {
buf := &bytes.Buffer{}
err := binary.Write(buf, binary.BigEndian, d.raw)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (d *Dir) UnmarshalBinary(data []byte) error {
buf := bytes.NewBuffer(data)
err := binary.Read(buf, binary.BigEndian, &d.raw)
if err != nil {
return err
}
return nil
}
func (d *Dir) offset() uint64 {
return uint64(d.raw[0]) | uint64(d.raw[1]&0xff)<<16 | uint64(d.raw[4])<<24
}
func (d *Dir) setOffset(offset uint64) {
d.raw[0] = uint16(offset)
d.raw[1] = uint16(((offset >> 16) & 0xff) | (uint64(d.raw[1]) & 0xff00))
d.raw[4] = uint16(offset >> 24)
}
func (d *Dir) setApproxSize(size uint64) {
// Note: max sizeInternal 16MB
if size > DirMaxDataSize {
panic(fmt.Sprintf("dir setApproxSize %d is too bigInternal", size))
}
if size <= DirDataSizeLv0 {
d.setBigInternal(0)
d.setSizeInternal(uint8((size - 1) / DirDataSizeLv0))
} else if size <= DirDataSizeLv1 {
d.setBigInternal(1)
d.setSizeInternal(uint8((size - 1) / DirDataSizeLv1))
} else if size <= DirDataSizeLv2 {
d.setBigInternal(2)
d.setSizeInternal(uint8((size - 1) / DirDataSizeLv2))
} else {
d.setBigInternal(3)
d.setSizeInternal(uint8((size - 1) / DirDataSizeLv3))
}
}
func (d *Dir) approxSize() uint64 {
big := d.bigInternal()
size := d.sizeInternal()
return (SectorSize << (big * 3)) * uint64(size+1)
}
func (d *Dir) prev() uint16 {
return d.raw[2]
}
func (d *Dir) setPrev(prev uint16) {
d.raw[2] = prev
}
func (d *Dir) bigInternal() uint8 {
return uint8(d.raw[1] >> 8 & 0x3)
}
func (d *Dir) setBigInternal(big uint8) {
d.raw[1] = (d.raw[1] & 0xfcff) | uint16(big)<<8
}
func (d *Dir) sizeInternal() uint8 {
return uint8(d.raw[1] >> 10 & 0x3f)
}
func (d *Dir) setSizeInternal(size uint8) {
d.raw[1] = (d.raw[1] & 0x83ff) | uint16(size)<<10
}
func (d *Dir) tag() uint16 {
return d.raw[2] & 0xfff
}
func (d *Dir) setTag(tag uint16) {
d.raw[2] = (d.raw[2] & 0xf000) | (tag & 0xfff)
}
func (d *Dir) phase() bool {
return d.raw[2]>>12&0x1 == 1
}
func (d *Dir) setPhase(phase bool) {
if phase {
d.raw[2] |= 1 << 12
} else {
d.raw[2] &= 1 << 12
}
}
func (d *Dir) head() bool {
return d.raw[2]>>13&0x1 == 1
}
func (d *Dir) setHead(head bool) {
if head {
d.raw[2] |= 1 << 13
} else {
d.raw[2] &= 1 << 13
}
}
func (d *Dir) pinned() bool {
return d.raw[2]>>14&0x1 == 1
}
func (d *Dir) setPinned(pinned bool) {
if pinned {
d.raw[2] |= 1 << 14
} else {
d.raw[2] &= 1 << 14
}
}
func (d *Dir) token() bool {
return d.raw[2]>>15&0x1 == 1
}
func (d *Dir) setToken(token bool) {
if token {
d.raw[2] |= 1 << 15
} else {
d.raw[2] &= ^uint16(1 << 15)
}
}
func (d *Dir) next() uint16 {
return d.raw[3]
}
func (d *Dir) setNext(next uint16) {
d.raw[3] = next
}