File: allocators.h

package info (click to toggle)
cccl 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,248 kB
  • sloc: cpp: 264,457; python: 6,421; sh: 2,762; perl: 460; makefile: 114; xml: 13
file content (244 lines) | stat: -rw-r--r-- 5,505 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef ALLOCATORS_H
#define ALLOCATORS_H

#include <cuda/std/type_traits>
#include <cuda/std/utility>

#include "test_macros.h"

template <class T>
class A1
{
  int id_;

public:
  __device__ __host__ explicit A1(int id = 0) TEST_NOEXCEPT : id_(id) {}

  typedef T value_type;

  __device__ __host__ int id() const
  {
    return id_;
  }

  STATIC_MEMBER_VAR(copy_called, bool);
  STATIC_MEMBER_VAR(move_called, bool);
  STATIC_MEMBER_VAR(allocate_called, bool);

  __device__ __host__ static cuda::std::pair<T*, cuda::std::size_t>& deallocate_called()
  {
    NV_IF_ELSE_TARGET(NV_IS_DEVICE,
                      (__shared__ cuda::std::pair<T*, cuda::std::size_t> v; return v;),
                      (static cuda::std::pair<T*, cuda::std::size_t> v = 0; return v;))
  }

  __device__ __host__ A1(const A1& a) TEST_NOEXCEPT : id_(a.id())
  {
    copy_called() = true;
  }
  __device__ __host__ A1(A1&& a) TEST_NOEXCEPT : id_(a.id())
  {
    move_called() = true;
  }
  __device__ __host__ A1& operator=(const A1& a) TEST_NOEXCEPT
  {
    id_           = a.id();
    copy_called() = true;
    return *this;
  }
  __device__ __host__ A1& operator=(A1&& a) TEST_NOEXCEPT
  {
    id_           = a.id();
    move_called() = true;
    return *this;
  }

  template <class U>
  __device__ __host__ A1(const A1<U>& a) TEST_NOEXCEPT : id_(a.id())
  {
    copy_called() = true;
  }
  template <class U>
  __device__ __host__ A1(A1<U>&& a) TEST_NOEXCEPT : id_(a.id())
  {
    move_called() = true;
  }

  __device__ __host__ T* allocate(cuda::std::size_t n)
  {
    allocate_called() = true;
    return (T*) n;
  }

  __device__ __host__ void deallocate(T* p, cuda::std::size_t n)
  {
    deallocate_called() = cuda::std::pair<T*, cuda::std::size_t>(p, n);
  }

  __device__ __host__ cuda::std::size_t max_size() const
  {
    return id_;
  }
};

template <class T, class U>
inline __device__ __host__ bool operator==(const A1<T>& x, const A1<U>& y)
{
  return x.id() == y.id();
}

template <class T, class U>
inline __device__ __host__ bool operator!=(const A1<T>& x, const A1<U>& y)
{
  return !(x == y);
}

template <class T>
class A2
{
  int id_;

public:
  __device__ __host__ explicit A2(int id = 0) TEST_NOEXCEPT : id_(id) {}

  typedef T value_type;

  typedef unsigned size_type;
  typedef int difference_type;

  typedef cuda::std::true_type propagate_on_container_move_assignment;

  __device__ __host__ int id() const
  {
    return id_;
  }

  STATIC_MEMBER_VAR(copy_called, bool);
  STATIC_MEMBER_VAR(move_called, bool);
  STATIC_MEMBER_VAR(allocate_called, bool);

  __device__ __host__ A2(const A2& a) TEST_NOEXCEPT : id_(a.id())
  {
    copy_called() = true;
  }
  __device__ __host__ A2(A2&& a) TEST_NOEXCEPT : id_(a.id())
  {
    move_called() = true;
  }
  __device__ __host__ A2& operator=(const A2& a) TEST_NOEXCEPT
  {
    id_           = a.id();
    copy_called() = true;
    return *this;
  }
  __device__ __host__ A2& operator=(A2&& a) TEST_NOEXCEPT
  {
    id_           = a.id();
    move_called() = true;
    return *this;
  }

  __device__ __host__ T* allocate(cuda::std::size_t, const void* hint)
  {
    allocate_called() = true;
    return (T*) const_cast<void*>(hint);
  }
};

template <class T, class U>
inline __device__ __host__ bool operator==(const A2<T>& x, const A2<U>& y)
{
  return x.id() == y.id();
}

template <class T, class U>
inline __device__ __host__ bool operator!=(const A2<T>& x, const A2<U>& y)
{
  return !(x == y);
}

template <class T>
class A3
{
  int id_;

public:
  __device__ __host__ explicit A3(int id = 0) TEST_NOEXCEPT : id_(id) {}

  typedef T value_type;

  typedef cuda::std::true_type propagate_on_container_copy_assignment;
  typedef cuda::std::true_type propagate_on_container_swap;

  __device__ __host__ int id() const
  {
    return id_;
  }

  STATIC_MEMBER_VAR(copy_called, bool);
  STATIC_MEMBER_VAR(move_called, bool);
  STATIC_MEMBER_VAR(constructed, bool);
  STATIC_MEMBER_VAR(destroy_called, bool);

  __device__ __host__ A3(const A3& a) TEST_NOEXCEPT : id_(a.id())
  {
    copy_called() = true;
  }
  __device__ __host__ A3(A3&& a) TEST_NOEXCEPT : id_(a.id())
  {
    move_called() = true;
  }
  __device__ __host__ A3& operator=(const A3& a) TEST_NOEXCEPT
  {
    id_           = a.id();
    copy_called() = true;
    return *this;
  }
  __device__ __host__ A3& operator=(A3&& a) TEST_NOEXCEPT
  {
    id_           = a.id();
    move_called() = true;
    return *this;
  }

  template <class U, class... Args>
  __device__ __host__ void construct(U* p, Args&&... args)
  {
    ::new (p) U(cuda::std::forward<Args>(args)...);
    constructed() = true;
  }

  template <class U>
  __device__ __host__ void destroy(U* p)
  {
    p->~U();
    destroy_called() = true;
  }

  __device__ __host__ A3 select_on_container_copy_construction() const
  {
    return A3(-1);
  }
};

template <class T, class U>
inline __device__ __host__ bool operator==(const A3<T>& x, const A3<U>& y)
{
  return x.id() == y.id();
}

template <class T, class U>
inline __device__ __host__ bool operator!=(const A3<T>& x, const A3<U>& y)
{
  return !(x == y);
}

#endif // ALLOCATORS_H