// 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