summaryrefslogtreecommitdiffhomepage
path: root/include/ocl/allocator_op.hpp
blob: f03edc98df91c2d1d052618e51d54ebf3e0d2cd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// 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-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)
		{
			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()
		{
			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