EtherGuard-VPN/device/pools.go

92 lines
2.6 KiB
Go
Raw Normal View History

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