xref: /aosp_15_r20/external/virglrenderer/src/vrend_strbuf.h (revision bbecb9d118dfdb95f99bd754f8fa9be01f189df3)
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    bool external_buffer;
46 };
47 
strbuf_set_error(struct vrend_strbuf * sb)48 static inline void strbuf_set_error(struct vrend_strbuf *sb)
49 {
50    sb->error_state = true;
51 }
52 
strbuf_get_error(struct vrend_strbuf * sb)53 static inline bool strbuf_get_error(struct vrend_strbuf *sb)
54 {
55    return sb->error_state;
56 }
57 
strbuf_get_len(struct vrend_strbuf * sb)58 static inline size_t strbuf_get_len(struct vrend_strbuf *sb)
59 {
60    return sb->size;
61 }
62 
strbuf_reset(struct vrend_strbuf * sb)63 static inline void strbuf_reset(struct vrend_strbuf *sb)
64 {
65    sb->size = 0;
66 }
67 
68 
strbuf_free(struct vrend_strbuf * sb)69 static inline void strbuf_free(struct vrend_strbuf *sb)
70 {
71    if (!sb->external_buffer)
72       free(sb->buf);
73 }
74 
strbuf_alloc(struct vrend_strbuf * sb,int initial_size)75 static inline bool strbuf_alloc(struct vrend_strbuf *sb, int initial_size)
76 {
77    sb->buf = malloc(initial_size);
78    if (!sb->buf)
79       return false;
80    sb->alloc_size = initial_size;
81    sb->buf[0] = 0;
82    sb->error_state = false;
83    sb->external_buffer = false;
84    sb->size = 0;
85    return true;
86 }
87 
strbuf_alloc_fixed(struct vrend_strbuf * sb,char * buf,int size)88 static inline bool strbuf_alloc_fixed(struct vrend_strbuf *sb, char *buf, int size)
89 {
90    assert(buf);
91    sb->buf = buf;
92    sb->alloc_size = size;
93    sb->buf[0] = 0;
94    sb->error_state = false;
95    sb->external_buffer = true;
96    sb->size = 0;
97    return true;
98 }
99 
100 
101 /* this might need tuning */
102 #define STRBUF_MIN_MALLOC 1024
103 
strbuf_grow(struct vrend_strbuf * sb,int len)104 static inline bool strbuf_grow(struct vrend_strbuf *sb, int len)
105 {
106    if (sb->size + len + 1 > sb->alloc_size) {
107 
108       /* We can't grow an external buffer */
109       if (sb->external_buffer) {
110          strbuf_set_error(sb);
111          return false;
112       }
113       /* Reallocate to the larger size of current alloc + min realloc,
114        * or the resulting string size if larger.
115        */
116       size_t new_size = MAX2(sb->size + len + 1, sb->alloc_size + STRBUF_MIN_MALLOC);
117       char *new = realloc(sb->buf, new_size);
118       if (!new) {
119          strbuf_set_error(sb);
120          return false;
121       }
122       sb->buf = new;
123       sb->alloc_size = new_size;
124    }
125    return true;
126 }
127 
strbuf_append_buffer(struct vrend_strbuf * sb,const char * data,size_t len)128 static inline void strbuf_append_buffer(struct vrend_strbuf *sb, const char *data, size_t len)
129 {
130    assert(!memchr(data, '\0', len));
131    if (strbuf_get_error(sb) ||
132        !strbuf_grow(sb, len))
133       return;
134    memcpy(sb->buf + sb->size, data, len);
135    sb->size += len;
136    sb->buf[sb->size] = '\0';
137 }
138 
strbuf_append(struct vrend_strbuf * sb,const char * addstr)139 static inline void strbuf_append(struct vrend_strbuf *sb, const char *addstr)
140 {
141    strbuf_append_buffer(sb, addstr, strlen(addstr));
142 }
143 
strbuf_vappendf(struct vrend_strbuf * sb,const char * fmt,va_list ap)144 static inline void strbuf_vappendf(struct vrend_strbuf *sb, const char *fmt, va_list ap)
145 {
146    va_list cp;
147    va_copy(cp, ap);
148 
149    int len = vsnprintf(sb->buf + sb->size, sb->alloc_size - sb->size, fmt, ap);
150    if (len >= (int)(sb->alloc_size - sb->size)) {
151       if (!strbuf_grow(sb, len)) {
152          goto end;
153       }
154       vsnprintf(sb->buf + sb->size, sb->alloc_size - sb->size, fmt, cp);
155    }
156    sb->size += len;
157 end:
158    va_end(ap);
159 }
160 
161 __attribute__((format(printf, 2, 3)))
strbuf_appendf(struct vrend_strbuf * sb,const char * fmt,...)162 static inline void strbuf_appendf(struct vrend_strbuf *sb, const char *fmt, ...)
163 {
164    va_list va;
165    va_start(va, fmt);
166    strbuf_vappendf(sb, fmt, va);
167    va_end(va);
168 }
169 
strbuf_vfmt(struct vrend_strbuf * sb,const char * fmt,va_list ap)170 static inline void strbuf_vfmt(struct vrend_strbuf *sb, const char *fmt, va_list ap)
171 {
172    va_list cp;
173    va_copy(cp, ap);
174 
175    int len = vsnprintf(sb->buf, sb->alloc_size, fmt, ap);
176    if (len >= (int)(sb->alloc_size)) {
177       if (!strbuf_grow(sb, len))
178         goto end;
179       vsnprintf(sb->buf, sb->alloc_size, fmt, cp);
180    }
181    sb->size = len;
182 end:
183    va_end(ap);
184 }
185 
186 __attribute__((format(printf, 2, 3)))
strbuf_fmt(struct vrend_strbuf * sb,const char * fmt,...)187 static inline void strbuf_fmt(struct vrend_strbuf *sb, const char *fmt, ...)
188 {
189    va_list va;
190    va_start(va, fmt);
191    strbuf_vfmt(sb, fmt, va);
192    va_end(va);
193 }
194 
195 struct vrend_strarray {
196    int num_strings;
197    int num_alloced_strings;
198    struct vrend_strbuf *strings;
199 };
200 
strarray_alloc(struct vrend_strarray * sa,int init_alloc)201 static inline bool strarray_alloc(struct vrend_strarray *sa, int init_alloc)
202 {
203    sa->num_strings = 0;
204    sa->num_alloced_strings = init_alloc;
205    sa->strings = calloc(init_alloc, sizeof(struct vrend_strbuf));
206    if (!sa->strings)
207       return false;
208    return true;
209 }
210 
strarray_addstrbuf(struct vrend_strarray * sa,const struct vrend_strbuf * sb)211 static inline bool strarray_addstrbuf(struct vrend_strarray *sa, const struct vrend_strbuf *sb)
212 {
213    assert(sa->num_strings < sa->num_alloced_strings);
214    if (sa->num_strings >= sa->num_alloced_strings)
215       return false;
216    sa->strings[sa->num_strings] = *sb;
217    sa->num_strings++;
218    return true;
219 }
220 
strarray_free(struct vrend_strarray * sa,bool free_strings)221 static inline void strarray_free(struct vrend_strarray *sa, bool free_strings)
222 {
223    if (free_strings) {
224       for (int i = 0; i < sa->num_strings; i++)
225          strbuf_free(&sa->strings[i]);
226    }
227    free(sa->strings);
228 }
229 
strarray_dump(struct vrend_strarray * sa)230 static inline void strarray_dump(struct vrend_strarray *sa)
231 {
232    for (int i = 0; i < sa->num_strings; i++)
233       vrend_printf("%s", sa->strings[i].buf);
234 }
235 
strarray_dump_with_line_numbers(struct vrend_strarray * sa)236 static inline void strarray_dump_with_line_numbers(struct vrend_strarray *sa)
237 {
238    int lineno = 1;
239    int len;
240    char *line, *end;
241    for (int i = 0; i < sa->num_strings; i++) {
242       end = sa->strings[i].buf - 1;
243       do {
244          line = end + 1;
245          end = strchr(line, '\n');
246          if (end) {
247             len = end - line;
248          } else {
249             len = strlen(line);
250          }
251          if (len)
252             vrend_printf("%4d: %.*s\n", lineno++, len, line);
253       } while (end);
254    }
255 }
256 
257 
258 #endif
259