Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/embree/common/sys/alloc.h
9912 views
1
// Copyright 2009-2021 Intel Corporation
2
// SPDX-License-Identifier: Apache-2.0
3
4
#pragma once
5
6
#include "platform.h"
7
#include <vector>
8
#include <set>
9
10
namespace embree
11
{
12
#define ALIGNED_STRUCT_(align) \
13
void* operator new(size_t size) { return alignedMalloc(size,align); } \
14
void operator delete(void* ptr) { alignedFree(ptr); } \
15
void* operator new[](size_t size) { return alignedMalloc(size,align); } \
16
void operator delete[](void* ptr) { alignedFree(ptr); }
17
18
#define ALIGNED_CLASS_(align) \
19
public: \
20
ALIGNED_STRUCT_(align) \
21
private:
22
23
/*! aligned allocation */
24
void* alignedMalloc(size_t size, size_t align);
25
void alignedFree(void* ptr);
26
27
28
enum class EmbreeUSMMode {
29
DEFAULT = 0,
30
DEVICE_READ_WRITE = 0,
31
DEVICE_READ_ONLY = 1
32
};
33
34
enum class EmbreeMemoryType {
35
USM_HOST = 0,
36
USM_DEVICE = 1,
37
USM_SHARED = 2,
38
MALLOC = 3
39
};
40
41
#if defined(EMBREE_SYCL_SUPPORT)
42
43
/*! aligned allocation using SYCL USM */
44
void* alignedSYCLMalloc(sycl::context* context, sycl::device* device, size_t size, size_t align, EmbreeUSMMode mode);
45
void* alignedSYCLMalloc(sycl::context* context, sycl::device* device, size_t size, size_t align, EmbreeUSMMode mode, EmbreeMemoryType type);
46
void alignedSYCLFree(sycl::context* context, void* ptr);
47
48
#endif
49
50
/*! allocator that performs aligned allocations */
51
template<typename T, size_t alignment>
52
struct aligned_allocator
53
{
54
typedef T value_type;
55
typedef T* pointer;
56
typedef const T* const_pointer;
57
typedef T& reference;
58
typedef const T& const_reference;
59
typedef std::size_t size_type;
60
typedef std::ptrdiff_t difference_type;
61
62
__forceinline pointer allocate( size_type n ) {
63
return (pointer) alignedMalloc(n*sizeof(value_type),alignment);
64
}
65
66
__forceinline void deallocate( pointer p, size_type n ) {
67
return alignedFree(p);
68
}
69
70
__forceinline void construct( pointer p, const_reference val ) {
71
new (p) T(val);
72
}
73
74
__forceinline void destroy( pointer p ) {
75
p->~T();
76
}
77
};
78
79
/*! allocates pages directly from OS */
80
bool win_enable_selockmemoryprivilege(bool verbose);
81
bool os_init(bool hugepages, bool verbose);
82
void* os_malloc (size_t bytes, bool& hugepages);
83
size_t os_shrink (void* ptr, size_t bytesNew, size_t bytesOld, bool hugepages);
84
void os_free (void* ptr, size_t bytes, bool hugepages);
85
void os_advise (void* ptr, size_t bytes);
86
87
/*! allocator that performs OS allocations */
88
template<typename T>
89
struct os_allocator
90
{
91
typedef T value_type;
92
typedef T* pointer;
93
typedef const T* const_pointer;
94
typedef T& reference;
95
typedef const T& const_reference;
96
typedef std::size_t size_type;
97
typedef std::ptrdiff_t difference_type;
98
99
__forceinline os_allocator ()
100
: hugepages(false) {}
101
102
__forceinline pointer allocate( size_type n ) {
103
return (pointer) os_malloc(n*sizeof(value_type),hugepages);
104
}
105
106
__forceinline void deallocate( pointer p, size_type n ) {
107
return os_free(p,n*sizeof(value_type),hugepages);
108
}
109
110
__forceinline void construct( pointer p, const_reference val ) {
111
new (p) T(val);
112
}
113
114
__forceinline void destroy( pointer p ) {
115
p->~T();
116
}
117
118
bool hugepages;
119
};
120
121
/*! allocator that newer performs allocations */
122
template<typename T>
123
struct no_allocator
124
{
125
typedef T value_type;
126
typedef T* pointer;
127
typedef const T* const_pointer;
128
typedef T& reference;
129
typedef const T& const_reference;
130
typedef std::size_t size_type;
131
typedef std::ptrdiff_t difference_type;
132
133
__forceinline pointer allocate( size_type n ) {
134
abort(); //throw std::runtime_error("no allocation supported");
135
}
136
137
__forceinline void deallocate( pointer p, size_type n ) {
138
}
139
140
__forceinline void construct( pointer p, const_reference val ) {
141
new (p) T(val);
142
}
143
144
__forceinline void destroy( pointer p ) {
145
p->~T();
146
}
147
};
148
149
/*! allocator for IDs */
150
template<typename T, size_t max_id>
151
struct IDPool
152
{
153
typedef T value_type;
154
155
IDPool ()
156
: nextID(0) {}
157
158
T allocate()
159
{
160
/* return ID from list */
161
if (!IDs.empty())
162
{
163
T id = *IDs.begin();
164
IDs.erase(IDs.begin());
165
return id;
166
}
167
168
/* allocate new ID */
169
else
170
{
171
if (size_t(nextID)+1 > max_id)
172
return -1;
173
174
return nextID++;
175
}
176
}
177
178
/* adds an ID provided by the user */
179
bool add(T id)
180
{
181
if (id > max_id)
182
return false;
183
184
/* check if ID should be in IDs set */
185
if (id < nextID) {
186
auto p = IDs.find(id);
187
if (p == IDs.end()) return false;
188
IDs.erase(p);
189
return true;
190
}
191
192
/* otherwise increase ID set */
193
else
194
{
195
for (T i=nextID; i<id; i++) {
196
IDs.insert(i);
197
}
198
nextID = id+1;
199
return true;
200
}
201
}
202
203
void deallocate( T id )
204
{
205
assert(id < nextID);
206
MAYBE_UNUSED auto done = IDs.insert(id).second;
207
assert(done);
208
}
209
210
private:
211
std::set<T> IDs; //!< stores deallocated IDs to be reused
212
T nextID; //!< next ID to use when IDs vector is empty
213
};
214
}
215
216
217