summaryrefslogtreecommitdiffhomepage
path: root/include/ocl/allocator_op.hpp
blob: dcdbdaa049e15f7f5c9156fbc9c61054db5b1ccf (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
/*
 * File: allocator_op.hpp
 * Purpose: Allocate from a pool.
 * Author: Amlal El Mahrouss (amlal@nekernel.org)
 * Copyright 2025, Amlal El Mahrouss, Licensed under the Boost Software License. Licensed under the BSL 1.0 license
 */

#ifndef __OCL_CORE_ALLOC
#define __OCL_CORE_ALLOC

#include <ocl/detail/config.hpp>
#include <memory>

namespace ocl
{
	/// @note these are guidelines on allocating a resource
	template <typename type>
	struct global_new_op final
	{
		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>
		auto var_alloc(var_type&&... args) -> pointer_type
		{
			return new type{std::forward<var_type>(args)...};
		}
	};

	template <typename type>
	struct global_array_delete_op final
	{
		using pointer_type = type*;

		auto operator()(pointer_type t) -> void
		{
			delete[] t;
		}
	};

	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)
		{
			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_array_delete_op<type>>;
} // namespace ocl

#endif // ifndef __OCL_CORE_ALLOC