Skip to content

Commit 3c96d5b

Browse files
authored
Revert "Darwin: Prohibit static initializers in Matter.framework" (#34286)
This reverts commit f199ba4.
1 parent bf4edc4 commit 3c96d5b

14 files changed

+47
-80
lines changed

src/app/AttributeAccessInterfaceCache.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class AttributeAccessInterfaceCache
4848
kDefinitelyUsed
4949
};
5050

51-
constexpr AttributeAccessInterfaceCache() = default;
51+
AttributeAccessInterfaceCache() { Invalidate(); }
5252

5353
/**
5454
* @brief Invalidate the whole cache. Must be called every time list of AAI registrations changes.
@@ -106,8 +106,6 @@ class AttributeAccessInterfaceCache
106106
private:
107107
struct AttributeAccessCacheEntry
108108
{
109-
constexpr AttributeAccessCacheEntry() = default;
110-
111109
EndpointId endpointId = kInvalidEndpointId;
112110
ClusterId clusterId = kInvalidClusterId;
113111
AttributeAccessInterface * accessor = nullptr;
@@ -139,8 +137,8 @@ class AttributeAccessInterfaceCache
139137
return &mCacheSlots[0];
140138
}
141139

142-
AttributeAccessCacheEntry mCacheSlots[1] = {};
143-
AttributeAccessCacheEntry mLastUnusedEntry{};
140+
AttributeAccessCacheEntry mCacheSlots[1];
141+
AttributeAccessCacheEntry mLastUnusedEntry;
144142
};
145143

146144
} // namespace app

src/app/EventLoggingTypes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ struct Timestamp
100100
kSystem = 0,
101101
kEpoch
102102
};
103-
constexpr Timestamp() = default;
103+
Timestamp() {}
104104
Timestamp(Type aType, uint64_t aValue) : mType(aType), mValue(aValue) {}
105105
Timestamp(System::Clock::Timestamp aValue) : mType(Type::kSystem), mValue(aValue.count()) {}
106106
static Timestamp Epoch(System::Clock::Timestamp aValue)

src/app/EventManagement.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ using namespace chip::TLV;
3232

3333
namespace chip {
3434
namespace app {
35-
EventManagement EventManagement::sInstance;
35+
static EventManagement sInstance;
3636

3737
/**
3838
* @brief

src/app/EventManagement.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace app {
7373
inline constexpr const uint32_t kEventManagementProfile = 0x1;
7474
inline constexpr const uint32_t kFabricIndexTag = 0x1;
7575
inline constexpr size_t kMaxEventSizeReserve = 512;
76-
inline constexpr uint16_t kRequiredEventField =
76+
constexpr uint16_t kRequiredEventField =
7777
(1 << to_underlying(EventDataIB::Tag::kPriority)) | (1 << to_underlying(EventDataIB::Tag::kPath));
7878

7979
/**
@@ -388,9 +388,6 @@ class EventManagement
388388
void SetScheduledEventInfo(EventNumber & aEventNumber, uint32_t & aInitialWrittenEventBytes) const;
389389

390390
private:
391-
constexpr EventManagement() = default;
392-
static EventManagement sInstance;
393-
394391
/**
395392
* @brief
396393
* Internal structure for traversing events.
@@ -558,9 +555,9 @@ class EventManagement
558555
MonotonicallyIncreasingCounter<EventNumber> * mpEventNumberCounter = nullptr;
559556

560557
EventNumber mLastEventNumber = 0; ///< Last event Number vended
561-
Timestamp mLastEventTimestamp{}; ///< The timestamp of the last event in this buffer
558+
Timestamp mLastEventTimestamp; ///< The timestamp of the last event in this buffer
562559

563-
System::Clock::Milliseconds64 mMonotonicStartupTime{};
560+
System::Clock::Milliseconds64 mMonotonicStartupTime;
564561
};
565562
} // namespace app
566563
} // namespace chip

src/app/clusters/descriptor/descriptor.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <app/AttributeAccessInterfaceRegistry.h>
2828
#include <app/util/attribute-storage.h>
2929
#include <app/util/endpoint-config-api.h>
30-
#include <lib/core/Global.h>
3130
#include <lib/support/CodeUtils.h>
3231
#include <lib/support/logging/CHIPLogging.h>
3332

@@ -206,9 +205,7 @@ CHIP_ERROR DescriptorAttrAccess::ReadClusterRevision(EndpointId endpoint, Attrib
206205
return aEncoder.Encode(kClusterRevision);
207206
}
208207

209-
namespace {
210-
Global<DescriptorAttrAccess> gAttrAccess;
211-
}
208+
DescriptorAttrAccess gAttrAccess;
212209

213210
CHIP_ERROR DescriptorAttrAccess::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
214211
{
@@ -247,5 +244,5 @@ CHIP_ERROR DescriptorAttrAccess::Read(const ConcreteReadAttributePath & aPath, A
247244

248245
void MatterDescriptorPluginServerInitCallback()
249246
{
250-
registerAttributeAccessOverride(&gAttrAccess.get());
247+
registerAttributeAccessOverride(&gAttrAccess);
251248
}

src/darwin/Framework/Matter.xcodeproj/project.pbxproj

-2
Original file line numberDiff line numberDiff line change
@@ -2497,7 +2497,6 @@
24972497
"-Wl,-unexported_symbol,\"__Unwind_*\"",
24982498
"-Wl,-unexported_symbol,\"_unw_*\"",
24992499
"-Wl,-hidden-lCHIP",
2500-
"-Wl,-no_inits",
25012500
);
25022501
"OTHER_LDFLAGS[sdk=macosx*]" = (
25032502
"-framework",
@@ -2516,7 +2515,6 @@
25162515
"-Wl,-unexported_symbol,\"__Unwind_*\"",
25172516
"-Wl,-unexported_symbol,\"_unw_*\"",
25182517
"-Wl,-hidden-lCHIP",
2519-
"-Wl,-no_inits",
25202518
);
25212519
PRODUCT_BUNDLE_IDENTIFIER = com.csa.matter;
25222520
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";

src/include/platform/internal/GenericConfigurationManagerImpl.ipp

+4-8
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,14 @@
4949
#include <platform/ThreadStackManager.h>
5050
#endif
5151

52-
#include <optional>
53-
5452
// TODO : may be we can make it configurable
5553
#define BLE_ADVERTISEMENT_VERSION 0
5654

5755
namespace chip {
5856
namespace DeviceLayer {
5957
namespace Internal {
6058

61-
namespace {
62-
std::optional<System::Clock::Seconds32> gFirmwareBuildChipEpochTime;
63-
}
59+
static Optional<System::Clock::Seconds32> sFirmwareBuildChipEpochTime;
6460

6561
#if CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER
6662

@@ -292,9 +288,9 @@ template <class ConfigClass>
292288
CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetFirmwareBuildChipEpochTime(System::Clock::Seconds32 & chipEpochTime)
293289
{
294290
// If the setter was called and we have a value in memory, return this.
295-
if (gFirmwareBuildChipEpochTime.has_value())
291+
if (sFirmwareBuildChipEpochTime.HasValue())
296292
{
297-
chipEpochTime = gFirmwareBuildChipEpochTime.value();
293+
chipEpochTime = sFirmwareBuildChipEpochTime.Value();
298294
return CHIP_NO_ERROR;
299295
}
300296
#ifdef CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME_MATTER_EPOCH_S
@@ -327,7 +323,7 @@ CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::SetFirmwareBuildChipEpo
327323
//
328324
// Implementations that can't use the hard-coded time for whatever reason
329325
// should set this at each init.
330-
gFirmwareBuildChipEpochTime = chipEpochTime;
326+
sFirmwareBuildChipEpochTime.SetValue(chipEpochTime);
331327
return CHIP_NO_ERROR;
332328
}
333329

src/lib/support/IntrusiveList.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ enum class IntrusiveMode
8484
class IntrusiveListNodePrivateBase
8585
{
8686
public:
87-
constexpr IntrusiveListNodePrivateBase() : mPrev(nullptr), mNext(nullptr) {}
87+
IntrusiveListNodePrivateBase() : mPrev(nullptr), mNext(nullptr) {}
8888
~IntrusiveListNodePrivateBase() { VerifyOrDie(!IsInList()); }
8989

9090
// Note: The copy construct/assignment is not provided because the list node state is not copyable.
@@ -98,7 +98,7 @@ class IntrusiveListNodePrivateBase
9898

9999
private:
100100
friend class IntrusiveListBase;
101-
constexpr IntrusiveListNodePrivateBase(IntrusiveListNodePrivateBase * prev, IntrusiveListNodePrivateBase * next) :
101+
IntrusiveListNodePrivateBase(IntrusiveListNodePrivateBase * prev, IntrusiveListNodePrivateBase * next) :
102102
mPrev(prev), mNext(next)
103103
{}
104104

@@ -284,7 +284,7 @@ class IntrusiveListBase
284284
// ^ |
285285
// \------------------------------------------/
286286
//
287-
constexpr IntrusiveListBase() : mNode(&mNode, &mNode) {}
287+
IntrusiveListBase() : mNode(&mNode, &mNode) {}
288288
~IntrusiveListBase()
289289
{
290290
VerifyOrDie(Empty());
@@ -399,7 +399,7 @@ template <typename T, IntrusiveMode Mode = IntrusiveMode::Strict, typename Hook
399399
class IntrusiveList : public IntrusiveListBase
400400
{
401401
public:
402-
constexpr IntrusiveList() : IntrusiveListBase() {}
402+
IntrusiveList() : IntrusiveListBase() {}
403403

404404
IntrusiveList(IntrusiveList &&) = default;
405405
IntrusiveList & operator=(IntrusiveList &&) = default;

src/messaging/ReliableMessageProtocolConfig.cpp

+19-25
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,28 @@
3232
#include <app/icd/server/ICDConfigurationData.h> // nogncheck
3333
#endif
3434

35-
#include <optional>
36-
3735
namespace chip {
3836

3937
using namespace System::Clock::Literals;
4038

4139
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
42-
namespace {
43-
// Use std::optional for globals to avoid static initializers
44-
std::optional<System::Clock::Timeout> gIdleRetransTimeoutOverride;
45-
std::optional<System::Clock::Timeout> gActiveRetransTimeoutOverride;
46-
std::optional<System::Clock::Timeout> gActiveThresholdTimeOverride;
47-
} // namespace
40+
static Optional<System::Clock::Timeout> idleRetransTimeoutOverride = NullOptional;
41+
static Optional<System::Clock::Timeout> activeRetransTimeoutOverride = NullOptional;
42+
static Optional<System::Clock::Timeout> activeThresholdTimeOverride = NullOptional;
4843

4944
void OverrideLocalMRPConfig(System::Clock::Timeout idleRetransTimeout, System::Clock::Timeout activeRetransTimeout,
5045
System::Clock::Timeout activeThresholdTime)
5146
{
52-
gIdleRetransTimeoutOverride = idleRetransTimeout;
53-
gActiveRetransTimeoutOverride = activeRetransTimeout;
54-
gActiveThresholdTimeOverride = activeThresholdTime;
47+
idleRetransTimeoutOverride.SetValue(idleRetransTimeout);
48+
activeRetransTimeoutOverride.SetValue(activeRetransTimeout);
49+
activeThresholdTimeOverride.SetValue(activeThresholdTime);
5550
}
5651

5752
void ClearLocalMRPConfigOverride()
5853
{
59-
gActiveRetransTimeoutOverride = std::nullopt;
60-
gIdleRetransTimeoutOverride = std::nullopt;
61-
gActiveThresholdTimeOverride = std::nullopt;
54+
activeRetransTimeoutOverride.ClearValue();
55+
idleRetransTimeoutOverride.ClearValue();
56+
activeThresholdTimeOverride.ClearValue();
6257
}
6358
#endif
6459

@@ -67,15 +62,14 @@ namespace {
6762

6863
// This is not a static member of ReliableMessageProtocolConfig because the free
6964
// function GetLocalMRPConfig() needs access to it.
70-
// Use std::optional to avoid a static initializer
71-
std::optional<ReliableMessageProtocolConfig> sDynamicLocalMPRConfig;
65+
Optional<ReliableMessageProtocolConfig> sDynamicLocalMPRConfig;
7266

7367
} // anonymous namespace
7468

7569
bool ReliableMessageProtocolConfig::SetLocalMRPConfig(const Optional<ReliableMessageProtocolConfig> & localMRPConfig)
7670
{
7771
auto oldConfig = GetLocalMRPConfig();
78-
sDynamicLocalMPRConfig = localMRPConfig.std_optional();
72+
sDynamicLocalMPRConfig = localMRPConfig;
7973
return oldConfig != GetLocalMRPConfig();
8074
}
8175
#endif // CHIP_DEVICE_CONFIG_ENABLE_DYNAMIC_MRP_CONFIG
@@ -95,9 +89,9 @@ Optional<ReliableMessageProtocolConfig> GetLocalMRPConfig()
9589
ReliableMessageProtocolConfig config(CHIP_CONFIG_MRP_LOCAL_IDLE_RETRY_INTERVAL, CHIP_CONFIG_MRP_LOCAL_ACTIVE_RETRY_INTERVAL);
9690

9791
#if CHIP_DEVICE_CONFIG_ENABLE_DYNAMIC_MRP_CONFIG
98-
if (sDynamicLocalMPRConfig.has_value())
92+
if (sDynamicLocalMPRConfig.HasValue())
9993
{
100-
config = sDynamicLocalMPRConfig.value();
94+
config = sDynamicLocalMPRConfig.Value();
10195
}
10296
#endif // CHIP_DEVICE_CONFIG_ENABLE_DYNAMIC_MRP_CONFIG
10397

@@ -111,19 +105,19 @@ Optional<ReliableMessageProtocolConfig> GetLocalMRPConfig()
111105
#endif
112106

113107
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
114-
if (gIdleRetransTimeoutOverride.has_value())
108+
if (idleRetransTimeoutOverride.HasValue())
115109
{
116-
config.mIdleRetransTimeout = gIdleRetransTimeoutOverride.value();
110+
config.mIdleRetransTimeout = idleRetransTimeoutOverride.Value();
117111
}
118112

119-
if (gActiveRetransTimeoutOverride.has_value())
113+
if (activeRetransTimeoutOverride.HasValue())
120114
{
121-
config.mActiveRetransTimeout = gActiveRetransTimeoutOverride.value();
115+
config.mActiveRetransTimeout = activeRetransTimeoutOverride.Value();
122116
}
123117

124-
if (gActiveThresholdTimeOverride.has_value())
118+
if (activeThresholdTimeOverride.HasValue())
125119
{
126-
config.mActiveThresholdTime = gActiveThresholdTimeOverride.value();
120+
config.mActiveThresholdTime = activeRetransTimeoutOverride.Value();
127121
}
128122
#endif
129123

src/messaging/ReliableMessageProtocolConfig.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,8 @@ inline constexpr System::Clock::Milliseconds32 kDefaultActiveTime = System::Cloc
191191
*/
192192
struct ReliableMessageProtocolConfig
193193
{
194-
constexpr ReliableMessageProtocolConfig(System::Clock::Milliseconds32 idleInterval,
195-
System::Clock::Milliseconds32 activeInterval,
196-
System::Clock::Milliseconds16 activeThreshold = kDefaultActiveTime) :
194+
ReliableMessageProtocolConfig(System::Clock::Milliseconds32 idleInterval, System::Clock::Milliseconds32 activeInterval,
195+
System::Clock::Milliseconds16 activeThreshold = kDefaultActiveTime) :
197196
mIdleRetransTimeout(idleInterval),
198197
mActiveRetransTimeout(activeInterval), mActiveThresholdTime(activeThreshold)
199198
{}

src/platform/Darwin/ConfigurationManagerImpl.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <platform/Darwin/ConfigurationManagerImpl.h>
2727

2828
#include <lib/core/CHIPVendorIdentifiers.hpp>
29-
#include <lib/core/Global.h>
3029
#include <platform/CHIPDeviceConfig.h>
3130
#include <platform/ConfigurationManager.h>
3231
#include <platform/Darwin/DiagnosticDataProviderImpl.h>
@@ -139,13 +138,10 @@ CHIP_ERROR GetMACAddressFromInterfaces(io_iterator_t primaryInterfaceIterator, u
139138
}
140139
#endif // TARGET_OS_OSX
141140

142-
namespace {
143-
AtomicGlobal<ConfigurationManagerImpl> gInstance;
144-
}
145-
146141
ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance()
147142
{
148-
return gInstance.get();
143+
static ConfigurationManagerImpl sInstance;
144+
return sInstance;
149145
}
150146

151147
CHIP_ERROR ConfigurationManagerImpl::Init()

src/platform/Darwin/ConfigurationManagerImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
namespace chip {
3535
namespace DeviceLayer {
3636

37-
inline constexpr int kCountryCodeLength = 2;
37+
static constexpr int kCountryCodeLength = 2;
3838

3939
/**
4040
* Concrete implementation of the ConfigurationManager singleton object for the Darwin platform.

src/platform/Darwin/DeviceInstanceInfoProviderImpl.cpp

-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include "DeviceInstanceInfoProviderImpl.h"
2020

21-
#include <lib/core/Global.h>
2221
#include <platform/Darwin/PosixConfig.h>
2322

2423
namespace chip {
@@ -34,14 +33,5 @@ CHIP_ERROR DeviceInstanceInfoProviderImpl::GetProductId(uint16_t & productId)
3433
return Internal::PosixConfig::ReadConfigValue(Internal::PosixConfig::kConfigKey_ProductId, productId);
3534
}
3635

37-
namespace {
38-
AtomicGlobal<DeviceInstanceInfoProviderImpl> gInstance;
39-
} // namespace
40-
41-
DeviceInstanceInfoProviderImpl & DeviceInstanceInfoProviderMgrImpl()
42-
{
43-
return gInstance.get();
44-
}
45-
4636
} // namespace DeviceLayer
4737
} // namespace chip

src/platform/Darwin/DeviceInstanceInfoProviderImpl.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ class DeviceInstanceInfoProviderImpl : public Internal::GenericDeviceInstanceInf
3030
CHIP_ERROR GetVendorId(uint16_t & vendorId) override;
3131
CHIP_ERROR GetProductId(uint16_t & productId) override;
3232

33-
DeviceInstanceInfoProviderImpl() : DeviceInstanceInfoProviderImpl(ConfigurationManagerImpl::GetDefaultInstance()) {}
3433
DeviceInstanceInfoProviderImpl(ConfigurationManagerImpl & configManager) :
3534
Internal::GenericDeviceInstanceInfoProvider<Internal::PosixConfig>(configManager)
3635
{}
3736
};
3837

39-
DeviceInstanceInfoProviderImpl & DeviceInstanceInfoProviderMgrImpl();
40-
38+
inline DeviceInstanceInfoProviderImpl & DeviceInstanceInfoProviderMgrImpl()
39+
{
40+
static DeviceInstanceInfoProviderImpl sInstance(ConfigurationManagerImpl::GetDefaultInstance());
41+
return sInstance;
42+
}
4143
} // namespace DeviceLayer
4244
} // namespace chip

0 commit comments

Comments
 (0)