diff options
author | Richard Braun <rbraun@sceen.net> | 2012-11-03 17:30:15 +0100 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2012-11-03 17:30:15 +0100 |
commit | 27484e07cdd0be148743cb8e4acd99e96264eca9 (patch) | |
tree | 3d32b0b274a879e5f814ff366b7d8bd6f2c973ba /kern/macros.h | |
parent | 6b04aaae27ef9cc26487da1dd2acf75f3b49e3f5 (diff) |
Merge lib into kern
There are no precise enough criteria to justify the separation of these
two directories.
Diffstat (limited to 'kern/macros.h')
-rw-r--r-- | kern/macros.h | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/kern/macros.h b/kern/macros.h new file mode 100644 index 00000000..012dd15b --- /dev/null +++ b/kern/macros.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2009, 2010 Richard Braun. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * + * Helper macros. + */ + +#ifndef _KERN_MACROS_H +#define _KERN_MACROS_H + +#ifndef __ASSEMBLER__ +#include <kern/stddef.h> +#endif /* __ASSEMBLER__ */ + +#define MACRO_BEGIN ({ +#define MACRO_END }) + +#define __QUOTE(x) #x +#define QUOTE(x) __QUOTE(x) + +#ifdef __ASSEMBLER__ +#define DECL_CONST(x, s) x +#else /* __ASSEMBLER__ */ +#define __DECL_CONST(x, s) x##s +#define DECL_CONST(x, s) __DECL_CONST(x, s) +#endif /* __ASSEMBLER__ */ + +#define STRLEN(x) (sizeof(x) - 1) +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +#define P2ALIGNED(x, a) (((x) & ((a) - 1)) == 0) +#define ISP2(x) P2ALIGNED(x, x) +#define P2ALIGN(x, a) ((x) & -(a)) +#define P2ROUND(x, a) (-(-(x) & -(a))) +#define P2END(x, a) (-(~(x) & -(a))) + +#define structof(ptr, type, member) \ + ((type *)((char *)ptr - offsetof(type, member))) + +#define alignof(x) __alignof__(x) + +#define likely(expr) __builtin_expect(!!(expr), 1) +#define unlikely(expr) __builtin_expect(!!(expr), 0) + +#define barrier() asm volatile("" : : : "memory") + +#define __noreturn __attribute__((noreturn)) +#define __aligned(x) __attribute__((aligned(x))) +#define __always_inline inline __attribute__((always_inline)) +#define __section(x) __attribute__((section(x))) +#define __packed __attribute__((packed)) +#define __alias(x) __attribute__((alias(x))) + +#define __format_printf(fmt, args) \ + __attribute__((format(printf, fmt, args))) + +#endif /* _KERN_MACROS_H */ |