diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-07-18 11:02:04 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-07-18 11:02:04 +0100 |
| commit | c44d6912bc2f37e8d1c239cfb9981bd5cd366c58 (patch) | |
| tree | 13ba46b3f3aa1d82e45a3cfde79b80ab4abc4864 /lib | |
| parent | 142a0bc12ac8178ba7a3ac824a92c775ce6afbba (diff) | |
feat: tracked_ptr class and system.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/memory/tracked_ptr.hpp | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/lib/memory/tracked_ptr.hpp b/lib/memory/tracked_ptr.hpp new file mode 100644 index 0000000..b94c9b4 --- /dev/null +++ b/lib/memory/tracked_ptr.hpp @@ -0,0 +1,176 @@ +/* + * File: memory/tracked_ptr.hpp + * Purpose: Custom smart pointer implementation in C++ + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp all rights reserved. + */ + +#pragma once + +#include <cstddef> +#include <utility> +#include <memory> + +namespace snu::memory +{ + template <typename T> + class tracked_allocator; + + template <typename T> + class tracked_mgr; + + template <typename T, typename Mgr> + class tracked_ptr; + + template <typename T> + class tracked_allocator + { + public: + std::size_t allocated_count_ = 0; + std::size_t deallocated_count_ = 0; + bool is_initialized_ = false; + + public: + template <typename... U> + void allocate(T*& ptr, U&&... args) + { + if (!is_initialized_) + { + is_initialized_ = true; + } + + ptr = new T(args...); + + if (ptr) + { + ++allocated_count_; + } + else + { + throw std::bad_alloc(); + } + } + + void deallocate(T*& ptr) + { + if (ptr) + { + if (allocated_count_) + --allocated_count_; + + ++deallocated_count_; + + ::operator delete(ptr); + ptr = nullptr; + } + } + }; + + template <typename T> + class tracked_mgr + { + private: + tracked_allocator<T> allocator_; + + public: + const tracked_allocator<T>& allocator() + { + return allocator_; + } + + template <typename... U> + T* retain(U&&... args) + { + T* ptr = nullptr; + allocator_.allocate(ptr, std::forward<U>(args)...); + return ptr; + } + + void dispose(T* ptr) + { + allocator_.deallocate(ptr); + } + }; + + template <typename T, typename Mgr = tracked_mgr<T>> + class tracked_ptr + { + public: + static Mgr& manager() + { + static Mgr mgr; + return mgr; + } + + public: + template <typename... U> + explicit tracked_ptr(U&&... args) + : ptr_(nullptr) + { + ptr_ = tracked_ptr::manager().retain(std::forward<U>(args)...); + } + + ~tracked_ptr() noexcept + { + tracked_ptr::manager().dispose(ptr_); + } + + tracked_ptr(const tracked_ptr&) = delete; + tracked_ptr& operator=(const tracked_ptr&) = delete; + + public: + T* get() const + { + return ptr_; + } + + T* data() + { + return ptr_; + } + + T& operator*() const + { + return *ptr_; + } + + T* operator->() const + { + return ptr_; + } + + explicit operator bool() const + { + return ptr_ != nullptr; + } + + void swap(tracked_ptr& other) + { + std::swap(ptr_, other.ptr_); + } + + public: + /// Move constructor + tracked_ptr(tracked_ptr&& other) noexcept + : ptr_(other.ptr_) + { + other.ptr_ = nullptr; + } + + /// Move assignment operator + tracked_ptr& operator=(tracked_ptr&& other) noexcept + { + if (this != &other) + { + this->reset(); + ptr_ = other.ptr_; + other.ptr_ = nullptr; + } + + return *this; + } + + private: + T* ptr_ = nullptr; + }; +} // namespace snu::memory
\ No newline at end of file |
