mesa/main/ff_frag: Don't retrieve format if not necessary.
[mesa.git] / src / compiler / nir / nir_array.h
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 * Authors:
24 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #ifndef NIR_ARRAY_H
29 #define NIR_ARRAY_H
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 typedef struct {
36 void *mem_ctx;
37 size_t size;
38 size_t alloc;
39 void *data;
40 } nir_array;
41
42 static inline void
43 nir_array_init(nir_array *arr, void *mem_ctx)
44 {
45 arr->mem_ctx = mem_ctx;
46 arr->size = 0;
47 arr->alloc = 0;
48 arr->data = NULL;
49 }
50
51 static inline void
52 nir_array_fini(nir_array *arr)
53 {
54 if (arr->mem_ctx)
55 ralloc_free(arr->data);
56 else
57 free(arr->data);
58 }
59
60 #define NIR_ARRAY_INITIAL_SIZE 64
61
62 /* Increments the size of the array by the given ammount and returns a
63 * pointer to the beginning of the newly added space.
64 */
65 static inline void *
66 nir_array_grow(nir_array *arr, size_t additional)
67 {
68 size_t new_size = arr->size + additional;
69 if (new_size > arr->alloc) {
70 if (arr->alloc == 0)
71 arr->alloc = NIR_ARRAY_INITIAL_SIZE;
72
73 while (new_size > arr->alloc)
74 arr->alloc *= 2;
75
76 if (arr->mem_ctx)
77 arr->data = reralloc_size(arr->mem_ctx, arr->data, arr->alloc);
78 else
79 arr->data = realloc(arr->data, arr->alloc);
80 }
81
82 void *ptr = (void *)((char *)arr->data + arr->size);
83 arr->size = new_size;
84
85 return ptr;
86 }
87
88 #define nir_array_add(arr, type, elem) \
89 *(type *)nir_array_grow(arr, sizeof(type)) = (elem)
90
91 #define nir_array_foreach(arr, type, elem) \
92 for (type *elem = (type *)(arr)->data; \
93 elem < (type *)((char *)(arr)->data + (arr)->size); elem++)
94
95 #ifdef __cplusplus
96 } /* extern "C" */
97 #endif
98
99 #endif /* NIR_ARRAY_H */