vrend: add string buffer array helpers (v2.1)

v2: add assert, lose + 1 (Gert)
v2.1: use debug printf

Reviewed-By: Gert Wollny <gert.wollny@collabora.com>
Reviewed-by: Gurchetan Singh <gurchetansingh@chromium.org>
Signed-off-by: Dave Airlie <airlied@redhat.com>
macos/master
Dave Airlie 6 years ago
parent 293f1dc1dc
commit d8ab0ede3b
  1. 44
      src/vrend_strbuf.h

@ -31,6 +31,8 @@
#include <stdarg.h> #include <stdarg.h>
#include "util/u_math.h" #include "util/u_math.h"
#include "vrend_debug.h"
/* shader string buffer */ /* shader string buffer */
struct vrend_strbuf { struct vrend_strbuf {
/* NULL terminated string storage */ /* NULL terminated string storage */
@ -159,4 +161,46 @@ static inline void strbuf_appendf(struct vrend_strbuf *sb, const char *fmt, ...)
strbuf_vappendf(sb, fmt, va); strbuf_vappendf(sb, fmt, va);
va_end(va); va_end(va);
} }
struct vrend_strarray {
int num_strings;
int num_alloced_strings;
struct vrend_strbuf *strings;
};
static inline bool strarray_alloc(struct vrend_strarray *sa, int init_alloc)
{
sa->num_strings = 0;
sa->num_alloced_strings = init_alloc;
sa->strings = calloc(init_alloc, sizeof(struct vrend_strbuf));
if (!sa->strings)
return false;
return true;
}
static inline bool strarray_addstrbuf(struct vrend_strarray *sa, struct vrend_strbuf *sb)
{
assert(sa->num_strings < sa->num_alloced_strings);
if (sa->num_strings >= sa->num_alloced_strings)
return false;
sa->strings[sa->num_strings] = *sb;
sa->num_strings++;
return true;
}
static inline void strarray_free(struct vrend_strarray *sa, bool free_strings)
{
if (free_strings) {
for (int i = 0; i < sa->num_strings; i++)
strbuf_free(&sa->strings[i]);
}
free(sa->strings);
}
static inline void strarray_dump(struct vrend_strarray *sa)
{
for (int i = 0; i < sa->num_strings; i++)
vrend_printf("%s", sa->strings[i].buf);
}
#endif #endif

Loading…
Cancel
Save