blob: 5c4b1251e110c733a367aad5820182e41b3c7bb8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_KERNEL_H
2#define _LINUX_KERNEL_H
3
4/*
5 * 'kernel.h' contains some often-used function prototypes etc
6 */
7
8#ifdef __KERNEL__
9
10#include <stdarg.h>
11#include <linux/linkage.h>
12#include <linux/stddef.h>
13#include <linux/types.h>
14#include <linux/compiler.h>
15#include <linux/bitops.h>
David Howellsf0d1b0b2006-12-08 02:37:49 -080016#include <linux/log2.h>
Andrew Mortone0deaff42008-07-25 01:45:24 -070017#include <linux/typecheck.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/byteorder.h>
19#include <asm/bug.h>
20
Roman Zippel3eb3c742007-01-10 14:45:28 +010021extern const char linux_banner[];
22extern const char linux_proc_banner[];
23
Zhang, Yanmin44f564a2008-04-29 01:00:55 -070024#define USHORT_MAX ((u16)(~0U))
25#define SHORT_MAX ((s16)(USHORT_MAX>>1))
26#define SHORT_MIN (-SHORT_MAX - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define INT_MAX ((int)(~0U>>1))
28#define INT_MIN (-INT_MAX - 1)
29#define UINT_MAX (~0U)
30#define LONG_MAX ((long)(~0UL>>1))
31#define LONG_MIN (-LONG_MAX - 1)
32#define ULONG_MAX (~0UL)
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -070033#define LLONG_MAX ((long long)(~0ULL>>1))
34#define LLONG_MIN (-LLONG_MAX - 1)
35#define ULLONG_MAX (~0ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#define STACK_MAGIC 0xdeadbeef
38
Linus Torvalds2ea58142006-11-26 19:05:22 -080039#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
40#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
Matthew Wilcoxa83308e2007-09-11 15:23:47 -070041#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
Herbert Xuf10db622008-02-06 01:37:05 -080042#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
Linus Torvalds2ea58142006-11-26 19:05:22 -080043
Rusty Russellc5e631cf2007-05-06 14:51:05 -070044#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
45
Jan Beulich4552d5d2006-06-26 13:57:28 +020046#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
Steven Whitehouse930631e2006-09-25 23:32:40 -070047#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
David Howellsb4cac1a2006-07-10 04:44:54 -070048#define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Eduard - Gabriel Munteanuca31e142008-07-05 12:14:23 +030050#define _RET_IP_ (unsigned long)__builtin_return_address(0)
51#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
52
Jens Axboe2da96ac2007-10-12 12:40:38 +020053#ifdef CONFIG_LBD
54# include <asm/div64.h>
55# define sector_div(a, b) do_div(a, b)
56#else
57# define sector_div(n, b)( \
58{ \
59 int _res; \
60 _res = (n) % (b); \
61 (n) /= (b); \
62 _res; \
63} \
64)
65#endif
66
Andrew Morton218e1802007-05-10 03:15:18 -070067/**
68 * upper_32_bits - return bits 32-63 of a number
69 * @n: the number we're accessing
70 *
71 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
72 * the "right shift count >= width of type" warning when that quantity is
73 * 32-bits.
74 */
75#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define KERN_EMERG "<0>" /* system is unusable */
78#define KERN_ALERT "<1>" /* action must be taken immediately */
79#define KERN_CRIT "<2>" /* critical conditions */
80#define KERN_ERR "<3>" /* error conditions */
81#define KERN_WARNING "<4>" /* warning conditions */
82#define KERN_NOTICE "<5>" /* normal but significant condition */
83#define KERN_INFO "<6>" /* informational */
84#define KERN_DEBUG "<7>" /* debug-level messages */
85
Ingo Molnar47492522007-10-16 23:30:29 -070086/*
87 * Annotation for a "continued" line of log printout (only done after a
88 * line that had no enclosing \n). Only to be used by core/arch code
89 * during early bootup (a continued line is not SMP-safe otherwise).
90 */
91#define KERN_CONT ""
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093extern int console_printk[];
94
95#define console_loglevel (console_printk[0])
96#define default_message_loglevel (console_printk[1])
97#define minimum_console_loglevel (console_printk[2])
98#define default_console_loglevel (console_printk[3])
99
100struct completion;
akpm@osdl.orgdf2e71f2006-01-09 20:51:37 -0800101struct pt_regs;
102struct user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/**
105 * might_sleep - annotation for functions that can sleep
106 *
107 * this macro will print a stack trace if it is executed in an atomic
108 * context (spinlock, irq-handler, ...).
109 *
110 * This is a useful debugging help to be able to catch problems early and not
Jim Cromiee20ec992006-11-30 04:46:13 +0100111 * be bitten later when the calling function happens to sleep when it is not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * supposed to.
113 */
Ingo Molnarf8cbd992005-06-25 14:57:39 -0700114#ifdef CONFIG_PREEMPT_VOLUNTARY
Herbert Xu02b67cc32008-01-25 21:08:28 +0100115extern int _cond_resched(void);
116# define might_resched() _cond_resched()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#else
Ingo Molnarf8cbd992005-06-25 14:57:39 -0700118# define might_resched() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#endif
120
Ingo Molnarf8cbd992005-06-25 14:57:39 -0700121#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
122 void __might_sleep(char *file, int line);
123# define might_sleep() \
124 do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
125#else
126# define might_sleep() do { might_resched(); } while (0)
127#endif
128
Hua Zhong368a5fa2006-06-23 02:05:42 -0700129#define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
Ingo Molnarf8cbd992005-06-25 14:57:39 -0700130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define abs(x) ({ \
132 int __x = (x); \
133 (__x < 0) ? -__x : __x; \
134 })
135
Alan Sterne041c682006-03-27 01:16:30 -0800136extern struct atomic_notifier_head panic_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137extern long (*panic_blink)(long time);
138NORET_TYPE void panic(const char * fmt, ...)
Andi Kleena586df02007-07-21 17:10:00 +0200139 __attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
Andrew Mortondd287792006-03-23 03:00:57 -0800140extern void oops_enter(void);
141extern void oops_exit(void);
142extern int oops_may_print(void);
Harvey Harrisonec701582008-02-08 04:19:55 -0800143NORET_TYPE void do_exit(long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 ATTRIB_NORET;
145NORET_TYPE void complete_and_exit(struct completion *, long)
146 ATTRIB_NORET;
147extern unsigned long simple_strtoul(const char *,char **,unsigned int);
148extern long simple_strtol(const char *,char **,unsigned int);
149extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
150extern long long simple_strtoll(const char *,char **,unsigned int);
Yi Yang06b2a762008-02-08 04:21:57 -0800151extern int strict_strtoul(const char *, unsigned int, unsigned long *);
152extern int strict_strtol(const char *, unsigned int, long *);
153extern int strict_strtoull(const char *, unsigned int, unsigned long long *);
154extern int strict_strtoll(const char *, unsigned int, long long *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155extern int sprintf(char * buf, const char * fmt, ...)
156 __attribute__ ((format (printf, 2, 3)));
157extern int vsprintf(char *buf, const char *, va_list)
158 __attribute__ ((format (printf, 2, 0)));
159extern int snprintf(char * buf, size_t size, const char * fmt, ...)
160 __attribute__ ((format (printf, 3, 4)));
161extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
162 __attribute__ ((format (printf, 3, 0)));
163extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
164 __attribute__ ((format (printf, 3, 4)));
165extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
166 __attribute__ ((format (printf, 3, 0)));
Jeremy Fitzhardingee9059142006-06-25 05:49:17 -0700167extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
168 __attribute__ ((format (printf, 2, 3)));
Jeremy Fitzhardinge11443ec2007-04-30 15:09:56 -0700169extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171extern int sscanf(const char *, const char *, ...)
172 __attribute__ ((format (scanf, 2, 3)));
173extern int vsscanf(const char *, const char *, va_list)
174 __attribute__ ((format (scanf, 2, 0)));
175
176extern int get_option(char **str, int *pint);
177extern char *get_options(const char *str, int nints, int *ints);
178extern unsigned long long memparse(char *ptr, char **retptr);
179
Trent Piepho5e376612006-05-15 09:44:06 -0700180extern int core_kernel_text(unsigned long addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181extern int __kernel_text_address(unsigned long addr);
182extern int kernel_text_address(unsigned long addr);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -0800183struct pid;
184extern struct pid *session_of_pgrp(struct pid *pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Matt Mackalld59745c2005-05-01 08:59:02 -0700186#ifdef CONFIG_PRINTK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187asmlinkage int vprintk(const char *fmt, va_list args)
188 __attribute__ ((format (printf, 1, 0)));
189asmlinkage int printk(const char * fmt, ...)
Andi Kleena586df02007-07-21 17:10:00 +0200190 __attribute__ ((format (printf, 1, 2))) __cold;
Joe Perches7ef3d2f2008-02-08 04:21:25 -0800191
192extern int printk_ratelimit_jiffies;
193extern int printk_ratelimit_burst;
194extern int printk_ratelimit(void);
Dave Young5f97a5a2008-04-29 00:59:43 -0700195extern int __ratelimit(int ratelimit_jiffies, int ratelimit_burst);
Joe Perches7ef3d2f2008-02-08 04:21:25 -0800196extern int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst);
197extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
198 unsigned int interval_msec);
Matt Mackalld59745c2005-05-01 08:59:02 -0700199#else
200static inline int vprintk(const char *s, va_list args)
201 __attribute__ ((format (printf, 1, 0)));
202static inline int vprintk(const char *s, va_list args) { return 0; }
203static inline int printk(const char *s, ...)
204 __attribute__ ((format (printf, 1, 2)));
Andi Kleena586df02007-07-21 17:10:00 +0200205static inline int __cold printk(const char *s, ...) { return 0; }
Joe Perches7ef3d2f2008-02-08 04:21:25 -0800206static inline int printk_ratelimit(void) { return 0; }
207static inline int __printk_ratelimit(int ratelimit_jiffies, \
208 int ratelimit_burst) { return 0; }
209static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
210 unsigned int interval_msec) \
211 { return false; }
Matt Mackalld59745c2005-05-01 08:59:02 -0700212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Jiri Slabye17ba732008-05-12 15:44:40 +0200214extern void asmlinkage __attribute__((format(printf, 1, 2)))
Ingo Molnar076f9772008-01-30 13:33:06 +0100215 early_printk(const char *fmt, ...);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217unsigned long int_sqrt(unsigned long);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static inline void console_silent(void)
220{
221 console_loglevel = 0;
222}
223
224static inline void console_verbose(void)
225{
226 if (console_loglevel)
227 console_loglevel = 15;
228}
229
230extern void bust_spinlocks(int yes);
Kirill Korotaeve3e8a752007-02-10 01:46:19 -0800231extern void wake_up_klogd(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */
Adrian Bunkaa727102006-04-10 22:53:59 -0700233extern int panic_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234extern int panic_on_oops;
Don Zickus8da5add2006-09-26 10:52:27 +0200235extern int panic_on_unrecovered_nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236extern int tainted;
237extern const char *print_tainted(void);
238extern void add_taint(unsigned);
David Howellsb920de12008-02-08 04:19:31 -0800239extern int root_mountflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241/* Values used for system_state */
242extern enum system_states {
243 SYSTEM_BOOTING,
244 SYSTEM_RUNNING,
245 SYSTEM_HALT,
246 SYSTEM_POWER_OFF,
247 SYSTEM_RESTART,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500248 SYSTEM_SUSPEND_DISK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249} system_state;
250
251#define TAINT_PROPRIETARY_MODULE (1<<0)
252#define TAINT_FORCED_MODULE (1<<1)
253#define TAINT_UNSAFE_SMP (1<<2)
254#define TAINT_FORCED_RMMOD (1<<3)
255#define TAINT_MACHINE_CHECK (1<<4)
256#define TAINT_BAD_PAGE (1<<5)
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800257#define TAINT_USER (1<<6)
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700258#define TAINT_DIE (1<<7)
Éric Piel6ed31e92008-02-05 00:04:50 +0100259#define TAINT_OVERRIDDEN_ACPI_TABLE (1<<8)
Nur Hussein95b570c2008-04-29 00:58:39 -0700260#define TAINT_WARN (1<<9)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Andi Kleena586df02007-07-21 17:10:00 +0200262extern void dump_stack(void) __cold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Randy Dunlap99eaf3c2007-05-10 22:22:39 -0700264enum {
265 DUMP_PREFIX_NONE,
266 DUMP_PREFIX_ADDRESS,
267 DUMP_PREFIX_OFFSET
268};
Randy Dunlapc7909232007-06-08 13:47:04 -0700269extern void hex_dump_to_buffer(const void *buf, size_t len,
270 int rowsize, int groupsize,
271 char *linebuf, size_t linebuflen, bool ascii);
272extern void print_hex_dump(const char *level, const char *prefix_str,
273 int prefix_type, int rowsize, int groupsize,
Artem Bityutskiy6a0ed912007-08-07 23:43:14 +0300274 const void *buf, size_t len, bool ascii);
Randy Dunlapc7909232007-06-08 13:47:04 -0700275extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
Alan Sterneb9a9a52007-08-10 13:01:07 -0700276 const void *buf, size_t len);
Harvey Harrison3fc95772008-05-14 16:05:49 -0700277
278extern const char hex_asc[];
279#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
280#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
281
282static inline char *pack_hex_byte(char *buf, u8 byte)
283{
284 *buf++ = hex_asc_hi(byte);
285 *buf++ = hex_asc_lo(byte);
286 return buf;
287}
Randy Dunlap99eaf3c2007-05-10 22:22:39 -0700288
Emil Medve1f7c8232007-10-16 23:29:48 -0700289#define pr_emerg(fmt, arg...) \
290 printk(KERN_EMERG fmt, ##arg)
291#define pr_alert(fmt, arg...) \
292 printk(KERN_ALERT fmt, ##arg)
293#define pr_crit(fmt, arg...) \
294 printk(KERN_CRIT fmt, ##arg)
295#define pr_err(fmt, arg...) \
296 printk(KERN_ERR fmt, ##arg)
297#define pr_warning(fmt, arg...) \
298 printk(KERN_WARNING fmt, ##arg)
299#define pr_notice(fmt, arg...) \
300 printk(KERN_NOTICE fmt, ##arg)
301#define pr_info(fmt, arg...) \
302 printk(KERN_INFO fmt, ##arg)
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#ifdef DEBUG
Pavel Machek1cc6daf2006-08-08 01:37:15 +0200305/* If you are writing a driver, please use dev_dbg instead */
Emil Medve1f7c8232007-10-16 23:29:48 -0700306#define pr_debug(fmt, arg...) \
307 printk(KERN_DEBUG fmt, ##arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308#else
Joe Perches1429db82008-02-26 19:08:42 -0800309#define pr_debug(fmt, arg...) \
310 ({ if (0) printk(KERN_DEBUG fmt, ##arg); 0; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311#endif
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*
314 * Display an IP address in readable format.
315 */
316
317#define NIPQUAD(addr) \
318 ((unsigned char *)&addr)[0], \
319 ((unsigned char *)&addr)[1], \
320 ((unsigned char *)&addr)[2], \
321 ((unsigned char *)&addr)[3]
Joe Perches46b86a22006-01-13 14:29:07 -0800322#define NIPQUAD_FMT "%u.%u.%u.%u"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324#define NIP6(addr) \
325 ntohs((addr).s6_addr16[0]), \
326 ntohs((addr).s6_addr16[1]), \
327 ntohs((addr).s6_addr16[2]), \
328 ntohs((addr).s6_addr16[3]), \
329 ntohs((addr).s6_addr16[4]), \
330 ntohs((addr).s6_addr16[5]), \
331 ntohs((addr).s6_addr16[6]), \
332 ntohs((addr).s6_addr16[7])
Joe Perches46b86a22006-01-13 14:29:07 -0800333#define NIP6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x"
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -0800334#define NIP6_SEQFMT "%04x%04x%04x%04x%04x%04x%04x%04x"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336#if defined(__LITTLE_ENDIAN)
337#define HIPQUAD(addr) \
338 ((unsigned char *)&addr)[3], \
339 ((unsigned char *)&addr)[2], \
340 ((unsigned char *)&addr)[1], \
341 ((unsigned char *)&addr)[0]
342#elif defined(__BIG_ENDIAN)
343#define HIPQUAD NIPQUAD
344#else
345#error "Please fix asm/byteorder.h"
346#endif /* __LITTLE_ENDIAN */
347
348/*
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700349 * min()/max()/clamp() macros that also do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * strict type-checking.. See the
351 * "unnecessary" pointer comparison.
352 */
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700353#define min(x, y) ({ \
354 typeof(x) _min1 = (x); \
355 typeof(y) _min2 = (y); \
356 (void) (&_min1 == &_min2); \
357 _min1 < _min2 ? _min1 : _min2; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700359#define max(x, y) ({ \
360 typeof(x) _max1 = (x); \
361 typeof(y) _max2 = (y); \
362 (void) (&_max1 == &_max2); \
363 _max1 > _max2 ? _max1 : _max2; })
364
365/**
366 * clamp - return a value clamped to a given range with strict typechecking
367 * @val: current value
368 * @min: minimum allowable value
369 * @max: maximum allowable value
370 *
371 * This macro does strict typechecking of min/max to make sure they are of the
372 * same type as val. See the unnecessary pointer comparisons.
373 */
374#define clamp(val, min, max) ({ \
375 typeof(val) __val = (val); \
376 typeof(min) __min = (min); \
377 typeof(max) __max = (max); \
378 (void) (&__val == &__min); \
379 (void) (&__val == &__max); \
380 __val = __val < __min ? __min: __val; \
381 __val > __max ? __max: __val; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383/*
384 * ..and if you can't take the strict
385 * types, you can specify one yourself.
386 *
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700387 * Or not use min/max/clamp at all, of course.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 */
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700389#define min_t(type, x, y) ({ \
390 type __min1 = (x); \
391 type __min2 = (y); \
392 __min1 < __min2 ? __min1: __min2; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700394#define max_t(type, x, y) ({ \
395 type __max1 = (x); \
396 type __max2 = (y); \
397 __max1 > __max2 ? __max1: __max2; })
398
399/**
400 * clamp_t - return a value clamped to a given range using a given type
401 * @type: the type of variable to use
402 * @val: current value
403 * @min: minimum allowable value
404 * @max: maximum allowable value
405 *
406 * This macro does no typechecking and uses temporary variables of type
407 * 'type' to make all the comparisons.
408 */
409#define clamp_t(type, val, min, max) ({ \
410 type __val = (val); \
411 type __min = (min); \
412 type __max = (max); \
413 __val = __val < __min ? __min: __val; \
414 __val > __max ? __max: __val; })
415
416/**
417 * clamp_val - return a value clamped to a given range using val's type
418 * @val: current value
419 * @min: minimum allowable value
420 * @max: maximum allowable value
421 *
422 * This macro does no typechecking and uses temporary variables of whatever
423 * type the input argument 'val' is. This is useful when val is an unsigned
424 * type and min and max are literals that will otherwise be assigned a signed
425 * integer type.
426 */
427#define clamp_val(val, min, max) ({ \
428 typeof(val) __val = (val); \
429 typeof(val) __min = (min); \
430 typeof(val) __max = (max); \
431 __val = __val < __min ? __min: __val; \
432 __val > __max ? __max: __val; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434/**
435 * container_of - cast a member of a structure out to the containing structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * @ptr: the pointer to the member.
437 * @type: the type of the container struct this is embedded in.
438 * @member: the name of the member within the struct.
439 *
440 */
441#define container_of(ptr, type, member) ({ \
Daniel Walker78db2ad2007-05-12 16:28:35 -0700442 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
443 (type *)( (char *)__mptr - offsetof(type,member) );})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Kyle McMartind4d23ad2007-02-10 01:46:00 -0800445struct sysinfo;
446extern int do_sysinfo(struct sysinfo *info);
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448#endif /* __KERNEL__ */
449
450#define SI_LOAD_SHIFT 16
451struct sysinfo {
452 long uptime; /* Seconds since boot */
453 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
454 unsigned long totalram; /* Total usable main memory size */
455 unsigned long freeram; /* Available memory size */
456 unsigned long sharedram; /* Amount of shared memory */
457 unsigned long bufferram; /* Memory used by buffers */
458 unsigned long totalswap; /* Total swap space size */
459 unsigned long freeswap; /* swap space still available */
460 unsigned short procs; /* Number of current processes */
461 unsigned short pad; /* explicit padding for m68k */
462 unsigned long totalhigh; /* Total high memory size */
463 unsigned long freehigh; /* Available high memory size */
464 unsigned int mem_unit; /* Memory unit size in bytes */
465 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
466};
467
Nikita Danilovc0398ee2005-10-30 15:03:10 -0800468/* Force a compilation error if condition is true */
Andi Kleen921717a2005-09-13 01:25:13 -0700469#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Jan Beulich4552d5d2006-06-26 13:57:28 +0200471/* Force a compilation error if condition is true, but also produce a
472 result (of value 0 and type size_t), so the expression can be used
473 e.g. in a structure initializer (or where-ever else comma expressions
474 aren't permitted). */
475#define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/* Trap pasters of __FUNCTION__ at compile-time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478#define __FUNCTION__ (__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Christoph Lameter08e0f6a2006-09-27 01:50:06 -0700480/* This helps us to avoid #ifdef CONFIG_NUMA */
481#ifdef CONFIG_NUMA
482#define NUMA_BUILD 1
483#else
484#define NUMA_BUILD 0
485#endif
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487#endif