From cbd5c554fb28d3aa2ac834c28e6d89600e969f62 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 14:54:46 -0700 Subject: [PATCH] [wasm-api] Add blob_free --- src/hb-wasm-api-blob.hh | 49 ++++++++++++++++++++++++++++++++++++++++ src/hb-wasm-api-list.hh | 3 +++ src/hb-wasm-api.cc | 1 + src/hb-wasm-api.h | 3 ++- src/meson.build | 1 + src/wasm-sample/Makefile | 2 ++ src/wasm-sample/shape.c | 2 +- 7 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/hb-wasm-api-blob.hh diff --git a/src/hb-wasm-api-blob.hh b/src/hb-wasm-api-blob.hh new file mode 100644 index 000000000..8a8f9e2a9 --- /dev/null +++ b/src/hb-wasm-api-blob.hh @@ -0,0 +1,49 @@ +/* + * Copyright © 2023 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#ifndef HB_WASM_API_BLOB_HH +#define HB_WASM_API_BLOB_HH + +#include "hb-wasm-api.hh" + +namespace hb { +namespace wasm { + + +void +blob_free (HB_WASM_EXEC_ENV + ptr_t(blob_t) blobptr) +{ + if (unlikely (!validate_app_addr (blobptr, sizeof (blob_t)))) + return; + + blob_t *blob = (blob_t *) addr_app_to_native (blobptr); + + module_free (blob->data); +} + + +}} + +#endif /* HB_WASM_API_BLOB_HH */ diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 26357f197..6609c6860 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -51,6 +51,9 @@ static void debugprint4 (HB_WASM_EXEC_ENV char *str, int32_t i1, int32_t i2, int static NativeSymbol _hb_wasm_native_symbols[] = { + /* blob */ + NATIVE_SYMBOL ("(i)", blob_free), + /* buffer */ NATIVE_SYMBOL ("(ii)", buffer_copy_contents), diff --git a/src/hb-wasm-api.cc b/src/hb-wasm-api.cc index 20720f0b3..f09b3831d 100644 --- a/src/hb-wasm-api.cc +++ b/src/hb-wasm-api.cc @@ -27,6 +27,7 @@ #define module_inst wasm_runtime_get_module_inst (exec_env) +#include "hb-wasm-api-blob.hh" #include "hb-wasm-api-buffer.hh" #include "hb-wasm-api-face.hh" #include "hb-wasm-api-font.hh" diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 98cc1e4a9..f919fc0d8 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -91,7 +91,8 @@ typedef struct ptr_t(char) data; } blob_t; -/* TODO blob_free */ +HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV + ptr_t(blob_t) blob); /* buffer */ diff --git a/src/meson.build b/src/meson.build index db99bd79e..32076c278 100644 --- a/src/meson.build +++ b/src/meson.build @@ -327,6 +327,7 @@ hb_graphite2_headers = files('hb-graphite2.h') hb_wasm_sources = files( 'hb-wasm-api.cc', 'hb-wasm-api.hh', + 'hb-wasm-api-blob.hh', 'hb-wasm-api-buffer.hh', 'hb-wasm-api-face.hh', 'hb-wasm-api-font.hh', diff --git a/src/wasm-sample/Makefile b/src/wasm-sample/Makefile index e80d481e9..1f1704e6a 100644 --- a/src/wasm-sample/Makefile +++ b/src/wasm-sample/Makefile @@ -16,3 +16,5 @@ all: test.wasm.ttf clean: $(RM) test.wasm.ttf shape.so + +.PRECIOUS: shape.so diff --git a/src/wasm-sample/shape.c b/src/wasm-sample/shape.c index 480701771..89ea28242 100644 --- a/src/wasm-sample/shape.c +++ b/src/wasm-sample/shape.c @@ -16,7 +16,7 @@ shape (font_t font, buffer_t buffer) debugprint1 ("cmap length", blob.length); - free (blob.data); + blob_free (&blob); buffer_contents_t contents = buffer_copy_contents (buffer);