aboutsummaryrefslogtreecommitdiffstats
path: root/device.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-09-16 23:10:19 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-09-16 23:10:19 +0200
commit47d1140361eaeca7c0a4a940397f0a71b42c59ce (patch)
tree718aef809a74c9a7339d85ef3806a1276814bf29 /device.go
parentChange queueing drop order and fix memory leaks (diff)
downloadwireguard-go-47d1140361eaeca7c0a4a940397f0a71b42c59ce.tar.xz
wireguard-go-47d1140361eaeca7c0a4a940397f0a71b42c59ce.zip
device: preallocated buffers scheme
Not useful now but quite possibly later.
Diffstat (limited to 'device.go')
-rw-r--r--device.go33
1 files changed, 26 insertions, 7 deletions
diff --git a/device.go b/device.go
index 2245a61..b1f3569 100644
--- a/device.go
+++ b/device.go
@@ -19,6 +19,9 @@ const (
DeviceRoutineNumberAdditional = 2
)
+
+const preallocatedBuffers = 0
+
type Device struct {
isUp AtomicBool // device is (going) up
isClosed AtomicBool // device is closed? (acting as guard)
@@ -66,7 +69,8 @@ type Device struct {
}
pool struct {
- messageBuffers sync.Pool
+ messageBuffers *sync.Pool
+ reuseChan chan interface{}
}
queue struct {
@@ -243,11 +247,19 @@ func (device *Device) SetPrivateKey(sk NoisePrivateKey) error {
}
func (device *Device) GetMessageBuffer() *[MaxMessageSize]byte {
- return device.pool.messageBuffers.Get().(*[MaxMessageSize]byte)
+ if preallocatedBuffers == 0 {
+ return device.pool.messageBuffers.Get().(*[MaxMessageSize]byte)
+ } else {
+ return (<-device.pool.reuseChan).(*[MaxMessageSize]byte)
+ }
}
func (device *Device) PutMessageBuffer(msg *[MaxMessageSize]byte) {
- device.pool.messageBuffers.Put(msg)
+ if preallocatedBuffers == 0 {
+ device.pool.messageBuffers.Put(msg)
+ } else {
+ device.pool.reuseChan <- msg
+ }
}
func NewDevice(tunDevice tun.TUNDevice, logger *Logger) *Device {
@@ -274,10 +286,17 @@ func NewDevice(tunDevice tun.TUNDevice, logger *Logger) *Device {
device.indexTable.Init()
device.allowedips.Reset()
- device.pool.messageBuffers = sync.Pool{
- New: func() interface{} {
- return new([MaxMessageSize]byte)
- },
+ if preallocatedBuffers == 0 {
+ device.pool.messageBuffers = &sync.Pool{
+ New: func() interface{} {
+ return new([MaxMessageSize]byte)
+ },
+ }
+ } else {
+ device.pool.reuseChan = make(chan interface{}, preallocatedBuffers)
+ for i := 0; i < preallocatedBuffers; i += 1 {
+ device.pool.reuseChan <- new([MaxMessageSize]byte)
+ }
}
// create queues