From 63db97d75801d0cf8f9c42919c024930eb75f7cc Mon Sep 17 00:00:00 2001 From: Zihao Yu Date: Thu, 6 Feb 2020 23:39:10 +0800 Subject: [PATCH] libs,klib: collect useful arch-independent macros --- am/src/x86/qemu/x86-qemu.h | 28 +-------------------- libs/klib/include/klib-macros.h | 42 ++++++++++++++++++++++++++++++++ tests/klibtest/src/printf_test.c | 9 +------ 3 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 libs/klib/include/klib-macros.h diff --git a/am/src/x86/qemu/x86-qemu.h b/am/src/x86/qemu/x86-qemu.h index 830ca7de..4b39a25a 100644 --- a/am/src/x86/qemu/x86-qemu.h +++ b/am/src/x86/qemu/x86-qemu.h @@ -15,9 +15,7 @@ #ifndef __ASSEMBLER__ #include - -#define ROUNDUP(a, sz) ((((uintptr_t)a)+(sz)-1) & ~((sz)-1)) -#define ROUNDDOWN(a, sz) ((((uintptr_t)a)) & ~((sz)-1)) +#include struct kernel_stack { uint8_t stack[8192]; @@ -75,30 +73,6 @@ extern int __am_ncpu; extern struct cpu_local __am_cpuinfo[MAX_CPU]; #define CPU (&__am_cpuinfo[_cpu()]) -#define LENGTH(arr) (sizeof(arr) / sizeof((arr)[0])) -#define RANGE(st, ed) (_Area) { .start = (void *)st, .end = (void *)ed } -#define IN_RANGE(ptr, area) ((area).start <= (ptr) && (ptr) < (area).end) -#define STRINGIFY(s) #s -#define TOSTRING(s) STRINGIFY(s) -static inline void *upcast(uint32_t ptr) { - return (void *)(uintptr_t)ptr; -} - -static inline void puts(const char *s) { - for (; *s; s++) - _putc(*s); -} - -#define panic_on(cond, s) \ - do { \ - if (cond) { \ - puts("AM Panic: "); puts(s); \ - puts(" @ " __FILE__ ":" TOSTRING(__LINE__) " \n"); \ - _halt(1); \ - } \ - } while (0) - -#define panic(s) panic_on(1, s) #define bug_on(cond) \ do { \ diff --git a/libs/klib/include/klib-macros.h b/libs/klib/include/klib-macros.h new file mode 100644 index 00000000..1ab99bf5 --- /dev/null +++ b/libs/klib/include/klib-macros.h @@ -0,0 +1,42 @@ +#ifndef __KLIB_MACROS_H__ +#define __KLIB_MACROS_H__ + +#include + +#define ROUNDUP(a, sz) ((((uintptr_t)a)+(sz)-1) & ~((sz)-1)) +#define ROUNDDOWN(a, sz) ((((uintptr_t)a)) & ~((sz)-1)) + +#define LENGTH(arr) (sizeof(arr) / sizeof((arr)[0])) + +#define RANGE(st, ed) (_Area) { .start = (void *)st, .end = (void *)ed } +#define IN_RANGE(ptr, area) ((area).start <= (ptr) && (ptr) < (area).end) + +#define STRINGIFY(s) #s +#define TOSTRING(s) STRINGIFY(s) + +#define _CONCAT(x, y) x ## y +#define CONCAT(x, y) _CONCAT(x, y) + +#define static_assert(const_cond) \ + static char CONCAT(_static_assert_, __LINE__) [(const_cond) ? 1 : -1] __attribute__((unused)) + +static inline void *upcast(uint32_t ptr) { + return (void *)(uintptr_t)ptr; +} + +static inline void putstr(const char *s) { + while (*s) _putc(*s ++); +} + +#define panic_on(cond, s) \ + do { \ + if (cond) { \ + putstr("AM Panic: "); putstr(s); \ + putstr(" @ " __FILE__ ":" TOSTRING(__LINE__) " \n"); \ + _halt(1); \ + } \ + } while (0) + +#define panic(s) panic_on(1, s) + +#endif diff --git a/tests/klibtest/src/printf_test.c b/tests/klibtest/src/printf_test.c index 69ed3fa6..e36ec583 100644 --- a/tests/klibtest/src/printf_test.c +++ b/tests/klibtest/src/printf_test.c @@ -1,18 +1,11 @@ #include #include #include - -#define FLAGNUM 4 +#include #define PRINTABLE_CH " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" #define STRLEN(const_str) (sizeof(const_str) - 1) // sizeof counts the null byte -#define concat_temp(x, y) x ## y -#define concat(x, y) concat_temp(x, y) - -#define static_assert(const_cond) \ - static char concat(_static_assert_, __LINE__) [(const_cond ? 1 : -1)] __attribute__((unused)) - #define ARRAY_DEF(type, name, smax, smin, umax) \ static const type name [] = {0, smax / 17, smax, smin, smin + 1, umax / 17, smin / 17, umax} #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))