summaryrefslogtreecommitdiffstats
path: root/src/bfs.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/bfs.h')
-rw-r--r--src/bfs.h16
1 files changed, 13 insertions, 3 deletions
diff --git a/src/bfs.h b/src/bfs.h
index 078feed..bb83df2 100644
--- a/src/bfs.h
+++ b/src/bfs.h
@@ -8,6 +8,9 @@
#ifndef BFS_H
#define BFS_H
+#include <assert.h> // For __GLIBC__
+#include <stddef.h> // For offsetof
+
// Standard versions
/** Possible __STDC_VERSION__ values. */
@@ -53,12 +56,19 @@ extern const char bfs_cflags[];
extern const char bfs_ldflags[];
extern const char bfs_ldlibs[];
-// Get __GLIBC__
-#include <assert.h>
-
// Fundamental utilities
/**
+ * Given `ptr = &t->member`, return `t`.
+ */
+#define container_of(ptr, type, member) \
+ (container_of_typecheck(ptr, type, member), \
+ (type *)((char *)ptr - offsetof(type, member)))
+
+#define container_of_typecheck(ptr, type, field) \
+ (void)sizeof(ptr - &((type *)NULL)->field)
+
+/**
* A preprocessor conditional.
*
* BFS_VA_IF(A)(B)(C) => B