From e3c44bac404812920f6e19285bd126b087f3bc39 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 14 Mar 2026 13:43:27 +0100 Subject: [CHORE] Core library tweaks and improvements. Signed-off-by: Amlal El Mahrouss --- include/ocl/alloc_op.hpp | 98 +++++++++++++++++++++++++++++++++++++++++++ include/ocl/allocator_op.hpp | 99 +------------------------------------------- include/ocl/basic_hash.hpp | 6 +-- include/ocl/crc_hash.hpp | 6 +-- include/ocl/equiv.hpp | 8 ++-- 5 files changed, 109 insertions(+), 108 deletions(-) create mode 100644 include/ocl/alloc_op.hpp mode change 100644 => 120000 include/ocl/allocator_op.hpp (limited to 'include') diff --git a/include/ocl/alloc_op.hpp b/include/ocl/alloc_op.hpp new file mode 100644 index 0000000..d7f81fe --- /dev/null +++ b/include/ocl/alloc_op.hpp @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: BSL-1.0 +// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// Official repository: https://github.com/ocl-foss-org/core + +#ifndef OCL_CORE_ALLOCATOR_OP +#define OCL_CORE_ALLOCATOR_OP + +#include +#include +#include +#include + +namespace ocl +{ + + /// @note these are guidelines on allocating a resource + template + struct global_new_op final + { + using pointer_type = Type*; + using const_pointer_type = const Type*; + using pointer = Type*; + using const_pointer = const Type*; + using mutex_type = std::mutex; + using lock_type = std::scoped_lock; + + mutex_type m_; + + auto alloc() -> pointer_type + { + return new Type; + } + + template + auto array_alloc() -> pointer_type + { + return new Type[N]; + } + + template + auto var_alloc(VarType&&... args) -> pointer_type + { + return new Type{std::forward(args)...}; + } + }; + + template + struct global_delete_op final + { + using pointer_type = Type*; + using const_pointer_type = const Type*; + + auto operator()(pointer_type t) -> void + { + delete[] t; + } + }; + + /// \brief Backwards compat. alias of global_new_op. + template + using global_array_delete_op = global_new_op; + + /// \brief Allocator operations structure. Takes care of memory mgmt within a pool. + template + class allocator_op final + { + public: + allocator_op() = default; + ~allocator_op() = default; + + allocator_op& operator=(const allocator_op&) = delete; + allocator_op(const allocator_op&) = delete; + + template + auto construct_var(VarType&&... args) + { + static AllocNew alloc; + typename AllocNew::lock_type lt{alloc.m_}; + return std::shared_ptr(alloc.template var_alloc(std::forward(args)...), AllocDelete{}); + } + + template + auto construct_array() + { + static AllocNew alloc; + typename AllocNew::lock_type lt{alloc.m_}; + return std::shared_ptr(alloc.template array_alloc(), AllocDelete{}); + } + }; + + template + using allocator = allocator_op, global_delete_op>; + +} // namespace ocl + +#endif // ifndef OCL_CORE_ALLOCATOR_OP diff --git a/include/ocl/allocator_op.hpp b/include/ocl/allocator_op.hpp deleted file mode 100644 index eb700e1..0000000 --- a/include/ocl/allocator_op.hpp +++ /dev/null @@ -1,98 +0,0 @@ -// SPDX-License-Identifier: BSL-1.0 -// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// Official repository: https://github.com/ocl-foss-org/core - -#ifndef OCL_CORE_ALLOCATOR_OP -#define OCL_CORE_ALLOCATOR_OP - -#include -#include -#include -#include - -namespace ocl -{ - - /// @note these are guidelines on allocating a resource - template - struct global_new_op final - { - using pointer_type = type*; - using const_pointer_type = const type*; - using pointer = type*; - using const_pointer = const type*; - using mutex_type = std::mutex; - using lock_type = std::scoped_lock; - - mutex_type m_; - - auto alloc() -> pointer_type - { - return new type; - } - - template - auto array_alloc() -> pointer_type - { - return new type[N]; - } - - template - auto var_alloc(var_type&&... args) -> pointer_type - { - return new type{std::forward(args)...}; - } - }; - - template - struct global_delete_op final - { - using pointer_type = type*; - using const_pointer_type = const type*; - - auto operator()(pointer_type t) -> void - { - delete[] t; - } - }; - - /// \brief Backwards compat. alias of global_new_op. - template - using global_array_delete_op = global_new_op; - - /// \brief Allocator operations structure. Takes care of memory mgmt within a pool. - template - class allocator_op - { - public: - allocator_op() = default; - ~allocator_op() = default; - - allocator_op& operator=(const allocator_op&) = delete; - allocator_op(const allocator_op&) = delete; - - template - auto construct_var(var_type&&... args) - { - static allocator_new alloc; - typename allocator_new::lock_type lt{alloc.m_}; - return std::shared_ptr(alloc.template var_alloc(std::forward(args)...), allocator_delete{}); - } - - template - auto construct_array() - { - static allocator_new alloc; - typename allocator_new::lock_type lt{alloc.m_}; - return std::shared_ptr(alloc.template array_alloc(), allocator_delete{}); - } - }; - - template - using allocator = allocator_op, global_delete_op>; - -} // namespace ocl - -#endif // ifndef OCL_CORE_ALLOCATOR_OP diff --git a/include/ocl/allocator_op.hpp b/include/ocl/allocator_op.hpp new file mode 120000 index 0000000..dd802f0 --- /dev/null +++ b/include/ocl/allocator_op.hpp @@ -0,0 +1 @@ +alloc_op.hpp \ No newline at end of file diff --git a/include/ocl/basic_hash.hpp b/include/ocl/basic_hash.hpp index 1e58a5b..b1771f7 100644 --- a/include/ocl/basic_hash.hpp +++ b/include/ocl/basic_hash.hpp @@ -14,11 +14,11 @@ namespace ocl { /// \brief Hash helper. - template + template struct basic_hash { - using result_type = typename T::result_type; - using type = T; + using result_type = typename Type::result_type; + using type = Type; // AMLALE: If it throws, we can't compute the hash correctly. constexpr result_type hash() noexcept diff --git a/include/ocl/crc_hash.hpp b/include/ocl/crc_hash.hpp index e36ee1b..1e202a5 100644 --- a/include/ocl/crc_hash.hpp +++ b/include/ocl/crc_hash.hpp @@ -110,10 +110,10 @@ namespace std hash() = default; ~hash() = default; - template - inline size_t operator()(T* in_) const + template + inline size_t operator()(Type* in_) const { - return ocl::crc_hash_trait::crc(reinterpret_cast(in_), sizeof(T)); + return ocl::crc_hash_trait::crc(reinterpret_cast(in_), sizeof(Type)); } template diff --git a/include/ocl/equiv.hpp b/include/ocl/equiv.hpp index bcdc89d..6f7e3a9 100644 --- a/include/ocl/equiv.hpp +++ b/include/ocl/equiv.hpp @@ -13,10 +13,10 @@ namespace ocl { - template + template struct is_real final { - using type = T; + using type = Type; static constexpr auto value = false; }; @@ -48,8 +48,8 @@ namespace ocl }; /// \brief alias equiv_to to boolean type. - template - using equiv_to_bool = equiv_to; + template + using equiv_to_bool = equiv_to; } // namespace ocl -- cgit v1.2.3