1/*
2 Copyright (c) 2020-2021 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#ifndef __TBB__small_object_pool_H
18#define __TBB__small_object_pool_H
19
20#include "_config.h"
21#include "_assert.h"
22
23#include "../profiling.h"
24#include <cstddef>
25#include <cstdint>
26#include <atomic>
27
28namespace tbb {
29namespace detail {
30
31namespace d1 {
32class small_object_pool {
33protected:
34 small_object_pool() = default;
35};
36struct execution_data;
37}
38
39namespace r1 {
40TBB_EXPORT void* __TBB_EXPORTED_FUNC allocate(d1::small_object_pool*& pool, std::size_t number_of_bytes,
41 const d1::execution_data& ed);
42TBB_EXPORT void* __TBB_EXPORTED_FUNC allocate(d1::small_object_pool*& pool, std::size_t number_of_bytes);
43TBB_EXPORT void __TBB_EXPORTED_FUNC deallocate(d1::small_object_pool& pool, void* ptr, std::size_t number_of_bytes,
44 const d1::execution_data& ed);
45TBB_EXPORT void __TBB_EXPORTED_FUNC deallocate(d1::small_object_pool& pool, void* ptr, std::size_t number_of_bytes);
46}
47
48namespace d1 {
49class small_object_allocator {
50public:
51 template <typename Type, typename... Args>
52 Type* new_object(execution_data& ed, Args&&... args) {
53 void* allocated_object = r1::allocate(pool&: m_pool, number_of_bytes: sizeof(Type), ed);
54
55 auto constructed_object = new(allocated_object) Type(std::forward<Args>(args)...);
56 return constructed_object;
57 }
58
59 template <typename Type, typename... Args>
60 Type* new_object(Args&&... args) {
61 void* allocated_object = r1::allocate(pool&: m_pool, number_of_bytes: sizeof(Type));
62
63 auto constructed_object = new(allocated_object) Type(std::forward<Args>(args)...);
64 return constructed_object;
65 }
66
67 template <typename Type>
68 void delete_object(Type* object, const execution_data& ed) {
69 // Copy this since it can be a member of the passed object and
70 // unintentionally destroyed when Type destructor is called below
71 small_object_allocator alloc = *this;
72 object->~Type();
73 alloc.deallocate(object, ed);
74 }
75
76 template <typename Type>
77 void delete_object(Type* object) {
78 // Copy this since it can be a member of the passed object and
79 // unintentionally destroyed when Type destructor is called below
80 small_object_allocator alloc = *this;
81 object->~Type();
82 alloc.deallocate(object);
83 }
84
85 template <typename Type>
86 void deallocate(Type* ptr, const execution_data& ed) {
87 call_itt_task_notify(destroy, ptr);
88
89 __TBB_ASSERT(m_pool != nullptr, "Pool must be valid for deallocate call");
90 r1::deallocate(*m_pool, ptr, sizeof(Type), ed);
91 }
92
93 template <typename Type>
94 void deallocate(Type* ptr) {
95 call_itt_task_notify(destroy, ptr);
96
97 __TBB_ASSERT(m_pool != nullptr, "Pool must be valid for deallocate call");
98 r1::deallocate(*m_pool, ptr, sizeof(Type));
99 }
100private:
101 small_object_pool* m_pool{};
102};
103
104} // namespace d1
105} // namespace detail
106} // namespace tbb
107
108#endif /* __TBB__small_object_pool_H */
109

source code of include/oneapi/tbb/detail/_small_object_pool.h