-
Notifications
You must be signed in to change notification settings - Fork 301
/
typeconvert.h
207 lines (172 loc) · 5.65 KB
/
typeconvert.h
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
//-----------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
#pragma once
template <size_t Size>
class StackAllocator
{
public:
StackAllocator() = default;
// Non-copiable, non-movable
StackAllocator(const StackAllocator&) = delete;
StackAllocator& operator=(const StackAllocator&) = delete;
StackAllocator(StackAllocator&&) = delete;
StackAllocator& operator=(StackAllocator&&) = delete;
template <typename T>
T* Allocate(size_t count = 1)
{
static_assert(std::is_trivial_v<T>,
"This class may only be used to allocate trivial types, as it does not invoke constructors.");
// Allocate from the fixed bucket before falling back to dynamic
Bucket* lastBucket = m_dynamic.empty() ? static_cast<Bucket*>(&m_fixed) : static_cast<Bucket*>(&m_dynamic.back());
size_t sizeInBytes = sizeof(T) * count;
void* memory = lastBucket->TryAllocate(sizeInBytes, alignof(T));
if (!memory)
{
// Not enough capacity remains; allocate a new dynamic bucket
size_t minimumSize = sizeInBytes;
m_dynamic.emplace_back(minimumSize);
memory = m_dynamic.back().TryAllocate(sizeInBytes, alignof(T));
}
assert(memory != nullptr);
return reinterpret_cast<T*>(memory);
}
void Reset()
{
m_fixed.allocatedSize = 0;
m_dynamic.clear();
}
private:
struct Bucket
{
void* data;
size_t allocatedSize;
size_t capacity;
Bucket() = default;
// Non-copiable, non-movable
Bucket(const Bucket&) = delete;
Bucket& operator=(const Bucket&) = delete;
Bucket(Bucket&&) = delete;
Bucket& operator=(Bucket&&) = delete;
template <typename T>
static T RoundUpToMultiple(T value, T multiple)
{
static_assert(std::is_integral_v<T>);
T remainder = value % multiple;
if (remainder != 0)
{
value += multiple - remainder;
}
return value;
}
void* TryAllocate(size_t sizeInBytes, size_t alignment)
{
size_t alignedOffset = RoundUpToMultiple(allocatedSize, alignment);
size_t newAllocatedSize = alignedOffset + sizeInBytes;
if (newAllocatedSize > capacity)
{
return nullptr; // Not enough capacity
}
allocatedSize = newAllocatedSize;
return static_cast<byte*>(data) + alignedOffset;
}
};
struct FixedBucket : Bucket
{
std::array<byte, Size> stack;
FixedBucket()
{
this->data = stack.data();
this->allocatedSize = 0;
this->capacity = stack.size();
}
};
struct DynamicBucket : Bucket
{
explicit DynamicBucket(size_t minimumSize)
{
this->allocatedSize = 0;
this->capacity = RoundUpToMultiple<size_t>(minimumSize, 4096); // Round up to nearest page granularity
this->data = VirtualAlloc(nullptr, this->capacity, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
ThrowIfNull(this->data);
}
~DynamicBucket()
{
if (this->data)
{
(void)VirtualFree(this->data, 0, MEM_RELEASE);
}
}
};
// This allocator first retrieves memory from a fixed-size stack-allocated array before falling back to dynamically
// allocated memory if the fixed stack array is exhausted.
FixedBucket m_fixed;
std::deque<DynamicBucket> m_dynamic;
};
template <size_t StackSize>
class DmlTypeConverter : public StackAllocator<StackSize>
{
public:
template <typename T>
T* AllocateUsing(const T& src)
{
T* t = this->Allocate<T>();
*t = src;
return t;
}
template <typename T>
T* AllocateArrayUsing(dml::Span<const T> src)
{
T* t = this->Allocate<T>(src.size());
std::copy(src.begin(), src.end(), t);
return t;
}
DML_BUFFER_BINDING Convert(const DmlBufferBinding& src)
{
DML_BUFFER_BINDING dst;
dst.Buffer = src.buffer;
dst.Offset = src.offset;
dst.SizeInBytes = src.sizeInBytes;
return dst;
}
DML_BUFFER_ARRAY_BINDING Convert(const DmlBufferArrayBinding& src)
{
const size_t count = src.bindings.size();
DML_BUFFER_BINDING* bindings = this->Allocate<DML_BUFFER_BINDING>(count);
for (size_t i = 0; i < count; ++i)
{
bindings[i] = Convert(src.bindings[i]);
}
DML_BUFFER_ARRAY_BINDING dst;
dst.BindingCount = static_cast<UINT>(count);
dst.Bindings = bindings;
return dst;
}
DML_BINDING_DESC ToBindingDesc(const DmlNoneBinding& src)
{
DML_BINDING_DESC dst;
dst.Type = DML_BINDING_TYPE_NONE;
dst.Desc = nullptr;
return dst;
}
DML_BINDING_DESC ToBindingDesc(const DmlBufferBinding& src)
{
auto* binding = this->Allocate<DML_BUFFER_BINDING>();
*binding = Convert(src);
DML_BINDING_DESC dst;
dst.Type = DML_BINDING_TYPE_BUFFER;
dst.Desc = binding;
return dst;
}
DML_BINDING_DESC ToBindingDesc(const DmlBufferArrayBinding& src)
{
auto* binding = this->Allocate<DML_BUFFER_ARRAY_BINDING>();
*binding = Convert(src);
DML_BINDING_DESC dst;
dst.Type = DML_BINDING_TYPE_BUFFER_ARRAY;
dst.Desc = binding;
return dst;
}
};