Wed May 11 03:17:22 2016 UTC ()
provide const versions of container_of macros.

discussed with riastradh@ by email


(rtr)
diff -r1.122 -r1.123 src/sys/lib/libkern/libkern.h

cvs diff -r1.122 -r1.123 src/sys/lib/libkern/libkern.h (expand / switch to unified diff)

--- src/sys/lib/libkern/libkern.h 2016/05/02 19:18:29 1.122
+++ src/sys/lib/libkern/libkern.h 2016/05/11 03:17:22 1.123
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: libkern.h,v 1.122 2016/05/02 19:18:29 christos Exp $ */ 1/* $NetBSD: libkern.h,v 1.123 2016/05/11 03:17:22 rtr Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1992, 1993 4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -327,35 +327,42 @@ tolower(int ch) @@ -327,35 +327,42 @@ tolower(int ch)
327 * struct bar b; 327 * struct bar b;
328 * struct foo *fp = b.b_foo; 328 * struct foo *fp = b.b_foo;
329 * 329 *
330 * Now we can get at b from fp by: 330 * Now we can get at b from fp by:
331 * 331 *
332 * struct bar *bp = container_of(fp, struct bar, b_foo); 332 * struct bar *bp = container_of(fp, struct bar, b_foo);
333 * 333 *
334 * The 0*sizeof((PTR) - ...) causes the compiler to warn if the type of 334 * The 0*sizeof((PTR) - ...) causes the compiler to warn if the type of
335 * *fp does not match the type of struct bar::b_foo. 335 * *fp does not match the type of struct bar::b_foo.
336 * We skip the validation for coverity runs to avoid warnings. 336 * We skip the validation for coverity runs to avoid warnings.
337 */ 337 */
338#ifdef __COVERITY__ 338#ifdef __COVERITY__
339#define __validate_container_of(PTR, TYPE, FIELD) 0 339#define __validate_container_of(PTR, TYPE, FIELD) 0
 340#define __validate_const_container_of(PTR, TYPE, FIELD) 0
340#else 341#else
341#define __validate_container_of(PTR, TYPE, FIELD) \ 342#define __validate_container_of(PTR, TYPE, FIELD) \
342 (0 * sizeof((PTR) - &((TYPE *)(((char *)(PTR)) - \ 343 (0 * sizeof((PTR) - &((TYPE *)(((char *)(PTR)) - \
343 offsetof(TYPE, FIELD)))->FIELD)) 344 offsetof(TYPE, FIELD)))->FIELD))
 345#define __validate_const_container_of(PTR, TYPE, FIELD) \
 346 (0 * sizeof((PTR) - &((const TYPE *)(((const char *)(PTR)) - \
 347 offsetof(TYPE, FIELD)))->FIELD))
344#endif 348#endif
345 349
346#define container_of(PTR, TYPE, FIELD) \ 350#define container_of(PTR, TYPE, FIELD) \
347 ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD)) \ 351 ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD)) \
348 + __validate_container_of(PTR, TYPE, FIELD)) 352 + __validate_container_of(PTR, TYPE, FIELD))
 353#define const_container_of(PTR, TYPE, FIELD) \
 354 ((const TYPE *)(((const char *)(PTR)) - offsetof(TYPE, FIELD)) \
 355 + __validate_const_container_of(PTR, TYPE, FIELD))
349 356
350#define MTPRNG_RLEN 624 357#define MTPRNG_RLEN 624
351struct mtprng_state { 358struct mtprng_state {
352 unsigned int mt_idx;  359 unsigned int mt_idx;
353 uint32_t mt_elem[MTPRNG_RLEN]; 360 uint32_t mt_elem[MTPRNG_RLEN];
354 uint32_t mt_count; 361 uint32_t mt_count;
355 uint32_t mt_sparse[3]; 362 uint32_t mt_sparse[3];
356}; 363};
357 364
358/* Prototypes for which GCC built-ins exist. */ 365/* Prototypes for which GCC built-ins exist. */
359void *memcpy(void *, const void *, size_t); 366void *memcpy(void *, const void *, size_t);
360int memcmp(const void *, const void *, size_t); 367int memcmp(const void *, const void *, size_t);
361void *memset(void *, int, size_t); 368void *memset(void *, int, size_t);