Skip to content

Commit 38c45d2

Browse files
committed
use CAVL2_TO_OWNER
1 parent 910c3ae commit 38c45d2

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

lib/cavl2/cavl2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ static inline CAVL2_T* cavl2_trivial_factory(void* const user)
215215
///
216216
/// The result is undefined if the tree_node_ptr is not a valid pointer to the tree node. Check for NULL first.
217217
#define CAVL2_TO_OWNER(tree_node_ptr, owner_type, owner_tree_node_field) \
218-
((owner_type*) (void*) (((char*) (tree_node_ptr)) - offsetof(owner_type, owner_tree_node_field))) // NOLINT
218+
(((tree_node_ptr) == NULL) ? NULL : (owner_type*) (void*) (((char*) (tree_node_ptr)) - offsetof(owner_type, owner_tree_node_field))) // NOLINT
219219

220220
// ---------------------------------------- END OF PUBLIC API SECTION ----------------------------------------
221221
// ---------------------------------------- POLICE LINE DO NOT CROSS ----------------------------------------

libcanard/canard.c

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@
7272

7373
#define INITIAL_TOGGLE_STATE true
7474

75-
#define CONTAINER_OF(type, ptr, member) \
76-
((const type*) (((ptr) == NULL) ? NULL : (const void*) (((const char*) (ptr)) - offsetof(type, member))))
77-
#define MUTABLE_CONTAINER_OF(type, ptr, member) \
78-
((type*) (((ptr) == NULL) ? NULL : (void*) (((char*) (ptr)) - offsetof(type, member))))
79-
8075
// --------------------------------------------- TRANSFER CRC ---------------------------------------------
8176

8277
typedef uint16_t TransferCRC;
@@ -341,8 +336,8 @@ CANARD_PRIVATE ptrdiff_t txAVLPriorityPredicate( //
341336
{
342337
typedef struct CanardTxQueueItem TxItem;
343338

344-
const TxItem* const target = CONTAINER_OF(TxItem, user_reference, priority_base);
345-
const TxItem* const other = CONTAINER_OF(TxItem, node, priority_base);
339+
const TxItem* const target = CAVL2_TO_OWNER(user_reference, TxItem, priority_base);
340+
const TxItem* const other = CAVL2_TO_OWNER(node, TxItem, priority_base);
346341
CANARD_ASSERT((target != NULL) && (other != NULL));
347342
return (target->frame.extended_can_id >= other->frame.extended_can_id) ? +1 : -1;
348343
}
@@ -357,8 +352,8 @@ CANARD_PRIVATE ptrdiff_t txAVLDeadlinePredicate( //
357352
{
358353
typedef struct CanardTxQueueItem TxItem;
359354

360-
const TxItem* const target = CONTAINER_OF(TxItem, user_reference, deadline_base);
361-
const TxItem* const other = CONTAINER_OF(TxItem, node, deadline_base);
355+
const TxItem* const target = CAVL2_TO_OWNER(user_reference, TxItem, deadline_base);
356+
const TxItem* const other = CAVL2_TO_OWNER(node, TxItem, deadline_base);
362357
CANARD_ASSERT((target != NULL) && (other != NULL));
363358
return (target->tx_deadline_usec >= other->tx_deadline_usec) ? +1 : -1;
364359
}
@@ -638,7 +633,7 @@ CANARD_PRIVATE size_t txFlushExpiredTransfers(struct CanardTxQueue* const
638633

639634
size_t count = 0;
640635
struct CanardTreeNode* tx_node = cavl2_min(que->deadline_root);
641-
struct CanardTxQueueItem* tx_item = MUTABLE_CONTAINER_OF(struct CanardTxQueueItem, tx_node, deadline_base);
636+
struct CanardTxQueueItem* tx_item = CAVL2_TO_OWNER(tx_node, struct CanardTxQueueItem, deadline_base);
642637
while (NULL != tx_item)
643638
{
644639
if (now_usec <= tx_item->tx_deadline_usec)
@@ -648,7 +643,7 @@ CANARD_PRIVATE size_t txFlushExpiredTransfers(struct CanardTxQueue* const
648643
count += txPopAndFreeTransfer(que, ins, tx_item, true); // drop the whole transfer
649644

650645
tx_node = cavl2_min(que->deadline_root);
651-
tx_item = MUTABLE_CONTAINER_OF(struct CanardTxQueueItem, tx_node, deadline_base);
646+
tx_item = CAVL2_TO_OWNER(tx_node, struct CanardTxQueueItem, deadline_base);
652647
}
653648
return count;
654649
}
@@ -1113,7 +1108,7 @@ rxSubscriptionPredicateOnPortID(const void* user_reference, // NOSONAR Cavl API
11131108
{
11141109
CANARD_ASSERT((user_reference != NULL) && (node != NULL));
11151110
const CanardPortID sought = *((const CanardPortID*) user_reference);
1116-
const CanardPortID other = CONTAINER_OF(struct CanardRxSubscription, node, base)->port_id;
1111+
const CanardPortID other = CAVL2_TO_OWNER(node, struct CanardRxSubscription, base)->port_id;
11171112
static const int8_t NegPos[2] = {-1, +1};
11181113
// Clang-Tidy mistakenly identifies a narrowing cast to int8_t here, which is incorrect.
11191114
return (sought == other) ? 0 : NegPos[sought > other]; // NOLINT no narrowing conversion is taking place here
@@ -1124,7 +1119,7 @@ rxSubscriptionPredicateOnStruct(const void* user_reference, // NOSONAR Cavl API
11241119
const struct CanardTreeNode* const node)
11251120
{
11261121
return rxSubscriptionPredicateOnPortID( //
1127-
&MUTABLE_CONTAINER_OF(struct CanardRxSubscription, user_reference, base)->port_id,
1122+
&CAVL2_TO_OWNER(user_reference, struct CanardRxSubscription, base)->port_id,
11281123
node);
11291124
}
11301125

@@ -1238,7 +1233,7 @@ struct CanardTxQueueItem* canardTxPeek(const struct CanardTxQueue* const que)
12381233
if (que != NULL)
12391234
{
12401235
struct CanardTreeNode* const priority_node = cavl2_min(que->priority_root);
1241-
out = MUTABLE_CONTAINER_OF(struct CanardTxQueueItem, priority_node, priority_base);
1236+
out = CAVL2_TO_OWNER(priority_node, struct CanardTxQueueItem, priority_base);
12421237
}
12431238
return out;
12441239
}
@@ -1348,11 +1343,10 @@ int8_t canardRxAccept(struct CanardInstance* const ins,
13481343
// This is the reason the function has a logarithmic time complexity of the number of subscriptions.
13491344
// Note also that this one of the two variable-complexity operations in the RX pipeline; the other one
13501345
// is memcpy(). Excepting these two cases, the entire RX pipeline contains neither loops nor recursion.
1351-
struct CanardTreeNode* const sub_node = cavl2_find(ins->rx_subscriptions[(size_t) model.transfer_kind],
1346+
struct CanardTreeNode* const sub_node = cavl2_find(ins->rx_subscriptions[(size_t) model.transfer_kind],
13521347
&model.port_id,
13531348
&rxSubscriptionPredicateOnPortID);
1354-
struct CanardRxSubscription* const sub =
1355-
MUTABLE_CONTAINER_OF(struct CanardRxSubscription, sub_node, base);
1349+
struct CanardRxSubscription* const sub = CAVL2_TO_OWNER(sub_node, struct CanardRxSubscription, base);
13561350
if (out_subscription != NULL)
13571351
{
13581352
*out_subscription = sub; // Expose selected instance to the caller.
@@ -1435,7 +1429,7 @@ int8_t canardRxUnsubscribe(struct CanardInstance* const ins,
14351429
cavl2_find(ins->rx_subscriptions[tk], &port_id_mutable, &rxSubscriptionPredicateOnPortID);
14361430
if (sub_node != NULL)
14371431
{
1438-
struct CanardRxSubscription* const sub = MUTABLE_CONTAINER_OF(struct CanardRxSubscription, sub_node, base);
1432+
struct CanardRxSubscription* const sub = CAVL2_TO_OWNER(sub_node, struct CanardRxSubscription, base);
14391433
cavl2_remove(&ins->rx_subscriptions[tk], sub_node);
14401434
CANARD_ASSERT(sub->port_id == port_id);
14411435
out = 1;
@@ -1475,7 +1469,7 @@ int8_t canardRxGetSubscription(struct CanardInstance* const ins,
14751469
cavl2_find(ins->rx_subscriptions[tk], &port_id_mutable, &rxSubscriptionPredicateOnPortID);
14761470
if (sub_node != NULL)
14771471
{
1478-
struct CanardRxSubscription* const sub = MUTABLE_CONTAINER_OF(struct CanardRxSubscription, sub_node, base);
1472+
struct CanardRxSubscription* const sub = CAVL2_TO_OWNER(sub_node, struct CanardRxSubscription, base);
14791473
CANARD_ASSERT(sub->port_id == port_id);
14801474
if (out_subscription != NULL)
14811475
{

0 commit comments

Comments
 (0)