diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-14 13:12:14 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-14 13:12:14 +0100 |
| commit | 0424f52c142e64cfadaa37288feee775fafc2bf0 (patch) | |
| tree | 8d60109d4a53e1583cefc46a5bd0b8fe6cae7385 | |
| parent | 2448ac2ef13d96e4ef3b7a9a15ef1cfdc2195831 (diff) | |
feat! core: new ocl::allocator API (breaking changes) and example.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | examples/simple_allocator_op/CMakeLists.txt | 15 | ||||
| -rw-r--r-- | examples/simple_allocator_op/example.cc | 14 | ||||
| -rw-r--r-- | include/ocl/allocator_op.hpp | 37 |
3 files changed, 54 insertions, 12 deletions
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 <ocl/print.hpp> +#include <ocl/allocator_op.hpp> + +/// @brief Basic Send test +int main() +{ + ocl::allocator<int> 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 <typename type> struct global_new_op final { - inline auto operator()() -> type* + using pointer_type = type*; + + auto alloc() -> pointer_type { return new type; } + template <size_t N> + auto array_alloc() -> pointer_type + { + return new type[N]; + } + template <typename... var_type> - inline auto var_alloc(var_type&&... args) -> type* + auto var_alloc(var_type&&... args) -> pointer_type { return new type{std::forward<var_type>(args)...}; } }; - /// @note these are guidelines on deleting a resource template <typename type> - 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 <typename ret_type, typename allocator_new, typename allocator_delete> 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 <typename... var_type> - auto construct(var_type&&... args) -> std::shared_ptr<ret_type> + auto construct_var(var_type... args) { - return std::shared_ptr<ret_type>(alloc_op_.template var_alloc<var_type...>(args...), free_op_); + return std::shared_ptr<ret_type>(allocator_new{}.template var_alloc<var_type...>(std::forward<var_type...>(args)...), allocator_delete{}); } + + template<size_t N> + auto construct_array() + { + return std::shared_ptr<ret_type>(allocator_new{}.template array_alloc<N>(), allocator_delete{}); + } + }; template <typename type> - using allocator = allocator_op<type, global_new_op<type>, global_delete_op<type>>; + using allocator = allocator_op<type, global_new_op<type>, global_array_delete_op<type>>; } // namespace ocl #endif // ifndef __OCL_CORE_ALLOC
\ No newline at end of file |
