-
Notifications
You must be signed in to change notification settings - Fork 0
/
aligned_unique_test.cpp
71 lines (50 loc) · 1.48 KB
/
aligned_unique_test.cpp
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
#include <cstdio>
#include <sps/aligned.hpp>
#include <sps/align.hpp>
#include <sps/aligned_allocator.hpp>
struct A : public sps::dynaligned<A> {
A() {}
~A() {
// printf(".\n");
}
int k = 2;
};
template <class T> class Float : public sps::dynaligned<Float<T>, 16> {
public:
T value;
};
struct Foo : public sps::aligned<32> {
Foo(int x, int y) : x(x), y(y) {
printf(".\n");
};
int x;
int y;
~Foo() {
printf("x\n");
}
};
int main() {
Float<double> f;
// Single object
auto x = aligned::make_unique<double,16>(16.0);
printf("0x%lx\n", reinterpret_cast<uintptr_t>(x.get()));
printf("%ld\n", (uintptr_t)x.get() % 16);
// Okay
auto foo = aligned::make_unique<Foo>(3, 4);
// Not working
// auto z0 = aligned::make_unique<A>();
// Works
auto z = make_unique_array(std::allocator<Foo>(), 2, 3, 4);
printf("0x%lx\n", (uintptr_t) &(z.get()[0]));
printf("%ld\n", (uintptr_t) &(z.get()[1]) % 16);
auto z0 = make_unique_aligned_array<Foo, 32, int, int>(aligned_allocator<Foo, 32>(), 2, 3, 4);
printf("0x%x\n", (uintptr_t) &(z0.get()[1]));
printf("%d\n", (uintptr_t) &(z0.get()[1]) % 32);
// Disabled for arrays of known bounds:
// auto = aligned::make_unique<double[16]>(16);
// Forbidden --- there is no default constructor:
// auto foo = aligned::make_unique<Foo[]>(16);
// Forbidden --- calling constructor & destructors on each array element is
// not implemented:
// auto s = aligned::make_unique<std::string[]>(16);
}