Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-30 08:46:19

0001 /*
0002     Copyright (c) 2020-2021 Intel Corporation
0003 
0004     Licensed under the Apache License, Version 2.0 (the "License");
0005     you may not use this file except in compliance with the License.
0006     You may obtain a copy of the License at
0007 
0008         http://www.apache.org/licenses/LICENSE-2.0
0009 
0010     Unless required by applicable law or agreed to in writing, software
0011     distributed under the License is distributed on an "AS IS" BASIS,
0012     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013     See the License for the specific language governing permissions and
0014     limitations under the License.
0015 */
0016 
0017 #ifndef __TBB__small_object_pool_H
0018 #define __TBB__small_object_pool_H
0019 
0020 #include "_config.h"
0021 #include "_assert.h"
0022 
0023 #include "../profiling.h"
0024 #include <cstddef>
0025 #include <cstdint>
0026 #include <atomic>
0027 
0028 namespace tbb {
0029 namespace detail {
0030 
0031 namespace d1 {
0032 class small_object_pool {
0033 protected:
0034     small_object_pool() = default;
0035 };
0036 struct execution_data;
0037 }
0038 
0039 namespace r1 {
0040 TBB_EXPORT void* __TBB_EXPORTED_FUNC allocate(d1::small_object_pool*& pool, std::size_t number_of_bytes,
0041                                     const d1::execution_data& ed);
0042 TBB_EXPORT void* __TBB_EXPORTED_FUNC allocate(d1::small_object_pool*& pool, std::size_t number_of_bytes);
0043 TBB_EXPORT void  __TBB_EXPORTED_FUNC deallocate(d1::small_object_pool& pool, void* ptr, std::size_t number_of_bytes,
0044                                         const d1::execution_data& ed);
0045 TBB_EXPORT void  __TBB_EXPORTED_FUNC deallocate(d1::small_object_pool& pool, void* ptr, std::size_t number_of_bytes);
0046 }
0047 
0048 namespace d1 {
0049 class small_object_allocator {
0050 public:
0051     template <typename Type, typename... Args>
0052     Type* new_object(execution_data& ed, Args&&... args) {
0053         void* allocated_object = r1::allocate(m_pool, sizeof(Type), ed);
0054 
0055         auto constructed_object = new(allocated_object) Type(std::forward<Args>(args)...);
0056         return constructed_object;
0057     }
0058 
0059     template <typename Type, typename... Args>
0060     Type* new_object(Args&&... args) {
0061         void* allocated_object = r1::allocate(m_pool, sizeof(Type));
0062 
0063         auto constructed_object = new(allocated_object) Type(std::forward<Args>(args)...);
0064         return constructed_object;
0065     }
0066 
0067     template <typename Type>
0068     void delete_object(Type* object, const execution_data& ed) {
0069         // Copy this since it can be a member of the passed object and
0070         // unintentionally destroyed when Type destructor is called below
0071         small_object_allocator alloc = *this;
0072         object->~Type();
0073         alloc.deallocate(object, ed);
0074     }
0075 
0076     template <typename Type>
0077     void delete_object(Type* object) {
0078         // Copy this since it can be a member of the passed object and
0079         // unintentionally destroyed when Type destructor is called below
0080         small_object_allocator alloc = *this;
0081         object->~Type();
0082         alloc.deallocate(object);
0083     }
0084 
0085     template <typename Type>
0086     void deallocate(Type* ptr, const execution_data& ed) {
0087         call_itt_task_notify(destroy, ptr);
0088 
0089         __TBB_ASSERT(m_pool != nullptr, "Pool must be valid for deallocate call");
0090         r1::deallocate(*m_pool, ptr, sizeof(Type), ed);
0091     }
0092 
0093     template <typename Type>
0094     void deallocate(Type* ptr) {
0095         call_itt_task_notify(destroy, ptr);
0096 
0097         __TBB_ASSERT(m_pool != nullptr, "Pool must be valid for deallocate call");
0098         r1::deallocate(*m_pool, ptr, sizeof(Type));
0099     }
0100 private:
0101     small_object_pool* m_pool{};
0102 };
0103 
0104 } // namespace d1
0105 } // namespace detail
0106 } // namespace tbb
0107 
0108 #endif /* __TBB__small_object_pool_H */