Merge pull request #10695 from heuripedes/small-buffer-opt

Small buffer optimizations
This commit is contained in:
Autechre 2020-05-24 03:48:21 +02:00 committed by GitHub
commit 32a4607eb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 12 deletions

View File

@ -940,14 +940,22 @@ static INLINE unsigned font_get_replacement(const char* src, const char* start)
return 0;
}
static char* font_driver_reshape_msg(const char* msg)
static char* font_driver_reshape_msg(const char* msg, unsigned char *buffer, size_t buffer_size)
{
/* worst case transformations are 2 bytes to 4 bytes */
unsigned char* buffer = (unsigned char*)malloc((strlen(msg) * 2) + 1);
unsigned char* dst_buffer = buffer;
const unsigned char* src = (const unsigned char*)msg;
unsigned char* dst = (unsigned char*)buffer;
unsigned char* dst;
bool reverse = false;
/* fallback to heap allocated buffer if the buffer is too small */
if (buffer_size < (strlen(msg) * 2) + 1)
{
/* worst case transformations are 2 bytes to 4 bytes -- aliaspider */
dst_buffer = (unsigned char*)malloc((strlen(msg) * 2) + 1);
}
dst = (unsigned char*)dst_buffer;
while (*src || reverse)
{
if (reverse)
@ -1025,7 +1033,7 @@ static char* font_driver_reshape_msg(const char* msg)
end:
*dst = '\0';
return (char*)buffer;
return (char*)dst_buffer;
}
#endif
@ -1042,15 +1050,16 @@ void font_driver_render_msg(
if (msg && *msg && font && font->renderer && font->renderer->render_msg)
{
#ifdef HAVE_LANGEXTRA
char *new_msg = font_driver_reshape_msg(msg);
unsigned char tmp_buffer[64];
char *new_msg = font_driver_reshape_msg(msg, tmp_buffer, sizeof(tmp_buffer));
#else
char *new_msg = (char*)msg;
#endif
font->renderer->render_msg(data,
font->renderer_data, new_msg, params);
#ifdef HAVE_LANGEXTRA
free(new_msg);
if (new_msg != (char*)tmp_buffer)
free(new_msg);
#endif
}
}

View File

@ -1706,9 +1706,15 @@ bool gfx_animation_ticker_smooth(gfx_animation_ctx_ticker_smooth_t *ticker)
if (src_str_len < 1)
goto end;
src_char_widths = (unsigned*)calloc(src_str_len, sizeof(unsigned));
if (!src_char_widths)
goto end;
unsigned small_src_char_widths[64] = {0};
src_char_widths = small_src_char_widths;
if (src_str_len > ARRAY_SIZE(small_src_char_widths))
{
src_char_widths = (unsigned*)calloc(src_str_len, sizeof(unsigned));
if (!src_char_widths)
goto end;
}
str_ptr = ticker->src_str;
for (i = 0; i < src_str_len; i++)
@ -1881,7 +1887,7 @@ bool gfx_animation_ticker_smooth(gfx_animation_ctx_ticker_smooth_t *ticker)
end:
if (src_char_widths)
if (src_char_widths != small_src_char_widths && src_char_widths)
{
free(src_char_widths);
src_char_widths = NULL;