diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-05-02 01:34:12 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-05-02 01:34:12 +0800 |
| commit | a9c458c6d5adf90739d06b4395a44db75d037e89 (patch) | |
| tree | dcc0ece3334ab8e1756fc3c95a67f9953ba70de1 | |
| parent | e1561995d4064847de60daf5342f8e7ff69e7e30 (diff) | |
| download | lume-a9c458c6d5adf90739d06b4395a44db75d037e89.tar.gz lume-a9c458c6d5adf90739d06b4395a44db75d037e89.tar.xz | |
feat: add centralized compiler macros and integrate into rbtree headers
| -rw-r--r-- | na/include/lume/compiler.h | 27 | ||||
| -rw-r--r-- | na/include/lume/rbtree.h | 5 | ||||
| -rw-r--r-- | na/include/lume/rbtree_augmented.h | 20 |
3 files changed, 28 insertions, 24 deletions
diff --git a/na/include/lume/compiler.h b/na/include/lume/compiler.h new file mode 100644 index 0000000..fa05363 --- /dev/null +++ b/na/include/lume/compiler.h @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Copyright (C) 2026 Richard Qin */ + +#pragma once + +/** + * compiler.h - Centralized management of compiler-specific macros and built-in wrappers. + */ + +/* Memory barrier macros */ +#define WRITE_ONCE(x, val) \ + do { \ + __asm__ volatile("" ::: "memory"); \ + (x) = (val); \ + __asm__ volatile("" ::: "memory"); \ + } while (0) + +#define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x)) + +#define barrier() __asm__ volatile("" ::: "memory") + +/* Branch prediction hints */ +#define unlikely(x) __builtin_expect(!!(x), 0) +#define likely(x) __builtin_expect(!!(x), 1) + +/* Function attributes */ +#define __always_inline inline __attribute__((always_inline)) diff --git a/na/include/lume/rbtree.h b/na/include/lume/rbtree.h index 331875c..8bc8104 100644 --- a/na/include/lume/rbtree.h +++ b/na/include/lume/rbtree.h @@ -8,6 +8,7 @@ #pragma once +#include <lume/compiler.h> #include <lume/rbtree_types.h> /* ------------------------------------------------------------------ */ @@ -20,10 +21,6 @@ reinterpret_cast<char *>(ptr) - __builtin_offsetof(type, member)) #endif -#ifndef READ_ONCE -#define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x)) -#endif - /* ------------------------------------------------------------------ */ /* Core macros */ /* ------------------------------------------------------------------ */ diff --git a/na/include/lume/rbtree_augmented.h b/na/include/lume/rbtree_augmented.h index 07f367b..d4559c4 100644 --- a/na/include/lume/rbtree_augmented.h +++ b/na/include/lume/rbtree_augmented.h @@ -13,26 +13,6 @@ #include <lume/rbtree.h> /* ------------------------------------------------------------------ */ -/* Linux-compat macros for freestanding kernel */ -/* ------------------------------------------------------------------ */ - -#ifndef WRITE_ONCE -#define WRITE_ONCE(x, val) do { \ - __asm__ volatile("" ::: "memory"); \ - (x) = (val); \ - __asm__ volatile("" ::: "memory"); \ -} while (0) -#endif - -#ifndef unlikely -#define unlikely(x) __builtin_expect(!!(x), 0) -#endif - -#ifndef __always_inline -#define __always_inline inline __attribute__((always_inline)) -#endif - -/* ------------------------------------------------------------------ */ /* Augmented callback structure */ /* ------------------------------------------------------------------ */ |
