[wasm-api] Make buffer_copy_contents reuse contents

This commit is contained in:
Behdad Esfahbod 2023-02-25 09:18:40 -07:00
parent 65966e0c3d
commit e7540043de
4 changed files with 28 additions and 3 deletions

View File

@ -106,8 +106,30 @@ HB_WASM_API (bool_t, buffer_copy_contents) (HB_WASM_EXEC_ENV
if (buffer->have_output)
buffer->sync ();
if (!buffer->have_positions)
buffer->clear_positions ();
unsigned length = buffer->len;
if (length <= contents->length)
{
unsigned bytes = length * sizeof (hb_glyph_info_t);
glyph_info_t *info = (glyph_info_t *) (validate_app_addr (contents->info, bytes) ? addr_app_to_native (contents->info) : nullptr);
glyph_position_t *pos = (glyph_position_t *) (validate_app_addr (contents->pos, bytes) ? addr_app_to_native (contents->pos) : nullptr);
if (unlikely (!info || !pos))
{
contents->length = 0;
return false;
}
memcpy (info, buffer->info, bytes);
memcpy (pos, buffer->pos, bytes);
return true;
}
contents->length = length;
contents->info = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->info, length * sizeof (buffer->info[0]));
contents->pos = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->pos, length * sizeof (buffer->pos[0]));
@ -141,7 +163,9 @@ HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV
glyph_info_t *info = (glyph_info_t *) (validate_app_addr (contents->info, bytes) ? addr_app_to_native (contents->info) : nullptr);
glyph_position_t *pos = (glyph_position_t *) (validate_app_addr (contents->pos, bytes) ? addr_app_to_native (contents->pos) : nullptr);
buffer->clear_positions (); /* This is wasteful, but we need it to set have_positions=true. */
if (!buffer->have_positions)
buffer->clear_positions (); /* This is wasteful. */
memcpy (buffer->info, info, bytes);
memcpy (buffer->pos, pos, bytes);
buffer->len = length;

View File

@ -139,6 +139,7 @@ typedef struct
ptr_t(glyph_info_t) info;
ptr_t(glyph_position_t) pos;
} buffer_contents_t;
#define BUFFER_CONTENTS_INIT {0, 0, 0}
HB_WASM_API (bool_t, buffer_contents_realloc) (HB_WASM_EXEC_ENV
ptr_d(buffer_contents_t, contents),

View File

@ -76,7 +76,7 @@ shape (void *shape_plan,
direction = DIRECTION_REVERSE (direction);
}
buffer_contents_t contents;
buffer_contents_t contents = BUFFER_CONTENTS_INIT;
if (!buffer_copy_contents (buffer, &contents))
return false;

View File

@ -26,7 +26,7 @@ shape (void *shape_plan,
blob_free (&blob);
buffer_contents_t contents;
buffer_contents_t contents = BUFFER_CONTENTS_INIT;
if (!buffer_copy_contents (buffer, &contents))
return false;