@@ -71,32 +71,13 @@ @implementation MTRServerCluster {
71
71
std::unique_ptr<MTRServerAttributeAccessInterface> _attributeAccessInterface;
72
72
// We can't use something like std::unique_ptr<EmberAfAttributeMetadata[]>
73
73
// because EmberAfAttributeMetadata does not have a default constructor, so
74
- // we can't alloc and then initializer later. And we need a contiguous
75
- // buffer for all the attribute metadata, so we need to do this by hand.
76
- EmberAfAttributeMetadata * _matterAttributeMetadata;
77
- size_t _matterAttributeMetadataCount;
74
+ // we can't alloc and then initializer later.
75
+ std::vector<EmberAfAttributeMetadata> _matterAttributeMetadata;
78
76
79
77
std::unique_ptr<CommandId[]> _matterAcceptedCommandList;
80
78
std::unique_ptr<CommandId[]> _matterGeneratedCommandList;
81
79
}
82
80
83
- - (void )dealloc
84
- {
85
- [self deallocateAttributeMetadata ];
86
- }
87
-
88
- - (void )deallocateAttributeMetadata
89
- {
90
- if (_matterAttributeMetadata != nullptr ) {
91
- for (size_t i = 0 ; i < _matterAttributeMetadataCount; ++i) {
92
- _matterAttributeMetadata[i].~EmberAfAttributeMetadata ();
93
- }
94
- free (_matterAttributeMetadata);
95
- _matterAttributeMetadata = nullptr ;
96
- }
97
- _matterAttributeMetadataCount = 0 ;
98
- }
99
-
100
81
- (nullable instancetype )initWithClusterID : (NSNumber *)clusterID revision : (NSNumber *)revision
101
82
{
102
83
auto clusterIDValue = clusterID.unsignedLongLongValue ;
@@ -225,18 +206,13 @@ - (BOOL)addAttribute:(MTRServerAttribute *)attribute
225
206
return YES ;
226
207
}
227
208
228
- #define MTR_DECLARE_LIST_ATTRIBUTE (attrID ) \
229
- DECLARE_DYNAMIC_ATTRIBUTE (attrID, ARRAY, 0 , 0 )
230
-
231
209
static constexpr EmberAfAttributeMetadata sDescriptorAttributesMetadata [] = {
232
- MTR_DECLARE_LIST_ATTRIBUTE (MTRAttributeIDTypeClusterDescriptorAttributeDeviceTypeListID),
233
- MTR_DECLARE_LIST_ATTRIBUTE (MTRAttributeIDTypeClusterDescriptorAttributeServerListID),
234
- MTR_DECLARE_LIST_ATTRIBUTE (MTRAttributeIDTypeClusterDescriptorAttributeClientListID),
235
- MTR_DECLARE_LIST_ATTRIBUTE (MTRAttributeIDTypeClusterDescriptorAttributePartsListID),
210
+ DECLARE_DYNAMIC_ATTRIBUTE (MTRAttributeIDTypeClusterDescriptorAttributeDeviceTypeListID, ARRAY, 0 , 0 ),
211
+ DECLARE_DYNAMIC_ATTRIBUTE (MTRAttributeIDTypeClusterDescriptorAttributeServerListID, ARRAY, 0 , 0 ),
212
+ DECLARE_DYNAMIC_ATTRIBUTE (MTRAttributeIDTypeClusterDescriptorAttributeClientListID, ARRAY, 0 , 0 ),
213
+ DECLARE_DYNAMIC_ATTRIBUTE (MTRAttributeIDTypeClusterDescriptorAttributePartsListID, ARRAY, 0 , 0 ),
236
214
};
237
215
238
- #undef MTR_DECLARE_LIST_ATTRIBUTE
239
-
240
216
- (BOOL )associateWithController : (nullable MTRDeviceController *)controller
241
217
{
242
218
MTRDeviceController * existingController = _deviceController;
@@ -292,49 +268,40 @@ - (BOOL)associateWithController:(nullable MTRDeviceController *)controller
292
268
return NO ;
293
269
}
294
270
295
- _matterAttributeMetadata = static_cast <EmberAfAttributeMetadata *>(calloc (attributeCount, sizeof (EmberAfAttributeMetadata)));
296
- if (_matterAttributeMetadata == nullptr ) {
297
- return NO ;
298
- }
299
-
300
- _matterAttributeMetadataCount = attributeCount;
301
-
302
271
size_t attrIndex = 0 ;
303
272
for (; attrIndex < _attributes.count ; ++attrIndex) {
304
273
auto * attr = _attributes[attrIndex];
305
- // Placement-new into the right slot.
306
- new (&_matterAttributeMetadata[attrIndex])
307
- EmberAfAttributeMetadata (DECLARE_DYNAMIC_ATTRIBUTE (static_cast <AttributeId>(attr.attributeID .unsignedLongLongValue ),
308
- // The type does not actually matter, since we plan to
309
- // handle this entirely via AttributeAccessInterface.
310
- // Claim Array because that one will keep random IM
311
- // code from trying to do things with the attribute
312
- // store.
313
- ARRAY,
314
- // Size in bytes does not matter, since we plan to
315
- // handle this entirely via AttributeAccessInterface.
316
- 0 ,
317
- // ATTRIBUTE_MASK_NULLABLE is not relevant because we
318
- // are handling this all via AttributeAccessInterface.
319
- 0 ));
274
+ _matterAttributeMetadata.emplace_back (EmberAfAttributeMetadata (DECLARE_DYNAMIC_ATTRIBUTE (static_cast <AttributeId>(attr.attributeID .unsignedLongLongValue ),
275
+ // The type does not actually matter, since we plan to
276
+ // handle this entirely via AttributeAccessInterface.
277
+ // Claim Array because that one will keep random IM
278
+ // code from trying to do things with the attribute
279
+ // store.
280
+ ARRAY,
281
+ // Size in bytes does not matter, since we plan to
282
+ // handle this entirely via AttributeAccessInterface.
283
+ 0 ,
284
+ // ATTRIBUTE_MASK_NULLABLE is not relevant because we
285
+ // are handling this all via AttributeAccessInterface.
286
+ 0 )));
320
287
}
321
288
322
289
if (needsFeatureMap) {
323
- new (& _matterAttributeMetadata[attrIndex]) EmberAfAttributeMetadata (DECLARE_DYNAMIC_ATTRIBUTE (MTRAttributeIDTypeGlobalAttributeFeatureMapID,
324
- BITMAP32, 4 , 0 ));
290
+ _matterAttributeMetadata. emplace_back ( EmberAfAttributeMetadata (DECLARE_DYNAMIC_ATTRIBUTE (MTRAttributeIDTypeGlobalAttributeFeatureMapID,
291
+ BITMAP32, 4 , 0 ))) ;
325
292
++attrIndex;
326
293
}
327
294
328
295
if (needsDescriptorAttributes) {
329
296
for (auto & data : sDescriptorAttributesMetadata ) {
330
- new (& _matterAttributeMetadata[attrIndex]) EmberAfAttributeMetadata (data);
297
+ _matterAttributeMetadata. emplace_back (data);
331
298
++attrIndex;
332
299
}
333
300
}
334
301
335
302
// Add our ClusterRevision bit.
336
- new (& _matterAttributeMetadata[attrIndex]) EmberAfAttributeMetadata (DECLARE_DYNAMIC_ATTRIBUTE (MTRAttributeIDTypeGlobalAttributeClusterRevisionID,
337
- INT16U, 2 , 0 ));
303
+ _matterAttributeMetadata. emplace_back ( EmberAfAttributeMetadata (DECLARE_DYNAMIC_ATTRIBUTE (MTRAttributeIDTypeGlobalAttributeClusterRevisionID,
304
+ INT16U, 2 , 0 ))) ;
338
305
++attrIndex;
339
306
340
307
_attributeAccessInterface = std::make_unique<MTRServerAttributeAccessInterface>(_parentEndpoint,
@@ -362,7 +329,7 @@ - (void)invalidate
362
329
// queue after associateWithController succeeds, but we are no longer being
363
330
// looked at from that queue, so it's safe to reset it here.
364
331
_matterAccessGrants = [NSSet set ];
365
- [ self deallocateAttributeMetadata ] ;
332
+ _matterAttributeMetadata. clear () ;
366
333
_attributeAccessInterface.reset ();
367
334
_matterAcceptedCommandList.reset ();
368
335
_matterGeneratedCommandList.reset ();
@@ -413,7 +380,9 @@ - (void)setParentEndpoint:(EndpointId)endpoint
413
380
414
381
- (Span<const EmberAfAttributeMetadata>)matterAttributeMetadata
415
382
{
416
- return Span<const EmberAfAttributeMetadata>(_matterAttributeMetadata, _matterAttributeMetadataCount);
383
+ // This is always called after our _matterAttributeMetadata has been set up
384
+ // by associateWithController.
385
+ return Span<const EmberAfAttributeMetadata>(_matterAttributeMetadata.data (), _matterAttributeMetadata.size ());
417
386
}
418
387
419
388
- (CommandId *)matterAcceptedCommands
0 commit comments