summaryrefslogtreecommitdiffhomepage
path: root/pools.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-09-22 06:29:02 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-09-24 00:37:43 +0200
commit833597b585f460aaa17bad93ad59290ec282e77e (patch)
tree209278c3c686fefffc56067b03a4f66017e009ae /pools.go
parentcf81a28dd30bd8714432d2ff108d64c7f4b65e50 (diff)
More pooling
Diffstat (limited to 'pools.go')
-rw-r--r--pools.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/pools.go b/pools.go
new file mode 100644
index 0000000..fe219f4
--- /dev/null
+++ b/pools.go
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2017-2018 WireGuard LLC. All Rights Reserved.
+ */
+
+package main
+
+import "sync"
+
+var preallocatedBuffers = 0
+
+func (device *Device) PopulatePools() {
+ if preallocatedBuffers == 0 {
+ device.pool.messageBufferPool = &sync.Pool{
+ New: func() interface{} {
+ return new([MaxMessageSize]byte)
+ },
+ }
+ device.pool.inboundElementPool = &sync.Pool{
+ New: func() interface{} {
+ return new(QueueInboundElement)
+ },
+ }
+ device.pool.outboundElementPool = &sync.Pool{
+ New: func() interface{} {
+ return new(QueueOutboundElement)
+ },
+ }
+ } else {
+ device.pool.messageBufferReuseChan = make(chan *[MaxMessageSize]byte, preallocatedBuffers)
+ for i := 0; i < preallocatedBuffers; i += 1 {
+ device.pool.messageBufferReuseChan <- new([MaxMessageSize]byte)
+ }
+ device.pool.inboundElementReuseChan = make(chan *QueueInboundElement, preallocatedBuffers)
+ for i := 0; i < preallocatedBuffers; i += 1 {
+ device.pool.inboundElementReuseChan <- new(QueueInboundElement)
+ }
+ device.pool.outboundElementReuseChan = make(chan *QueueOutboundElement, preallocatedBuffers)
+ for i := 0; i < preallocatedBuffers; i += 1 {
+ device.pool.outboundElementReuseChan <- new(QueueOutboundElement)
+ }
+ }
+}
+
+func (device *Device) GetMessageBuffer() *[MaxMessageSize]byte {
+ if preallocatedBuffers == 0 {
+ return device.pool.messageBufferPool.Get().(*[MaxMessageSize]byte)
+ } else {
+ return <-device.pool.messageBufferReuseChan
+ }
+}
+
+func (device *Device) PutMessageBuffer(msg *[MaxMessageSize]byte) {
+ if preallocatedBuffers == 0 {
+ device.pool.messageBufferPool.Put(msg)
+ } else {
+ device.pool.messageBufferReuseChan <- msg
+ }
+}
+
+func (device *Device) GetInboundElement() *QueueInboundElement {
+ if preallocatedBuffers == 0 {
+ return device.pool.inboundElementPool.Get().(*QueueInboundElement)
+ } else {
+ return <-device.pool.inboundElementReuseChan
+ }
+}
+
+func (device *Device) PutInboundElement(msg *QueueInboundElement) {
+ if preallocatedBuffers == 0 {
+ device.pool.inboundElementPool.Put(msg)
+ } else {
+ device.pool.inboundElementReuseChan <- msg
+ }
+}
+
+func (device *Device) GetOutboundElement() *QueueOutboundElement {
+ if preallocatedBuffers == 0 {
+ return device.pool.outboundElementPool.Get().(*QueueOutboundElement)
+ } else {
+ return <-device.pool.outboundElementReuseChan
+ }
+}
+
+func (device *Device) PutOutboundElement(msg *QueueOutboundElement) {
+ if preallocatedBuffers == 0 {
+ device.pool.outboundElementPool.Put(msg)
+ } else {
+ device.pool.outboundElementReuseChan <- msg
+ }
+}