Wed Jul 24 03:28:09 2013 UTC ()
Fix up some bit-hacking and pointer-futzing in <linux/kernel.h>.

- Avoid C arithmetic pitfalls and multiple evaluatoin in round_up.
- Add round_down.
- Explain why upper_32_bits and lower_32_bits exist.
- Explain what container_of does.


(riastradh)
diff -r1.1.2.17 -r1.1.2.18 src/sys/external/bsd/drm2/include/linux/kernel.h

cvs diff -r1.1.2.17 -r1.1.2.18 src/sys/external/bsd/drm2/include/linux/Attic/kernel.h (expand / switch to unified diff)

--- src/sys/external/bsd/drm2/include/linux/Attic/kernel.h 2013/07/24 03:03:37 1.1.2.17
+++ src/sys/external/bsd/drm2/include/linux/Attic/kernel.h 2013/07/24 03:28:09 1.1.2.18
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: kernel.h,v 1.1.2.17 2013/07/24 03:03:37 riastradh Exp $ */ 1/* $NetBSD: kernel.h,v 1.1.2.18 2013/07/24 03:28:09 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell. 8 * by Taylor R. Campbell.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -36,31 +36,46 @@ @@ -36,31 +36,46 @@
36#include <sys/types.h> 36#include <sys/types.h>
37#include <sys/param.h> 37#include <sys/param.h>
38 38
39#define __printf __printflike 39#define __printf __printflike
40#define __user 40#define __user
41#define __must_check /* __attribute__((warn_unused_result)), if GCC */ 41#define __must_check /* __attribute__((warn_unused_result)), if GCC */
42#define __always_unused __unused 42#define __always_unused __unused
43 43
44#define barrier() __insn_barrier() 44#define barrier() __insn_barrier()
45#define unlikely(X) __predict_false(X) 45#define unlikely(X) __predict_false(X)
46 46
47#define uninitialized_var(x) x 47#define uninitialized_var(x) x
48 48
49#define round_up(X, Y) roundup2(X, Y) 49/*
 50 * Rounding to powers of two -- carefully avoiding multiple evaluation
 51 * of arguments and pitfalls with C integer arithmetic rules.
 52 */
 53#define round_up(X, N) ((((X) - 1) | ((N) - 1)) + 1)
 54#define round_down(X, N) ((X) & ~(uintmax_t)((N) - 1))
50 55
 56/*
 57 * These select 32-bit halves of what may be 32- or 64-bit quantities,
 58 * for which straight 32-bit shifts may be undefined behaviour (and do
 59 * the wrong thing on most machines: return the input unshifted by
 60 * ignoring the upper bits of the shift count).
 61 */
51#define upper_32_bits(X) ((uint32_t) (((X) >> 16) >> 16)) 62#define upper_32_bits(X) ((uint32_t) (((X) >> 16) >> 16))
52#define lower_32_bits(X) ((uint32_t) ((X) & 0xffffffffUL)) 63#define lower_32_bits(X) ((uint32_t) ((X) & 0xffffffffUL))
53 64
 65/*
 66 * Given x = &c->f, container_of(x, T, f) gives us back c, where T is
 67 * the type of c.
 68 */
54#define container_of(PTR, TYPE, FIELD) \ 69#define container_of(PTR, TYPE, FIELD) \
55 ((void)sizeof((PTR) - \ 70 ((void)sizeof((PTR) - \
56 &((TYPE *)(((char *)(PTR)) - \ 71 &((TYPE *)(((char *)(PTR)) - \
57 offsetof(TYPE, FIELD)))->FIELD), \ 72 offsetof(TYPE, FIELD)))->FIELD), \
58 ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD)))) 73 ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD))))
59 74
60#define ARRAY_SIZE(ARRAY) __arraycount(ARRAY) 75#define ARRAY_SIZE(ARRAY) __arraycount(ARRAY)
61 76
62#define swap(X, Y) do \ 77#define swap(X, Y) do \
63{ \ 78{ \
64 /* XXX Kludge for type-safety. */ \ 79 /* XXX Kludge for type-safety. */ \
65 if (&(X) != &(Y)) { \ 80 if (&(X) != &(Y)) { \
66 CTASSERT(sizeof(X) == sizeof(Y)); \ 81 CTASSERT(sizeof(X) == sizeof(Y)); \