Open C++ Libraries.org develop
Loading...
Searching...
No Matches
allocator_fwd.hpp
1// SPDX-License-Identifier: BSL-1.0
2// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org)
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5// Official repository: https://github.com/open-cpp-libraries/core
6
7#ifndef OCL_CORE_ALLOCATOR_OP
8#define OCL_CORE_ALLOCATOR_OP
9
10#include <ocl/detail/config.hpp>
11#include <ocl/detail/alloc/config.hpp>
12
14
15namespace ocl
16{
17
18 namespace detail
19 {
20
22 struct bad_alloc_traits final
23 {
24 using pointer = void*;
25 pointer ptr_{};
26 boost::source_location loc_;
27 };
28
29 inline void throw_bad_alloc(bad_alloc_traits::pointer p, const boost::source_location& loc = BOOST_CURRENT_LOCATION)
30 {
32 ba.ptr_ = p;
33 ba.loc_ = loc;
34
35 throw ba;
36 }
37
38 } // namespace detail
39
41 template <typename Type>
42 struct global_new_op final
43 {
44 public:
45 using pointer_type = Type*;
46 using const_pointer_type = const Type*;
47 using pointer = Type*;
48 using const_pointer = const Type*;
49 using mutex_type = std::mutex;
50 using lock_type = std::scoped_lock<mutex_type>;
51 using type = Type;
52
53 public:
54 mutex_type m_;
55
56 auto single_alloc() -> pointer_type
57 {
58 return new type{};
59 }
60
61 template <size_t N>
62 auto array_alloc() -> pointer_type
63 {
64 static_assert(N > 0, "N == 0, whereas N shall be greater than zero.");
65 return new type[N]{};
66 }
67
68 template <typename... VarType>
69 auto var_alloc(VarType&&... args) -> pointer_type
70 {
71 return new type{std::forward<VarType>(args)...};
72 }
73 };
74
75 template <typename Type>
76 struct global_delete_op final
77 {
78 using pointer_type = Type*;
79 using const_pointer_type = const Type*;
80
81 global_delete_op() = default;
82 ~global_delete_op() = default;
83
84 auto operator()(pointer_type t, const bool array_delete = false) -> void
85 {
86 if (t == nullptr)
87 return;
88
89 if (array_delete)
90 delete[] t;
91 else
92 delete t;
93 }
94 };
95
97 template <typename Type>
99
101 template <typename RetType, typename AllocNew, typename AllocDelete>
102 class allocator_op final
103 {
104 public:
105 allocator_op() = default;
106 ~allocator_op() = default;
107
108 allocator_op& operator=(const allocator_op&) = delete;
109 allocator_op(const allocator_op&) = delete;
110
111 using size_type = std::size_t;
112 using type = RetType;
113
114 template <typename... VarType>
115 auto construct_var(VarType&&... args)
116 {
117 static AllocNew alloc;
118 typename AllocNew::lock_type lt{alloc.m_};
119 return std::shared_ptr<type>(alloc.template var_alloc<VarType...>(std::forward<VarType...>(args)...), AllocDelete{});
120 }
121
122 template <size_type N>
123 auto construct_array()
124 {
125 static AllocNew alloc;
126 typename AllocNew::lock_type lt{alloc.m_};
127 return std::shared_ptr<type>(alloc.template array_alloc<N>(), AllocDelete{});
128 }
129 };
130
131 template <typename Type>
133
134} // namespace ocl
135
136#endif // ifndef OCL_CORE_ALLOCATOR_OP
Allocator operations structure. Takes care of memory mgmt within a pool.
Definition allocator_fwd.hpp:103
Memory allocator for OCL support.
Definition allocator_fwd.hpp:16
Definition allocator_fwd.hpp:23
Definition allocator_fwd.hpp:77
Definition allocator_fwd.hpp:43