shader: move indent-logic out of strbuf

There's no good reason to keep this code here; it's not like it saves us
a lot of logic, and it's unused apart from inside the glsl main.

This has the benefit of keeping struf a bit simpler; less mechanisms
needs to know about each other. Since I plan on adding more
functionality to strbuf, this will make things a lot easier.

Sadly, we don't have an easy way of doing unit-tests for the
vrend_shader internals. So for now I'm just dropping the tests here.
It's not ideal, I know, but yeah... Sorry? :P

Signed-off-by: Erik Faye-Lund <erik.faye-lund@collabora.com>
Reviewed-By: Gert Wollny <gert.wollny@collabora.com>
macos/master
Erik Faye-Lund 6 years ago
parent 93f7a4113e
commit 46f68df55c
  1. 23
      src/vrend_shader.c
  2. 31
      src/vrend_strbuf.h
  3. 53
      tests/test_virgl_strbuf.c

@ -143,6 +143,7 @@ struct dump_ctx {
int prog_type;
int size;
struct vrend_strbuf glsl_main;
int indent_level;
struct vrend_strbuf glsl_hdr;
struct vrend_strbuf glsl_ver_ext;
uint instno;
@ -547,19 +548,36 @@ static void require_glsl_ver(struct dump_ctx *ctx, int glsl_ver)
ctx->glsl_ver_required = glsl_ver;
}
static void emit_indent(struct dump_ctx *ctx)
{
if (ctx->indent_level > 0) {
/* very high levels of indentation doesn't improve readability */
int indent_level = MIN2(ctx->indent_level, 15);
char buf[16];
memset(buf, '\t', indent_level);
buf[indent_level] = '\0';
strbuf_append(&ctx->glsl_main, buf);
}
}
static void emit_buf(struct dump_ctx *ctx, const char *buf)
{
emit_indent(ctx);
strbuf_append(&ctx->glsl_main, buf);
}
static void indent_buf(struct dump_ctx *ctx)
{
return strbuf_indent(&ctx->glsl_main);
ctx->indent_level++;
}
static void outdent_buf(struct dump_ctx *ctx)
{
return strbuf_outdent(&ctx->glsl_main);
if (ctx->indent_level <= 0) {
strbuf_set_error(&ctx->glsl_main);
return;
}
ctx->indent_level--;
}
static void set_buf_error(struct dump_ctx *ctx)
@ -572,6 +590,7 @@ static void emit_buff(struct dump_ctx *ctx, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
emit_indent(ctx);
strbuf_vappendf(&ctx->glsl_main, fmt, va);
va_end(va);
}

@ -42,7 +42,6 @@ struct vrend_strbuf {
/* size of string stored without terminating NULL */
size_t size;
bool error_state;
int indent_level;
};
static inline void strbuf_set_error(struct vrend_strbuf *sb)
@ -55,20 +54,6 @@ static inline bool strbuf_get_error(struct vrend_strbuf *sb)
return sb->error_state;
}
static inline void strbuf_indent(struct vrend_strbuf *sb)
{
sb->indent_level++;
}
static inline void strbuf_outdent(struct vrend_strbuf *sb)
{
if (sb->indent_level <= 0) {
strbuf_set_error(sb);
return;
}
sb->indent_level--;
}
static inline size_t strbuf_get_len(struct vrend_strbuf *sb)
{
return sb->size;
@ -88,7 +73,6 @@ static inline bool strbuf_alloc(struct vrend_strbuf *sb, int initial_size)
sb->buf[0] = 0;
sb->error_state = false;
sb->size = 0;
sb->indent_level = 0;
return true;
}
@ -98,14 +82,13 @@ static inline bool strbuf_alloc(struct vrend_strbuf *sb, int initial_size)
static inline void strbuf_append_buffer(struct vrend_strbuf *sb, const char *data, size_t len)
{
assert(!memchr(data, '\0', len));
int new_len = len + sb->indent_level;
if (strbuf_get_error(sb))
return;
if (sb->size + new_len + 1 > sb->alloc_size) {
if (sb->size + len + 1 > sb->alloc_size) {
/* Reallocate to the larger size of current alloc + min realloc,
* or the resulting string size if larger.
*/
size_t new_size = MAX2(sb->size + new_len + 1, sb->alloc_size + STRBUF_MIN_MALLOC);
size_t new_size = MAX2(sb->size + len + 1, sb->alloc_size + STRBUF_MIN_MALLOC);
char *new = realloc(sb->buf, new_size);
if (!new) {
strbuf_set_error(sb);
@ -114,14 +97,8 @@ static inline void strbuf_append_buffer(struct vrend_strbuf *sb, const char *dat
sb->buf = new;
sb->alloc_size = new_size;
}
if (sb->indent_level) {
memset(sb->buf + sb->size, '\t', sb->indent_level);
sb->size += sb->indent_level;
sb->buf[sb->size] = '\0';
new_len -= sb->indent_level;
}
memcpy(sb->buf + sb->size, data, new_len);
sb->size += new_len;
memcpy(sb->buf + sb->size, data, len);
sb->size += len;
sb->buf[sb->size] = '\0';
}

@ -159,57 +159,6 @@ START_TEST(strbuf_test_boundary2)
}
END_TEST
START_TEST(strbuf_test_indent)
{
struct vrend_strbuf sb;
bool ret;
char str[9];
ret = strbuf_alloc(&sb, 1024);
ck_assert_int_eq(ret, true);
for (int i = 0; i < 8; i++)
str[i] = 'a' + i;
str[8] = 0;
strbuf_indent(&sb);
strbuf_append(&sb, str);
strbuf_outdent(&sb);
strbuf_append(&sb, str);
/* make sure the TAB got added */
ck_assert_int_eq(strbuf_get_error(&sb), false);
ck_assert_str_eq(sb.buf, "\tabcdefghabcdefgh");
strbuf_free(&sb);
}
END_TEST
START_TEST(strbuf_test_indent2)
{
struct vrend_strbuf sb;
bool ret;
char str[513];
ret = strbuf_alloc(&sb, 1024);
ck_assert_int_eq(ret, true);
strbuf_indent(&sb);
for (int i = 0; i < 512; i++)
str[i] = 'a' + (i % 26);
str[512] = 0;
strbuf_append(&sb, str);
strbuf_outdent(&sb);
/* make sure the TAB got added */
ck_assert_int_eq(strbuf_get_len(&sb), strlen(str) + 1);
strbuf_append(&sb, str);
ck_assert_int_eq(strbuf_get_error(&sb), false);
ck_assert_int_eq(strbuf_get_len(&sb), strlen(sb.buf));
/* we should have 512 + 512 + 1 at least */
ck_assert_int_ge(sb.alloc_size, strbuf_get_len(&sb) + 1);
ck_assert_int_gt(sb.alloc_size, 1024);
strbuf_free(&sb);
}
END_TEST
START_TEST(strbuf_test_appendf)
{
struct vrend_strbuf sb;
@ -251,8 +200,6 @@ static Suite *init_suite(void)
tcase_add_test(tc_core, strbuf_add_huge_string);
tcase_add_test(tc_core, strbuf_test_boundary);
tcase_add_test(tc_core, strbuf_test_boundary2);
tcase_add_test(tc_core, strbuf_test_indent);
tcase_add_test(tc_core, strbuf_test_indent2);
tcase_add_test(tc_core, strbuf_test_appendf);
tcase_add_test(tc_core, strbuf_test_appendf_str);
return s;

Loading…
Cancel
Save