aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64
diff options
context:
space:
mode:
authorFabian Imhof <fabian.imhof@ost.ch>2025-03-09 09:30:01 +0000
committerFabian Imhof <fabian.imhof@ost.ch>2025-03-09 09:30:01 +0000
commit06d5e5872838bd1528493b62b4dc28d44b54aa47 (patch)
treea27000afb5e704138bb88374878a060ef523cd63 /arch/x86_64
parent051307f49f4cdfb86c527a475ab21feea4a664d7 (diff)
downloadteachos-06d5e5872838bd1528493b62b4dc28d44b54aa47.tar.xz
teachos-06d5e5872838bd1528493b62b4dc28d44b54aa47.zip
add doxygen comments to shared and unique pointer
Diffstat (limited to 'arch/x86_64')
-rw-r--r--arch/x86_64/include/arch/stl/shared_pointer.hpp93
-rw-r--r--arch/x86_64/include/arch/stl/unique_pointer.hpp71
-rw-r--r--arch/x86_64/src/memory/main.cpp8
3 files changed, 162 insertions, 10 deletions
diff --git a/arch/x86_64/include/arch/stl/shared_pointer.hpp b/arch/x86_64/include/arch/stl/shared_pointer.hpp
index 4a9dec5..80ca7fe 100644
--- a/arch/x86_64/include/arch/stl/shared_pointer.hpp
+++ b/arch/x86_64/include/arch/stl/shared_pointer.hpp
@@ -5,15 +5,33 @@
namespace teachos::arch::stl
{
+ /**
+ * @brief A simple implementation of a shared pointer.
+ *
+ * This class provides reference counting and automatic resource management
+ * for dynamically allocated objects.
+ *
+ * @tparam T The type of the managed object.
+ */
template<typename T>
struct shared_pointer
{
+ /**
+ * @brief Constructs a shared pointer.
+ *
+ * @param pointer Raw pointer to manage (default is nullptr).
+ */
explicit shared_pointer(T * pointer = nullptr)
: pointer(pointer)
, ref_count(new std::atomic<int>(pointer != nullptr ? 1 : 0))
{
}
+ /**
+ * @brief Copy constructor.
+ *
+ * @param other The shared_pointer to copy from.
+ */
shared_pointer(const shared_pointer & other)
: pointer(other.pointer)
, ref_count(other.ref_count)
@@ -24,6 +42,11 @@ namespace teachos::arch::stl
}
}
+ /**
+ * @brief Move constructor.
+ *
+ * @param other The shared_pointer to move from.
+ */
shared_pointer(shared_pointer && other) noexcept
: pointer(other.pointer)
, ref_count(other.ref_count)
@@ -32,6 +55,12 @@ namespace teachos::arch::stl
other.ref_count = nullptr;
}
+ /**
+ * @brief Copy assignment operator.
+ *
+ * @param other The shared_pointer to copy from.
+ * @return Reference to this shared_pointer.
+ */
shared_pointer & operator=(const shared_pointer & other)
{
if (this != &other)
@@ -49,6 +78,12 @@ namespace teachos::arch::stl
return *this;
}
+ /**
+ * @brief Move assignment operator.
+ *
+ * @param other The shared_pointer to move from.
+ * @return Reference to this shared_pointer.
+ */
shared_pointer & operator=(shared_pointer && other) noexcept
{
if (this != &other)
@@ -63,8 +98,14 @@ namespace teachos::arch::stl
return *this;
}
+ /**
+ * @brief Destructor. Cleans up resources if necessary.
+ */
~shared_pointer() { cleanup(); }
+ /**
+ * @brief Releases ownership and deletes the object if necessary.
+ */
void cleanup()
{
if (pointer != nullptr && ref_count != nullptr && --(*ref_count) == 0)
@@ -74,6 +115,11 @@ namespace teachos::arch::stl
}
}
+ /**
+ * @brief Resets the shared pointer with a new raw pointer.
+ *
+ * @param p New raw pointer (default is nullptr).
+ */
void reset(T * p = nullptr)
{
cleanup();
@@ -81,18 +127,43 @@ namespace teachos::arch::stl
ref_count = new std::atomic<int>(p != nullptr ? 1 : 0);
}
+ /**
+ * @brief Swaps the contents of this shared pointer with another.
+ *
+ * @param other The shared_pointer to swap with.
+ */
void swap(shared_pointer & other)
{
std::swap(pointer, other.pointer);
std::swap(ref_count, other.ref_count);
}
+ /**
+ * @brief Dereference operator.
+ *
+ * @return Reference to the managed object.
+ */
T & operator*() const { return *pointer; }
+ /**
+ * @brief Member access operator.
+ *
+ * @return Pointer to the managed object.
+ */
T * operator->() const { return pointer; }
+ /**
+ * @brief Returns the raw pointer.
+ *
+ * @return Pointer to the managed object.
+ */
T * get() const { return pointer; }
+ /**
+ * @brief Returns the reference count.
+ *
+ * @return Number of shared_pointer instances managing the same object.
+ */
int use_count() const
{
if (pointer != nullptr)
@@ -103,15 +174,33 @@ namespace teachos::arch::stl
return 0;
}
+ /**
+ * @brief Checks if this is the only shared pointer managing the object.
+ *
+ * @return True if the use count is 1, otherwise false.
+ */
bool unique() const { return use_count() == 1; }
+ /**
+ * @brief Checks if the shared pointer is not empty.
+ *
+ * @return True if the pointer is not null, otherwise false.
+ */
explicit operator bool() const { return pointer != nullptr; }
private:
- T * pointer;
- std::atomic<int> * ref_count;
+ T * pointer; ///< The managed object.
+ std::atomic<int> * ref_count; ///< Reference count.
};
+ /**
+ * @brief Creates a shared pointer instance.
+ *
+ * @tparam T The type of object to allocate.
+ * @tparam Args Argument types for the constructor of T.
+ * @param args Arguments for the constructor of T.
+ * @return A shared_pointer instance managing a newly created object.
+ */
template<typename T, typename... Args>
shared_pointer<T> make_shared(Args &&... args)
{
diff --git a/arch/x86_64/include/arch/stl/unique_pointer.hpp b/arch/x86_64/include/arch/stl/unique_pointer.hpp
index 0ec3c38..08c862d 100644
--- a/arch/x86_64/include/arch/stl/unique_pointer.hpp
+++ b/arch/x86_64/include/arch/stl/unique_pointer.hpp
@@ -3,25 +3,56 @@
namespace teachos::arch::stl
{
+ /**
+ * @brief A simple unique pointer implementation.
+ *
+ * @tparam T Type of the managed object.
+ */
template<typename T>
struct unique_pointer
{
+ /**
+ * @brief Constructs a unique pointer.
+ *
+ * @param ptr Pointer to manage, default is nullptr.
+ */
explicit unique_pointer(T * ptr = nullptr)
: pointer(ptr)
{
}
+ /**
+ * @brief Destructor that deletes the managed object.
+ */
~unique_pointer() { delete pointer; }
+ /**
+ * @brief Deleted copy constructor to enforce unique ownership.
+ */
unique_pointer(const unique_pointer &) = delete;
+
+ /**
+ * @brief Deleted copy assignment operator to enforce unique ownership.
+ */
unique_pointer & operator=(const unique_pointer &) = delete;
+ /**
+ * @brief Move constructor.
+ *
+ * @param other Unique pointer to move from.
+ */
unique_pointer(unique_pointer && other) noexcept
: pointer(other.pointer)
{
other.pointer = nullptr;
}
+ /**
+ * @brief Move assignment operator.
+ *
+ * @param other Unique pointer to move from.
+ * @return Reference to this unique pointer.
+ */
unique_pointer & operator=(unique_pointer && other) noexcept
{
if (this != &other)
@@ -33,12 +64,32 @@ namespace teachos::arch::stl
return *this;
}
+ /**
+ * @brief Dereference operator.
+ *
+ * @return Reference to the managed object.
+ */
T & operator*() const { return *pointer; }
+ /**
+ * @brief Member access operator.
+ *
+ * @return Pointer to the managed object.
+ */
T * operator->() const { return pointer; }
+ /**
+ * @brief Gets the raw pointer.
+ *
+ * @return The managed pointer.
+ */
T * get() const { return pointer; }
+ /**
+ * @brief Releases ownership of the managed object.
+ *
+ * @return The raw pointer and sets internal pointer to nullptr.
+ */
T * release()
{
T * temp = pointer;
@@ -46,18 +97,36 @@ namespace teachos::arch::stl
return temp;
}
+ /**
+ * @brief Resets the managed object.
+ *
+ * @param ptr New pointer to manage (default is nullptr).
+ */
void reset(T * ptr = nullptr)
{
delete pointer;
pointer = ptr;
}
+ /**
+ * @brief Swaps the managed object with another unique pointer.
+ *
+ * @param other Unique pointer to swap with.
+ */
void swap(unique_pointer & other) { std::swap(pointer, other.pointer); }
private:
- T * pointer;
+ T * pointer; ///< The managed pointer.
};
+ /**
+ * @brief Creates a unique pointer instance.
+ *
+ * @tparam T Type of the managed object.
+ * @tparam Args Argument types for T's constructor.
+ * @param args Arguments for T's constructor.
+ * @return A unique pointer managing a newly created T object.
+ */
template<typename T, typename... Args>
unique_pointer<T> make_unique(Args &&... args)
{
diff --git a/arch/x86_64/src/memory/main.cpp b/arch/x86_64/src/memory/main.cpp
index 08308db..15e89c0 100644
--- a/arch/x86_64/src/memory/main.cpp
+++ b/arch/x86_64/src/memory/main.cpp
@@ -52,11 +52,5 @@ namespace teachos::arch::memory
remap_heap(allocator, active_table);
video::vga::text::write("Heap remapping successful", video::vga::text::common_attributes::green_on_black);
video::vga::text::newline();
-
- auto test2 = stl::make_unique<int>(0);
- auto test3 = stl::make_shared<int>(0);
- if (test2 && test3)
- {
- }
}
-} // namespace teachos::arch::memory
+} // namespace teachos::arch::memory \ No newline at end of file