diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-04-11 09:35:22 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-04-11 09:35:22 -0600 |
commit | 15989609ffafc5b5eef4dbde49419842a8b249fd (patch) | |
tree | e164d337e199923d1f77148a56d909af37549f14 | |
parent | 72028362733820bbd22fd6bbe84c432110329739 (diff) |
libmisc: LM_CAST_FIELD_TO_STRUCT: Allow the pointer to be NULL
-rw-r--r-- | libcr_ipc/rwmutex.c | 5 | ||||
-rw-r--r-- | libmisc/include/libmisc/macro.h | 11 |
2 files changed, 8 insertions, 8 deletions
diff --git a/libcr_ipc/rwmutex.c b/libcr_ipc/rwmutex.c index 506459a..4c5da81 100644 --- a/libcr_ipc/rwmutex.c +++ b/libcr_ipc/rwmutex.c @@ -52,9 +52,8 @@ void cr_rwmutex_rlock(cr_rwmutex_t *mu) { lm_sll_pop_from_front(&mu->waiters); mu->nreaders++; mu->locked = true; - struct cr_rwmutex_waiter *waiter = mu->waiters.front - ? lm_sll_node_cast(struct cr_rwmutex_waiter, mu->waiters.front) - : NULL; + struct cr_rwmutex_waiter *waiter = + lm_sll_node_cast(struct cr_rwmutex_waiter, mu->waiters.front); if (waiter && waiter->is_reader) { assert(mu->unpausing); cr_unpause(waiter->cid); diff --git a/libmisc/include/libmisc/macro.h b/libmisc/include/libmisc/macro.h index 4591b68..6cb15fb 100644 --- a/libmisc/include/libmisc/macro.h +++ b/libmisc/include/libmisc/macro.h @@ -18,11 +18,12 @@ #define LM_ARRAY_LEN(ary) (sizeof(ary)/sizeof((ary)[0])) -#define LM_CAST_FIELD_TO_STRUCT(STRUCT_TYP, FIELD_NAME, PTR_TO_FIELD) ({ \ - /* The _fptr assignment is to get the compiler to do type checking. */ \ - typeof(((STRUCT_TYP *)NULL)->FIELD_NAME) *_fptr = (PTR_TO_FIELD); \ - assert(_fptr); \ - ((STRUCT_TYP*)( ((void*)_fptr) - offsetof(STRUCT_TYP, FIELD_NAME))); \ +#define LM_CAST_FIELD_TO_STRUCT(STRUCT_TYP, FIELD_NAME, PTR_TO_FIELD) ({ \ + /* The _fptr assignment is to get the compiler to do type checking. */ \ + typeof(((STRUCT_TYP *)NULL)->FIELD_NAME) *_fptr = (PTR_TO_FIELD); \ + _fptr \ + ? ((STRUCT_TYP*)( ((void*)_fptr) - offsetof(STRUCT_TYP, FIELD_NAME))) \ + : NULL; \ }) /* numeric */ |