Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/vulkan/util/vk_alloc.h
7099 views
1
/*
2
* Copyright © 2015 Intel Corporation
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
* IN THE SOFTWARE.
22
*/
23
#ifndef VK_ALLOC_H
24
#define VK_ALLOC_H
25
26
/* common allocation inlines for vulkan drivers */
27
28
#include <string.h>
29
#include <vulkan/vulkan.h>
30
31
#include "util/u_math.h"
32
#include "util/macros.h"
33
34
35
const VkAllocationCallbacks *
36
vk_default_allocator(void);
37
38
static inline void *
39
vk_alloc(const VkAllocationCallbacks *alloc,
40
size_t size, size_t align,
41
VkSystemAllocationScope scope)
42
{
43
return alloc->pfnAllocation(alloc->pUserData, size, align, scope);
44
}
45
46
static inline void *
47
vk_zalloc(const VkAllocationCallbacks *alloc,
48
size_t size, size_t align,
49
VkSystemAllocationScope scope)
50
{
51
void *mem = vk_alloc(alloc, size, align, scope);
52
if (mem == NULL)
53
return NULL;
54
55
memset(mem, 0, size);
56
57
return mem;
58
}
59
60
static inline void *
61
vk_realloc(const VkAllocationCallbacks *alloc,
62
void *ptr, size_t size, size_t align,
63
VkSystemAllocationScope scope)
64
{
65
return alloc->pfnReallocation(alloc->pUserData, ptr, size, align, scope);
66
}
67
68
static inline void
69
vk_free(const VkAllocationCallbacks *alloc, void *data)
70
{
71
if (data == NULL)
72
return;
73
74
alloc->pfnFree(alloc->pUserData, data);
75
}
76
77
static inline char *
78
vk_strdup(const VkAllocationCallbacks *alloc, const char *s,
79
VkSystemAllocationScope scope)
80
{
81
if (s == NULL)
82
return NULL;
83
84
size_t size = strlen(s) + 1;
85
char *copy = vk_alloc(alloc, size, 1, scope);
86
if (copy == NULL)
87
return NULL;
88
89
memcpy(copy, s, size);
90
91
return copy;
92
}
93
94
static inline void *
95
vk_alloc2(const VkAllocationCallbacks *parent_alloc,
96
const VkAllocationCallbacks *alloc,
97
size_t size, size_t align,
98
VkSystemAllocationScope scope)
99
{
100
if (alloc)
101
return vk_alloc(alloc, size, align, scope);
102
else
103
return vk_alloc(parent_alloc, size, align, scope);
104
}
105
106
static inline void *
107
vk_zalloc2(const VkAllocationCallbacks *parent_alloc,
108
const VkAllocationCallbacks *alloc,
109
size_t size, size_t align,
110
VkSystemAllocationScope scope)
111
{
112
void *mem = vk_alloc2(parent_alloc, alloc, size, align, scope);
113
if (mem == NULL)
114
return NULL;
115
116
memset(mem, 0, size);
117
118
return mem;
119
}
120
121
static inline void
122
vk_free2(const VkAllocationCallbacks *parent_alloc,
123
const VkAllocationCallbacks *alloc,
124
void *data)
125
{
126
if (alloc)
127
vk_free(alloc, data);
128
else
129
vk_free(parent_alloc, data);
130
}
131
132
/* A multi-pointer allocator
133
*
134
* When copying data structures from the user (such as a render pass), it's
135
* common to need to allocate data for a bunch of different things. Instead
136
* of doing several allocations and having to handle all of the error checking
137
* that entails, it can be easier to do a single allocation. This struct
138
* helps facilitate that. The intended usage looks like this:
139
*
140
* VK_MULTIALLOC(ma)
141
* vk_multialloc_add(&ma, &main_ptr, 1);
142
* vk_multialloc_add(&ma, &substruct1, substruct1Count);
143
* vk_multialloc_add(&ma, &substruct2, substruct2Count);
144
*
145
* if (!vk_multialloc_alloc(&ma, pAllocator, VK_ALLOCATION_SCOPE_FOO))
146
* return vk_error(VK_ERROR_OUT_OF_HOST_MEORY);
147
*/
148
struct vk_multialloc {
149
size_t size;
150
size_t align;
151
152
uint32_t ptr_count;
153
void **ptrs[8];
154
};
155
156
#define VK_MULTIALLOC_INIT \
157
((struct vk_multialloc) { 0, })
158
159
#define VK_MULTIALLOC(_name) \
160
struct vk_multialloc _name = VK_MULTIALLOC_INIT
161
162
static ALWAYS_INLINE void
163
vk_multialloc_add_size_align(struct vk_multialloc *ma,
164
void **ptr, size_t size, size_t align)
165
{
166
assert(util_is_power_of_two_nonzero(align));
167
if (size == 0) {
168
*ptr = NULL;
169
return;
170
}
171
172
size_t offset = ALIGN_POT(ma->size, align);
173
ma->size = offset + size;
174
ma->align = MAX2(ma->align, align);
175
176
/* Store the offset in the pointer. */
177
*ptr = (void *)(uintptr_t)offset;
178
179
assert(ma->ptr_count < ARRAY_SIZE(ma->ptrs));
180
ma->ptrs[ma->ptr_count++] = ptr;
181
}
182
183
#define vk_multialloc_add_size(_ma, _ptr, _type, _size) \
184
do { \
185
_type **_tmp = (_ptr); \
186
(void)_tmp; \
187
vk_multialloc_add_size_align((_ma), (void **)(_ptr), \
188
(_size), alignof(_type)); \
189
} while(0)
190
191
#define vk_multialloc_add(_ma, _ptr, _type, _count) \
192
vk_multialloc_add_size(_ma, _ptr, _type, (_count) * sizeof(**(_ptr)));
193
194
#define VK_MULTIALLOC_DECL_SIZE(_ma, _type, _name, _size) \
195
_type *_name; \
196
vk_multialloc_add_size(_ma, &_name, _type, _size);
197
198
#define VK_MULTIALLOC_DECL(_ma, _type, _name, _count) \
199
VK_MULTIALLOC_DECL_SIZE(_ma, _type, _name, (_count) * sizeof(_type));
200
201
static ALWAYS_INLINE void *
202
vk_multialloc_alloc(struct vk_multialloc *ma,
203
const VkAllocationCallbacks *alloc,
204
VkSystemAllocationScope scope)
205
{
206
char *ptr = vk_alloc(alloc, ma->size, ma->align, scope);
207
if (!ptr)
208
return NULL;
209
210
/* Fill out each of the pointers with their final value.
211
*
212
* for (uint32_t i = 0; i < ma->ptr_count; i++)
213
* *ma->ptrs[i] = ptr + (uintptr_t)*ma->ptrs[i];
214
*
215
* Unfortunately, even though ma->ptr_count is basically guaranteed to be a
216
* constant, GCC is incapable of figuring this out and unrolling the loop
217
* so we have to give it a little help.
218
*/
219
STATIC_ASSERT(ARRAY_SIZE(ma->ptrs) == 8);
220
#define _VK_MULTIALLOC_UPDATE_POINTER(_i) \
221
if ((_i) < ma->ptr_count) \
222
*ma->ptrs[_i] = ptr + (uintptr_t)*ma->ptrs[_i]
223
_VK_MULTIALLOC_UPDATE_POINTER(0);
224
_VK_MULTIALLOC_UPDATE_POINTER(1);
225
_VK_MULTIALLOC_UPDATE_POINTER(2);
226
_VK_MULTIALLOC_UPDATE_POINTER(3);
227
_VK_MULTIALLOC_UPDATE_POINTER(4);
228
_VK_MULTIALLOC_UPDATE_POINTER(5);
229
_VK_MULTIALLOC_UPDATE_POINTER(6);
230
_VK_MULTIALLOC_UPDATE_POINTER(7);
231
#undef _VK_MULTIALLOC_UPDATE_POINTER
232
233
return ptr;
234
}
235
236
static ALWAYS_INLINE void *
237
vk_multialloc_alloc2(struct vk_multialloc *ma,
238
const VkAllocationCallbacks *parent_alloc,
239
const VkAllocationCallbacks *alloc,
240
VkSystemAllocationScope scope)
241
{
242
return vk_multialloc_alloc(ma, alloc ? alloc : parent_alloc, scope);
243
}
244
245
#endif
246
247