22
22
#include < lib/support/CodeUtils.h>
23
23
#include < lib/support/Pool.h>
24
24
#include < messaging/ReliableMessageProtocolConfig.h>
25
+ #include < system/SystemConfig.h>
25
26
#include < system/TimeSource.h>
26
27
#include < transport/PeerMessageCounter.h>
27
28
#include < transport/Session.h>
@@ -34,8 +35,7 @@ namespace Transport {
34
35
* @brief
35
36
* An UnauthenticatedSession stores the binding of TransportAddress, and message counters.
36
37
*/
37
- class UnauthenticatedSession : public Session ,
38
- public ReferenceCounted<UnauthenticatedSession, NoopDeletor<UnauthenticatedSession>, 0 >
38
+ class UnauthenticatedSession : public Session , public ReferenceCounted <UnauthenticatedSession, UnauthenticatedSession, 0 >
39
39
{
40
40
public:
41
41
enum class SessionRole
@@ -44,6 +44,7 @@ class UnauthenticatedSession : public Session,
44
44
kResponder ,
45
45
};
46
46
47
+ protected:
47
48
UnauthenticatedSession (SessionRole sessionRole, NodeId ephemeralInitiatorNodeID, const ReliableMessageProtocolConfig & config) :
48
49
mEphemeralInitiatorNodeId (ephemeralInitiatorNodeID), mSessionRole (sessionRole),
49
50
mLastActivityTime (System::SystemClock().GetMonotonicTimestamp()),
@@ -52,6 +53,7 @@ class UnauthenticatedSession : public Session,
52
53
{}
53
54
~UnauthenticatedSession () override { VerifyOrDie (GetReferenceCount () == 0 ); }
54
55
56
+ public:
55
57
UnauthenticatedSession (const UnauthenticatedSession &) = delete ;
56
58
UnauthenticatedSession & operator =(const UnauthenticatedSession &) = delete ;
57
59
UnauthenticatedSession (UnauthenticatedSession &&) = delete ;
@@ -68,8 +70,8 @@ class UnauthenticatedSession : public Session,
68
70
69
71
Session::SessionType GetSessionType () const override { return Session::SessionType::kUnauthenticated ; }
70
72
71
- void Retain () override { ReferenceCounted<UnauthenticatedSession, NoopDeletor< UnauthenticatedSession> , 0 >::Retain (); }
72
- void Release () override { ReferenceCounted<UnauthenticatedSession, NoopDeletor< UnauthenticatedSession> , 0 >::Release (); }
73
+ void Retain () override { ReferenceCounted<UnauthenticatedSession, UnauthenticatedSession, 0 >::Retain (); }
74
+ void Release () override { ReferenceCounted<UnauthenticatedSession, UnauthenticatedSession, 0 >::Release (); }
73
75
74
76
bool IsActiveSession () const override { return true ; }
75
77
@@ -132,6 +134,23 @@ class UnauthenticatedSession : public Session,
132
134
133
135
PeerMessageCounter & GetPeerMessageCounter () { return mPeerMessageCounter ; }
134
136
137
+ static void Release (UnauthenticatedSession * obj)
138
+ {
139
+ // When using heap pools, we need to make sure to release ourselves back to
140
+ // the pool. When not using heap pools, we don't want the extra cost of the
141
+ // table pointer here, and the table itself handles entry reuse and cleanup
142
+ // as needed.
143
+ #if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
144
+ obj->ReleaseSelfToPool ();
145
+ #else
146
+ // Just do nothing.
147
+ #endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
148
+ }
149
+
150
+ #if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
151
+ virtual void ReleaseSelfToPool () = 0;
152
+ #endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
153
+
135
154
private:
136
155
const NodeId mEphemeralInitiatorNodeId ;
137
156
const SessionRole mSessionRole ;
@@ -142,6 +161,35 @@ class UnauthenticatedSession : public Session,
142
161
PeerMessageCounter mPeerMessageCounter ;
143
162
};
144
163
164
+ template <size_t kMaxSessionCount >
165
+ class UnauthenticatedSessionTable ;
166
+
167
+ namespace detail {
168
+
169
+ template <size_t kMaxSessionCount >
170
+ class UnauthenticatedSessionPoolEntry : public UnauthenticatedSession
171
+ {
172
+ public:
173
+ UnauthenticatedSessionPoolEntry (SessionRole sessionRole, NodeId ephemeralInitiatorNodeID,
174
+ const ReliableMessageProtocolConfig & config,
175
+ UnauthenticatedSessionTable<kMaxSessionCount > & sessionTable) :
176
+ UnauthenticatedSession (sessionRole, ephemeralInitiatorNodeID, config)
177
+ #if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
178
+ ,
179
+ mSessionTable (sessionTable)
180
+ #endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
181
+ {}
182
+
183
+ private:
184
+ #if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
185
+ virtual void ReleaseSelfToPool ();
186
+
187
+ UnauthenticatedSessionTable<kMaxSessionCount > & mSessionTable ;
188
+ #endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
189
+ };
190
+
191
+ } // namespace detail
192
+
145
193
/*
146
194
* @brief
147
195
* An table which manages UnauthenticatedSessions
@@ -153,7 +201,17 @@ template <size_t kMaxSessionCount>
153
201
class UnauthenticatedSessionTable
154
202
{
155
203
public:
156
- ~UnauthenticatedSessionTable () { mEntries .ReleaseAll (); }
204
+ ~UnauthenticatedSessionTable ()
205
+ {
206
+ #if !CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
207
+ // When not using heap pools, our entries never actually get released
208
+ // back to the pool (which lets us make the entries 4 bytes smaller by
209
+ // not storing a reference to the table in them) and we LRU reuse ones
210
+ // that have 0 refcount. But we should release them all here, to ensure
211
+ // that we don't hit fatal asserts in our pool destructor.
212
+ mEntries .ReleaseAll ();
213
+ #endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
214
+ }
157
215
158
216
/* *
159
217
* Get a responder session with the given ephemeralInitiatorNodeID. If the session doesn't exist in the cache, allocate a new
@@ -203,6 +261,9 @@ class UnauthenticatedSessionTable
203
261
}
204
262
205
263
private:
264
+ using EntryType = detail::UnauthenticatedSessionPoolEntry<kMaxSessionCount >;
265
+ friend EntryType;
266
+
206
267
/* *
207
268
* Allocates a new session out of the internal resource pool.
208
269
*
@@ -213,17 +274,23 @@ class UnauthenticatedSessionTable
213
274
CHIP_ERROR AllocEntry (UnauthenticatedSession::SessionRole sessionRole, NodeId ephemeralInitiatorNodeID,
214
275
const ReliableMessageProtocolConfig & config, UnauthenticatedSession *& entry)
215
276
{
216
- entry = mEntries .CreateObject (sessionRole, ephemeralInitiatorNodeID, config);
217
- if (entry != nullptr )
277
+ auto entryToUse = mEntries .CreateObject (sessionRole, ephemeralInitiatorNodeID, config, *this );
278
+ if (entryToUse != nullptr )
279
+ {
280
+ entry = entryToUse;
218
281
return CHIP_NO_ERROR;
282
+ }
219
283
220
- entry = FindLeastRecentUsedEntry ();
221
- if (entry == nullptr )
284
+ #if !CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
285
+ entryToUse = FindLeastRecentUsedEntry ();
286
+ #endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
287
+ if (entryToUse == nullptr )
222
288
{
223
289
return CHIP_ERROR_NO_MEMORY;
224
290
}
225
291
226
- mEntries .ResetObject (entry, sessionRole, ephemeralInitiatorNodeID, config);
292
+ mEntries .ResetObject (entryToUse, sessionRole, ephemeralInitiatorNodeID, config, *this );
293
+ entry = entryToUse;
227
294
return CHIP_NO_ERROR;
228
295
}
229
296
@@ -242,12 +309,12 @@ class UnauthenticatedSessionTable
242
309
return result;
243
310
}
244
311
245
- UnauthenticatedSession * FindLeastRecentUsedEntry ()
312
+ EntryType * FindLeastRecentUsedEntry ()
246
313
{
247
- UnauthenticatedSession * result = nullptr ;
314
+ EntryType * result = nullptr ;
248
315
System::Clock::Timestamp oldestTime = System::Clock::Timestamp (std::numeric_limits<System::Clock::Timestamp::rep>::max ());
249
316
250
- mEntries .ForEachActiveObject ([&](UnauthenticatedSession * entry) {
317
+ mEntries .ForEachActiveObject ([&](EntryType * entry) {
251
318
if (entry->GetReferenceCount () == 0 && entry->GetLastActivityTime () < oldestTime)
252
319
{
253
320
result = entry;
@@ -259,8 +326,18 @@ class UnauthenticatedSessionTable
259
326
return result;
260
327
}
261
328
262
- ObjectPool<UnauthenticatedSession, kMaxSessionCount > mEntries ;
329
+ void ReleaseEntry (EntryType * entry) { mEntries .ReleaseObject (entry); }
330
+
331
+ ObjectPool<EntryType, kMaxSessionCount > mEntries ;
263
332
};
264
333
334
+ #if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
335
+ template <size_t kMaxSessionCount >
336
+ void detail::UnauthenticatedSessionPoolEntry<kMaxSessionCount >::ReleaseSelfToPool()
337
+ {
338
+ mSessionTable .ReleaseEntry (this );
339
+ }
340
+ #endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
341
+
265
342
} // namespace Transport
266
343
} // namespace chip
0 commit comments