util: refactor misc utility stuff
This commit is contained in:
parent
623daf58ed
commit
2a0ed8121a
7 changed files with 106 additions and 37 deletions
|
@ -4,9 +4,9 @@
|
||||||
|
|
||||||
#include <arch/multiboot.h>
|
#include <arch/multiboot.h>
|
||||||
|
|
||||||
#include <gay/cdefs.h>
|
|
||||||
#include <gay/kprintf.h>
|
#include <gay/kprintf.h>
|
||||||
#include <gay/types.h>
|
#include <gay/types.h>
|
||||||
|
#include <gay/util.h>
|
||||||
|
|
||||||
enum vga_color {
|
enum vga_color {
|
||||||
VGA_COLOR_BLACK = 0,
|
VGA_COLOR_BLACK = 0,
|
||||||
|
|
|
@ -2,6 +2,20 @@
|
||||||
|
|
||||||
#pragma once
|
#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
|
#ifndef NULL
|
||||||
#define NULL ((void *)0)
|
#define NULL ((void *)0)
|
||||||
#endif
|
#endif
|
||||||
|
@ -14,31 +28,6 @@
|
||||||
#define typeof(expr) __typeof(expr)
|
#define typeof(expr) __typeof(expr)
|
||||||
#endif
|
#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.
|
* This file is part of GayBSD.
|
||||||
* Copyright (c) 2021 fef <owo@fef.moe>.
|
* Copyright (c) 2021 fef <owo@fef.moe>.
|
||||||
|
|
|
@ -9,6 +9,8 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#cmakedefine DEBUG
|
||||||
|
|
||||||
#define GAY_VERSION_MAJOR @gaybsd_VERSION_MAJOR@
|
#define GAY_VERSION_MAJOR @gaybsd_VERSION_MAJOR@
|
||||||
#define GAY_VERSION_MINOR @gaybsd_VERSION_MINOR@
|
#define GAY_VERSION_MINOR @gaybsd_VERSION_MINOR@
|
||||||
#define GAY_VERSION_PATCH @gaybsd_VERSION_PATCH@
|
#define GAY_VERSION_PATCH @gaybsd_VERSION_PATCH@
|
||||||
|
|
|
@ -8,8 +8,8 @@
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#include <gay/cdefs.h>
|
||||||
#include <gay/types.h>
|
#include <gay/types.h>
|
||||||
#include <gay/toolchain.h>
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print to the kernel log.
|
* @brief Print to the kernel log.
|
||||||
|
|
77
include/gay/util.h
Normal file
77
include/gay/util.h
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
/* See the end of this file for copyright and license terms. */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <gay/cdefs.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 <owo@fef.moe>.
|
||||||
|
*
|
||||||
|
* 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 <https://git.pixie.town/thufie/npl-builder>; 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.
|
||||||
|
*/
|
|
@ -10,6 +10,7 @@ target_sources(gay_kernel PRIVATE
|
||||||
clist.c
|
clist.c
|
||||||
kprintf.c
|
kprintf.c
|
||||||
main.c
|
main.c
|
||||||
|
util.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# This file is part of GayBSD.
|
# This file is part of GayBSD.
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
/* See the end of this file for copyright and license terms. */
|
/* See the end of this file for copyright and license terms. */
|
||||||
|
|
||||||
#pragma once
|
#include <gay/types.h>
|
||||||
|
#include <gay/util.h>
|
||||||
|
|
||||||
#ifndef __clang__
|
void *ptr_align(void *ptr, int log)
|
||||||
#error "Unsupported compiler, please use clang"
|
{
|
||||||
#endif
|
uintptr_t shifted = 1 << abs(log);
|
||||||
|
uintptr_t aligned = (uintptr_t)ptr & ~(shifted - 1);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
if (log > 0 && aligned != (uintptr_t)ptr)
|
||||||
#define __restrict
|
aligned += shifted;
|
||||||
#else
|
|
||||||
#define __restrict restrict
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define __section(name) __attribute__(( section(#name) ))
|
return (void *)aligned;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of GayBSD.
|
* This file is part of GayBSD.
|
Loading…
Reference in a new issue