#ifndef _KERNEL_CPP_CONTAINERS_HPP #define _KERNEL_CPP_CONTAINERS_HPP #include #include // Freestanding placement new in global namespace inline void* operator new(size_t /*size*/, void* ptr) noexcept { return ptr; } inline void* operator new[](size_t /*size*/, void* ptr) noexcept { return ptr; } extern "C" { #include #include } namespace kernel { // ----------------------------------------------------------------------------- // Move Semantics & Utility Helpers // ----------------------------------------------------------------------------- template struct RemoveReference { typedef T type; }; template struct RemoveReference { typedef T type; }; template struct RemoveReference { typedef T type; }; template constexpr typename RemoveReference::type&& move(T&& arg) noexcept { return static_cast::type&&>(arg); } template constexpr T&& forward(typename RemoveReference::type& arg) noexcept { return static_cast(arg); } template constexpr T&& forward(typename RemoveReference::type&& arg) noexcept { return static_cast(arg); } // ----------------------------------------------------------------------------- // UniquePtr (Freestanding Smart Pointer RAII) // ----------------------------------------------------------------------------- template class UniquePtr { private: T* m_ptr; public: constexpr UniquePtr() noexcept : m_ptr(nullptr) {} constexpr UniquePtr(nullptr_t) noexcept : m_ptr(nullptr) {} explicit UniquePtr(T* ptr) noexcept : m_ptr(ptr) {} ~UniquePtr() { reset(); } // Non-copyable UniquePtr(const UniquePtr&) = delete; UniquePtr& operator=(const UniquePtr&) = delete; // Move constructible & assignable UniquePtr(UniquePtr&& other) noexcept : m_ptr(other.m_ptr) { other.m_ptr = nullptr; } // Converting Move Constructor for Polymorphic Base Classes template UniquePtr(UniquePtr&& other) noexcept : m_ptr(other.release()) {} UniquePtr& operator=(UniquePtr&& other) noexcept { if (this != &other) { reset(other.m_ptr); other.m_ptr = nullptr; } return *this; } template UniquePtr& operator=(UniquePtr&& other) noexcept { reset(other.release()); return *this; } T* get() const noexcept { return m_ptr; } T* release() noexcept { T* tmp = m_ptr; m_ptr = nullptr; return tmp; } void reset(T* ptr = nullptr) noexcept { if (m_ptr) { delete m_ptr; } m_ptr = ptr; } T& operator*() const { return *m_ptr; } T* operator->() const noexcept { return m_ptr; } explicit operator bool() const noexcept { return m_ptr != nullptr; } }; template UniquePtr make_unique(Args&&... args) { return UniquePtr(new T(forward(args)...)); } // ----------------------------------------------------------------------------- // Vector (Freestanding Dynamic Array) // ----------------------------------------------------------------------------- template class Vector { private: T* m_data; size_t m_size; size_t m_capacity; void reallocate(size_t new_capacity) { if (new_capacity == 0) new_capacity = 4; T* new_data = static_cast(kmalloc(new_capacity * sizeof(T))); if (!new_data) return; for (size_t i = 0; i < m_size; i++) { new (static_cast(&new_data[i])) T(move(m_data[i])); m_data[i].~T(); } if (m_data) kfree(m_data); m_data = new_data; m_capacity = new_capacity; } public: Vector() : m_data(nullptr), m_size(0), m_capacity(0) {} explicit Vector(size_t initial_capacity) : m_data(nullptr), m_size(0), m_capacity(0) { reallocate(initial_capacity); } ~Vector() { clear(); if (m_data) kfree(m_data); } Vector(const Vector&) = delete; Vector& operator=(const Vector&) = delete; Vector(Vector&& other) noexcept : m_data(other.m_data), m_size(other.m_size), m_capacity(other.m_capacity) { other.m_data = nullptr; other.m_size = 0; other.m_capacity = 0; } void push_back(const T& val) { if (m_size >= m_capacity) { reallocate(m_capacity == 0 ? 4 : m_capacity * 2); } new (static_cast(&m_data[m_size])) T(val); m_size++; } void push_back(T&& val) { if (m_size >= m_capacity) { reallocate(m_capacity == 0 ? 4 : m_capacity * 2); } new (static_cast(&m_data[m_size])) T(move(val)); m_size++; } void pop_back() { if (m_size > 0) { m_size--; m_data[m_size].~T(); } } void clear() { for (size_t i = 0; i < m_size; i++) { m_data[i].~T(); } m_size = 0; } T& operator[](size_t index) { return m_data[index]; } const T& operator[](size_t index) const { return m_data[index]; } size_t size() const noexcept { return m_size; } size_t capacity() const noexcept { return m_capacity; } bool empty() const noexcept { return m_size == 0; } T* begin() noexcept { return m_data; } T* end() noexcept { return m_data + m_size; } const T* begin() const noexcept { return m_data; } const T* end() const noexcept { return m_data + m_size; } }; // ----------------------------------------------------------------------------- // String (Freestanding Dynamic String) // ----------------------------------------------------------------------------- class String { private: char* m_buffer; size_t m_length; size_t m_capacity; void ensure_capacity(size_t req) { if (req + 1 > m_capacity) { size_t new_cap = req + 16; char* new_buf = static_cast(kmalloc(new_cap)); if (m_buffer) { memcpy(new_buf, m_buffer, m_length + 1); kfree(m_buffer); } else { new_buf[0] = '\0'; } m_buffer = new_buf; m_capacity = new_cap; } } public: String() : m_buffer(nullptr), m_length(0), m_capacity(0) { ensure_capacity(0); m_buffer[0] = '\0'; } String(const char* str) : m_buffer(nullptr), m_length(0), m_capacity(0) { if (str) { m_length = strlen(str); ensure_capacity(m_length); memcpy(m_buffer, str, m_length + 1); } else { ensure_capacity(0); m_buffer[0] = '\0'; } } ~String() { if (m_buffer) kfree(m_buffer); } String(const String& other) : m_buffer(nullptr), m_length(other.m_length), m_capacity(0) { ensure_capacity(m_length); memcpy(m_buffer, other.m_buffer, m_length + 1); } String& operator=(const String& other) { if (this != &other) { m_length = other.m_length; ensure_capacity(m_length); memcpy(m_buffer, other.m_buffer, m_length + 1); } return *this; } String(String&& other) noexcept : m_buffer(other.m_buffer), m_length(other.m_length), m_capacity(other.m_capacity) { other.m_buffer = nullptr; other.m_length = 0; other.m_capacity = 0; } String& operator+=(const char* str) { if (str) { size_t add_len = strlen(str); ensure_capacity(m_length + add_len); memcpy(m_buffer + m_length, str, add_len + 1); m_length += add_len; } return *this; } const char* c_str() const noexcept { return m_buffer ? m_buffer : ""; } size_t length() const noexcept { return m_length; } bool empty() const noexcept { return m_length == 0; } }; } // namespace kernel #endif // _KERNEL_CPP_CONTAINERS_HPP