-
Notifications
You must be signed in to change notification settings - Fork 1
/
buffers.go
257 lines (210 loc) · 7.6 KB
/
buffers.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// ©Hayabusa Cloud Co., Ltd. 2022. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package sox
import (
"net"
"os"
"unsafe"
)
// Buffers is alias of net.Buffers
type Buffers = net.Buffers
var PageSize = uintptr(os.Getpagesize())
// AlignedMem returns a byte slice with the specified size
// and starting address aligned to the memory page size.
func AlignedMem(size int) []byte {
p := make([]byte, uintptr(size)+PageSize-1)
ptr := uintptr(unsafe.Pointer(unsafe.SliceData(p)))
ptr = ((ptr + PageSize - 1) / PageSize) * PageSize
return unsafe.Slice((*byte)(unsafe.Pointer(ptr)), size)
}
// AlignedMemBlocks returns n bytes slices which
// has length with memory page size and address
// starts from multiple of memory page size
func AlignedMemBlocks(n int) (blocks [][]byte) {
if n < 1 {
panic("bad block num")
}
blocks = make([][]byte, n)
p := make([]byte, int(PageSize)*(n+1))
ptr := uintptr(unsafe.Pointer(&p[0]))
off := ptr - (ptr & ^(PageSize - 1))
for i := range n {
blocks[i] = unsafe.Slice(&p[uintptr(i)*PageSize-off], PageSize)
}
return
}
// AlignedMemBlock returns one aligned block
func AlignedMemBlock() []byte {
return AlignedMemBlocks(1)[0]
}
// NewBuffers creates and initializes a new Buffers with given n and size
func NewBuffers(n int, size int) Buffers {
if n < 1 {
return Buffers{}
}
ret := make(Buffers, n)
for i := range n {
if size > 0 {
ret[i] = make([]byte, size)
} else {
ret[i] = []byte{}
}
}
return ret
}
const (
_ = 1 << (iota * 2)
_
// BufferSizePico represents the size of a pico buffer
BufferSizePico
// BufferSizeNano represents the size of a nano buffer.
BufferSizeNano
// BufferSizeMicro represents the size of a micro buffer.
BufferSizeMicro
// BufferSizeSmall represents the size of a small buffer.
BufferSizeSmall
// BufferSizeMedium represents the size of a medium buffer.
BufferSizeMedium
// BufferSizeLarge represents the size of a large buffer.
BufferSizeLarge
// BufferSizeHuge represents the size of a huge buffer.
BufferSizeHuge
// BufferSizeGiant represents the size of a giant buffer.
BufferSizeGiant
bufferSizeDefault = BufferSizeMedium
)
// NewPicoBuffer returns a new PicoBuffer.
func NewPicoBuffer() PicoBuffer { return PicoBuffer{} }
// NewNanoBuffer returns a new instance of NanoBuffer.
func NewNanoBuffer() NanoBuffer { return NanoBuffer{} }
// NewMicroBuffer returns a new instance of MicroBuffer.
func NewMicroBuffer() MicroBuffer { return MicroBuffer{} }
// NewSmallBuffer returns a new instance of SmallBuffer.
func NewSmallBuffer() SmallBuffer { return SmallBuffer{} }
// NewMediumBuffer returns a new instance of MediumBuffer.
func NewMediumBuffer() MediumBuffer { return MediumBuffer{} }
// NewLargeBuffer returns a new instance of LargeBuffer.
func NewLargeBuffer() LargeBuffer { return LargeBuffer{} }
// NewHugeBuffer returns a new instance of HugeBuffer.
func NewHugeBuffer() HugeBuffer { return HugeBuffer{} }
// NewGiantBuffer returns a new instance of GiantBuffer.
func NewGiantBuffer() GiantBuffer { return GiantBuffer{} }
// BufferType is an interface that represents different types of buffers, including
// PicoBuffer, NanoBuffer, MicroBuffer, SmallBuffer, MediumBuffer, LargeBuffer,
// HugeBuffer, and GiantBuffer. Each buffer type is a byte array with a specific size.
type BufferType interface {
PicoBuffer | NanoBuffer | MicroBuffer | SmallBuffer | MediumBuffer | LargeBuffer | HugeBuffer | GiantBuffer
}
// PicoBuffer represents a byte array with size of BufferSizePico
type PicoBuffer [BufferSizePico]byte
func (b PicoBuffer) Reset() {}
// NanoBuffer represents a byte array with size of BufferSizeNano
type NanoBuffer [BufferSizeNano]byte
func (b NanoBuffer) Reset() {}
// MicroBuffer represents a byte array with size of BufferSizeMicro
type MicroBuffer [BufferSizeMicro]byte
func (b MicroBuffer) Reset() {}
// SmallBuffer represents a byte array with size of BufferSizeSmall
type SmallBuffer [BufferSizeSmall]byte
func (b SmallBuffer) Reset() {}
// MediumBuffer represents a byte array with size of BufferSizeMedium
type MediumBuffer [BufferSizeMedium]byte
func (b MediumBuffer) Reset() {}
// LargeBuffer represents a byte array with size of BufferSizeLarge
type LargeBuffer [BufferSizeLarge]byte
func (b LargeBuffer) Reset() {}
// HugeBuffer represents a byte array with size of BufferSizeHuge
type HugeBuffer [BufferSizeHuge]byte
func (b HugeBuffer) Reset() {}
// GiantBuffer represents a byte array with size of BufferSizeGiant
type GiantBuffer [BufferSizeGiant]byte
func (b GiantBuffer) Reset() {}
func picoArrayFromSlice(s []byte, offset int64) PicoBuffer {
ptr := unsafe.Add(unsafe.Pointer(unsafe.SliceData(s)), offset)
return *(*[BufferSizePico]byte)(ptr)
}
func nanoArrayFromSlice(s []byte, offset int64) NanoBuffer {
ptr := unsafe.Add(unsafe.Pointer(unsafe.SliceData(s)), offset)
return *(*[BufferSizeNano]byte)(ptr)
}
func microArrayFromSlice(s []byte, offset int64) MicroBuffer {
ptr := unsafe.Add(unsafe.Pointer(unsafe.SliceData(s)), offset)
return *(*[BufferSizeMicro]byte)(ptr)
}
func smallArrayFromSlice(s []byte, offset int64) SmallBuffer {
ptr := unsafe.Add(unsafe.Pointer(unsafe.SliceData(s)), offset)
return *(*[BufferSizeSmall]byte)(ptr)
}
func mediumArrayFromSlice(s []byte, offset int64) MediumBuffer {
ptr := unsafe.Add(unsafe.Pointer(unsafe.SliceData(s)), offset)
return *(*[BufferSizeMedium]byte)(ptr)
}
func largeArrayFromSlice(s []byte, offset int64) LargeBuffer {
ptr := unsafe.Add(unsafe.Pointer(unsafe.SliceData(s)), offset)
return *(*[BufferSizeLarge]byte)(ptr)
}
func hugeArrayFromSlice(s []byte, offset int64) HugeBuffer {
ptr := unsafe.Add(unsafe.Pointer(unsafe.SliceData(s)), offset)
return *(*[BufferSizeHuge]byte)(ptr)
}
func giantArrayFromSlice(s []byte, offset int64) GiantBuffer {
ptr := unsafe.Add(unsafe.Pointer(unsafe.SliceData(s)), offset)
return *(*[BufferSizeGiant]byte)(ptr)
}
func sliceOfPicoArray(s []byte, offset int64, n int) []PicoBuffer {
if n < 1 {
panic("bad array num")
}
base := unsafe.Pointer(unsafe.SliceData(s))
return unsafe.Slice((*PicoBuffer)(unsafe.Add(base, offset)), n)
}
func sliceOfNanoArray(s []byte, offset int64, n int) []NanoBuffer {
if n < 1 {
panic("bad array num")
}
base := unsafe.Pointer(unsafe.SliceData(s))
return unsafe.Slice((*NanoBuffer)(unsafe.Add(base, offset)), n)
}
func sliceOfMicroArray(s []byte, offset int64, n int) []MicroBuffer {
if n < 1 {
panic("bad array num")
}
base := unsafe.Pointer(unsafe.SliceData(s))
return unsafe.Slice((*MicroBuffer)(unsafe.Add(base, offset)), n)
}
func sliceOfSmallArray(s []byte, offset int64, n int) []SmallBuffer {
if n < 1 {
panic("bad array num")
}
base := unsafe.Pointer(unsafe.SliceData(s))
return unsafe.Slice((*SmallBuffer)(unsafe.Add(base, offset)), n)
}
func sliceOfMediumArray(s []byte, offset int64, n int) []MediumBuffer {
if n < 1 {
panic("bad array num")
}
base := unsafe.Pointer(unsafe.SliceData(s))
return unsafe.Slice((*MediumBuffer)(unsafe.Add(base, offset)), n)
}
func sliceOfLargeArray(s []byte, offset int64, n int) []LargeBuffer {
if n < 1 {
panic("bad array num")
}
base := unsafe.Pointer(unsafe.SliceData(s))
return unsafe.Slice((*LargeBuffer)(unsafe.Add(base, offset)), n)
}
func sliceOfHugeArray(s []byte, offset int64, n int) []HugeBuffer {
if n < 1 {
panic("bad array num")
}
base := unsafe.Pointer(unsafe.SliceData(s))
return unsafe.Slice((*HugeBuffer)(unsafe.Add(base, offset)), n)
}
func sliceOfGiantArray(s []byte, offset int64, n int) []GiantBuffer {
if n < 1 {
panic("bad array num")
}
base := unsafe.Pointer(unsafe.SliceData(s))
return unsafe.Slice((*GiantBuffer)(unsafe.Add(base, offset)), n)
}