Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Merge branch 'devel'
[palacios.git] / kitten / include / lwk / kernel.h
1 #ifndef _LWK_KERNEL_H
2 #define _LWK_KERNEL_H
3
4 /*
5  * 'kernel.h' contains some often used function prototypes etc
6  */
7
8 #include <stdarg.h>
9 #include <lwk/linkage.h>
10 #include <lwk/stddef.h>
11 #include <lwk/types.h>
12 #include <lwk/compiler.h>
13 #include <lwk/kmem.h>
14 #include <lwk/errno.h>
15 #include <lwk/utsname.h>
16 #include <lwk/print.h>
17 #include <arch/byteorder.h>
18 #include <arch/bug.h>
19
20 extern const char lwk_banner[];
21 extern struct utsname linux_utsname;
22
23 #define INT_MAX         ((int)(~0U>>1))
24 #define INT_MIN         (-INT_MAX - 1)
25 #define UINT_MAX        (~0U)
26 #define LONG_MAX        ((long)(~0UL>>1))
27 #define LONG_MIN        (-LONG_MAX - 1)
28 #define ULONG_MAX       (~0UL)
29
30 #define STACK_MAGIC     0xdeadbeef
31
32 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
33 #define ALIGN(x,a) (((x)+(a)-1)&~((a)-1))
34 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
35
36 void panic(const char * fmt, ...);
37
38 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
39 extern long simple_strtol(const char *,char **,unsigned int);
40 extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
41 extern long long simple_strtoll(const char *,char **,unsigned int);
42 extern int get_option(char **str, int *pint);
43 extern char *get_options(const char *str, int nints, int *ints);
44 extern unsigned long long memparse(char *ptr, char **retptr);
45
46 /*
47  * min()/max() macros that also do
48  * strict type-checking.. See the
49  * "unnecessary" pointer comparison.
50  */
51 #define min(x,y) ({ \
52         typeof(x) _x = (x);     \
53         typeof(y) _y = (y);     \
54         (void) (&_x == &_y);            \
55         _x < _y ? _x : _y; })
56
57 #define max(x,y) ({ \
58         typeof(x) _x = (x);     \
59         typeof(y) _y = (y);     \
60         (void) (&_x == &_y);            \
61         _x > _y ? _x : _y; })
62
63 /*
64  * ..and if you can't take the strict
65  * types, you can specify one yourself.
66  *
67  * Or not use min/max at all, of course.
68  */
69 #define min_t(type,x,y) \
70         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
71 #define max_t(type,x,y) \
72         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
73
74 /*
75  * Round x up to the nearest y aligned boundary.  y must be a power of two.
76  */
77 #define round_up(x,y) (((x) + (y) - 1) & ~((y)-1))
78
79 /*
80  * Round x down to the nearest y aligned boundary.  y must be a power of two.
81  */
82 #define round_down(x,y) ((x) & ~((y)-1))
83
84 /**
85  * container_of - cast a member of a structure out to the containing structure
86  * @ptr:        the pointer to the member.
87  * @type:       the type of the container struct this is embedded in.
88  * @member:     the name of the member within the struct.
89  */
90 #define container_of(ptr, type, member) ({                      \
91         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
92         (type *)( (char *)__mptr - offsetof(type,member) );})
93
94 /*
95  * Check at compile time that something is of a particular type.
96  * Always evaluates to 1 so you may use it easily in comparisons.
97  */
98 #define typecheck(type,x) \
99 ({      type __dummy; \
100         typeof(x) __dummy2; \
101         (void)(&__dummy == &__dummy2); \
102         1; \
103 })
104
105 /*
106  * Check at compile time that 'function' is a certain type, or is a pointer
107  * to that type (needs to use typedef for the function type.)
108  */
109 #define typecheck_fn(type,function) \
110 ({      typeof(type) __tmp = function; \
111         (void)__tmp; \
112 })
113
114 /*
115  * Check at compile time that 'type' is a multiple of align.
116  */
117 #define aligncheck(type,align) \
118         extern int __align_check[ (sizeof(type) % (align) == 0 ? 0 : 1/0) ]
119
120 /*
121  * Check at compile time that the type 'type' has the expected 'size'.
122  * NOTE: 'type' must be a single word, so this doesn't work for structures.
123  *       For structures, use sizecheck_struct().
124  */
125 #define sizecheck(type,size) \
126         extern int __size_check_##type[ (sizeof(type) == (size) ? 0 : 1/0) ]
127
128 /*
129  * Check at compile time that the structure 'name' has the expected size 'size'.
130  */
131 #define sizecheck_struct(name,size) \
132         extern int __size_check_struct_##name[ (sizeof(struct name) == (size) ? 0 : 1/0) ]
133
134 /*
135  * Force a compilation error if condition is true
136  */
137 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
138
139 #endif