From 2a0ed8121ac690bc8aabefa30e837ffa90e9e754 Mon Sep 17 00:00:00 2001 From: fef Date: Tue, 21 Sep 2021 17:34:27 +0200 Subject: [PATCH] util: refactor misc utility stuff --- arch/x86/boot/boot.c | 2 +- include/gay/cdefs.h | 39 +++++------- include/gay/config.h.in | 2 + include/gay/kprintf.h | 2 +- include/gay/util.h | 77 ++++++++++++++++++++++++ kernel/CMakeLists.txt | 1 + include/gay/toolchain.h => kernel/util.c | 20 +++--- 7 files changed, 106 insertions(+), 37 deletions(-) create mode 100644 include/gay/util.h rename include/gay/toolchain.h => kernel/util.c (70%) diff --git a/arch/x86/boot/boot.c b/arch/x86/boot/boot.c index 9030d9f..f5f7b2f 100644 --- a/arch/x86/boot/boot.c +++ b/arch/x86/boot/boot.c @@ -4,9 +4,9 @@ #include -#include #include #include +#include enum vga_color { VGA_COLOR_BLACK = 0, diff --git a/include/gay/cdefs.h b/include/gay/cdefs.h index e1a50ab..e13c280 100644 --- a/include/gay/cdefs.h +++ b/include/gay/cdefs.h @@ -2,6 +2,20 @@ #pragma once +#ifndef __clang__ +#error "Unsupported compiler, please use clang" +#endif + +#ifdef __cplusplus +#define __restrict +#else +#define __restrict restrict +#endif + +#define __aligned(bytes) __attribute__(( aligned(bytes) )) + +#define __section(name) __attribute__(( section(#name) )) + #ifndef NULL #define NULL ((void *)0) #endif @@ -14,31 +28,6 @@ #define typeof(expr) __typeof(expr) #endif -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) - -/** - * @brief Cast a pointer to a struct member out to the containing structure. - * - * @param ptr Pointer to the struct member - * @param type Type of the containing structure - * @param member Name of the member within the containing structure - * @returns Pointer to the containing structure - */ -#define container_of(ptr, type, member) \ - ((type *)( (void *)(ptr) - offsetof(type, member) )) - -/** - * @brief Do an evil raw cast. - * - * @param type Type to cast to - * @param expr Expression to cast - * @returns The raw value of `expr`, cast to `type` - */ -#define raw_cast(type, expr) ({ \ - typeof(expr) __expr = (expr); \ - *(type *)&__expr; \ -}) - /* * This file is part of GayBSD. * Copyright (c) 2021 fef . diff --git a/include/gay/config.h.in b/include/gay/config.h.in index cfe0357..c4ca0e0 100644 --- a/include/gay/config.h.in +++ b/include/gay/config.h.in @@ -9,6 +9,8 @@ #pragma once +#cmakedefine DEBUG + #define GAY_VERSION_MAJOR @gaybsd_VERSION_MAJOR@ #define GAY_VERSION_MINOR @gaybsd_VERSION_MINOR@ #define GAY_VERSION_PATCH @gaybsd_VERSION_PATCH@ diff --git a/include/gay/kprintf.h b/include/gay/kprintf.h index 9a1717b..d3bc236 100644 --- a/include/gay/kprintf.h +++ b/include/gay/kprintf.h @@ -8,8 +8,8 @@ #include +#include #include -#include /** * @brief Print to the kernel log. diff --git a/include/gay/util.h b/include/gay/util.h new file mode 100644 index 0000000..a9f6771 --- /dev/null +++ b/include/gay/util.h @@ -0,0 +1,77 @@ +/* See the end of this file for copyright and license terms. */ + +#pragma once + +#include + +/** + * @brief Get the number of elements in a statically allocated array. + * + * @param a The array + * @returns The number of elements + */ +#define ARRAY_SIZE(a) ( sizeof(a) / sizeof(a[0]) ) + +#define abs(x) ({ \ + typeof(x) __x = (x); \ + __x < 0 ? -__x : __x; \ +}) + +#define max(a, b) ({ \ + typeof(a) __a = (a); \ + typeof(b) __b = (b); \ + __a > __b ? __a : __b; \ +}) + +#define min(a, b) ({ \ + typeof(a) __a = (a); \ + typeof(b) __b = (b); \ + __a < __b ? __a : __b; \ +}) + +/** + * @brief Cast a pointer to a struct member out to the containing structure. + * + * @param ptr Pointer to the struct member + * @param type Type of the containing structure + * @param member Name of the member within the containing structure + * @returns Pointer to the containing structure + */ +#define container_of(ptr, type, member) \ + ((type *)( (void *)(ptr) - offsetof(type, member) )) + +/** + * @brief Do an evil raw cast. + * + * @param type Type to cast to + * @param expr Expression to cast + * @returns The raw value of `expr`, cast to `type` + */ +#define raw_cast(type, expr) ({ \ + typeof(expr) __expr = (expr); \ + *(type *)&__expr; \ +}) + +/** + * @brief Align `ptr` such that `ptr % n == 0`. + * + * @param ptr Pointer to align + * @param log The log2 of the alignment. If negative, the pointer will be + * aligned to the next lower address, otherwise to the next higher one. + * @returns The aligned pointer + */ +void *ptr_align(void *ptr, int log); + +/* + * This file is part of GayBSD. + * Copyright (c) 2021 fef . + * + * GayBSD is nonviolent software: you may only use, redistribute, and/or + * modify it under the terms of the Cooperative Nonviolent Public License + * (CNPL) as found in the LICENSE file in the source code root directory + * or at ; either version 7 + * of the license, or (at your option) any later version. + * + * GayBSD comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPL for details. + */ diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 7e3ae65..3067b0a 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -10,6 +10,7 @@ target_sources(gay_kernel PRIVATE clist.c kprintf.c main.c + util.c ) # This file is part of GayBSD. diff --git a/include/gay/toolchain.h b/kernel/util.c similarity index 70% rename from include/gay/toolchain.h rename to kernel/util.c index f776eac..d1b2dfc 100644 --- a/include/gay/toolchain.h +++ b/kernel/util.c @@ -1,18 +1,18 @@ /* See the end of this file for copyright and license terms. */ -#pragma once +#include +#include -#ifndef __clang__ -#error "Unsupported compiler, please use clang" -#endif +void *ptr_align(void *ptr, int log) +{ + uintptr_t shifted = 1 << abs(log); + uintptr_t aligned = (uintptr_t)ptr & ~(shifted - 1); -#ifdef __cplusplus -#define __restrict -#else -#define __restrict restrict -#endif + if (log > 0 && aligned != (uintptr_t)ptr) + aligned += shifted; -#define __section(name) __attribute__(( section(#name) )) + return (void *)aligned; +} /* * This file is part of GayBSD.