Skip to content

Commit 4db8794

Browse files
union
1 parent ec63c2d commit 4db8794

File tree

7 files changed

+13
-9
lines changed

7 files changed

+13
-9
lines changed

libudpard/udpard.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,21 @@ typedef struct udpard_bytes_mut_t
158158
void* data;
159159
} udpard_bytes_mut_t;
160160

161+
/// The size can be changed arbitrarily. This value is compromise between copy size and footprint and utility.
162+
#define UDPARD_USER_CONTEXT_PTR_COUNT 6
163+
161164
/// The library carries the user-provided context from inputs to outputs without interpreting it,
162165
/// allowing the application to associate its own data with various entities inside the library.
163-
typedef struct udpard_user_context_t
166+
typedef union udpard_user_context_t
164167
{
165-
void* data[6]; ///< The size is chosen rather arbitrarily; the library doesn't care. Can be configured ad-hoc.
168+
void* ptr[UDPARD_USER_CONTEXT_PTR_COUNT];
169+
unsigned char bytes[sizeof(void*) * UDPARD_USER_CONTEXT_PTR_COUNT];
166170
} udpard_user_context_t;
167171
#ifdef __cplusplus
168172
#define UDPARD_USER_CONTEXT_NULL \
169173
udpard_user_context_t {}
170174
#else
171-
#define UDPARD_USER_CONTEXT_NULL ((udpard_user_context_t){ .data = { NULL } })
175+
#define UDPARD_USER_CONTEXT_NULL ((udpard_user_context_t){ .ptr = { NULL } })
172176
#endif
173177

174178
/// Zeros if invalid/unset/unavailable.

tests/src/helpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static inline udpard_bytes_scattered_t make_scattered(const void* const data, co
6868
static inline udpard_user_context_t make_user_context(void* const obj)
6969
{
7070
udpard_user_context_t out = UDPARD_USER_CONTEXT_NULL;
71-
out.data[0] = obj;
71+
out.ptr[0] = obj;
7272
return out;
7373
}
7474

tests/src/test_e2e_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ constexpr udpard_tx_vtable_t tx_vtable{ .eject = &capture_tx_frame };
7272
// Feedback callback records completion.
7373
void record_feedback(udpard_tx_t*, const udpard_tx_feedback_t fb)
7474
{
75-
auto* st = static_cast<FeedbackState*>(fb.user.data[0]);
75+
auto* st = static_cast<FeedbackState*>(fb.user.ptr[0]);
7676
if (st != nullptr) {
7777
st->count++;
7878
st->success = fb.success;

tests/src/test_e2e_edge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ constexpr udpard_tx_vtable_t tx_vtable{ .eject = &capture_tx_frame };
5555

5656
void fb_record(udpard_tx_t*, const udpard_tx_feedback_t fb)
5757
{
58-
auto* st = static_cast<FbState*>(fb.user.data[0]);
58+
auto* st = static_cast<FbState*>(fb.user.ptr[0]);
5959
if (st != nullptr) {
6060
st->count++;
6161
st->success = fb.success;

tests/src/test_e2e_random.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ constexpr udpard_tx_vtable_t tx_vtable{ .eject = &capture_tx_frame };
107107

108108
void record_feedback(udpard_tx_t*, const udpard_tx_feedback_t fb)
109109
{
110-
auto* ctx = static_cast<Context*>(fb.user.data[0]);
110+
auto* ctx = static_cast<Context*>(fb.user.ptr[0]);
111111
if (ctx != nullptr) {
112112
if (fb.success) {
113113
ctx->reliable_feedback_success++;

tests/src/test_e2e_responses.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct FeedbackState
6363

6464
void record_feedback(udpard_tx_t*, const udpard_tx_feedback_t fb)
6565
{
66-
auto* st = static_cast<FeedbackState*>(fb.user.data[0]);
66+
auto* st = static_cast<FeedbackState*>(fb.user.ptr[0]);
6767
if (st != nullptr) {
6868
st->count++;
6969
st->success = fb.success;

tests/src/test_intrusive_tx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static bool eject_with_log(udpard_tx_t* const tx, udpard_tx_ejection_t* const ej
5858
static void record_feedback(udpard_tx_t* const tx, const udpard_tx_feedback_t fb)
5959
{
6060
(void)tx;
61-
feedback_state_t* const st = (feedback_state_t*)fb.user.data[0];
61+
feedback_state_t* const st = (feedback_state_t*)fb.user.ptr[0];
6262
if (st != NULL) {
6363
st->count++;
6464
st->last = fb;

0 commit comments

Comments
 (0)