diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2026-03-14 13:43:27 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2026-03-14 13:43:27 +0100 |
| commit | e3c44bac404812920f6e19285bd126b087f3bc39 (patch) | |
| tree | 67b9ffe7a46f5e6b3355e982072ddfeb520e6c4a | |
| parent | 4f0ada06151e9397bd119c9f0808d7c4c0ed55c7 (diff) | |
[CHORE] Core library tweaks and improvements.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | include/ocl/alloc_op.hpp | 98 | ||||
| l---------[-rw-r--r--] | include/ocl/allocator_op.hpp | 99 | ||||
| -rw-r--r-- | include/ocl/basic_hash.hpp | 6 | ||||
| -rw-r--r-- | include/ocl/crc_hash.hpp | 6 | ||||
| -rw-r--r-- | include/ocl/equiv.hpp | 8 |
5 files changed, 109 insertions, 108 deletions
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 <ocl/detail/config.hpp> +#include <memory> +#include <mutex> +#include <thread> + +namespace ocl +{ + + /// @note these are guidelines on allocating a resource + template <typename Type> + 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>; + + mutex_type m_; + + auto alloc() -> pointer_type + { + return new Type; + } + + template <size_t N> + auto array_alloc() -> pointer_type + { + return new Type[N]; + } + + template <typename... VarType> + auto var_alloc(VarType&&... args) -> pointer_type + { + return new Type{std::forward<VarType>(args)...}; + } + }; + + template <typename Type> + 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 <typename Type> + using global_array_delete_op = global_new_op<Type>; + + /// \brief Allocator operations structure. Takes care of memory mgmt within a pool. + template <typename RetType, typename AllocNew, typename AllocDelete> + 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 <typename... VarType> + auto construct_var(VarType&&... args) + { + static AllocNew alloc; + typename AllocNew::lock_type lt{alloc.m_}; + return std::shared_ptr<RetType>(alloc.template var_alloc<VarType...>(std::forward<VarType...>(args)...), AllocDelete{}); + } + + template <std::size_t N> + auto construct_array() + { + static AllocNew alloc; + typename AllocNew::lock_type lt{alloc.m_}; + return std::shared_ptr<RetType>(alloc.template array_alloc<N>(), AllocDelete{}); + } + }; + + template <typename Type> + using allocator = allocator_op<Type, global_new_op<Type>, global_delete_op<Type>>; + +} // namespace ocl + +#endif // ifndef OCL_CORE_ALLOCATOR_OP diff --git a/include/ocl/allocator_op.hpp b/include/ocl/allocator_op.hpp index eb700e1..dd802f0 100644..120000 --- a/include/ocl/allocator_op.hpp +++ b/include/ocl/allocator_op.hpp @@ -1,98 +1 @@ -// 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 <ocl/detail/config.hpp> -#include <memory> -#include <mutex> -#include <thread> - -namespace ocl -{ - - /// @note these are guidelines on allocating a resource - template <typename type> - 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>; - - mutex_type m_; - - auto alloc() -> pointer_type - { - return new type; - } - - template <size_t N> - auto array_alloc() -> pointer_type - { - return new type[N]; - } - - template <typename... var_type> - auto var_alloc(var_type&&... args) -> pointer_type - { - return new type{std::forward<var_type>(args)...}; - } - }; - - template <typename type> - 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 <typename type> - using global_array_delete_op = global_new_op<type>; - - /// \brief Allocator operations structure. Takes care of memory mgmt within a pool. - template <typename ret_type, typename allocator_new, typename allocator_delete> - class allocator_op - { - public: - allocator_op() = default; - ~allocator_op() = default; - - allocator_op& operator=(const allocator_op&) = delete; - allocator_op(const allocator_op&) = delete; - - template <typename... var_type> - auto construct_var(var_type&&... args) - { - static allocator_new alloc; - typename allocator_new::lock_type lt{alloc.m_}; - return std::shared_ptr<ret_type>(alloc.template var_alloc<var_type...>(std::forward<var_type...>(args)...), allocator_delete{}); - } - - template <std::size_t N> - auto construct_array() - { - static allocator_new alloc; - typename allocator_new::lock_type lt{alloc.m_}; - return std::shared_ptr<ret_type>(alloc.template array_alloc<N>(), allocator_delete{}); - } - }; - - template <typename type> - using allocator = allocator_op<type, global_new_op<type>, global_delete_op<type>>; - -} // namespace ocl - -#endif // ifndef OCL_CORE_ALLOCATOR_OP +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 <class T> + template <class Type> 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 <typename T> - inline size_t operator()(T* in_) const + template <typename Type> + inline size_t operator()(Type* in_) const { - return ocl::crc_hash_trait::crc<uint32_t>(reinterpret_cast<const char*>(in_), sizeof(T)); + return ocl::crc_hash_trait::crc<uint32_t>(reinterpret_cast<const char*>(in_), sizeof(Type)); } template <bool is_mem = true> 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 <typename T> + template <typename Type> 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 <typename T> - using equiv_to_bool = equiv_to<bool, T>; + template <typename Type> + using equiv_to_bool = equiv_to<bool, Type>; } // namespace ocl |
