Skip to content

Commit 8048508

Browse files
hlefnwf
authored andcommitted
tcpip: use our function wrapper everywhere.
David's `FunctionWrapper` is much more space-efficient than a normal lambda. These changes make us gain about 1.3 KB. Signed-off-by: Hugo Lefeuvre <[email protected]>
1 parent 7c4f065 commit 8048508

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

lib/tcpip/network_wrapper.cc

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <NetAPI.h>
1414
#include <debug.hh>
1515
#include <endianness.hh>
16-
#include <function_wrapper.hh>
1716
#include <limits>
1817
#include <locks.hh>
1918
#include <platform-ethernet.hh>
@@ -108,7 +107,8 @@ namespace
108107
* stack.
109108
*/
110109
template<bool IsCloseOperation = false>
111-
int with_sealed_socket(auto operation, Sealed<SealedSocket> sealedSocket)
110+
int with_sealed_socket(FunctionWrapper<int(SealedSocket *socket)> operation,
111+
Sealed<SealedSocket> sealedSocket)
112112
{
113113
return with_restarting_checks(
114114
[&]() {
@@ -145,8 +145,8 @@ namespace
145145
* (which must not try to release the lock after the lock has been
146146
* deallocated).
147147
*/
148-
int with_sealed_socket(Timeout *timeout,
149-
auto operation,
148+
int with_sealed_socket(Timeout *timeout,
149+
FunctionWrapper<int(SealedSocket *socket)> operation,
150150
Sealed<SealedSocket> sealedSocket)
151151
{
152152
return with_sealed_socket(
@@ -248,10 +248,10 @@ namespace
248248
* timeout on the socket (for the direction identified by `directionFlag`),
249249
* calls `fn`, and then updates the timeout with the number of ticks taken.
250250
*/
251-
auto with_freertos_timeout(Timeout *timeout,
252-
FreeRTOS_Socket_t *socket,
253-
auto directionFlag,
254-
auto &&fn)
251+
auto with_freertos_timeout(Timeout *timeout,
252+
FreeRTOS_Socket_t *socket,
253+
auto directionFlag,
254+
FunctionWrapper<int(void)> fn)
255255
{
256256
auto startTick = thread_systemtick_get();
257257
TickType_t remaining = timeout->remaining;
@@ -468,8 +468,9 @@ Socket network_socket_create_and_bind(Timeout *timeout,
468468
bool isListening,
469469
uint16_t maxConnections)
470470
{
471-
return with_restarting_checks(
472-
[&]() -> Socket {
471+
Socket ret = nullptr;
472+
with_restarting_checks(
473+
[&]() {
473474
// TODO: This should have nice RAII wrappers!
474475
// Add the socket lock to the linked list and make sure that the RAII
475476
// wrapper removes it from there.
@@ -478,7 +479,7 @@ Socket network_socket_create_and_bind(Timeout *timeout,
478479
if (socketWrapper == nullptr)
479480
{
480481
Debug::log("Failed to allocate socket wrapper.");
481-
return nullptr;
482+
return -ENOMEM;
482483
}
483484

484485
// Set the socket epoch
@@ -498,7 +499,7 @@ Socket network_socket_create_and_bind(Timeout *timeout,
498499
// allocated the token with this malloc capability. Same for
499500
// other calls to `token_obj_destroy` in this function.
500501
token_obj_destroy(mallocCapability, socket_key(), sealedSocket);
501-
return nullptr;
502+
return -ENOMEM;
502503
}
503504
socketWrapper->socket = socket;
504505

@@ -518,7 +519,7 @@ Socket network_socket_create_and_bind(Timeout *timeout,
518519
close_socket_retry(timeout, socket);
519520
// Cannot fail, see above.
520521
token_obj_destroy(mallocCapability, socket_key(), sealedSocket);
521-
return nullptr;
522+
return -ENOMEM;
522523
}
523524

524525
// Acquire the lock until we complete the bind to ensure that
@@ -550,7 +551,7 @@ Socket network_socket_create_and_bind(Timeout *timeout,
550551
close_socket_retry(timeout, socket);
551552
token_obj_destroy(
552553
mallocCapability, socket_key(), sealedSocket);
553-
return nullptr;
554+
return -EAGAIN;
554555
}
555556

556557
if (isListening)
@@ -578,7 +579,7 @@ Socket network_socket_create_and_bind(Timeout *timeout,
578579
close_socket_retry(timeout, socket);
579580
token_obj_destroy(
580581
mallocCapability, socket_key(), sealedSocket);
581-
return nullptr;
582+
return -EAGAIN;
582583
}
583584

584585
FreeRTOS_setsockopt(
@@ -595,13 +596,15 @@ Socket network_socket_create_and_bind(Timeout *timeout,
595596
Debug::log("Failed to add socket to the socket reset list.");
596597
close_socket_retry(timeout, socket);
597598
token_obj_destroy(mallocCapability, socket_key(), sealedSocket);
598-
return nullptr;
599+
return -EAGAIN;
599600
}
600601

601602
c.release();
602-
return sealedSocket;
603+
ret = sealedSocket;
604+
return 0;
603605
},
604-
static_cast<Socket>(nullptr) /* return nullptr if we are restarting */);
606+
-EAGAIN /* this will be returned if we are restarting */);
607+
return ret;
605608
}
606609

607610
Socket network_socket_accept_tcp(Timeout *timeout,

lib/tcpip/tcpip-internal.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#pragma once
55
#include <FreeRTOS_IP.h>
66
#include <ds/linked_list.h>
7+
#include <function_wrapper.hh>
78
#include <locks.hh>
89
#include <unwind.h>
910

@@ -106,9 +107,10 @@ extern std::atomic<uint8_t> userThreadCount;
106107
* thread, see below) should go through this unless it manipulates
107108
* `userThreadCount` and sets up the error handler manually.
108109
*/
109-
auto with_restarting_checks(auto operation, auto errorValue)
110+
int with_restarting_checks(FunctionWrapper<int(void)> operation,
111+
auto errorValue)
110112
{
111-
auto ret = errorValue;
113+
int ret = errorValue;
112114

113115
on_error(
114116
[&]() {
@@ -145,9 +147,10 @@ auto with_restarting_checks(auto operation, auto errorValue)
145147
* when not to call - if it does call at an inapproriate time during reset,
146148
* this is a firewall bug. Thus, do not check `restartState` here.
147149
*/
148-
auto with_restarting_checks_driver(auto operation, auto errorValue)
150+
int with_restarting_checks_driver(FunctionWrapper<int(void)> operation,
151+
auto errorValue)
149152
{
150-
auto ret = errorValue;
153+
int ret = errorValue;
151154

152155
on_error(
153156
[&]() {

0 commit comments

Comments
 (0)