From 5bf81c37580a46052dad58db730d7840d55c17c8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 11 Mar 2025 22:46:16 -0600 Subject: [PATCH] [common] Make hb_malloc() et al public New API: +hb_malloc() +hb_calloc() +hb_realloc() +hb_free() --- docs/harfbuzz-sections.txt | 4 +++ src/fontations/lib.rs | 9 +------ src/hb-common.cc | 52 ++++++++++++++++++++++++++++++++++++++ src/hb-common.h | 11 ++++++++ src/hb-static.cc | 6 +---- src/hb.hh | 4 --- 6 files changed, 69 insertions(+), 17 deletions(-) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index f09c85128..c90853678 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -184,6 +184,10 @@ uint8_t HB_EXTERN HB_DEPRECATED HB_DEPRECATED_FOR +hb_malloc +hb_calloc +hb_realloc +hb_free HB_H_IN HB_OT_H_IN diff --git a/src/fontations/lib.rs b/src/fontations/lib.rs index 38c0eb2d8..e3b4c240f 100644 --- a/src/fontations/lib.rs +++ b/src/fontations/lib.rs @@ -24,13 +24,6 @@ use skrifa::outline::DrawSettings; use skrifa::OutlineGlyphCollection; use skrifa::{GlyphId, MetadataProvider}; -extern "C" { - pub fn hb_malloc(_: usize) -> *const ::std::os::raw::c_void; - pub fn hb_calloc(_: usize, _: usize) -> *const ::std::os::raw::c_void; - pub fn hb_realloc(_: *const ::std::os::raw::c_void, _: usize) -> *const ::std::os::raw::c_void; - pub fn hb_free(_: *const ::std::os::raw::c_void); -} - struct MyAllocator; unsafe impl GlobalAlloc for MyAllocator { @@ -43,7 +36,7 @@ unsafe impl GlobalAlloc for MyAllocator { } unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 { - hb_realloc(ptr as *const c_void, new_size) as *mut u8 + hb_realloc(ptr as *mut c_void, new_size) as *mut u8 } unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { diff --git a/src/hb-common.cc b/src/hb-common.cc index d3355c971..3344a1bcf 100644 --- a/src/hb-common.cc +++ b/src/hb-common.cc @@ -1217,6 +1217,58 @@ uint8_t return hb_color_get_blue (color); } +/** + * hb_malloc: + * @size: The size of the memory to allocate. + * + * Allocates @size bytes of memory, using the allocator set at + * compile-time. Typically just malloc(). + * + * Return value: A pointer to the allocated memory. + * + * XSince: REPLACEME + **/ +void* hb_malloc(size_t size) { return hb_malloc_impl (size); } + +/** + * hb_calloc: + * @nmemb: The number of elements to allocate. + * @size: The size of each element. + * + * Allocates @nmemb elements of @size bytes each, initialized to zero, + * using the allocator set at compile-time. Typically just calloc(). + * + * Return value: A pointer to the allocated memory. + * + * XSince: REPLACEME + **/ +void* hb_calloc(size_t nmemb, size_t size) { return hb_calloc_impl (nmemb, size); } + +/** + * hb_realloc: + * @ptr: The pointer to the memory to reallocate. + * @size: The new size of the memory. + * + * Reallocates the memory pointed to by @ptr to @size bytes, using the + * allocator set at compile-time. Typically just realloc(). + * + * Return value: A pointer to the reallocated memory. + * + * XSince: REPLACEME + **/ +void* hb_realloc(void *ptr, size_t size) { return hb_realloc_impl (ptr, size); } + +/** + * hb_free: + * @ptr: The pointer to the memory to free. + * + * Frees the memory pointed to by @ptr, using the allocator set at + * compile-time. Typically just free(). + * + * XSince: REPLACEME + **/ +void hb_free(void *ptr) { hb_free_impl (ptr); } + /* If there is no visibility control, then hb-static.cc will NOT * define anything. Instead, we get it to define one set in here diff --git a/src/hb-common.h b/src/hb-common.h index 110854548..1d7b824b6 100644 --- a/src/hb-common.h +++ b/src/hb-common.h @@ -65,6 +65,7 @@ typedef unsigned __int64 uint64_t; #else # include #endif +#include #if defined(__GNUC__) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) #define HB_DEPRECATED __attribute__((__deprecated__)) @@ -948,6 +949,16 @@ typedef struct hb_glyph_extents_t { */ typedef struct hb_font_t hb_font_t; +/* Not of much use to clients. */ +HB_EXTERN void* +hb_malloc (size_t size); +HB_EXTERN void* +hb_calloc (size_t nmemb, size_t size); +HB_EXTERN void* +hb_realloc (void *ptr, size_t size); +HB_EXTERN void +hb_free (void *ptr); + HB_END_DECLS #endif /* HB_COMMON_H */ diff --git a/src/hb-static.cc b/src/hb-static.cc index 634299778..e328bc48a 100644 --- a/src/hb-static.cc +++ b/src/hb-static.cc @@ -40,12 +40,8 @@ #include "hb-ot-hmtx-table.hh" #include "hb-ot-maxp-table.hh" -void* hb_malloc(size_t size) { return hb_malloc_impl (size); } -void* hb_calloc(size_t nmemb, size_t size) { return hb_calloc_impl (nmemb, size); } -void* hb_realloc(void *ptr, size_t size) { return hb_realloc_impl (ptr, size); } -void hb_free(void *ptr) { hb_free_impl (ptr); } - #ifndef HB_NO_VISIBILITY + #include "hb-ot-name-language-static.hh" uint64_t const _hb_NullPool[(HB_NULL_POOL_SIZE + sizeof (uint64_t) - 1) / sizeof (uint64_t)] = {}; diff --git a/src/hb.hh b/src/hb.hh index e245ad288..ffbfef099 100644 --- a/src/hb.hh +++ b/src/hb.hh @@ -533,10 +533,6 @@ extern "C" void hb_free_impl(void *ptr); #define hb_realloc_impl realloc #define hb_free_impl free #endif -extern "C" void* hb_malloc(size_t size); -extern "C" void* hb_calloc(size_t nmemb, size_t size); -extern "C" void* hb_realloc(void *ptr, size_t size); -extern "C" void hb_free(void *ptr);