Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/lib/libkern provide const versions of container_of macros.



details:   https://anonhg.NetBSD.org/src/rev/860cf9872a9c
branches:  trunk
changeset: 815307:860cf9872a9c
user:      rtr <rtr%NetBSD.org@localhost>
date:      Wed May 11 03:17:22 2016 +0000

description:
provide const versions of container_of macros.

discussed with riastradh@ by email

diffstat:

 sys/lib/libkern/libkern.h |  9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)

diffs (32 lines):

diff -r fe9bd46ab5a3 -r 860cf9872a9c sys/lib/libkern/libkern.h
--- a/sys/lib/libkern/libkern.h Wed May 11 02:28:33 2016 +0000
+++ b/sys/lib/libkern/libkern.h Wed May 11 03:17:22 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: libkern.h,v 1.122 2016/05/02 19:18:29 christos Exp $   */
+/*     $NetBSD: libkern.h,v 1.123 2016/05/11 03:17:22 rtr Exp $        */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -337,15 +337,22 @@
  */
 #ifdef __COVERITY__
 #define __validate_container_of(PTR, TYPE, FIELD) 0
+#define __validate_const_container_of(PTR, TYPE, FIELD) 0
 #else
 #define __validate_container_of(PTR, TYPE, FIELD)                      \
     (0 * sizeof((PTR) - &((TYPE *)(((char *)(PTR)) -                   \
     offsetof(TYPE, FIELD)))->FIELD))
+#define __validate_const_container_of(PTR, TYPE, FIELD)                        \
+    (0 * sizeof((PTR) - &((const TYPE *)(((const char *)(PTR)) -       \
+    offsetof(TYPE, FIELD)))->FIELD))
 #endif
 
 #define        container_of(PTR, TYPE, FIELD)                                  \
     ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD))                 \
        + __validate_container_of(PTR, TYPE, FIELD))
+#define        const_container_of(PTR, TYPE, FIELD)                            \
+    ((const TYPE *)(((const char *)(PTR)) - offsetof(TYPE, FIELD))     \
+       + __validate_const_container_of(PTR, TYPE, FIELD))
 
 #define        MTPRNG_RLEN             624
 struct mtprng_state {



Home | Main Index | Thread Index | Old Index