2019-01-02 00:55:51 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-09-22 04:29:02 +00:00
|
|
|
*
|
2021-01-28 16:52:15 +00:00
|
|
|
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
2018-09-22 04:29:02 +00:00
|
|
|
*/
|
|
|
|
|
2019-03-03 03:04:41 +00:00
|
|
|
package device
|
2018-09-22 04:29:02 +00:00
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
func (device *Device) PopulatePools() {
|
2018-09-25 00:31:02 +00:00
|
|
|
if PreallocatedBuffersPerPool == 0 {
|
2018-09-22 04:29:02 +00:00
|
|
|
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 {
|
2018-09-25 00:31:02 +00:00
|
|
|
device.pool.messageBufferReuseChan = make(chan *[MaxMessageSize]byte, PreallocatedBuffersPerPool)
|
2020-12-22 19:38:24 +00:00
|
|
|
for i := 0; i < PreallocatedBuffersPerPool; i++ {
|
2018-09-22 04:29:02 +00:00
|
|
|
device.pool.messageBufferReuseChan <- new([MaxMessageSize]byte)
|
|
|
|
}
|
2018-09-25 00:31:02 +00:00
|
|
|
device.pool.inboundElementReuseChan = make(chan *QueueInboundElement, PreallocatedBuffersPerPool)
|
2020-12-22 19:38:24 +00:00
|
|
|
for i := 0; i < PreallocatedBuffersPerPool; i++ {
|
2018-09-22 04:29:02 +00:00
|
|
|
device.pool.inboundElementReuseChan <- new(QueueInboundElement)
|
|
|
|
}
|
2018-09-25 00:31:02 +00:00
|
|
|
device.pool.outboundElementReuseChan = make(chan *QueueOutboundElement, PreallocatedBuffersPerPool)
|
2020-12-22 19:38:24 +00:00
|
|
|
for i := 0; i < PreallocatedBuffersPerPool; i++ {
|
2018-09-22 04:29:02 +00:00
|
|
|
device.pool.outboundElementReuseChan <- new(QueueOutboundElement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (device *Device) GetMessageBuffer() *[MaxMessageSize]byte {
|
2018-09-25 00:31:02 +00:00
|
|
|
if PreallocatedBuffersPerPool == 0 {
|
2018-09-22 04:29:02 +00:00
|
|
|
return device.pool.messageBufferPool.Get().(*[MaxMessageSize]byte)
|
|
|
|
} else {
|
|
|
|
return <-device.pool.messageBufferReuseChan
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (device *Device) PutMessageBuffer(msg *[MaxMessageSize]byte) {
|
2018-09-25 00:31:02 +00:00
|
|
|
if PreallocatedBuffersPerPool == 0 {
|
2018-09-22 04:29:02 +00:00
|
|
|
device.pool.messageBufferPool.Put(msg)
|
|
|
|
} else {
|
|
|
|
device.pool.messageBufferReuseChan <- msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (device *Device) GetInboundElement() *QueueInboundElement {
|
2018-09-25 00:31:02 +00:00
|
|
|
if PreallocatedBuffersPerPool == 0 {
|
2018-09-22 04:29:02 +00:00
|
|
|
return device.pool.inboundElementPool.Get().(*QueueInboundElement)
|
|
|
|
} else {
|
|
|
|
return <-device.pool.inboundElementReuseChan
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:00:52 +00:00
|
|
|
func (device *Device) PutInboundElement(elem *QueueInboundElement) {
|
|
|
|
elem.clearPointers()
|
2018-09-25 00:31:02 +00:00
|
|
|
if PreallocatedBuffersPerPool == 0 {
|
2020-12-16 00:00:52 +00:00
|
|
|
device.pool.inboundElementPool.Put(elem)
|
2018-09-22 04:29:02 +00:00
|
|
|
} else {
|
2020-12-16 00:00:52 +00:00
|
|
|
device.pool.inboundElementReuseChan <- elem
|
2018-09-22 04:29:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (device *Device) GetOutboundElement() *QueueOutboundElement {
|
2018-09-25 00:31:02 +00:00
|
|
|
if PreallocatedBuffersPerPool == 0 {
|
2018-09-22 04:29:02 +00:00
|
|
|
return device.pool.outboundElementPool.Get().(*QueueOutboundElement)
|
|
|
|
} else {
|
|
|
|
return <-device.pool.outboundElementReuseChan
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:00:52 +00:00
|
|
|
func (device *Device) PutOutboundElement(elem *QueueOutboundElement) {
|
|
|
|
elem.clearPointers()
|
2018-09-25 00:31:02 +00:00
|
|
|
if PreallocatedBuffersPerPool == 0 {
|
2020-12-16 00:00:52 +00:00
|
|
|
device.pool.outboundElementPool.Put(elem)
|
2018-09-22 04:29:02 +00:00
|
|
|
} else {
|
2020-12-16 00:00:52 +00:00
|
|
|
device.pool.outboundElementReuseChan <- elem
|
2018-09-22 04:29:02 +00:00
|
|
|
}
|
|
|
|
}
|