microbit: BUGFIX: nRF5xn global singleton preventing gcc optimisation when module not initialized

A statically allocated nRF5xn singleton "deviceInstance" prevents gcc from optimising out the .bss memory used by the ble-nrf51822 module (approx 1.2K of RAM), even when the module is not used or initialised by application code. This can cause memory usage issues for heavily constrianed systems.

This patch replaces the statically allocated singleton with an equivalent mechanism based on lazy instantiation. This approach permits gcc to optimise the code out when not in use.
This commit is contained in:
Joe Finney 2016-01-02 20:05:05 +00:00
parent d8227522cd
commit d214d82075

View file

@ -33,7 +33,7 @@ extern "C" {
/**
* The singleton which represents the nRF51822 transport for the BLE.
*/
static nRF5xn deviceInstance;
static nRF5xn *deviceInstance = NULL;
/**
* BLE-API requires an implementation of the following function in order to
@ -47,7 +47,10 @@ createBLEInstance(void)
nRF5xn& nRF5xn::Instance(BLE::InstanceID_t instanceId)
{
return deviceInstance;
if (deviceInstance == NULL)
deviceInstance = new nRF5xn();
return *deviceInstance;
}
nRF5xn::nRF5xn(void) :