Skip to content

Commit 274719d

Browse files
Remove PoolCommon::ResetObject (project-chip#32120)
* Remove PoolCommon: bad name, non-member method, very very limited usage * Restyle * Added comment about returning null on failure * Fix compile on fixed size pools * Restyle * Switch to not use a cast * Fix types ... use auto because types are messy, unsure why * Rename for smaller diff * Rename for smaller diff * Update src/transport/UnauthenticatedSessionTable.h Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> --------- Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent 2619da7 commit 274719d

File tree

3 files changed

+18
-27
lines changed

3 files changed

+18
-27
lines changed

src/lib/support/Pool.h

+2-14
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,6 @@ class StaticAllocatorBitmap : public internal::StaticAllocatorBase
104104
std::atomic<tBitChunkType> * mUsage;
105105
};
106106

107-
template <class T>
108-
class PoolCommon
109-
{
110-
public:
111-
template <typename... Args>
112-
void ResetObject(T * element, Args &&... args)
113-
{
114-
element->~T();
115-
new (element) T(std::forward<Args>(args)...);
116-
}
117-
};
118-
119107
template <typename T, typename Function>
120108
class LambdaProxy
121109
{
@@ -211,7 +199,7 @@ struct HeapObjectList : HeapObjectListNode
211199
* @tparam N a positive integer max number of elements the pool provides.
212200
*/
213201
template <class T, size_t N>
214-
class BitMapObjectPool : public internal::StaticAllocatorBitmap, public internal::PoolCommon<T>
202+
class BitMapObjectPool : public internal::StaticAllocatorBitmap
215203
{
216204
public:
217205
BitMapObjectPool() : StaticAllocatorBitmap(mData.mMemory, mUsage, N, sizeof(T)) {}
@@ -314,7 +302,7 @@ class HeapObjectPoolExitHandling
314302
* @tparam T type to be allocated.
315303
*/
316304
template <class T>
317-
class HeapObjectPool : public internal::Statistics, public internal::PoolCommon<T>, public HeapObjectPoolExitHandling
305+
class HeapObjectPool : public internal::Statistics, public HeapObjectPoolExitHandling
318306
{
319307
public:
320308
HeapObjectPool() {}

src/lib/support/PoolWrapper.h

+3-9
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ class PoolInterface
3333

3434
virtual ~PoolInterface() {}
3535

36-
virtual U * CreateObject(ConstructorArguments... args) = 0;
37-
virtual void ReleaseObject(U * element) = 0;
38-
virtual void ReleaseAll() = 0;
39-
virtual void ResetObject(U * element, ConstructorArguments... args) = 0;
36+
virtual U * CreateObject(ConstructorArguments... args) = 0;
37+
virtual void ReleaseObject(U * element) = 0;
38+
virtual void ReleaseAll() = 0;
4039

4140
template <typename Function>
4241
Loop ForEachActiveObject(Function && function)
@@ -82,11 +81,6 @@ class PoolProxy<T, N, M, std::tuple<U, ConstructorArguments...>> : public PoolIn
8281

8382
void ReleaseAll() override { Impl().ReleaseAll(); }
8483

85-
void ResetObject(U * element, ConstructorArguments... args) override
86-
{
87-
return Impl().ResetObject(static_cast<T *>(element), std::move(args)...);
88-
}
89-
9084
protected:
9185
Loop ForEachActiveObjectInner(void * context, typename PoolInterface<U, ConstructorArguments...>::Lambda lambda) override
9286
{

src/transport/UnauthenticatedSessionTable.h

+13-4
Original file line numberDiff line numberDiff line change
@@ -285,17 +285,26 @@ class UnauthenticatedSessionTable
285285
return CHIP_NO_ERROR;
286286
}
287287

288-
#if !CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
288+
#if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
289+
// permanent failure if heap was insufficient
290+
return CHIP_ERROR_NO_MEMORY;
291+
#else
289292
entryToUse = FindLeastRecentUsedEntry();
290-
#endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
293+
VerifyOrReturnError(entryToUse != nullptr, CHIP_ERROR_NO_MEMORY);
294+
295+
// Drop the least recent entry to allow for a new alloc.
296+
mEntries.ReleaseObject(entryToUse);
297+
entryToUse = mEntries.CreateObject(sessionRole, ephemeralInitiatorNodeID, config, *this);
298+
291299
if (entryToUse == nullptr)
292300
{
293-
return CHIP_ERROR_NO_MEMORY;
301+
// this is NOT expected: we freed an object to have space
302+
return CHIP_ERROR_INTERNAL;
294303
}
295304

296-
mEntries.ResetObject(entryToUse, sessionRole, ephemeralInitiatorNodeID, config, *this);
297305
entry = entryToUse;
298306
return CHIP_NO_ERROR;
307+
#endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
299308
}
300309

301310
CHECK_RETURN_VALUE UnauthenticatedSession * FindEntry(UnauthenticatedSession::SessionRole sessionRole,

0 commit comments

Comments
 (0)