You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.1 KiB
C
78 lines
2.1 KiB
C
/* See the end of this file for copyright and license terms. */
|
|
|
|
#pragma once
|
|
|
|
#include <gay/cdefs.h>
|
|
#include <gay/types.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 Determine whether `ptr` is kinda sus. */
|
|
static __always_inline bool sus_nil(const void *ptr)
|
|
{
|
|
return ptr < (void *)0x1000;
|
|
}
|
|
|
|
static __always_inline uintptr_t _align_floor(uintptr_t ptr, usize size)
|
|
{
|
|
return ptr - ptr % size;
|
|
}
|
|
#define align_floor(ptr, size) ( (typeof(ptr))_align_floor((uintptr_t)(ptr), size) )
|
|
|
|
static __always_inline uintptr_t _align_ceil(uintptr_t ptr, usize size)
|
|
{
|
|
uintptr_t aligned = ptr - ptr % size;
|
|
if (aligned < ptr)
|
|
aligned += size;
|
|
return aligned;
|
|
}
|
|
#define align_ceil(ptr, size) ( (typeof(ptr))_align_ceil((uintptr_t)(ptr), size) )
|
|
|
|
/*
|
|
* 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.
|
|
*/
|