Skip to content

Commit eb10cc1

Browse files
committed
Optimise SocketStats
* Remove unnecessary virtual destructor. * Use inline empty functions when disabled. * Remove pointless `const`s from declarations.
1 parent 6677249 commit eb10cc1

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

UNITTESTS/stubs/SocketStats_Stub.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ int SocketStats::get_entry_position(const Socket *const reference_id)
2222
{
2323
return 0;
2424
}
25-
#endif
2625

2726
size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *stats, size_t count)
2827
{
@@ -62,3 +61,4 @@ void SocketStats::stats_update_recv_bytes(const Socket *const reference_id, size
6261
{
6362
return;
6463
}
64+
#endif

features/netsocket/SocketStats.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ int SocketStats::get_entry_position(const Socket *const reference_id)
3838
}
3939
return -1;
4040
}
41-
#endif
4241

4342
size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *stats, size_t count)
4443
{
4544
MBED_ASSERT(stats != NULL);
4645
size_t i = 0;
47-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
4846
memset(stats, 0, count * sizeof(mbed_stats_socket_t));
4947
_mutex->lock();
5048
for (uint32_t j = 0; j < count; j++) {
@@ -54,17 +52,11 @@ size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *stats, size_
5452
}
5553
}
5654
_mutex->unlock();
57-
#endif
5855
return i;
5956
}
6057

61-
SocketStats::SocketStats()
62-
{
63-
}
64-
6558
void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
6659
{
67-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
6860
_mutex->lock();
6961
if (get_entry_position(reference_id) >= 0) {
7062
// Duplicate entry
@@ -92,13 +84,10 @@ void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
9284
_stats[position].reference_id = (Socket *)reference_id;
9385
}
9486
_mutex->unlock();
95-
#endif
96-
return;
9787
}
9888

9989
void SocketStats::stats_update_socket_state(const Socket *const reference_id, socket_state state)
10090
{
101-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
10291
_mutex->lock();
10392
int position = get_entry_position(reference_id);
10493
if (position >= 0) {
@@ -108,53 +97,45 @@ void SocketStats::stats_update_socket_state(const Socket *const reference_id, so
10897
#endif
10998
}
11099
_mutex->unlock();
111-
#endif
112100
}
113101

114102
void SocketStats::stats_update_peer(const Socket *const reference_id, const SocketAddress &peer)
115103
{
116-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
117104
_mutex->lock();
118105
int position = get_entry_position(reference_id);
119106
if ((position >= 0) && (!_stats[position].peer)) {
120107
_stats[position].peer = peer;
121108
}
122109
_mutex->unlock();
123-
#endif
124110
}
125111

126112
void SocketStats::stats_update_proto(const Socket *const reference_id, nsapi_protocol_t proto)
127113
{
128-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
129114
_mutex->lock();
130115
int position = get_entry_position(reference_id);
131116
if (position >= 0) {
132117
_stats[position].proto = proto;
133118
}
134119
_mutex->unlock();
135-
#endif
136120
}
137121

138122
void SocketStats::stats_update_sent_bytes(const Socket *const reference_id, size_t sent_bytes)
139123
{
140-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
141124
_mutex->lock();
142125
int position = get_entry_position(reference_id);
143126
if ((position >= 0) && ((int32_t)sent_bytes > 0)) {
144127
_stats[position].sent_bytes += sent_bytes;
145128
}
146129
_mutex->unlock();
147-
#endif
148130
}
149131

150132
void SocketStats::stats_update_recv_bytes(const Socket *const reference_id, size_t recv_bytes)
151133
{
152-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
153134
_mutex->lock();
154135
int position = get_entry_position(reference_id);
155136
if ((position >= 0) && ((int32_t)recv_bytes > 0)) {
156137
_stats[position].recv_bytes += recv_bytes;
157138
}
158139
_mutex->unlock();
159-
#endif
160140
}
141+
#endif // MBED_CONF_NSAPI_SOCKET_STATS_ENABLED

features/netsocket/SocketStats.h

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,14 @@ class SocketStats {
6060
public:
6161

6262
#if !defined(DOXYGEN_ONLY)
63-
/** Create an socket statictics object
63+
/** Create a socket statistics object
6464
*
6565
* Application users must not create class objects.
6666
* Entities reporting network statistics create the class object.
6767
* Application can fetch network statistics using static `mbed_stats_socket_get_each` API
6868
* without creating an object.
6969
*/
70-
SocketStats();
71-
virtual ~SocketStats()
72-
{
73-
}
70+
constexpr SocketStats() = default;
7471
#endif
7572
/**
7673
* Fill the passed array of structures with the socket statistics for each created socket.
@@ -96,7 +93,7 @@ class SocketStats {
9693
* the socket creation count exceeds `MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT`.
9794
*
9895
*/
99-
void stats_new_socket_entry(const Socket *const reference_id);
96+
void stats_new_socket_entry(const Socket *reference_id);
10097

10198
/** Updates the state of the socket and records `tick_last_change`.
10299
* API used by socket (TCP or UDP) layers only, not to be used by application.
@@ -105,7 +102,7 @@ class SocketStats {
105102
* @param state Parameter to update the current state of the socket.
106103
*
107104
*/
108-
void stats_update_socket_state(const Socket *const reference_id, socket_state state);
105+
void stats_update_socket_state(const Socket *reference_id, socket_state state);
109106

110107
/** Update the peer information of the socket.
111108
* API used by socket (TCP or UDP) layers only, not to be used by application.
@@ -114,7 +111,7 @@ class SocketStats {
114111
* @param peer Parameter to update destination peer information.
115112
*
116113
*/
117-
void stats_update_peer(const Socket *const reference_id, const SocketAddress &peer);
114+
void stats_update_peer(const Socket *reference_id, const SocketAddress &peer);
118115

119116
/** Update socket protocol.
120117
* API used by socket (TCP or UDP) layers only, not to be used by application.
@@ -123,7 +120,7 @@ class SocketStats {
123120
* @param proto Parameter to update the protocol type of socket.
124121
*
125122
*/
126-
void stats_update_proto(const Socket *const reference_id, nsapi_protocol_t proto);
123+
void stats_update_proto(const Socket *reference_id, nsapi_protocol_t proto);
127124

128125
/** Update bytes sent on socket, which is cumulative count per socket.
129126
* API used by socket (TCP or UDP) layers only, not to be used by application.
@@ -132,7 +129,7 @@ class SocketStats {
132129
* @param sent_bytes Parameter to append bytes sent over the socket.
133130
*
134131
*/
135-
void stats_update_sent_bytes(const Socket *const reference_id, size_t sent_bytes);
132+
void stats_update_sent_bytes(const Socket *reference_id, size_t sent_bytes);
136133

137134
/** Update bytes received on socket, which is cumulative count per socket
138135
* API used by socket (TCP or UDP) layers only, not to be used by application.
@@ -141,7 +138,7 @@ class SocketStats {
141138
* @param recv_bytes Parameter to append bytes the socket receives.
142139
*
143140
*/
144-
void stats_update_recv_bytes(const Socket *const reference_id, size_t recv_bytes);
141+
void stats_update_recv_bytes(const Socket *reference_id, size_t recv_bytes);
145142

146143
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
147144
private:
@@ -154,9 +151,40 @@ class SocketStats {
154151
* @param reference_id ID to identify the socket in the data array.
155152
*
156153
*/
157-
int get_entry_position(const Socket *const reference_id);
154+
int get_entry_position(const Socket *reference_id);
158155
#endif
159156
#endif
160157
};
161158

159+
#if !MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
160+
inline size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *, size_t)
161+
{
162+
return 0;
163+
}
164+
165+
inline void SocketStats::stats_new_socket_entry(const Socket *)
166+
{
167+
}
168+
169+
inline void SocketStats::stats_update_socket_state(const Socket *, socket_state)
170+
{
171+
}
172+
173+
inline void SocketStats::stats_update_peer(const Socket *, const SocketAddress &)
174+
{
175+
}
176+
177+
inline void SocketStats::stats_update_proto(const Socket *, nsapi_protocol_t)
178+
{
179+
}
180+
181+
inline void SocketStats::stats_update_sent_bytes(const Socket *, size_t)
182+
{
183+
}
184+
185+
inline void SocketStats::stats_update_recv_bytes(const Socket *, size_t)
186+
{
187+
}
188+
#endif // !MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
189+
162190
#endif

0 commit comments

Comments
 (0)