diff --git a/.gitignore b/.gitignore index afb7f5e..a10fd01 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,6 @@ dkms.conf /isodir serial.log *.cpio + +# Auxilar files +.vscode/ \ No newline at end of file diff --git a/kernel/include/kstdint.h b/kernel/include/kstdint.h index b71c842..93c42cb 100644 --- a/kernel/include/kstdint.h +++ b/kernel/include/kstdint.h @@ -1,14 +1,41 @@ #ifndef __K_STDINT #define __K_STDINT -typedef char int8_t; -typedef short int16_t; -typedef int int32_t; +// unsigned types +typedef unsigned long long uint64_t; +typedef unsigned int uint32_t; +typedef unsigned short uint16_t; +typedef unsigned char uint8_t; + +typedef uint64_t u64; +typedef uint32_t u32; +typedef uint16_t u16; +typedef uint8_t u8; + +// signed types typedef long long int64_t; +typedef int int32_t; +typedef short int16_t; +typedef char int8_t; -typedef unsigned char uint8_t; -typedef unsigned short uint16_t; -typedef unsigned int uint32_t; -typedef unsigned long long uint64_t; +typedef int64_t i64; +typedef int32_t i32; +typedef int16_t i16; +typedef int8_t i8; + +// floating point types +typedef double f64; +typedef float f32; + +// other +typedef int64_t ssize_t; +typedef uint64_t size_t; + +typedef i64 ssize; +typedef u64 usize; + +// memory manager +typedef u32 phys_addr_t; +typedef u32 virt_addr_t; #endif \ No newline at end of file diff --git a/kernel/include/kstdlib.h b/kernel/include/kstdlib.h index 38a4be6..53215bd 100644 --- a/kernel/include/kstdlib.h +++ b/kernel/include/kstdlib.h @@ -1,6 +1,78 @@ #ifndef __K_STDLIB #define __K_STDLIB +#include + + +#define BITS_PER_BYTE 8 + +/** @brief Ignore unused variable/static function.*/ #define IGNORE_UNUSED(x) ((void)x) +/** + * @brief Convert bits to bytes. + * + * @param [in] n - given number of bits. + * @return number of bytes needed for containing n bits. + */ +static inline usize bits_to_bytes(usize n) +{ + // assuming that a byte contains 8 bits + return (n + 7) >> 3; +} + +/** + * @brief Convert bytes to bits. + * + * @param [in] n - given number of bytes. + * @return number of bits in n bytes. + */ +static inline usize bytes_to_bits(usize n) +{ + // assuming that a byte contains 8 bits + return n << 3; +} + +/** + * @brief Get number of bits in value. + * + * @param [in] x - given value to count. + * @return number of bits in value. + */ +#define BITS_PER_TYPE(x) (bytes_to_bits(sizeof(x))) + +/** + * @brief Set the specific bit of given value. + * + * @param [out] value - given value to change. + * @param [in] pos - given bit position to set. + */ +static inline void set_bit(u32* value, i32 pos) +{ + *value |= (1U << pos); +} + +/** + * @brief Clear the specific bit of given value. + * + * @param [out] value - given value to change. + * @param [in] pos - given bit position to clear. + */ +static inline void clear_bit(u32* value, i32 pos) +{ + *value &= ~(1U << pos); +} + +/** + * @brief Get the specific bit of given value. + * + * @param [in] value - given value to test. + * @param [in] pos - given bit position to test. + * @return true if the bit is set, false otherwise. + */ +static inline bool test_bit(u32 value, i32 pos) +{ + return (value & (1U << pos)) != 0; +} + #endif \ No newline at end of file