-
Notifications
You must be signed in to change notification settings - Fork 3
/
BinaryFreeList.go
118 lines (106 loc) · 2.58 KB
/
BinaryFreeList.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
package utils
import (
"crypto/rand"
"encoding/binary"
"io"
)
const (
IoMaxSize = 1024
MaxSize = 0x02000000
)
type BinaryFreeList chan []byte
var BinarySerializer BinaryFreeList = make(chan []byte, IoMaxSize)
func (b BinaryFreeList) BorrowFront8() (buf []byte) {
select {
case buf = <-b:
default:
buf = make([]byte, 8)
}
return buf[:8]
}
// Return puts the provided byte slice back on the free list. The buffer MUST
// have been obtained via the Borrow function and therefore have a cap of 8.
func (b BinaryFreeList) Return(buf []byte) {
select {
case b <- buf:
default:
}
}
func (b BinaryFreeList) Uint8(r io.Reader) (uint8, error) {
buf := b.BorrowFront8()[:1]
if _, err := io.ReadFull(r, buf); err != nil {
b.Return(buf)
return 0, err
}
rv := buf[0]
b.Return(buf)
return rv, nil
}
func (b BinaryFreeList) Uint16(r io.Reader, byteOrder binary.ByteOrder) (uint16, error) {
buf := b.BorrowFront8()[:2]
if _, err := io.ReadFull(r, buf); err != nil {
b.Return(buf)
return 0, err
}
rv := byteOrder.Uint16(buf)
b.Return(buf)
return rv, nil
}
func (b BinaryFreeList) Uint32(r io.Reader, byteOrder binary.ByteOrder) (uint32, error) {
buf := b.BorrowFront8()
if _, err := io.ReadFull(r, buf); err != nil {
b.Return(buf)
return 0, err
}
rv := byteOrder.Uint32(buf)
b.Return(buf)
return rv, nil
}
func (b BinaryFreeList) Uint64(r io.Reader, byteOrder binary.ByteOrder) (uint64, error) {
buf := b.BorrowFront8()[:8]
if _, err := io.ReadFull(r, buf); err != nil {
b.Return(buf)
return 0, err
}
rv := byteOrder.Uint64(buf)
b.Return(buf)
return rv, nil
}
func (b BinaryFreeList) PutUint8(w io.Writer, val uint8) error {
buf := b.BorrowFront8()[:1]
buf[0] = val
_, err := w.Write(buf)
b.Return(buf)
return err
}
func (b BinaryFreeList) PutUint16(w io.Writer, byteOrder binary.ByteOrder, val uint16) error {
buf := b.BorrowFront8()[:2]
byteOrder.PutUint16(buf, val)
_, err := w.Write(buf)
b.Return(buf)
return err
}
func (b BinaryFreeList) PutUint32(w io.Writer, byteOrder binary.ByteOrder, val uint32) error {
buf := b.BorrowFront8()[:4]
byteOrder.PutUint32(buf, val)
_, err := w.Write(buf)
b.Return(buf)
return err
}
func (b BinaryFreeList) PutUint64(w io.Writer, byteOrder binary.ByteOrder, val uint64) error {
buf := b.BorrowFront8()[:8]
byteOrder.PutUint64(buf, val)
_, err := w.Write(buf)
b.Return(buf)
return err
}
func GetUint64FromReder(r io.Reader) (uint64, error) {
rv, err := BinarySerializer.Uint64(r, binary.BigEndian)
if err != nil {
return 0, err
}
return rv, nil
}
func RandomUint64() (uint64, error) {
return GetUint64FromReder(rand.Reader)
}