From 2ce9068d853ed59b5d731e1d6b3945b9fb0f078e Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Wed, 21 Jan 2015 14:09:17 +0900 Subject: linux/kernel.h: move *_MAX, *_MIN macros Linux defines *_MAX, *_MIN macros in include/linux/kernel.h. Let's follow this way to slim down the common.h. This change should have no impact because include/common.h already includes . Signed-off-by: Masahiro Yamada Signed-off-by: Sascha Hauer --- include/common.h | 13 ------------- include/linux/kernel.h | 13 +++++++++++++ 2 files changed, 13 insertions(+), 13 deletions(-) (limited to 'include') diff --git a/include/common.h b/include/common.h index fca9798bbb..f3f5c0fcac 100644 --- a/include/common.h +++ b/include/common.h @@ -225,19 +225,6 @@ static inline char *shell_expand(char *str) const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) -#define USHORT_MAX ((u16)(~0U)) -#define SHORT_MAX ((s16)(USHORT_MAX>>1)) -#define SHORT_MIN (-SHORT_MAX - 1) -#define INT_MAX ((int)(~0U>>1)) -#define INT_MIN (-INT_MAX - 1) -#define UINT_MAX (~0U) -#define LONG_MAX ((long)(~0UL>>1)) -#define LONG_MIN (-LONG_MAX - 1) -#define ULONG_MAX (~0UL) -#define LLONG_MAX ((long long)(~0ULL>>1)) -#define LLONG_MIN (-LLONG_MAX - 1) -#define ULLONG_MAX (~0ULL) - #define PAGE_SIZE 4096 #define PAGE_SHIFT 12 #define PAGE_ALIGN(s) (((s) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 98f12e1b93..33cf62e52d 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -4,6 +4,19 @@ #include #include +#define USHORT_MAX ((u16)(~0U)) +#define SHORT_MAX ((s16)(USHORT_MAX>>1)) +#define SHORT_MIN (-SHORT_MAX - 1) +#define INT_MAX ((int)(~0U>>1)) +#define INT_MIN (-INT_MAX - 1) +#define UINT_MAX (~0U) +#define LONG_MAX ((long)(~0UL>>1)) +#define LONG_MIN (-LONG_MAX - 1) +#define ULONG_MAX (~0UL) +#define LLONG_MAX ((long long)(~0ULL>>1)) +#define LLONG_MIN (-LLONG_MAX - 1) +#define ULLONG_MAX (~0ULL) + /* * This looks more complex than it should be. But we need to * get the type for the ~ right in round_down (it needs to be -- cgit v1.2.3 From d27077bd761277cf8075f8e489b511af1c2acef3 Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Wed, 21 Jan 2015 14:09:18 +0900 Subject: linux/kernel.h: rename USHORT_MAX, SHORT_MAX, SHORT_MIN Linux uses SHRT_* instead of SHORT_*. The of C does the same. Let's follow this standard. Signed-off-by: Masahiro Yamada Signed-off-by: Sascha Hauer --- fs/parseopt.c | 2 +- include/linux/kernel.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/fs/parseopt.c b/fs/parseopt.c index fbe53cfb02..12dbe1813c 100644 --- a/fs/parseopt.c +++ b/fs/parseopt.c @@ -26,7 +26,7 @@ again: } v = simple_strtoul(start + optlen + 1, &endp, 0); - if (v > USHORT_MAX) + if (v > USHRT_MAX) return; if (*endp == ',' || *endp == '\0') diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 33cf62e52d..4d7e41edb5 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -4,9 +4,9 @@ #include #include -#define USHORT_MAX ((u16)(~0U)) -#define SHORT_MAX ((s16)(USHORT_MAX>>1)) -#define SHORT_MIN (-SHORT_MAX - 1) +#define USHRT_MAX ((u16)(~0U)) +#define SHRT_MAX ((s16)(USHRT_MAX>>1)) +#define SHRT_MIN ((s16)(-SHRT_MAX - 1)) #define INT_MAX ((int)(~0U>>1)) #define INT_MIN (-INT_MAX - 1) #define UINT_MAX (~0U) -- cgit v1.2.3 From bfa1b12fed600b621057d347f083e620aeced9a7 Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Wed, 21 Jan 2015 14:09:19 +0900 Subject: linux/kernel.h: add more *_MAX, *_MIN macros Imported from Linux 3.19-rc5. Signed-off-by: Masahiro Yamada Signed-off-by: Sascha Hauer --- include/linux/kernel.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'include') diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 4d7e41edb5..a0395909d0 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -16,6 +16,20 @@ #define LLONG_MAX ((long long)(~0ULL>>1)) #define LLONG_MIN (-LLONG_MAX - 1) #define ULLONG_MAX (~0ULL) +#define SIZE_MAX (~(size_t)0) + +#define U8_MAX ((u8)~0U) +#define S8_MAX ((s8)(U8_MAX>>1)) +#define S8_MIN ((s8)(-S8_MAX - 1)) +#define U16_MAX ((u16)~0U) +#define S16_MAX ((s16)(U16_MAX>>1)) +#define S16_MIN ((s16)(-S16_MAX - 1)) +#define U32_MAX ((u32)~0U) +#define S32_MAX ((s32)(U32_MAX>>1)) +#define S32_MIN ((s32)(-S32_MAX - 1)) +#define U64_MAX ((u64)~0ULL) +#define S64_MAX ((s64)(U64_MAX>>1)) +#define S64_MIN ((s64)(-S64_MAX - 1)) /* * This looks more complex than it should be. But we need to -- cgit v1.2.3 From a07a472cac72d336574c438358bbf3c113b16fb5 Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Thu, 29 Jan 2015 11:14:05 +0900 Subject: linux/bug.h: move BUILD_BUG_ON*() macros In Linux, the macros BUILD_BUG_ON* are defined in include/linux/bug.h. To tidy up common.h, move BUILD_BUG_* there. MAYBE_BUILD_BUG_ON is not used in barebox and it was removed from Linux long time ago. Drop it from barebox, too. Signed-off-by: Masahiro Yamada Signed-off-by: Sascha Hauer --- include/common.h | 20 +------------------- include/linux/bug.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 include/linux/bug.h (limited to 'include') diff --git a/include/common.h b/include/common.h index f3f5c0fcac..c30f6bd735 100644 --- a/include/common.h +++ b/include/common.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -175,25 +176,6 @@ static inline char *shell_expand(char *str) } #endif -/* Force a compilation error if condition is true */ -#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition)) - -/* Force a compilation error if condition is constant and true */ -#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)])) - -/* Force a compilation error if a constant expression is not a power of 2 */ -#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ - BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) - -/* - * Force a compilation error if condition is true, but also produce a - * result (of value 0 and type size_t), so the expression can be used - * e.g. in a structure initializer (or where-ever else comma - * expressions aren't permitted). - */ -#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) -#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) - #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1) #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) #define ALIGN_DOWN(x, a) ((x) & ~((typeof(x))(a) - 1)) diff --git a/include/linux/bug.h b/include/linux/bug.h new file mode 100644 index 0000000000..3449837b57 --- /dev/null +++ b/include/linux/bug.h @@ -0,0 +1,28 @@ +#ifndef _LINUX_BUG_H +#define _LINUX_BUG_H + +#ifdef __CHECKER__ +#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) +#define BUILD_BUG_ON_ZERO(e) (0) +#define BUILD_BUG_ON_NULL(e) ((void*)0) +#define BUILD_BUG_ON(condition) (0) +#else /* __CHECKER__ */ + +/* Force a compilation error if a constant expression is not a power of 2 */ +#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ + BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) + +/* Force a compilation error if condition is true, but also produce a + result (of value 0 and type size_t), so the expression can be used + e.g. in a structure initializer (or where-ever else comma expressions + aren't permitted). */ +#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) +#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) + +/* Force a compilation error if condition is true */ +#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition)) + +#endif + + +#endif /* _LINUX_BUG_H */ -- cgit v1.2.3 From 8960b0ff81875d569bfd58d185076dd6265ba42b Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Thu, 29 Jan 2015 11:14:06 +0900 Subject: asm-generic/bug.h: move BUG(), BUG_ON(), WARN(), WARN_ON() macros In Linux, these macros are defined in include/asm-generic/bug.h. To tidy up common.h, move BUG(), BUG_ON(), WARN(), WARN_ON() there. Signed-off-by: Masahiro Yamada Signed-off-by: Sascha Hauer --- include/asm-generic/bug.h | 37 +++++++++++++++++++++++++++++++++++++ include/common.h | 32 -------------------------------- include/linux/bug.h | 2 ++ 3 files changed, 39 insertions(+), 32 deletions(-) create mode 100644 include/asm-generic/bug.h (limited to 'include') diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h new file mode 100644 index 0000000000..8583d2425f --- /dev/null +++ b/include/asm-generic/bug.h @@ -0,0 +1,37 @@ +#ifndef _ASM_GENERIC_BUG_H +#define _ASM_GENERIC_BUG_H + +#include + +#define BUG() do { \ + printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __FUNCTION__); \ + panic("BUG!"); \ +} while (0) +#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0) + + +#define __WARN() do { \ + printf("WARNING: at %s:%d/%s()!\n", __FILE__, __LINE__, __FUNCTION__); \ +} while (0) + +#ifndef WARN_ON +#define WARN_ON(condition) ({ \ + int __ret_warn_on = !!(condition); \ + if (unlikely(__ret_warn_on)) \ + __WARN(); \ + unlikely(__ret_warn_on); \ +}) +#endif + +#ifndef WARN +#define WARN(condition, format...) ({ \ + int __ret_warn_on = !!(condition); \ + if (unlikely(__ret_warn_on)) { \ + __WARN(); \ + puts("WARNING: "); \ + printf(format); \ + } \ + unlikely(__ret_warn_on); \ +}) +#endif +#endif diff --git a/include/common.h b/include/common.h index c30f6bd735..289f7c931a 100644 --- a/include/common.h +++ b/include/common.h @@ -50,38 +50,6 @@ #error "None of __LITTLE_ENDIAN and __BIG_ENDIAN are defined" #endif -#define BUG() do { \ - printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __FUNCTION__); \ - panic("BUG!"); \ -} while (0) -#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0) - - -#define __WARN() do { \ - printf("WARNING: at %s:%d/%s()!\n", __FILE__, __LINE__, __FUNCTION__); \ -} while (0) - -#ifndef WARN_ON -#define WARN_ON(condition) ({ \ - int __ret_warn_on = !!(condition); \ - if (unlikely(__ret_warn_on)) \ - __WARN(); \ - unlikely(__ret_warn_on); \ -}) -#endif - -#ifndef WARN -#define WARN(condition, format...) ({ \ - int __ret_warn_on = !!(condition); \ - if (unlikely(__ret_warn_on)) { \ - __WARN(); \ - puts("WARNING: "); \ - printf(format); \ - } \ - unlikely(__ret_warn_on); \ -}) -#endif - #include /* boot information for Linux kernel */ /* diff --git a/include/linux/bug.h b/include/linux/bug.h index 3449837b57..7295618c98 100644 --- a/include/linux/bug.h +++ b/include/linux/bug.h @@ -1,6 +1,8 @@ #ifndef _LINUX_BUG_H #define _LINUX_BUG_H +#include + #ifdef __CHECKER__ #define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) #define BUILD_BUG_ON_ZERO(e) (0) -- cgit v1.2.3 From 41be004e8de52ae64264763278efeb7581be1e53 Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Thu, 29 Jan 2015 11:14:07 +0900 Subject: linux/kernel.h: move some macros and prototypes The include/common.h is still cluttered (although much better than U-Boot). It contains various utility macros that originates in Linux. Move them to the original place, include/linux/kernel.h, to slim down include/common.h. Signed-off-by: Masahiro Yamada Signed-off-by: Sascha Hauer --- include/common.h | 58 ----------------------------------------------- include/linux/kernel.h | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 58 deletions(-) (limited to 'include') diff --git a/include/common.h b/include/common.h index 289f7c931a..eef371c4bf 100644 --- a/include/common.h +++ b/include/common.h @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -58,7 +57,6 @@ void reginfo(void); void __noreturn hang (void); -void __noreturn panic(const char *fmt, ...); char *size_human_readable(unsigned long long size); @@ -76,11 +74,6 @@ void __noreturn poweroff(void); void udelay (unsigned long); void mdelay (unsigned long); -/* lib_generic/vsprintf.c */ -ulong simple_strtoul(const char *cp,char **endp,unsigned int base); -unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base); -long simple_strtol(const char *cp,char **endp,unsigned int base); - /* lib_generic/crc32.c */ uint32_t crc32(uint32_t, const void*, unsigned int); uint32_t crc32_no_comp(uint32_t, const void*, unsigned int); @@ -144,13 +137,8 @@ static inline char *shell_expand(char *str) } #endif -#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1) -#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) #define ALIGN_DOWN(x, a) ((x) & ~((typeof(x))(a) - 1)) -#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) -#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) -#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) #define ARRAY_AND_SIZE(x) (x), ARRAY_SIZE(x) /* @@ -164,17 +152,6 @@ static inline char *shell_expand(char *str) char __##name[sizeof(type) * (size) + (align) - 1]; \ type *name = (type *)ALIGN((uintptr_t)__##name, align) -/** - * container_of - cast a member of a structure out to the containing structure - * @ptr: the pointer to the member. - * @type: the type of the container struct this is embedded in. - * @member: the name of the member within the struct. - * - */ -#define container_of(ptr, type, member) ({ \ - const typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offsetof(type,member) );}) - #define PAGE_SIZE 4096 #define PAGE_SHIFT 12 #define PAGE_ALIGN(s) (((s) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) @@ -216,41 +193,6 @@ void barebox_set_hostname(const char *); #define IOMEM(addr) ((void __force __iomem *)(addr)) #endif -#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) - -#define DIV_ROUND_CLOSEST(x, divisor)( \ -{ \ - typeof(divisor) __divisor = divisor; \ - (((x) + ((__divisor) / 2)) / (__divisor)); \ -} \ -) - -/** - * upper_32_bits - return bits 32-63 of a number - * @n: the number we're accessing - * - * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress - * the "right shift count >= width of type" warning when that quantity is - * 32-bits. - */ -#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) - -/** - * lower_32_bits - return bits 0-31 of a number - * @n: the number we're accessing - */ -#define lower_32_bits(n) ((u32)(n)) - -#define abs(x) ({ \ - long __x = (x); \ - (__x < 0) ? -__x : __x; \ - }) - -#define abs64(x) ({ \ - s64 __x = (x); \ - (__x < 0) ? -__x : __x; \ - }) - /* * Check if two regions overlap. returns true if they do, false otherwise */ diff --git a/include/linux/kernel.h b/include/linux/kernel.h index a0395909d0..3f2644cf65 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -2,6 +2,7 @@ #define _LINUX_KERNEL_H #include +#include #include #define USHRT_MAX ((u16)(~0U)) @@ -31,6 +32,13 @@ #define S64_MAX ((s64)(U64_MAX>>1)) #define S64_MIN ((s64)(-S64_MAX - 1)) +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1) +#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) +#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) +#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) + /* * This looks more complex than it should be. But we need to * get the type for the ~ right in round_down (it needs to be @@ -44,6 +52,47 @@ #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) #define round_down(x, y) ((x) & ~__round_mask(x, y)) +#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) + +#define DIV_ROUND_CLOSEST(x, divisor)( \ +{ \ + typeof(divisor) __divisor = divisor; \ + (((x) + ((__divisor) / 2)) / (__divisor)); \ +} \ +) + +/** + * upper_32_bits - return bits 32-63 of a number + * @n: the number we're accessing + * + * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress + * the "right shift count >= width of type" warning when that quantity is + * 32-bits. + */ +#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) + +/** + * lower_32_bits - return bits 0-31 of a number + * @n: the number we're accessing + */ +#define lower_32_bits(n) ((u32)(n)) + +#define abs(x) ({ \ + long __x = (x); \ + (__x < 0) ? -__x : __x; \ + }) + +#define abs64(x) ({ \ + s64 __x = (x); \ + (__x < 0) ? -__x : __x; \ + }) + +void __noreturn panic(const char *fmt, ...); + +extern unsigned long simple_strtoul(const char *,char **,unsigned int); +extern long simple_strtol(const char *,char **,unsigned int); +extern unsigned long long simple_strtoull(const char *,char **,unsigned int); + /* * min()/max()/clamp() macros that also do * strict type-checking.. See the @@ -197,4 +246,16 @@ static inline char *hex_byte_pack_upper(char *buf, u8 byte) return buf; } +/** + * container_of - cast a member of a structure out to the containing structure + * @ptr: the pointer to the member. + * @type: the type of the container struct this is embedded in. + * @member: the name of the member within the struct. + * + */ +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) + + #endif /* _LINUX_KERNEL_H */ -- cgit v1.2.3