diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-31 11:32:41 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-31 11:32:41 +0200 |
| commit | 1ead9a09f39d417efa484475306baf13c1653142 (patch) | |
| tree | 4575e481d92c401b1ea981e8004ecc28379d7c45 | |
| parent | 63844b7567a32e27f739755d1e2a232c721e34e5 (diff) | |
feat: `allocator_system` a better implementation and example added.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | dev/examples/allocator_system/CMakeLists.txt | 16 | ||||
| -rw-r--r-- | dev/examples/allocator_system/allocator_system.cc | 43 | ||||
| -rw-r--r-- | dev/lib/core/allocator_system.hpp | 26 |
3 files changed, 72 insertions, 13 deletions
diff --git a/dev/examples/allocator_system/CMakeLists.txt b/dev/examples/allocator_system/CMakeLists.txt new file mode 100644 index 0000000..ee19842 --- /dev/null +++ b/dev/examples/allocator_system/CMakeLists.txt @@ -0,0 +1,16 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + Alloc + VERSION 1.0 + LANGUAGES CXX) + +find_package(Boost REQUIRED COMPONENTS container) + +add_executable(Alloc allocator_system.cc) + +target_link_libraries(Alloc PRIVATE Boost::container) + +set_property(TARGET Alloc PROPERTY CXX_STANDARD 20) +target_include_directories(Alloc PUBLIC ../../) diff --git a/dev/examples/allocator_system/allocator_system.cc b/dev/examples/allocator_system/allocator_system.cc new file mode 100644 index 0000000..90248cd --- /dev/null +++ b/dev/examples/allocator_system/allocator_system.cc @@ -0,0 +1,43 @@ +#include <lib/core/allocator_system.hpp> +#include <iostream> + +struct MyClass +{ + int a; + std::string b; + + MyClass() : a(0), b("default") + { + std::cout << "MyClass() constructed\n"; + } + + MyClass(int x, std::string y) : a(x), b(std::move(y)) + { + std::cout << "MyClass(int, string) constructed\n"; + } + + ~MyClass() + { + std::cout << "~MyClass() destroyed\n"; + } +}; + +int main() +{ + using Alloc = ocl::standard_allocator_type<MyClass>; + Alloc allocator; + + // Test 1: claim() + unclaim() + std::cout << "=== Test 1: claim/unclaim ===\n"; + MyClass* raw = allocator.claim(); + std::cout << "raw->a = " << raw->a << ", raw->b = " << raw->b << "\n"; + allocator.unclaim(raw); // Manual delete + + // Test 2: construct() → shared_ptr + std::cout << "\n=== Test 2: construct (shared_ptr) ===\n"; + auto ptr = allocator.construct<int, std::string>(42, "hello"); + std::cout << "ptr->a = " << ptr->a << ", ptr->b = " << ptr->b << "\n"; + + // `shared_ptr` will automatically delete the object + std::cout << "\n=== End of main ===\n"; +} diff --git a/dev/lib/core/allocator_system.hpp b/dev/lib/core/allocator_system.hpp index 6b7e5db..1243ed5 100644 --- a/dev/lib/core/allocator_system.hpp +++ b/dev/lib/core/allocator_system.hpp @@ -14,33 +14,34 @@ namespace ocl { + template <typename type> struct new_op final { - template <typename type> inline auto operator()() -> type* { return new type; } - template <typename type, typename... var_type> - inline auto operator()(var_type&&... args) -> type* + template <typename... var_type> + inline auto var_alloc(var_type... args) -> type* { - return new type{std::forward(args...)}; + return new type{args...}; } }; + template <typename type> struct delete_op final { - template <typename type> inline auto operator()(type* t) -> void { delete t; } }; - template <typename allocator_new, typename allocator_delete> + template <typename ret_type, typename allocator_new, typename allocator_delete> class allocator_system { + allocator_new alloc_; allocator_delete del_; public: @@ -50,26 +51,25 @@ namespace ocl allocator_system& operator=(const allocator_system&) = delete; allocator_system(const allocator_system&) = delete; - template <typename ret_type> ret_type* claim() noexcept { - return allocator_new(sizeof(ret_type)); + return alloc_(); } - template <typename ret_type, typename... var_type> - auto construct(var_type&&... args) -> std::shared_ptr<ret_type> + template <typename... var_type> + auto construct(var_type... args) -> std::shared_ptr<ret_type> { - return std::shared_ptr<ret_type>(allocator_new(ret_type{std::forward<var_type...>(args...)}), del_()); + return std::shared_ptr<ret_type>(alloc_.template var_alloc<var_type...>(args...), allocator_delete{}); } - template <typename ret_type> void unclaim(ret_type* ptr) { del_(ptr); } }; - using standard_allocator_type = allocator_system<new_op, delete_op>; + template <typename type> + using standard_allocator_type = allocator_system<type, new_op<type>, delete_op<type>>; } // namespace ocl #endif // ifndef _OCL_ALLOCATOR_SYSTEM_HPP |
