1 /**************************************************************************
2 *
3 * Copyright (C) 2019 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR 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
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 **************************************************************************/
24 #ifndef VREND_STRBUF_H
25 #define VREND_STRBUF_H
26
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include "util/u_math.h"
33
34 #include "vrend_debug.h"
35
36 /* shader string buffer */
37 struct vrend_strbuf {
38 /* NULL terminated string storage */
39 char *buf;
40 /* allocation size (must be >= strlen(str) + 1) */
41 size_t alloc_size;
42 /* size of string stored without terminating NULL */
43 size_t size;
44 bool error_state;
45 };
46
strbuf_set_error(struct vrend_strbuf * sb)47 static inline void strbuf_set_error(struct vrend_strbuf *sb)
48 {
49 sb->error_state = true;
50 }
51
strbuf_get_error(struct vrend_strbuf * sb)52 static inline bool strbuf_get_error(struct vrend_strbuf *sb)
53 {
54 return sb->error_state;
55 }
56
strbuf_get_len(struct vrend_strbuf * sb)57 static inline size_t strbuf_get_len(struct vrend_strbuf *sb)
58 {
59 return sb->size;
60 }
61
strbuf_free(struct vrend_strbuf * sb)62 static inline void strbuf_free(struct vrend_strbuf *sb)
63 {
64 free(sb->buf);
65 }
66
strbuf_alloc(struct vrend_strbuf * sb,int initial_size)67 static inline bool strbuf_alloc(struct vrend_strbuf *sb, int initial_size)
68 {
69 sb->buf = malloc(initial_size);
70 if (!sb->buf)
71 return false;
72 sb->alloc_size = initial_size;
73 sb->buf[0] = 0;
74 sb->error_state = false;
75 sb->size = 0;
76 return true;
77 }
78
79 /* this might need tuning */
80 #define STRBUF_MIN_MALLOC 1024
81
strbuf_grow(struct vrend_strbuf * sb,int len)82 static inline bool strbuf_grow(struct vrend_strbuf *sb, int len)
83 {
84 if (sb->size + len + 1 > sb->alloc_size) {
85 /* Reallocate to the larger size of current alloc + min realloc,
86 * or the resulting string size if larger.
87 */
88 size_t new_size = MAX2(sb->size + len + 1, sb->alloc_size + STRBUF_MIN_MALLOC);
89 char *new = realloc(sb->buf, new_size);
90 if (!new) {
91 strbuf_set_error(sb);
92 return false;
93 }
94 sb->buf = new;
95 sb->alloc_size = new_size;
96 }
97 return true;
98 }
99
strbuf_append_buffer(struct vrend_strbuf * sb,const char * data,size_t len)100 static inline void strbuf_append_buffer(struct vrend_strbuf *sb, const char *data, size_t len)
101 {
102 assert(!memchr(data, '\0', len));
103 if (strbuf_get_error(sb) ||
104 !strbuf_grow(sb, len))
105 return;
106 memcpy(sb->buf + sb->size, data, len);
107 sb->size += len;
108 sb->buf[sb->size] = '\0';
109 }
110
strbuf_append(struct vrend_strbuf * sb,const char * addstr)111 static inline void strbuf_append(struct vrend_strbuf *sb, const char *addstr)
112 {
113 strbuf_append_buffer(sb, addstr, strlen(addstr));
114 }
115
strbuf_vappendf(struct vrend_strbuf * sb,const char * fmt,va_list ap)116 static inline void strbuf_vappendf(struct vrend_strbuf *sb, const char *fmt, va_list ap)
117 {
118 va_list cp;
119 va_copy(cp, ap);
120
121 int len = vsnprintf(sb->buf + sb->size, sb->alloc_size - sb->size, fmt, ap);
122 if (len >= (int)(sb->alloc_size - sb->size)) {
123 if (!strbuf_grow(sb, len))
124 return;
125 vsnprintf(sb->buf + sb->size, sb->alloc_size - sb->size, fmt, cp);
126 }
127 sb->size += len;
128 }
129
130 __attribute__((format(printf, 2, 3)))
strbuf_appendf(struct vrend_strbuf * sb,const char * fmt,...)131 static inline void strbuf_appendf(struct vrend_strbuf *sb, const char *fmt, ...)
132 {
133 va_list va;
134 va_start(va, fmt);
135 strbuf_vappendf(sb, fmt, va);
136 va_end(va);
137 }
138
strbuf_vfmt(struct vrend_strbuf * sb,const char * fmt,va_list ap)139 static inline void strbuf_vfmt(struct vrend_strbuf *sb, const char *fmt, va_list ap)
140 {
141 va_list cp;
142 va_copy(cp, ap);
143
144 int len = vsnprintf(sb->buf, sb->alloc_size, fmt, ap);
145 if (len >= (int)(sb->alloc_size)) {
146 if (!strbuf_grow(sb, len))
147 return;
148 vsnprintf(sb->buf, sb->alloc_size, fmt, cp);
149 }
150 sb->size = len;
151 }
152
153 __attribute__((format(printf, 2, 3)))
strbuf_fmt(struct vrend_strbuf * sb,const char * fmt,...)154 static inline void strbuf_fmt(struct vrend_strbuf *sb, const char *fmt, ...)
155 {
156 va_list va;
157 va_start(va, fmt);
158 strbuf_vfmt(sb, fmt, va);
159 va_end(va);
160 }
161
162 struct vrend_strarray {
163 int num_strings;
164 int num_alloced_strings;
165 struct vrend_strbuf *strings;
166 };
167
strarray_alloc(struct vrend_strarray * sa,int init_alloc)168 static inline bool strarray_alloc(struct vrend_strarray *sa, int init_alloc)
169 {
170 sa->num_strings = 0;
171 sa->num_alloced_strings = init_alloc;
172 sa->strings = calloc(init_alloc, sizeof(struct vrend_strbuf));
173 if (!sa->strings)
174 return false;
175 return true;
176 }
177
strarray_addstrbuf(struct vrend_strarray * sa,const struct vrend_strbuf * sb)178 static inline bool strarray_addstrbuf(struct vrend_strarray *sa, const struct vrend_strbuf *sb)
179 {
180 assert(sa->num_strings < sa->num_alloced_strings);
181 if (sa->num_strings >= sa->num_alloced_strings)
182 return false;
183 sa->strings[sa->num_strings] = *sb;
184 sa->num_strings++;
185 return true;
186 }
187
strarray_free(struct vrend_strarray * sa,bool free_strings)188 static inline void strarray_free(struct vrend_strarray *sa, bool free_strings)
189 {
190 if (free_strings) {
191 for (int i = 0; i < sa->num_strings; i++)
192 strbuf_free(&sa->strings[i]);
193 }
194 free(sa->strings);
195 }
196
strarray_dump(struct vrend_strarray * sa)197 static inline void strarray_dump(struct vrend_strarray *sa)
198 {
199 for (int i = 0; i < sa->num_strings; i++)
200 vrend_printf("%s", sa->strings[i].buf);
201 }
202
strarray_dump_with_line_numbers(struct vrend_strarray * sa)203 static inline void strarray_dump_with_line_numbers(struct vrend_strarray *sa)
204 {
205 int lineno = 1;
206 int len;
207 char *line, *end;
208 for (int i = 0; i < sa->num_strings; i++) {
209 end = sa->strings[i].buf - 1;
210 do {
211 line = end + 1;
212 end = strchr(line, '\n');
213 if (end) {
214 len = end - line;
215 } else {
216 len = strlen(line);
217 }
218 vrend_printf("%4d: %.*s\n", lineno++, len, line);
219 } while (end);
220 }
221 }
222
223
224 #endif
225