Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ dkms.conf
/isodir
serial.log
*.cpio

# Auxilar files
.vscode/
41 changes: 34 additions & 7 deletions kernel/include/kstdint.h
Original file line number Diff line number Diff line change
@@ -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
72 changes: 72 additions & 0 deletions kernel/include/kstdlib.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,78 @@
#ifndef __K_STDLIB
#define __K_STDLIB

#include <kstdint.h>


#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