-
Notifications
You must be signed in to change notification settings - Fork 0
/
mempool.go
158 lines (133 loc) · 2.96 KB
/
mempool.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
package mempool
import (
"sync"
)
type Allocator interface {
Malloc(size int) []byte
Realloc(buf []byte, size int) []byte
Append(buf []byte, more ...byte) []byte
AppendString(buf []byte, more string) []byte
Free(buf []byte)
}
// DefaultMemPool .
var DefaultMemPool = New(1024, 1024*1024*1024)
// MemPool .
type MemPool struct {
// Debug bool
// mux sync.Mutex
bufSize int
freeSize int
pool *sync.Pool
// allocCnt uint64
// freeCnt uint64
// allocStacks map[uintptr]string
}
// New .
func New(bufSize, freeSize int) Allocator {
if bufSize <= 0 {
bufSize = 64
}
if freeSize <= 0 {
freeSize = 64 * 1024
}
if freeSize < bufSize {
freeSize = bufSize
}
mp := &MemPool{
bufSize: bufSize,
freeSize: freeSize,
pool: &sync.Pool{},
// Debug: true,
}
mp.pool.New = func() interface{} {
buf := make([]byte, bufSize)
return &buf
}
return mp
}
// Malloc .
func (mp *MemPool) Malloc(size int) []byte {
if size > mp.freeSize {
return make([]byte, size)
}
pbuf := mp.pool.Get().(*[]byte)
n := cap(*pbuf)
if n < size {
*pbuf = append((*pbuf)[:n], make([]byte, size-n)...)
}
return (*pbuf)[:size]
}
// Realloc .
func (mp *MemPool) Realloc(buf []byte, size int) []byte {
if size <= cap(buf) {
return buf[:size]
}
if cap(buf) < mp.freeSize {
pbuf := mp.pool.Get().(*[]byte)
n := cap(buf)
if n < size {
*pbuf = append((*pbuf)[:n], make([]byte, size-n)...)
}
*pbuf = (*pbuf)[:size]
copy(*pbuf, buf)
mp.Free(buf)
return *pbuf
}
return append(buf[:cap(buf)], make([]byte, size-cap(buf))...)[:size]
}
// Append .
func (mp *MemPool) Append(buf []byte, more ...byte) []byte {
return append(buf, more...)
}
// AppendString .
func (mp *MemPool) AppendString(buf []byte, more string) []byte {
return append(buf, more...)
}
// Free .
func (mp *MemPool) Free(buf []byte) {
if cap(buf) > mp.freeSize {
return
}
mp.pool.Put(&buf)
}
// NativeAllocator definition.
type NativeAllocator struct{}
// Malloc .
func (a *NativeAllocator) Malloc(size int) []byte {
return make([]byte, size)
}
// Realloc .
func (a *NativeAllocator) Realloc(buf []byte, size int) []byte {
if size <= cap(buf) {
return buf[:size]
}
newBuf := make([]byte, size)
copy(newBuf, buf)
return newBuf
}
// Free .
func (a *NativeAllocator) Free(buf []byte) {
}
// Malloc exports default package method.
func Malloc(size int) []byte {
return DefaultMemPool.Malloc(size)
}
// Realloc exports default package method.
func Realloc(buf []byte, size int) []byte {
return DefaultMemPool.Realloc(buf, size)
}
// Append exports default package method.
func Append(buf []byte, more ...byte) []byte {
return DefaultMemPool.Append(buf, more...)
}
// AppendString exports default package method.
func AppendString(buf []byte, more string) []byte {
return DefaultMemPool.AppendString(buf, more)
}
// Free exports default package method.
func Free(buf []byte) {
DefaultMemPool.Free(buf)
}
func Init(bufSize, freeSize int) {
DefaultMemPool = New(bufSize, freeSize)
}