vk: Indent tables to align '=' at column 48
[mesa.git] / src / vulkan / util.c
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
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 "private.h"
32
33 void
34 __anv_finishme(const char *file, int line, const char *format, ...)
35 {
36 va_list ap;
37 char buffer[256];
38
39 va_start(ap, format);
40 vsnprintf(buffer, sizeof(buffer), format, ap);
41 va_end(ap);
42
43 fprintf(stderr, "%s:%d: FINISHME: %s\n", file, line, buffer);
44 }
45
46 int
47 anv_vector_init(struct anv_vector *vector, uint32_t element_size, uint32_t size)
48 {
49 assert(is_power_of_two(size));
50 assert(element_size < size && is_power_of_two(element_size));
51
52 vector->head = 0;
53 vector->tail = 0;
54 vector->element_size = element_size;
55 vector->size = size;
56 vector->data = malloc(size);
57
58 return vector->data != NULL;
59 }
60
61 void *
62 anv_vector_add(struct anv_vector *vector)
63 {
64 uint32_t offset, size, split, tail;
65 void *data;
66
67 if (vector->head - vector->tail == vector->size) {
68 size = vector->size * 2;
69 data = malloc(size);
70 if (data == NULL)
71 return NULL;
72 split = ALIGN_U32(vector->tail, vector->size);
73 tail = vector->tail & (vector->size - 1);
74 if (vector->head - split < vector->size) {
75 memcpy(data + tail,
76 vector->data + tail,
77 split - vector->tail);
78 memcpy(data + vector->size,
79 vector->data, vector->head - split);
80 } else {
81 memcpy(data + tail,
82 vector->data + tail,
83 vector->head - vector->tail);
84 }
85 free(vector->data);
86 vector->data = data;
87 vector->size = size;
88 }
89
90 assert(vector->head - vector->tail < vector->size);
91
92 offset = vector->head & (vector->size - 1);
93 vector->head += vector->element_size;
94
95 return vector->data + offset;
96 }
97
98 void *
99 anv_vector_remove(struct anv_vector *vector)
100 {
101 uint32_t offset;
102
103 if (vector->head == vector->tail)
104 return NULL;
105
106 assert(vector->head - vector->tail <= vector->size);
107
108 offset = vector->tail & (vector->size - 1);
109 vector->tail += vector->element_size;
110
111 return vector->data + offset;
112 }