From 8b79e826863db75ab2f250fdbcc691576d71dbea Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sun, 30 May 2021 17:14:23 +0200 Subject: [PATCH] nrf: avoid heap allocation in waitForEvent Because arm.SVCall1 lets pointers escape, the return value of sd_softdevice_is_enabled (passed as a pointer in a parameter) will escape and thus this value will be heap allocated. Use a global variable for this purpose instead to avoid the heap allocation. This is safe as waitForEvent may only be called outside of interrupts. --- src/runtime/runtime_nrf_softdevice.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/runtime/runtime_nrf_softdevice.go b/src/runtime/runtime_nrf_softdevice.go index 36269e11..b1c97033 100644 --- a/src/runtime/runtime_nrf_softdevice.go +++ b/src/runtime/runtime_nrf_softdevice.go @@ -10,6 +10,9 @@ import ( //export sd_app_evt_wait func sd_app_evt_wait() +// This is a global variable to avoid a heap allocation in waitForEvents. +var softdeviceEnabled uint8 + func waitForEvents() { // Call into the SoftDevice to sleep. This is necessary here because a // normal wfe will not put the chip in low power mode (it still consumes @@ -18,10 +21,9 @@ func waitForEvents() { // First check whether the SoftDevice is enabled. Unfortunately, // sd_app_evt_wait cannot be called when the SoftDevice is not enabled. - var enabled uint8 - arm.SVCall1(0x12, &enabled) // sd_softdevice_is_enabled + arm.SVCall1(0x12, &softdeviceEnabled) // sd_softdevice_is_enabled - if enabled != 0 { + if softdeviceEnabled != 0 { // Now pick the appropriate SVCall number. Hopefully they won't change // in the future with a different SoftDevice version. if nrf.Device == "nrf51" {