summaryrefslogtreecommitdiffstats
path: root/src/config.h
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-03-28 14:18:08 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-03-29 12:26:43 -0400
commit135b98c26456adbfbc72fb12e4753ee0716b1f92 (patch)
tree992541d0f4b215dd44a6648db0f796a7a05da2dc /src/config.h
parente40855d3b98aa3f65c19608b3d8c70f54b714063 (diff)
downloadbfs-135b98c26456adbfbc72fb12e4753ee0716b1f92.tar.xz
list: New generic linked list API
Diffstat (limited to 'src/config.h')
-rw-r--r--src/config.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/config.h b/src/config.h
index 34ae11d..cee8511 100644
--- a/src/config.h
+++ b/src/config.h
@@ -147,6 +147,17 @@
*/
#define BFS_COUNTOF(array) (sizeof(array) / sizeof(0[array]))
+// BFS_CONTAINER_OF() helper
+static inline char *bfs_container_offset(char *ptr, ptrdiff_t offset, size_t unused) {
+ return ptr ? ptr - offset : NULL;
+}
+
+/**
+ * Move a pointer from a field to its outer struct.
+ */
+#define BFS_CONTAINER_OF(ptr, type, member) \
+ ((type *)bfs_container_offset((char *)(ptr), offsetof(type, member), sizeof((ptr) - &((type *)NULL)->member)))
+
// Lower bound on BFS_FLEX_SIZEOF()
#define BFS_FLEX_LB(type, member, length) (offsetof(type, member) + sizeof(((type *)NULL)->member[0]) * (length))