Home | History | Annotate | Line # | Download | only in linux
kernel.h revision 1.1
      1 /*	$NetBSD: kernel.h,v 1.1 2013/09/05 15:28:07 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _LINUX_KERNEL_H_
     33 #define _LINUX_KERNEL_H_
     34 
     35 #include <sys/cdefs.h>
     36 #include <sys/types.h>
     37 #include <sys/param.h>
     38 
     39 #define	__printf	__printflike
     40 #define	__user
     41 #define	__must_check	/* __attribute__((warn_unused_result)), if GCC */
     42 #define	__always_unused	__unused
     43 
     44 #define	barrier()	__insn_barrier()
     45 #define	unlikely(X)	__predict_false(X)
     46 
     47 #define	uninitialized_var(x)	x
     48 
     49 /* XXX These will multiply evaluate their arguments.  */
     50 #define	max_t(T, X, Y)	MAX(X, Y)
     51 #define	min_t(T, X, Y)	MIN(X, Y)
     52 
     53 /*
     54  * Rounding to what may or may not be powers of two.
     55  */
     56 #define	DIV_ROUND_UP(X, N)	(((X) + (N) - 1) / (N))
     57 
     58 /*
     59  * Rounding to powers of two -- carefully avoiding multiple evaluation
     60  * of arguments and pitfalls with C integer arithmetic rules.
     61  */
     62 #define	round_up(X, N)		((((X) - 1) | ((N) - 1)) + 1)
     63 #define	round_down(X, N)	((X) & ~(uintmax_t)((N) - 1))
     64 
     65 /*
     66  * These select 32-bit halves of what may be 32- or 64-bit quantities,
     67  * for which straight 32-bit shifts may be undefined behaviour (and do
     68  * the wrong thing on most machines: return the input unshifted by
     69  * ignoring the upper bits of the shift count).
     70  */
     71 #define	upper_32_bits(X)	((uint32_t) (((X) >> 16) >> 16))
     72 #define	lower_32_bits(X)	((uint32_t) ((X) & 0xffffffffUL))
     73 
     74 /*
     75  * Given x = &c->f, container_of(x, T, f) gives us back c, where T is
     76  * the type of c.
     77  */
     78 #define	container_of(PTR, TYPE, FIELD)					\
     79 	((void)sizeof((PTR) -						\
     80 		&((TYPE *)(((char *)(PTR)) -				\
     81 		    offsetof(TYPE, FIELD)))->FIELD),			\
     82 	    ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD))))
     83 
     84 #define	ARRAY_SIZE(ARRAY)	__arraycount(ARRAY)
     85 
     86 #define	swap(X, Y)	do						\
     87 {									\
     88 	/* XXX Kludge for type-safety.  */				\
     89 	if (&(X) != &(Y)) {						\
     90 		CTASSERT(sizeof(X) == sizeof(Y));			\
     91 		/* XXX Can't do this much better without typeof.  */	\
     92 		char __swap_tmp[sizeof(X)];				\
     93 		(void)memcpy(__swap_tmp, &(X), sizeof(X));		\
     94 		(void)memcpy(&(X), &(Y), sizeof(X));			\
     95 		(void)memcpy(&(Y), __swap_tmp, sizeof(X));		\
     96 	}								\
     97 } while (0)
     98 
     99 static inline int64_t
    100 abs64(int64_t x)
    101 {
    102 	return (x < 0? (-x) : x);
    103 }
    104 
    105 #endif  /* _LINUX_KERNEL_H_ */
    106