Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys Add container_of to libkern.
details: https://anonhg.NetBSD.org/src/rev/bc0dab24ea0e
branches: trunk
changeset: 337580:bc0dab24ea0e
user: riastradh <riastradh%NetBSD.org@localhost>
date: Mon Apr 20 15:22:17 2015 +0000
description:
Add container_of to libkern.
Given x = &c->f, container_of(x, T, f) yields c, where T is the type
of c.
Discussed on tech-kern a while ago:
https://mail-index.netbsd.org/tech-kern/2013/03/21/msg015131.html
diffstat:
sys/external/bsd/common/include/linux/kernel.h | 14 ++---------
sys/lib/libkern/libkern.h | 29 +++++++++++++++++++++++++-
2 files changed, 31 insertions(+), 12 deletions(-)
diffs (78 lines):
diff -r b83111b60da4 -r bc0dab24ea0e sys/external/bsd/common/include/linux/kernel.h
--- a/sys/external/bsd/common/include/linux/kernel.h Mon Apr 20 15:18:59 2015 +0000
+++ b/sys/external/bsd/common/include/linux/kernel.h Mon Apr 20 15:22:17 2015 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: kernel.h,v 1.6 2015/02/25 15:50:16 riastradh Exp $ */
+/* $NetBSD: kernel.h,v 1.7 2015/04/20 15:22:18 riastradh Exp $ */
/*-
* Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -37,6 +37,8 @@
#include <sys/param.h>
#include <sys/systm.h>
+#include <lib/libkern/libkern.h>
+
#define oops_in_progress (panicstr != NULL)
#define IS_ENABLED(option) 0 /* XXX Hmm... */
@@ -94,16 +96,6 @@
#define upper_32_bits(X) ((uint32_t) (((X) >> 16) >> 16))
#define lower_32_bits(X) ((uint32_t) ((X) & 0xffffffffUL))
-/*
- * Given x = &c->f, container_of(x, T, f) gives us back c, where T is
- * the type of c.
- */
-#define container_of(PTR, TYPE, FIELD) \
- ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD) + \
- 0*sizeof((PTR) - \
- &((TYPE *)(((char *)(PTR)) - \
- offsetof(TYPE, FIELD)))->FIELD)))
-
#define ARRAY_SIZE(ARRAY) __arraycount(ARRAY)
#define swap(X, Y) do \
diff -r b83111b60da4 -r bc0dab24ea0e sys/lib/libkern/libkern.h
--- a/sys/lib/libkern/libkern.h Mon Apr 20 15:18:59 2015 +0000
+++ b/sys/lib/libkern/libkern.h Mon Apr 20 15:22:17 2015 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: libkern.h,v 1.117 2015/01/16 18:36:31 christos Exp $ */
+/* $NetBSD: libkern.h,v 1.118 2015/04/20 15:22:17 riastradh Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -309,6 +309,33 @@
#endif
#endif
+/*
+ * Return the container of an embedded struct. Given x = &c->f,
+ * container_of(x, T, f) yields c, where T is the type of c. Example:
+ *
+ * struct foo { ... };
+ * struct bar {
+ * int b_x;
+ * struct foo b_foo;
+ * ...
+ * };
+ *
+ * struct bar b;
+ * struct foo *fp = b.b_foo;
+ *
+ * Now we can get at b from fp by:
+ *
+ * struct bar *bp = container_of(fp, struct bar, b_foo);
+ *
+ * The 0*sizeof((PTR) - ...) causes the compiler to warn if the type of
+ * *fp does not match the type of struct bar::b_foo.
+ */
+#define container_of(PTR, TYPE, FIELD) \
+ ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD) + \
+ 0*sizeof((PTR) - \
+ &((TYPE *)(((char *)(PTR)) - \
+ offsetof(TYPE, FIELD)))->FIELD)))
+
#define MTPRNG_RLEN 624
struct mtprng_state {
unsigned int mt_idx;
Home |
Main Index |
Thread Index |
Old Index