Skip to content

Commit e0e261e

Browse files
aykevldeadprogram
authored andcommitted
softdevice: avoid a heap allocation in the SoftDevice event handler
Events are delivered using interrupts. But there was an accidental heap allocation in the interrupt. Fix it by using a global instead. This is safe (and doesn't need volatile accesses), as the global is only ever accessed from that particular interrupt.
1 parent 3f79b9e commit e0e261e

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

adapter_sd.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ var DefaultAdapter = &Adapter{isDefault: true,
6161
return
6262
}}
6363

64+
var eventBufLen uint16
65+
6466
// Enable configures the BLE stack. It must be called before any
6567
// Bluetooth-related calls (unless otherwise indicated).
6668
func (a *Adapter) Enable() error {
@@ -71,7 +73,7 @@ func (a *Adapter) Enable() error {
7173
// Enable the IRQ that handles all events.
7274
intr := interrupt.New(nrf.IRQ_SWI2, func(interrupt.Interrupt) {
7375
for {
74-
eventBufLen := uint16(unsafe.Sizeof(eventBuf))
76+
eventBufLen = uint16(unsafe.Sizeof(eventBuf))
7577
errCode := C.sd_ble_evt_get((*uint8)(unsafe.Pointer(&eventBuf)), &eventBufLen)
7678
if errCode != 0 {
7779
// Possible error conditions:

0 commit comments

Comments
 (0)