Skip to content

Commit 97b2c7c

Browse files
pass ejection by pointer
1 parent 3a48b90 commit 97b2c7c

File tree

10 files changed

+52
-53
lines changed

10 files changed

+52
-53
lines changed

libudpard/udpard.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,25 +1251,23 @@ static void tx_eject_pending_frames(udpard_tx_t* const self, const udpard_us_t n
12511251
const tx_frame_t* const frame = tr->cursor[ifindex];
12521252
tx_frame_t* const frame_next = frame->next;
12531253
const bool last_attempt = !cavl2_is_inserted(self->index_staged, &tr->index_staged);
1254-
const bool last_frame = frame_next == NULL; // if not last attempt we will have to rewind to head.
1255-
const udpard_tx_ejection_t ejection = {
1256-
.now = now,
1257-
.deadline = tr->deadline,
1258-
.iface_index = ifindex,
1259-
.dscp = self->dscp_value_per_priority[tr->priority],
1260-
.destination = tr->destination[ifindex],
1261-
.datagram = tx_frame_view(frame),
1262-
.user = tr->user,
1263-
};
1264-
if (!self->vtable->eject(self, ejection)) { // The easy case -- no progress was made at this time;
1265-
break; // don't change anything, just try again later as-is
1254+
const bool last_frame = frame_next == NULL; // if not last attempt we will have to rewind to head.
1255+
udpard_tx_ejection_t ejection = { .now = now,
1256+
.deadline = tr->deadline,
1257+
.iface_index = ifindex,
1258+
.dscp = self->dscp_value_per_priority[tr->priority],
1259+
.destination = tr->destination[ifindex],
1260+
.datagram = tx_frame_view(frame),
1261+
.user = tr->user };
1262+
if (!self->vtable->eject(self, &ejection)) { // The easy case -- no progress was made at this time;
1263+
break; // don't change anything, just try again later as-is
12661264
}
12671265

12681266
// Frame ejected successfully. Update the transfer state to get ready for the next frame.
12691267
if (last_attempt) { // no need to keep frames that we will no longer use; free early to reduce pressure
12701268
UDPARD_ASSERT(tr->head[ifindex] == tr->cursor[ifindex]);
12711269
tr->head[ifindex] = frame_next;
1272-
udpard_tx_refcount_dec(ejection.datagram);
1270+
udpard_tx_refcount_dec(tx_frame_view(frame));
12731271
}
12741272
tr->cursor[ifindex] = frame_next;
12751273

libudpard/udpard.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ typedef struct udpard_tx_ejection_t
365365

366366
/// Specifies when the frame should be considered expired and dropped if not yet transmitted by then;
367367
/// it is optional to use depending on the implementation of the NIC driver (most traditional drivers ignore it).
368+
/// The library guarantees that now >= deadline at the time of ejection -- expired frames are purged beforehand.
368369
udpard_us_t deadline;
369370

370371
uint_fast8_t iface_index; ///< The interface index on which the datagram is to be transmitted.
@@ -385,7 +386,7 @@ typedef struct udpard_tx_vtable_t
385386
{
386387
/// Invoked from udpard_tx_poll() et al to push outgoing UDP datagrams into the socket/NIC driver.
387388
/// The callback must not mutate the TX pipeline (no udpard_tx_push/cancel/free).
388-
bool (*eject)(udpard_tx_t*, udpard_tx_ejection_t);
389+
bool (*eject)(udpard_tx_t*, udpard_tx_ejection_t*);
389390
} udpard_tx_vtable_t;
390391

391392
/// The application must create a single instance of this struct to manage the TX pipeline.

tests/src/test_e2e_api.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ void tx_refcount_free(void* const user, const size_t size, void* const payload)
4242
udpard_tx_refcount_dec(udpard_bytes_t{ .size = size, .data = payload });
4343
}
4444

45-
bool capture_tx_frame(udpard_tx_t* const tx, const udpard_tx_ejection_t ejection)
45+
bool capture_tx_frame(udpard_tx_t* const tx, udpard_tx_ejection_t* const ejection)
4646
{
4747
auto* frames = static_cast<std::vector<CapturedFrame>*>(tx->user);
4848
if (frames == nullptr) {
4949
return false;
5050
}
51-
udpard_tx_refcount_inc(ejection.datagram);
52-
void* const data = const_cast<void*>(ejection.datagram.data); // NOLINT(cppcoreguidelines-pro-type-const-cast)
53-
frames->push_back(CapturedFrame{ .datagram = { .size = ejection.datagram.size, .data = data },
54-
.iface_index = ejection.iface_index });
51+
udpard_tx_refcount_inc(ejection->datagram);
52+
void* const data = const_cast<void*>(ejection->datagram.data); // NOLINT(cppcoreguidelines-pro-type-const-cast)
53+
frames->push_back(CapturedFrame{ .datagram = { .size = ejection->datagram.size, .data = data },
54+
.iface_index = ejection->iface_index });
5555
return true;
5656
}
5757

tests/src/test_e2e_edge.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ void tx_refcount_free(void* const user, const size_t size, void* const payload)
3838
udpard_tx_refcount_dec(udpard_bytes_t{ .size = size, .data = payload });
3939
}
4040

41-
bool capture_tx_frame(udpard_tx_t* const tx, const udpard_tx_ejection_t ejection)
41+
bool capture_tx_frame(udpard_tx_t* const tx, udpard_tx_ejection_t* const ejection)
4242
{
4343
auto* frames = static_cast<std::vector<CapturedFrame>*>(tx->user);
4444
if (frames == nullptr) {
4545
return false;
4646
}
47-
udpard_tx_refcount_inc(ejection.datagram);
48-
void* const data = const_cast<void*>(ejection.datagram.data); // NOLINT(cppcoreguidelines-pro-type-const-cast)
49-
frames->push_back(CapturedFrame{ .datagram = { .size = ejection.datagram.size, .data = data },
50-
.iface_index = ejection.iface_index });
47+
udpard_tx_refcount_inc(ejection->datagram);
48+
void* const data = const_cast<void*>(ejection->datagram.data); // NOLINT(cppcoreguidelines-pro-type-const-cast)
49+
frames->push_back(CapturedFrame{ .datagram = { .size = ejection->datagram.size, .data = data },
50+
.iface_index = ejection->iface_index });
5151
return true;
5252
}
5353

tests/src/test_e2e_random.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,16 @@ void tx_refcount_free(void* const user, const size_t size, void* const payload)
9090
udpard_tx_refcount_dec(udpard_bytes_t{ .size = size, .data = payload });
9191
}
9292

93-
bool capture_tx_frame(udpard_tx_t* const tx, const udpard_tx_ejection_t ejection)
93+
bool capture_tx_frame(udpard_tx_t* const tx, udpard_tx_ejection_t* const ejection)
9494
{
9595
auto* frames = static_cast<std::vector<CapturedFrame>*>(tx->user);
9696
if (frames == nullptr) {
9797
return false;
9898
}
99-
udpard_tx_refcount_inc(ejection.datagram);
100-
void* const data = const_cast<void*>(ejection.datagram.data); // NOLINT(cppcoreguidelines-pro-type-const-cast)
101-
frames->push_back(CapturedFrame{ .datagram = { .size = ejection.datagram.size, .data = data },
102-
.iface_index = ejection.iface_index });
99+
udpard_tx_refcount_inc(ejection->datagram);
100+
void* const data = const_cast<void*>(ejection->datagram.data); // NOLINT(cppcoreguidelines-pro-type-const-cast)
101+
frames->push_back(CapturedFrame{ .datagram = { .size = ejection->datagram.size, .data = data },
102+
.iface_index = ejection->iface_index });
103103
return true;
104104
}
105105

tests/src/test_e2e_responses.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ void tx_refcount_free(void* const user, const size_t size, void* const payload)
2828
udpard_tx_refcount_dec(udpard_bytes_t{ .size = size, .data = payload });
2929
}
3030

31-
bool capture_tx_frame(udpard_tx_t* const tx, const udpard_tx_ejection_t ejection)
31+
bool capture_tx_frame(udpard_tx_t* const tx, udpard_tx_ejection_t* const ejection)
3232
{
3333
auto* frames = static_cast<std::vector<CapturedFrame>*>(tx->user);
3434
if (frames == nullptr) {
3535
return false;
3636
}
37-
udpard_tx_refcount_inc(ejection.datagram);
38-
void* const data = const_cast<void*>(ejection.datagram.data); // NOLINT
39-
frames->push_back(CapturedFrame{ .datagram = { .size = ejection.datagram.size, .data = data },
40-
.iface_index = ejection.iface_index });
37+
udpard_tx_refcount_inc(ejection->datagram);
38+
void* const data = const_cast<void*>(ejection->datagram.data); // NOLINT
39+
frames->push_back(CapturedFrame{ .datagram = { .size = ejection->datagram.size, .data = data },
40+
.iface_index = ejection->iface_index });
4141
return true;
4242
}
4343

tests/src/test_integration_sockets.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ struct CapturedFrame
9999
// Callbacks
100100
// =====================================================================================================================
101101

102-
bool capture_frame(udpard_tx_t* const tx, const udpard_tx_ejection_t ejection)
102+
bool capture_frame(udpard_tx_t* const tx, udpard_tx_ejection_t* const ejection)
103103
{
104104
auto* frames = static_cast<std::vector<CapturedFrame>*>(tx->user);
105105
if (frames == nullptr) {
106106
return false;
107107
}
108108

109109
CapturedFrame frame{};
110-
frame.data.assign(static_cast<const uint8_t*>(ejection.datagram.data),
111-
static_cast<const uint8_t*>(ejection.datagram.data) + ejection.datagram.size);
112-
frame.iface_index = ejection.iface_index;
110+
frame.data.assign(static_cast<const uint8_t*>(ejection->datagram.data),
111+
static_cast<const uint8_t*>(ejection->datagram.data) + ejection->datagram.size);
112+
frame.iface_index = ejection->iface_index;
113113
frames->push_back(frame);
114114

115115
return true;

tests/src/test_intrusive_guards.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static udpard_mem_resource_t make_mem(void* const tag)
3232
return out;
3333
}
3434

35-
static bool eject_stub(udpard_tx_t* const tx, const udpard_tx_ejection_t ejection)
35+
static bool eject_stub(udpard_tx_t* const tx, udpard_tx_ejection_t* const ejection)
3636
{
3737
(void)tx;
3838
(void)ejection;

tests/src/test_intrusive_rx.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,38 +1603,38 @@ typedef struct
16031603
size_t captured_count;
16041604
} tx_fixture_t;
16051605

1606-
static bool tx_capture_ack(udpard_tx_t* const tx, const udpard_tx_ejection_t ejection)
1606+
static bool tx_capture_ack(udpard_tx_t* const tx, udpard_tx_ejection_t* const ejection)
16071607
{
16081608
tx_fixture_t* const self = (tx_fixture_t*)tx->user;
16091609
if ((self == NULL) || (self->captured_count >= (sizeof(self->captured) / sizeof(self->captured[0])))) {
16101610
return false;
16111611
}
1612-
udpard_tx_refcount_inc(ejection.datagram);
1612+
udpard_tx_refcount_inc(ejection->datagram);
16131613
meta_t meta = { 0 };
16141614
uint32_t frame_index = 0;
16151615
uint32_t frame_offset = 0;
16161616
uint32_t prefix_crc = 0;
16171617
udpard_bytes_t payload = { 0 };
1618-
const bool ok =
1619-
header_deserialize((udpard_bytes_mut_t){ .size = ejection.datagram.size, .data = (void*)ejection.datagram.data },
1620-
&meta,
1621-
&frame_index,
1622-
&frame_offset,
1623-
&prefix_crc,
1624-
&payload);
1618+
const bool ok = header_deserialize(
1619+
(udpard_bytes_mut_t){ .size = ejection->datagram.size, .data = (void*)ejection->datagram.data },
1620+
&meta,
1621+
&frame_index,
1622+
&frame_offset,
1623+
&prefix_crc,
1624+
&payload);
16251625
if (ok && (frame_index == 0U) && (frame_offset == 0U) && (payload.size == UDPARD_P2P_HEADER_BYTES)) {
16261626
const byte_t* const pl = (const byte_t*)payload.data;
16271627
if (pl[0] == P2P_KIND_ACK) {
16281628
ack_tx_info_t* const info = &self->captured[self->captured_count++];
16291629
info->priority = meta.priority;
16301630
info->transfer_id = meta.transfer_id;
16311631
info->topic_hash = meta.topic_hash;
1632-
info->destination = ejection.destination;
1632+
info->destination = ejection->destination;
16331633
(void)deserialize_u64(pl + 8U, &info->acked_topic_hash);
16341634
(void)deserialize_u64(pl + 16U, &info->acked_transfer_id);
16351635
}
16361636
}
1637-
udpard_tx_refcount_dec(ejection.datagram);
1637+
udpard_tx_refcount_dec(ejection->datagram);
16381638
return true;
16391639
}
16401640

tests/src/test_intrusive_tx.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static void noop_free(void* const user, const size_t size, void* const pointer)
3333
}
3434

3535
// Ejects with a configurable outcome.
36-
static bool eject_with_flag(udpard_tx_t* const tx, const udpard_tx_ejection_t ejection)
36+
static bool eject_with_flag(udpard_tx_t* const tx, udpard_tx_ejection_t* const ejection)
3737
{
3838
(void)ejection;
3939
eject_state_t* const st = (eject_state_t*)tx->user;
@@ -45,11 +45,11 @@ static bool eject_with_flag(udpard_tx_t* const tx, const udpard_tx_ejection_t ej
4545
}
4646

4747
// Records ejection timestamps for later inspection.
48-
static bool eject_with_log(udpard_tx_t* const tx, const udpard_tx_ejection_t ejection)
48+
static bool eject_with_log(udpard_tx_t* const tx, udpard_tx_ejection_t* const ejection)
4949
{
5050
eject_log_t* const st = (eject_log_t*)tx->user;
5151
if ((st != NULL) && (st->count < (sizeof(st->when) / sizeof(st->when[0])))) {
52-
st->when[st->count++] = ejection.now;
52+
st->when[st->count++] = ejection->now;
5353
}
5454
return true;
5555
}

0 commit comments

Comments
 (0)