diff options
| author | Tyge Løvset <[email protected]> | 2022-10-31 10:29:18 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-10-31 10:29:18 +0100 |
| commit | b648d1bb80958fffed9ad27ee06b2d662d2d67bd (patch) | |
| tree | 2010c42c8866cf23b7c36827e3fc11b35d5dce20 /include/stc | |
| parent | c73af10e4025e5fc9956d868c8682c08f323bfc6 (diff) | |
| download | STC-modified-b648d1bb80958fffed9ad27ee06b2d662d2d67bd.tar.gz STC-modified-b648d1bb80958fffed9ad27ee06b2d662d2d67bd.zip | |
Improved typesafe c_container_of() macro.
Diffstat (limited to 'include/stc')
| -rw-r--r-- | include/stc/ccommon.h | 12 | ||||
| -rw-r--r-- | include/stc/clist.h | 10 |
2 files changed, 9 insertions, 13 deletions
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 65d90987..21a66923 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -62,14 +62,10 @@ #define c_static_assert(cond) \ typedef char c_paste(_static_assert_line_, __LINE__)[(cond) ? 1 : -1] #define c_unchecked_container_of(ptr, type, member) \ - ((type *)((char *)(ptr) - offsetof(type, member))) -#if __STDC_VERSION__ >= 202300L || defined STC_CHECKED_CONTAINER_OF -# define c_container_of(ptr, type, member) \ - (((type *)((char *)(ptr) - offsetof(type, member))) + \ - ((typeof(ptr))0 != (typeof(&((type *)0)->member))0)) -#else -# define c_container_of(p,t,m) c_unchecked_container_of(p,t,m) -#endif + ((type*)((char*)(ptr) - offsetof(type, member))) +#define c_container_of(p, T, m) \ + c_unchecked_container_of((p) + sizeof((p)==&((T*)0)->m) - sizeof(1==1), T, m) + #ifndef __cplusplus # define c_alloc(T) c_malloc(sizeof(T)) # define c_alloc_n(T, n) c_malloc(sizeof(T)*(n)) diff --git a/include/stc/clist.h b/include/stc/clist.h index 5b452fd7..019698ed 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -64,7 +64,7 @@ SELF##_value value; \ } -#define clist_node_(vp) c_unchecked_container_of(vp, _cx_node, value) +#define _clist_tonode(vp) c_container_of(vp, _cx_node, value) _c_clist_types(clist_VOID, int); _c_clist_complete_types(clist_VOID, dummy); @@ -166,7 +166,7 @@ _cx_memb(_end)(const _cx_self* self) STC_INLINE void _cx_memb(_next)(_cx_iter* it) { - _cx_node* node = it->prev = clist_node_(it->ref); + _cx_node* node = it->prev = _clist_tonode(it->ref); it->ref = (node == *it->_last ? NULL : &node->next->value); } @@ -324,7 +324,7 @@ _cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value) { STC_DEF _cx_iter _cx_memb(_erase_at)(_cx_self* self, _cx_iter it) { - _cx_node *node = clist_node_(it.ref); + _cx_node *node = _clist_tonode(it.ref); it.ref = (node == self->last) ? NULL : &node->next->value; _cx_memb(_erase_node_after)(self, it.prev); return it; @@ -333,7 +333,7 @@ _cx_memb(_erase_at)(_cx_self* self, _cx_iter it) { STC_DEF _cx_iter _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2) { if (!it1.ref) return it2; - _cx_node *node = it1.prev, *end = it2.ref ? clist_node_(it2.ref) : NULL, + _cx_node *node = it1.prev, *end = it2.ref ? _clist_tonode(it2.ref) : NULL, *done = end ? end : self->last->next; if (node != end) do { _cx_memb(_erase_node_after)(self, node); @@ -392,7 +392,7 @@ _cx_memb(_split_off)(_cx_self* self, _cx_iter it1, _cx_iter it2) { _cx_node *p1 = it1.prev, *p2 = it2.ref ? it2.prev : self->last; p1->next = p2->next; - p2->next = clist_node_(it1.ref); + p2->next = _clist_tonode(it1.ref); if (self->last == p2) self->last = (p1 == p2) ? NULL : p1; cx.last = p2; |
