[heap] move static heap construction to singleton accessor (#8242)

Move static heap construction to singleton accessor to avoid
non-trivial construction at global scope, which has an unpredictable
ordering.
This commit is contained in:
Tom Rebbert
2022-10-17 18:57:50 -07:00
committed by GitHub
parent c3bc9481e2
commit 778fbfa82d
2 changed files with 16 additions and 3 deletions
+14 -1
View File
@@ -50,7 +50,8 @@ OT_DEFINE_ALIGNED_VAR(gInstanceRaw, sizeof(Instance), uint64_t);
#if OPENTHREAD_MTD || OPENTHREAD_FTD
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
Utils::Heap Instance::sHeap;
OT_DEFINE_ALIGNED_VAR(sHeapRaw, sizeof(Utils::Heap), uint64_t);
Utils::Heap *Instance::sHeap{nullptr};
#endif
#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
bool Instance::sDnsNameCompressionEnabled = true;
@@ -240,6 +241,18 @@ Instance::Instance(void)
{
}
#if (OPENTHREAD_MTD || OPENTHREAD_FTD) && !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
Utils::Heap &Instance::GetHeap(void)
{
if (nullptr == sHeap)
{
sHeap = new (&sHeapRaw) Utils::Heap();
}
return *sHeap;
}
#endif
#if !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
Instance &Instance::InitSingle(void)
+2 -2
View File
@@ -290,7 +290,7 @@ public:
* @returns A reference to the Heap object.
*
*/
static Utils::Heap &GetHeap(void) { return sHeap; }
static Utils::Heap &GetHeap(void);
#endif
#if OPENTHREAD_CONFIG_COAP_API_ENABLE
@@ -381,7 +381,7 @@ private:
// Random::Manager is initialized before other objects. Note that it
// requires MbedTls which itself may use Heap.
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
static Utils::Heap sHeap;
static Utils::Heap *sHeap;
#endif
Crypto::MbedTls mMbedTls;
#endif // OPENTHREAD_MTD || OPENTHREAD_FTD