libluna: Document Bitset.h

This commit is contained in:
apio 2023-08-23 14:45:53 +02:00
parent 3f1e6fc2ff
commit 798a6d63aa
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 67 additions and 3 deletions

View File

@ -24,13 +24,16 @@ Result<void> check_pledge(Thread* thread, Promise promise)
// Thread::process_pending_signals(). // Thread::process_pending_signals().
thread->signal_handlers[SIGABRT - 1].sa_handler = SIG_DFL; thread->signal_handlers[SIGABRT - 1].sa_handler = SIG_DFL;
// Unblock SIGABRT.
thread->signal_mask.set(SIGABRT - 1, false);
// If there are any other pending signals, they might be processed before SIGABRT. Avoid that by resetting the // If there are any other pending signals, they might be processed before SIGABRT. Avoid that by resetting the
// thread's pending signals. // thread's pending signals.
thread->pending_signals = 0; thread->pending_signals.clear();
thread->send_signal(SIGABRT); thread->send_signal(SIGABRT);
// This should never arrive to userspace. // This should never arrive to userspace, unless we're init and have ignored SIGABRT.
return err(ENOSYS); return err(ENOSYS);
} }

View File

@ -1,27 +1,66 @@
/**
* @file Bitset.h
* @author apio (cloudapio.eu)
* @brief A wrapper class to modify/inspect specific bits of an integer type.
*
* @copyright Copyright (c) 2023, the Luna authors.
*
*/
#pragma once #pragma once
#include <luna/Check.h> #include <luna/Check.h>
/**
* @brief A wrapper class to modify/inspect specific bits of an integer type, to avoid manual error-prone bit shifting.
*
* @tparam T The integer type to use.
*/
template <typename T> class Bitset template <typename T> class Bitset
{ {
public: public:
Bitset() : m_value() /**
* @brief Construct a new empty Bitset object.
*/
Bitset() : m_value(0)
{ {
} }
/**
* @brief Construct a new Bitset object with a value.
*
* @param value The value to use.
*/
Bitset(T value) : m_value(value) Bitset(T value) : m_value(value)
{ {
} }
/**
* @brief Construct a copy of another Bitset object.
*
* @param other The original object.
*/
Bitset(const Bitset<T>& other) : m_value(other.m_value) Bitset(const Bitset<T>& other) : m_value(other.m_value)
{ {
} }
/**
* @brief Update the internal value of this bitset.
*
* @param value The new value.
* @return Bitset<T>& A reference to this itset.
*/
Bitset<T>& operator=(T value) Bitset<T>& operator=(T value)
{ {
m_value = value; m_value = value;
return *this; return *this;
} }
/**
* @brief Set this bitset to the same value as another bitset.
*
* @param other The pther bitset.
* @return Bitset<T>& A reference to this bitset.
*/
Bitset<T>& operator=(const Bitset<T>& other) Bitset<T>& operator=(const Bitset<T>& other)
{ {
if (&other == this) return *this; if (&other == this) return *this;
@ -29,11 +68,23 @@ template <typename T> class Bitset
return *this; return *this;
} }
/**
* @brief Return the value contained in this bitset.
*
* @return T The contained value.
*/
T value() const T value() const
{ {
return m_value; return m_value;
} }
/**
* @brief Modify one of the individual bits in this bitset.
*
* @param index The index of the bit (this will set/unset 1 << index). It must be less than the bit width of the
* underlying type.
* @param b The value to set the bit to.
*/
void set(int index, bool b) void set(int index, bool b)
{ {
check((usize)index < (sizeof(T) * 8)); check((usize)index < (sizeof(T) * 8));
@ -42,12 +93,22 @@ template <typename T> class Bitset
m_value &= ~(((T)1) << index); m_value &= ~(((T)1) << index);
} }
/**
* @brief Get the value of one of the individual bits in this bitset.
*
* @param index The index of the bit (this will read 1 << index). It must be less than the bit width of the
* underlying type.
* @return bool The value of the requested bit.
*/
bool get(int index) bool get(int index)
{ {
check((usize)index < (sizeof(T) * 8)); check((usize)index < (sizeof(T) * 8));
return m_value & (((T)1) << index); return m_value & (((T)1) << index);
} }
/**
* @brief Clear all the bits in this bitset.
*/
void clear() void clear()
{ {
m_value = 0; m_value = 0;