summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2017-07-24 08:07:32 +0200
committerwm4 <wm4@nowhere>2017-07-24 08:12:42 +0200
commit24dc91907a039b7a86bf6d5b2a0a914bf1f44c3d (patch)
tree0aeb8046536a1d2a799d352112418960387cc94d
parent3d0f86145ce211eee623b661d558ef3405d75933 (diff)
downloadmpv-24dc91907a039b7a86bf6d5b2a0a914bf1f44c3d.tar.bz2
mpv-24dc91907a039b7a86bf6d5b2a0a914bf1f44c3d.tar.xz
common, vo_opengl: add/use helper for formatted strings on the stack
Seems like I really like this C99 idiom. No reason not to generalize it do snprintf(). Introduce mp_tprintf(), which basically this idiom to snprintf(). This macro looks like it returns a string that was allocated with alloca() on the caller site, except it's portable C99/C11. (And unlike alloca(), the result is valid only within block scope.) Use it in 2 places in the vo_opengl code. But it has the potential to make a whole bunch of weird looking code look slightly nicer.
-rw-r--r--common/common.c9
-rw-r--r--common/common.h8
-rw-r--r--video/out/opengl/utils.c3
-rw-r--r--video/out/opengl/video.c12
4 files changed, 22 insertions, 10 deletions
diff --git a/common/common.c b/common/common.c
index eead096d5a..a07af8c461 100644
--- a/common/common.c
+++ b/common/common.c
@@ -287,3 +287,12 @@ char *mp_tag_str_buf(char *buf, size_t buf_size, uint32_t tag)
}
return buf;
}
+
+char *mp_tprintf_buf(char *buf, size_t buf_size, const char *format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+ vsnprintf(buf, buf_size, format, ap);
+ va_end(ap);
+ return buf;
+}
diff --git a/common/common.h b/common/common.h
index 4f944c99ba..fb40d251f1 100644
--- a/common/common.h
+++ b/common/common.h
@@ -101,4 +101,12 @@ char *mp_strerror_buf(char *buf, size_t buf_size, int errnum);
char *mp_tag_str_buf(char *buf, size_t buf_size, uint32_t tag);
#define mp_tag_str(t) mp_tag_str_buf((char[22]){0}, 22, t)
+// Return a printf(format, ...) formatted string of the given SIZE. SIZE must
+// be a compile time constant. The result is allocated on the stack and valid
+// only within the current block scope.
+#define mp_tprintf(SIZE, format, ...) \
+ mp_tprintf_buf((char[SIZE]){0}, (SIZE), (format), __VA_ARGS__)
+char *mp_tprintf_buf(char *buf, size_t buf_size, const char *format, ...)
+ PRINTF_ATTRIBUTE(3, 4);
+
#endif /* MPLAYER_MPCOMMON_H */
diff --git a/video/out/opengl/utils.c b/video/out/opengl/utils.c
index f5c639d98d..2624ad7715 100644
--- a/video/out/opengl/utils.c
+++ b/video/out/opengl/utils.c
@@ -919,8 +919,7 @@ static GLuint compile_program(struct gl_shader_cache *sc, const char *vertex,
compile_attach_shader(sc, prog, GL_VERTEX_SHADER, vertex);
compile_attach_shader(sc, prog, GL_FRAGMENT_SHADER, frag);
for (int n = 0; sc->vertex_entries[n].name; n++) {
- char vname[80];
- snprintf(vname, sizeof(vname), "vertex_%s", sc->vertex_entries[n].name);
+ char *vname = mp_tprintf(80, "vertex_%s", sc->vertex_entries[n].name);
gl->BindAttribLocation(prog, n, vname);
}
link_shader(sc, prog);
diff --git a/video/out/opengl/video.c b/video/out/opengl/video.c
index 9587eaaa5f..682825b6f9 100644
--- a/video/out/opengl/video.c
+++ b/video/out/opengl/video.c
@@ -1104,14 +1104,10 @@ static void pass_prepare_src_tex(struct gl_video *p)
if (!s->gl_tex)
continue;
- char texture_name[32];
- char texture_size[32];
- char texture_rot[32];
- char pixel_size[32];
- snprintf(texture_name, sizeof(texture_name), "texture%d", n);
- snprintf(texture_size, sizeof(texture_size), "texture_size%d", n);
- snprintf(texture_rot, sizeof(texture_rot), "texture_rot%d", n);
- snprintf(pixel_size, sizeof(pixel_size), "pixel_size%d", n);
+ char *texture_name = mp_tprintf(32, "texture%d", n);
+ char *texture_size = mp_tprintf(32, "texture_size%d", n);
+ char *texture_rot = mp_tprintf(32, "texture_rot%d", n);
+ char *pixel_size = mp_tprintf(32, "pixel_size%d", n);
if (gl_is_integer_format(s->gl_format)) {
gl_sc_uniform_tex_ui(sc, texture_name, s->gl_tex);