From c95b4f4fdc90f14123e6d14c23c4c2a25d32f74f Mon Sep 17 00:00:00 2001 From: Justus Winter <4winter@informatik.uni-hamburg.de> Date: Tue, 31 Mar 2015 12:57:05 +0200 Subject: [PATCH gnumach 1/7] kern: import `macros.h' from x15 Import the macro definitions from the x15 kernel project, and replace all similar definitions littered all over the place with it. Importing this file will make importing code from the x15 kernel easier. We are already using the red-black tree implementation and the slab allocator from it, and we will import even more code in the near future. * Makefrag.am (libkernel_a_SOURCES): Add new file. * kern/list.h: Do not define `structof', include `macros.h' instead. * kern/rbtree.h: Likewise. * kern/slab.c: Do not define `ARRAY_SIZE', include `macros.h' instead. * i386/grub/misc.h: Likewise. * kern/macros.h: New file. --- Makefrag.am | 1 + i386/grub/misc.h | 2 +- kern/list.h | 4 +--- kern/macros.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ kern/rbtree.h | 5 +--- kern/slab.c | 2 +- 6 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 kern/macros.h diff --git a/Makefrag.am b/Makefrag.am index 9166143..77110c8 100644 --- a/Makefrag.am +++ b/Makefrag.am @@ -172,6 +172,7 @@ libkernel_a_SOURCES += \ kern/machine.c \ kern/machine.h \ kern/macro_help.h \ + kern/macros.h \ kern/pc_sample.c \ kern/pc_sample.h \ kern/printf.c \ diff --git a/i386/grub/misc.h b/i386/grub/misc.h index c6cd456..b71140a 100644 --- a/i386/grub/misc.h +++ b/i386/grub/misc.h @@ -21,6 +21,7 @@ #define GRUB_MISC_HEADER 1 #include +#include #include #include #include @@ -32,7 +33,6 @@ #define ALIGN_UP_OVERHEAD(addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1)) #define ALIGN_DOWN(addr, align) \ ((addr) & ~((typeof (addr)) align - 1)) -#define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0])) #define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; } #define grub_dprintf(condition, ...) grub_real_dprintf(GRUB_FILE, __LINE__, condition, __VA_ARGS__) diff --git a/kern/list.h b/kern/list.h index ad782a8..be92762 100644 --- a/kern/list.h +++ b/kern/list.h @@ -31,9 +31,7 @@ #include #include - -#define structof(ptr, type, member) \ - ((type *)((char *)ptr - offsetof(type, member))) +#include /* * Structure used as both head and node. diff --git a/kern/macros.h b/kern/macros.h new file mode 100644 index 0000000..db38842 --- /dev/null +++ b/kern/macros.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2009, 2010, 2013 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 . + * + * + * Helper macros. + */ + +#ifndef _KERN_MACROS_H +#define _KERN_MACROS_H + +#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 DIV_CEIL(n, d) (((n) + (d) - 1) / (d)) + +#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 */ diff --git a/kern/rbtree.h b/kern/rbtree.h index f577f7e..4ee0e15 100644 --- a/kern/rbtree.h +++ b/kern/rbtree.h @@ -31,12 +31,9 @@ #include #include -#include +#include #include -#define structof(ptr, type, member) \ - ((type *)((char *)ptr - offsetof(type, member))) - /* * Indexes of the left and right nodes in the children array of a node. */ diff --git a/kern/slab.c b/kern/slab.c index 19ebfed..60378b5 100644 --- a/kern/slab.c +++ b/kern/slab.c @@ -79,6 +79,7 @@ #include #include #include +#include #include #include #include @@ -96,7 +97,6 @@ /* * Utility macros. */ -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define P2ALIGNED(x, a) (((x) & ((a) - 1)) == 0) #define ISP2(x) P2ALIGNED(x, x) #define P2ALIGN(x, a) ((x) & -(a)) -- 2.1.4