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.
Этот коммит содержится в:
Ayke van Laethem 2021-05-30 17:14:23 +02:00 коммит произвёл Ron Evans
родитель 22eeed2da1
коммит 8b79e82686

Просмотреть файл

@ -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" {