network: make DEFINE_NETDEV_CAST() assert on input and output

The macro used to return NULL if input was NULL or had the wrong type. Now
it asserts that input is nonnull and it has the expected type.

There are a few places where a missing or mismatched type was OK, but in a
majority of places, we would do both of the asserts. In various places we'd
only do one, but that was by ommission/mistake. So moving the asserts into the
macro allows us to save some lines.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2023-09-22 08:17:42 +02:00
parent 6c9935ba87
commit 117843fe95
23 changed files with 137 additions and 446 deletions
+7 -8
View File
@@ -182,14 +182,13 @@ extern const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX];
#define NETDEV_VTABLE(n) ((n)->kind != _NETDEV_KIND_INVALID ? netdev_vtable[(n)->kind] : NULL)
/* For casting a netdev into the various netdev kinds */
#define DEFINE_NETDEV_CAST(UPPERCASE, MixedCase) \
static inline MixedCase* UPPERCASE(NetDev *n) { \
if (_unlikely_(!n || \
n->kind != NETDEV_KIND_##UPPERCASE) || \
n->state == _NETDEV_STATE_INVALID) \
return NULL; \
\
return (MixedCase*) n; \
#define DEFINE_NETDEV_CAST(UPPERCASE, MixedCase) \
static inline MixedCase* UPPERCASE(NetDev *n) { \
assert(n); \
assert(n->kind == NETDEV_KIND_##UPPERCASE); \
assert(n->state < _NETDEV_STATE_MAX); \
\
return (MixedCase*) n; \
}
/* For casting the various netdev kinds into a netdev */