Skip to content

Commit 4398147

Browse files
kpschoedelpull[bot]
authored andcommitted
ObjectPool: Use Platform::New and rename modes (#13088)
#### Problem - Heap allocations should use `Platform::New` rather than raw `new`. - `ObjectPoolMem::kStatic` could be misleading, since the pool memory is part of the enclosing context, not static unless also declared `static`. #### Change overview - Use `Platform::New` and `Platform::Delete`. - Rename `ObjectPoolMem::kStatic` → `kInline` and `kDynamic` → `kHeap`. #### Testing CI
1 parent 9787339 commit 4398147

File tree

7 files changed

+45
-26
lines changed

7 files changed

+45
-26
lines changed

src/lib/dnssd/minimal_mdns/Server.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class ServerBase
228228

229229
// The PoolImpl impl is used as a base class because its destructor must be called after ServerBase's destructor.
230230
template <size_t kCount>
231-
class Server : private chip::PoolImpl<ServerBase::EndpointInfo, kCount, chip::ObjectPoolMem::kStatic,
231+
class Server : private chip::PoolImpl<ServerBase::EndpointInfo, kCount, chip::ObjectPoolMem::kInline,
232232
ServerBase::EndpointInfoPoolType::Interface>,
233233
public ServerBase
234234
{

src/lib/dnssd/minimal_mdns/tests/CheckOnlyServer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void MakePrintableName(char (&location)[N], FullQName name)
7171

7272
} // namespace
7373

74-
class CheckOnlyServer : private chip::PoolImpl<ServerBase::EndpointInfo, 0, chip::ObjectPoolMem::kStatic,
74+
class CheckOnlyServer : private chip::PoolImpl<ServerBase::EndpointInfo, 0, chip::ObjectPoolMem::kInline,
7575
ServerBase::EndpointInfoPoolType::Interface>,
7676
public ServerBase,
7777
public ParserDelegate,

src/lib/support/Pool.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Loop HeapObjectList::ForEachNode(void * context, Lambda lambda)
151151
if (p->mObject == nullptr)
152152
{
153153
p->Remove();
154-
delete p;
154+
Platform::Delete(p);
155155
}
156156
p = next;
157157
}

src/lib/support/Pool.h

+21-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#pragma once
2424

25+
#include <lib/support/CHIPMem.h>
2526
#include <lib/support/CodeUtils.h>
2627
#include <system/SystemConfig.h>
2728

@@ -276,10 +277,10 @@ class HeapObjectPool : public internal::Statistics, public internal::PoolCommon<
276277
template <typename... Args>
277278
T * CreateObject(Args &&... args)
278279
{
279-
T * object = new T(std::forward<Args>(args)...);
280+
T * object = Platform::New<T>(std::forward<Args>(args)...);
280281
if (object != nullptr)
281282
{
282-
auto node = new internal::HeapObjectListNode();
283+
auto node = Platform::New<internal::HeapObjectListNode>();
283284
if (node != nullptr)
284285
{
285286
node->mObject = object;
@@ -300,7 +301,7 @@ class HeapObjectPool : public internal::Statistics, public internal::PoolCommon<
300301
{
301302
// Note that the node is not removed here; that is deferred until the end of the next pool iteration.
302303
node->mObject = nullptr;
303-
delete object;
304+
Platform::Delete(object);
304305
DecreaseUsage();
305306
}
306307
}
@@ -336,28 +337,39 @@ class HeapObjectPool : public internal::Statistics, public internal::PoolCommon<
336337

337338
#endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
338339

340+
/**
341+
* Specify ObjectPool storage allocation.
342+
*/
339343
enum class ObjectPoolMem
340344
{
341-
kStatic,
345+
/**
346+
* Use storage inside the containing scope for both objects and pool management state.
347+
*/
348+
kInline,
342349
#if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
343-
kDynamic,
344-
kDefault = kDynamic
350+
/**
351+
* Allocate objects from the heap, with only pool management state in the containing scope.
352+
*
353+
* For this case, the ObjectPool size parameter is ignored.
354+
*/
355+
kHeap,
356+
kDefault = kHeap
345357
#else // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
346-
kDefault = kStatic
358+
kDefault = kInline
347359
#endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
348360
};
349361

350362
template <typename T, size_t N, ObjectPoolMem P = ObjectPoolMem::kDefault>
351363
class ObjectPool;
352364

353365
template <typename T, size_t N>
354-
class ObjectPool<T, N, ObjectPoolMem::kStatic> : public BitMapObjectPool<T, N>
366+
class ObjectPool<T, N, ObjectPoolMem::kInline> : public BitMapObjectPool<T, N>
355367
{
356368
};
357369

358370
#if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
359371
template <typename T, size_t N>
360-
class ObjectPool<T, N, ObjectPoolMem::kDynamic> : public HeapObjectPool<T>
372+
class ObjectPool<T, N, ObjectPoolMem::kHeap> : public HeapObjectPool<T>
361373
{
362374
};
363375
#endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP

src/lib/support/tests/TestPool.cpp

+14-13
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,20 @@ void TestReleaseNull(nlTestSuite * inSuite, void * inContext)
6262

6363
void TestReleaseNullStatic(nlTestSuite * inSuite, void * inContext)
6464
{
65-
TestReleaseNull<uint32_t, 10, ObjectPoolMem::kStatic>(inSuite, inContext);
65+
TestReleaseNull<uint32_t, 10, ObjectPoolMem::kInline>(inSuite, inContext);
6666
}
6767

6868
#if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
6969
void TestReleaseNullDynamic(nlTestSuite * inSuite, void * inContext)
7070
{
71-
TestReleaseNull<uint32_t, 10, ObjectPoolMem::kDynamic>(inSuite, inContext);
71+
TestReleaseNull<uint32_t, 10, ObjectPoolMem::kHeap>(inSuite, inContext);
7272
}
7373
#endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
7474

7575
template <typename T, size_t N, ObjectPoolMem P>
7676
void TestCreateReleaseObject(nlTestSuite * inSuite, void * inContext)
7777
{
78-
ObjectPool<uint32_t, N, ObjectPoolMem::kStatic> pool;
78+
ObjectPool<uint32_t, N, ObjectPoolMem::kInline> pool;
7979
uint32_t * obj[N];
8080

8181
NL_TEST_ASSERT(inSuite, pool.Allocated() == 0);
@@ -104,9 +104,9 @@ void TestCreateReleaseObject(nlTestSuite * inSuite, void * inContext)
104104
void TestCreateReleaseObjectStatic(nlTestSuite * inSuite, void * inContext)
105105
{
106106
constexpr const size_t kSize = 100;
107-
TestCreateReleaseObject<uint32_t, kSize, ObjectPoolMem::kStatic>(inSuite, inContext);
107+
TestCreateReleaseObject<uint32_t, kSize, ObjectPoolMem::kInline>(inSuite, inContext);
108108

109-
ObjectPool<uint32_t, kSize, ObjectPoolMem::kStatic> pool;
109+
ObjectPool<uint32_t, kSize, ObjectPoolMem::kInline> pool;
110110
uint32_t * obj[kSize];
111111

112112
for (size_t i = 0; i < kSize; ++i)
@@ -144,7 +144,7 @@ void TestCreateReleaseObjectStatic(nlTestSuite * inSuite, void * inContext)
144144
#if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
145145
void TestCreateReleaseObjectDynamic(nlTestSuite * inSuite, void * inContext)
146146
{
147-
TestCreateReleaseObject<uint32_t, 100, ObjectPoolMem::kDynamic>(inSuite, inContext);
147+
TestCreateReleaseObject<uint32_t, 100, ObjectPoolMem::kHeap>(inSuite, inContext);
148148
}
149149
#endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
150150

@@ -201,13 +201,13 @@ void TestCreateReleaseStruct(nlTestSuite * inSuite, void * inContext)
201201

202202
void TestCreateReleaseStructStatic(nlTestSuite * inSuite, void * inContext)
203203
{
204-
TestCreateReleaseStruct<ObjectPoolMem::kStatic>(inSuite, inContext);
204+
TestCreateReleaseStruct<ObjectPoolMem::kInline>(inSuite, inContext);
205205
}
206206

207207
#if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
208208
void TestCreateReleaseStructDynamic(nlTestSuite * inSuite, void * inContext)
209209
{
210-
TestCreateReleaseStruct<ObjectPoolMem::kDynamic>(inSuite, inContext);
210+
TestCreateReleaseStruct<ObjectPoolMem::kHeap>(inSuite, inContext);
211211
}
212212
#endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
213213

@@ -334,13 +334,13 @@ void TestForEachActiveObject(nlTestSuite * inSuite, void * inContext)
334334

335335
void TestForEachActiveObjectStatic(nlTestSuite * inSuite, void * inContext)
336336
{
337-
TestForEachActiveObject<ObjectPoolMem::kStatic>(inSuite, inContext);
337+
TestForEachActiveObject<ObjectPoolMem::kInline>(inSuite, inContext);
338338
}
339339

340340
#if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
341341
void TestForEachActiveObjectDynamic(nlTestSuite * inSuite, void * inContext)
342342
{
343-
TestForEachActiveObject<ObjectPoolMem::kDynamic>(inSuite, inContext);
343+
TestForEachActiveObject<ObjectPoolMem::kHeap>(inSuite, inContext);
344344
}
345345
#endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
346346

@@ -397,23 +397,24 @@ void TestPoolInterface(nlTestSuite * inSuite, void * inContext)
397397

398398
void TestPoolInterfaceStatic(nlTestSuite * inSuite, void * inContext)
399399
{
400-
TestPoolInterface<ObjectPoolMem::kStatic>(inSuite, inContext);
400+
TestPoolInterface<ObjectPoolMem::kInline>(inSuite, inContext);
401401
}
402402

403403
#if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
404404
void TestPoolInterfaceDynamic(nlTestSuite * inSuite, void * inContext)
405405
{
406-
TestPoolInterface<ObjectPoolMem::kDynamic>(inSuite, inContext);
406+
TestPoolInterface<ObjectPoolMem::kHeap>(inSuite, inContext);
407407
}
408408
#endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
409409

410410
int Setup(void * inContext)
411411
{
412-
return SUCCESS;
412+
return ::chip::Platform::MemoryInit() == CHIP_NO_ERROR ? SUCCESS : FAILURE;
413413
}
414414

415415
int Teardown(void * inContext)
416416
{
417+
::chip::Platform::MemoryShutdown();
417418
return SUCCESS;
418419
}
419420

src/system/tests/TestSystemTimer.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@ static int TestSetup(void * aContext)
442442
{
443443
TestContext & lContext = *reinterpret_cast<TestContext *>(aContext);
444444

445+
if (::chip::Platform::MemoryInit() != CHIP_NO_ERROR)
446+
{
447+
return FAILURE;
448+
}
449+
445450
#if CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_VERSION_MAJOR == 2 && LWIP_VERSION_MINOR == 0
446451
static sys_mbox_t * sLwIPEventQueue = NULL;
447452

@@ -471,6 +476,7 @@ static int TestTeardown(void * aContext)
471476
tcpip_finish(NULL, NULL);
472477
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0)
473478

479+
::chip::Platform::MemoryShutdown();
474480
return (SUCCESS);
475481
}
476482

src/transport/raw/TCP.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ class TCP : public TCPBase
281281
private:
282282
friend class TCPTest;
283283
TCPBase::ActiveConnectionState mConnectionsBuffer[kActiveConnectionsSize];
284-
PoolImpl<PendingPacket, kPendingPacketSize, ObjectPoolMem::kStatic, PendingPacketPoolType::Interface> mPendingPackets;
284+
PoolImpl<PendingPacket, kPendingPacketSize, ObjectPoolMem::kInline, PendingPacketPoolType::Interface> mPendingPackets;
285285
};
286286

287287
} // namespace Transport

0 commit comments

Comments
 (0)