DPC++ Runtime
Runtime libraries for oneAPI DPC++
sycl_mem_obj_allocator.hpp
Go to the documentation of this file.
1 //==------- sycl_mem_obj_allocator.hpp - SYCL standard header file ---------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
12 
13 namespace sycl {
15 namespace detail {
16 
17 template <typename DataT>
19 
21 
22 protected:
23  virtual void *getAllocatorImpl() = 0;
24 
25 public:
26  virtual ~SYCLMemObjAllocator() = default;
27  virtual void *allocate(std::size_t) = 0;
28  virtual void deallocate(void *, std::size_t) = 0;
29  virtual std::size_t getValueSize() const = 0;
30  virtual void setAlignment(std::size_t RequiredAlign) = 0;
31  template <typename AllocatorT> AllocatorT getAllocator() {
32  return *reinterpret_cast<AllocatorT *>(getAllocatorImpl());
33  }
34 };
35 
36 template <typename AllocatorT, typename OwnerDataT>
38 public:
39  SYCLMemObjAllocatorHolder(AllocatorT Allocator)
40  : MAllocator(Allocator),
41  MValueSize(sizeof(typename AllocatorT::value_type)) {}
42 
44  : MAllocator(AllocatorT()),
45  MValueSize(sizeof(typename AllocatorT::value_type)) {}
46 
48 
49  virtual void *allocate(std::size_t Count) override {
50  return reinterpret_cast<void *>(MAllocator.allocate(Count));
51  }
52 
53  virtual void deallocate(void *Ptr, std::size_t Count) override {
54  MAllocator.deallocate(
55  reinterpret_cast<typename AllocatorT::value_type *>(Ptr), Count);
56  }
57 
58  void setAlignment(std::size_t RequiredAlign) override {
59  setAlignImpl(RequiredAlign);
60  }
61 
62  virtual std::size_t getValueSize() const override { return MValueSize; }
63 
64 protected:
65  virtual void *getAllocatorImpl() override { return &MAllocator; }
66 
67 private:
68  template <typename T>
69  using EnableIfDefaultAllocator = std::enable_if_t<
70  std::is_same<T, sycl_memory_object_allocator<OwnerDataT>>::value>;
71 
72  template <typename T>
73  using EnableIfNonDefaultAllocator = std::enable_if_t<
74  !std::is_same<T, sycl_memory_object_allocator<OwnerDataT>>::value>;
75 
76  template <typename T = AllocatorT>
77  EnableIfNonDefaultAllocator<T> setAlignImpl(std::size_t) {
78  // Do nothing in case of user's allocator.
79  }
80 
81  template <typename T = AllocatorT>
82  EnableIfDefaultAllocator<T> setAlignImpl(std::size_t RequiredAlign) {
83  MAllocator.setAlignment(std::max<size_t>(RequiredAlign, 64));
84  }
85 
86  AllocatorT MAllocator;
87  std::size_t MValueSize;
88 };
89 } // namespace detail
90 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
91 } // namespace sycl
virtual void deallocate(void *Ptr, std::size_t Count) override
void setAlignment(std::size_t RequiredAlign) override
virtual void * allocate(std::size_t Count) override
virtual std::size_t getValueSize() const override
virtual void deallocate(void *, std::size_t)=0
virtual void * allocate(std::size_t)=0
virtual std::size_t getValueSize() const =0
virtual void setAlignment(std::size_t RequiredAlign)=0
#define __SYCL_INLINE_VER_NAMESPACE(X)
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14