}
func (device *Device) PutInboundElement(msg *QueueInboundElement) {
+ msg.clearPointers()
if PreallocatedBuffersPerPool == 0 {
device.pool.inboundElementPool.Put(msg)
} else {
}
func (device *Device) PutOutboundElement(msg *QueueOutboundElement) {
+ msg.clearPointers()
if PreallocatedBuffersPerPool == 0 {
device.pool.outboundElementPool.Put(msg)
} else {
endpoint conn.Endpoint
}
+// clearPointers clears elem fields that contain pointers.
+// This makes the garbage collector's life easier and
+// avoids accidentally keeping other objects around unnecessarily.
+// It also reduces the possible collateral damage from use-after-free bugs.
+func (elem *QueueInboundElement) clearPointers() {
+ elem.buffer = nil
+ elem.packet = nil
+ elem.keypair = nil
+ elem.endpoint = nil
+}
+
func (elem *QueueInboundElement) Drop() {
atomic.StoreInt32(&elem.dropped, AtomicTrue)
}
elem.buffer = device.GetMessageBuffer()
elem.Mutex = sync.Mutex{}
elem.nonce = 0
+ // keypair and peer were cleared (if necessary) by clearPointers.
+ return elem
+}
+
+// clearPointers clears elem fields that contain pointers.
+// This makes the garbage collector's life easier and
+// avoids accidentally keeping other objects around unnecessarily.
+// It also reduces the possible collateral damage from use-after-free bugs.
+func (elem *QueueOutboundElement) clearPointers() {
+ elem.buffer = nil
+ elem.packet = nil
elem.keypair = nil
elem.peer = nil
- return elem
}
func (elem *QueueOutboundElement) Drop() {