summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-10-31 10:29:18 +0100
committerTyge Løvset <[email protected]>2022-10-31 10:29:18 +0100
commitb648d1bb80958fffed9ad27ee06b2d662d2d67bd (patch)
tree2010c42c8866cf23b7c36827e3fc11b35d5dce20
parentc73af10e4025e5fc9956d868c8682c08f323bfc6 (diff)
downloadSTC-modified-b648d1bb80958fffed9ad27ee06b2d662d2d67bd.tar.gz
STC-modified-b648d1bb80958fffed9ad27ee06b2d662d2d67bd.zip
Improved typesafe c_container_of() macro.
-rw-r--r--benchmarks/misc/sso_bench.cpp4
-rw-r--r--include/stc/ccommon.h12
-rw-r--r--include/stc/clist.h10
3 files changed, 11 insertions, 15 deletions
diff --git a/benchmarks/misc/sso_bench.cpp b/benchmarks/misc/sso_bench.cpp
index 7742700a..0fffef7a 100644
--- a/benchmarks/misc/sso_bench.cpp
+++ b/benchmarks/misc/sso_bench.cpp
@@ -19,7 +19,7 @@ using StdVec = std::vector<std::string>;
using StdSet = std::set<std::string>;
-static const int BENCHMARK_SIZE = 4000000;
+static const int BENCHMARK_SIZE = 2000000;
static const int MAX_STRING_SIZE = 50;
static const char CHARS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=+-";
using time_point = std::chrono::high_resolution_clock::time_point;
@@ -120,7 +120,7 @@ int main() {
csrandom(seed);
sum = 0, n = 0;
- std::cerr << "\nstrsize\tmsecs\tcset<cstr>, size=" << BENCHMARK_SIZE/16 << "\n";
+ std::cerr << "\nstrsize\tmsecs\tcsset<cstr>, size=" << BENCHMARK_SIZE/16 << "\n";
for (int strsize = 1; strsize <= MAX_STRING_SIZE; strsize += 2) {
StcSet set = StcSet_with_capacity(BENCHMARK_SIZE/16);
sum += benchmark(set, BENCHMARK_SIZE/16, strsize), ++n;
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;