From 0424f52c142e64cfadaa37288feee775fafc2bf0 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 14 Dec 2025 13:12:14 +0100 Subject: feat! core: new ocl::allocator API (breaking changes) and example. Signed-off-by: Amlal El Mahrouss --- examples/simple_allocator_op/CMakeLists.txt | 15 ++++++++++++ examples/simple_allocator_op/example.cc | 14 +++++++++++ include/ocl/allocator_op.hpp | 37 +++++++++++++++++++---------- 3 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 examples/simple_allocator_op/CMakeLists.txt create mode 100644 examples/simple_allocator_op/example.cc diff --git a/examples/simple_allocator_op/CMakeLists.txt b/examples/simple_allocator_op/CMakeLists.txt new file mode 100644 index 0000000..fbda92b --- /dev/null +++ b/examples/simple_allocator_op/CMakeLists.txt @@ -0,0 +1,15 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + AllocatorExample + VERSION 1.0 + LANGUAGES CXX) + +find_package(Boost REQUIRED COMPONENTS container) + +add_executable(AllocatorExample example.cc) + +set_property(TARGET AllocatorExample PROPERTY CXX_STANDARD 20) +target_include_directories(AllocatorExample PUBLIC ../../include/) +target_link_libraries(AllocatorExample PRIVATE Boost::container) diff --git a/examples/simple_allocator_op/example.cc b/examples/simple_allocator_op/example.cc new file mode 100644 index 0000000..83be033 --- /dev/null +++ b/examples/simple_allocator_op/example.cc @@ -0,0 +1,14 @@ +#include +#include + +/// @brief Basic Send test +int main() +{ + ocl::allocator int_alloc; + auto foo = int_alloc.construct_array<1>(); + + *foo = 67; + ocl::io::print(*foo); + + return EXIT_SUCCESS; +} diff --git a/include/ocl/allocator_op.hpp b/include/ocl/allocator_op.hpp index eff3bac..ed6ae90 100644 --- a/include/ocl/allocator_op.hpp +++ b/include/ocl/allocator_op.hpp @@ -17,34 +17,40 @@ namespace ocl template struct global_new_op final { - inline auto operator()() -> type* + using pointer_type = type*; + + auto alloc() -> pointer_type { return new type; } + template + auto array_alloc() -> pointer_type + { + return new type[N]; + } + template - inline auto var_alloc(var_type&&... args) -> type* + auto var_alloc(var_type&&... args) -> pointer_type { return new type{std::forward(args)...}; } }; - /// @note these are guidelines on deleting a resource template - struct global_delete_op final + struct global_array_delete_op final { - inline auto operator()(type* t) -> void + using pointer_type = type*; + + auto operator()(pointer_type t) -> void { - delete t; + delete[] t; } }; template class allocator_op { - allocator_new alloc_op_{}; - allocator_delete free_op_{}; - public: allocator_op() = default; ~allocator_op() = default; @@ -53,14 +59,21 @@ namespace ocl allocator_op(const allocator_op&) = delete; template - auto construct(var_type&&... args) -> std::shared_ptr + auto construct_var(var_type... args) { - return std::shared_ptr(alloc_op_.template var_alloc(args...), free_op_); + return std::shared_ptr(allocator_new{}.template var_alloc(std::forward(args)...), allocator_delete{}); } + + template + auto construct_array() + { + return std::shared_ptr(allocator_new{}.template array_alloc(), allocator_delete{}); + } + }; template - using allocator = allocator_op, global_delete_op>; + using allocator = allocator_op, global_array_delete_op>; } // namespace ocl #endif // ifndef __OCL_CORE_ALLOC \ No newline at end of file -- cgit v1.2.3