blob: bf7abd49d1de11a748bdaecf898db0f88aa33b55 [file] [log] [blame]
Dave Airlief4e499e2016-10-07 09:16:09 +10001/*
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
24#include <stdarg.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29#include <assert.h>
30
31#include "radv_private.h"
32
33#include "util/u_math.h"
34
35/** Log an error message. */
36void radv_printflike(1, 2)
37 radv_loge(const char *format, ...)
38{
39 va_list va;
40
41 va_start(va, format);
42 radv_loge_v(format, va);
43 va_end(va);
44}
45
46/** \see radv_loge() */
47void
48radv_loge_v(const char *format, va_list va)
49{
50 fprintf(stderr, "vk: error: ");
51 vfprintf(stderr, format, va);
52 fprintf(stderr, "\n");
53}
54
55void radv_printflike(3, 4)
56 __radv_finishme(const char *file, int line, const char *format, ...)
57{
58 va_list ap;
59 char buffer[256];
60
61 va_start(ap, format);
62 vsnprintf(buffer, sizeof(buffer), format, ap);
63 va_end(ap);
64
65 fprintf(stderr, "%s:%d: FINISHME: %s\n", file, line, buffer);
66}
67
68void radv_noreturn radv_printflike(1, 2)
69 radv_abortf(const char *format, ...)
70{
71 va_list va;
72
73 va_start(va, format);
74 radv_abortfv(format, va);
75 va_end(va);
76}
77
78void radv_noreturn
79radv_abortfv(const char *format, va_list va)
80{
81 fprintf(stderr, "vk: error: ");
82 vfprintf(stderr, format, va);
83 fprintf(stderr, "\n");
84 abort();
85}
86
87VkResult
88__vk_errorf(VkResult error, const char *file, int line, const char *format, ...)
89{
90 va_list ap;
91 char buffer[256];
92
93#define ERROR_CASE(error) case error: error_str = #error; break;
94
95 const char *error_str;
96 switch ((int32_t)error) {
97
98 /* Core errors */
99 ERROR_CASE(VK_ERROR_OUT_OF_HOST_MEMORY)
100 ERROR_CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY)
101 ERROR_CASE(VK_ERROR_INITIALIZATION_FAILED)
102 ERROR_CASE(VK_ERROR_DEVICE_LOST)
103 ERROR_CASE(VK_ERROR_MEMORY_MAP_FAILED)
104 ERROR_CASE(VK_ERROR_LAYER_NOT_PRESENT)
105 ERROR_CASE(VK_ERROR_EXTENSION_NOT_PRESENT)
106 ERROR_CASE(VK_ERROR_INCOMPATIBLE_DRIVER)
107
108 /* Extension errors */
109 ERROR_CASE(VK_ERROR_OUT_OF_DATE_KHR)
110
111 default:
112 assert(!"Unknown error");
113 error_str = "unknown error";
114 }
115
116#undef ERROR_CASE
117
118 if (format) {
119 va_start(ap, format);
120 vsnprintf(buffer, sizeof(buffer), format, ap);
121 va_end(ap);
122
123 fprintf(stderr, "%s:%d: %s (%s)\n", file, line, buffer, error_str);
124 } else {
125 fprintf(stderr, "%s:%d: %s\n", file, line, error_str);
126 }
127
128 return error;
129}
130
131int
132radv_vector_init(struct radv_vector *vector, uint32_t element_size, uint32_t size)
133{
134 assert(util_is_power_of_two(size));
135 assert(element_size < size && util_is_power_of_two(element_size));
136
137 vector->head = 0;
138 vector->tail = 0;
139 vector->element_size = element_size;
140 vector->size = size;
141 vector->data = malloc(size);
142
143 return vector->data != NULL;
144}
145
146void *
147radv_vector_add(struct radv_vector *vector)
148{
149 uint32_t offset, size, split, src_tail, dst_tail;
150 void *data;
151
152 if (vector->head - vector->tail == vector->size) {
153 size = vector->size * 2;
154 data = malloc(size);
155 if (data == NULL)
156 return NULL;
157 src_tail = vector->tail & (vector->size - 1);
158 dst_tail = vector->tail & (size - 1);
159 if (src_tail == 0) {
160 /* Since we know that the vector is full, this means that it's
161 * linear from start to end so we can do one copy.
162 */
163 memcpy(data + dst_tail, vector->data, vector->size);
164 } else {
165 /* In this case, the vector is split into two pieces and we have
166 * to do two copies. We have to be careful to make sure each
167 * piece goes to the right locations. Thanks to the change in
168 * size, it may or may not still wrap around.
169 */
170 split = align_u32(vector->tail, vector->size);
171 assert(vector->tail <= split && split < vector->head);
172 memcpy(data + dst_tail, vector->data + src_tail,
173 split - vector->tail);
174 memcpy(data + (split & (size - 1)), vector->data,
175 vector->head - split);
176 }
177 free(vector->data);
178 vector->data = data;
179 vector->size = size;
180 }
181
182 assert(vector->head - vector->tail < vector->size);
183
184 offset = vector->head & (vector->size - 1);
185 vector->head += vector->element_size;
186
187 return vector->data + offset;
188}
189
190void *
191radv_vector_remove(struct radv_vector *vector)
192{
193 uint32_t offset;
194
195 if (vector->head == vector->tail)
196 return NULL;
197
198 assert(vector->head - vector->tail <= vector->size);
199
200 offset = vector->tail & (vector->size - 1);
201 vector->tail += vector->element_size;
202
203 return vector->data + offset;
204}