Module Name: src Committed By: riastradh Date: Mon Apr 20 15:22:18 UTC 2015
Modified Files: src/sys/external/bsd/common/include/linux: kernel.h src/sys/lib/libkern: libkern.h Log Message: 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 To generate a diff of this commit: cvs rdiff -u -r1.6 -r1.7 src/sys/external/bsd/common/include/linux/kernel.h cvs rdiff -u -r1.117 -r1.118 src/sys/lib/libkern/libkern.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/external/bsd/common/include/linux/kernel.h diff -u src/sys/external/bsd/common/include/linux/kernel.h:1.6 src/sys/external/bsd/common/include/linux/kernel.h:1.7 --- src/sys/external/bsd/common/include/linux/kernel.h:1.6 Wed Feb 25 15:50:16 2015 +++ src/sys/external/bsd/common/include/linux/kernel.h Mon Apr 20 15:22:18 2015 @@ -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 \ Index: src/sys/lib/libkern/libkern.h diff -u src/sys/lib/libkern/libkern.h:1.117 src/sys/lib/libkern/libkern.h:1.118 --- src/sys/lib/libkern/libkern.h:1.117 Fri Jan 16 18:36:31 2015 +++ src/sys/lib/libkern/libkern.h Mon Apr 20 15:22:17 2015 @@ -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 @@ tolower(int ch) #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;