libs,klib: collect useful arch-independent macros

This commit is contained in:
Zihao Yu 2020-02-06 23:39:10 +08:00
parent e670d1f2ca
commit 63db97d758
3 changed files with 44 additions and 35 deletions

View File

@ -15,9 +15,7 @@
#ifndef __ASSEMBLER__
#include <am.h>
#define ROUNDUP(a, sz) ((((uintptr_t)a)+(sz)-1) & ~((sz)-1))
#define ROUNDDOWN(a, sz) ((((uintptr_t)a)) & ~((sz)-1))
#include <klib-macros.h>
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 { \

View File

@ -0,0 +1,42 @@
#ifndef __KLIB_MACROS_H__
#define __KLIB_MACROS_H__
#include <am.h>
#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

View File

@ -1,18 +1,11 @@
#include <stdint.h>
#include <limits.h>
#include <klib.h>
#define FLAGNUM 4
#include <klib-macros.h>
#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]))