From 97966254577167f4c50200af1af89109a275da1c Mon Sep 17 00:00:00 2001 From: Alessandro Gatti Date: Sun, 22 Sep 2024 15:39:24 +0200 Subject: [PATCH] unix/modffi: Clean up FFI closures memory management. This commit removes custom FFI closures alloc/free functions, in favour of using the tracked allocation facility to allocate memory for FFI callback objects. This stems from linking issues in the Arm port when updating LibFFI to the latest stable version, as the overridden alloc/free functions didn't replace LibFFI's (unlike in other ports). The original code did no effective cleanup for allocated callback objects, so there is no real impact when switching allocation strategy. The tracked allocation feature used to be enabled only if the Bluetooth stack integration was enabled. This commit also enables tracked allocation support if FFI support is enabled. Co-authored-by: Damien George Signed-off-by: Alessandro Gatti --- ports/unix/alloc.c | 19 ------------------- ports/unix/modffi.c | 3 ++- ports/unix/mpconfigport.h | 4 ++-- 3 files changed, 4 insertions(+), 22 deletions(-) diff --git a/ports/unix/alloc.c b/ports/unix/alloc.c index e9cf52158..c508139a8 100644 --- a/ports/unix/alloc.c +++ b/ports/unix/alloc.c @@ -85,25 +85,6 @@ void mp_unix_mark_exec(void) { } } -#if MICROPY_FORCE_PLAT_ALLOC_EXEC -// Provide implementation of libffi ffi_closure_* functions in terms -// of the functions above. On a normal Linux system, this save a lot -// of code size. -void *ffi_closure_alloc(size_t size, void **code); -void ffi_closure_free(void *ptr); - -void *ffi_closure_alloc(size_t size, void **code) { - size_t dummy; - mp_unix_alloc_exec(size, code, &dummy); - return *code; -} - -void ffi_closure_free(void *ptr) { - (void)ptr; - // TODO -} -#endif - MP_REGISTER_ROOT_POINTER(void *mmap_region_head); #endif // MICROPY_EMIT_NATIVE || (MICROPY_PY_FFI && MICROPY_FORCE_PLAT_ALLOC_EXEC) diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c index 51beb355f..dc3ed4dc7 100644 --- a/ports/unix/modffi.c +++ b/ports/unix/modffi.c @@ -334,7 +334,8 @@ static mp_obj_t mod_ffi_callback(size_t n_args, const mp_obj_t *pos_args, mp_map const char *rettype = mp_obj_str_get_str(rettype_in); mp_int_t nparams = MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(paramtypes_in)); - mp_obj_fficallback_t *o = mp_obj_malloc_var(mp_obj_fficallback_t, params, ffi_type *, nparams, &fficallback_type); + mp_obj_fficallback_t *o = (mp_obj_fficallback_t *)m_tracked_calloc(offsetof(mp_obj_fficallback_t, params) + sizeof(ffi_type *) * nparams, sizeof(uint8_t)); + o->base.type = &fficallback_type; o->clo = ffi_closure_alloc(sizeof(ffi_closure), &o->func); diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 9c9d9228e..412754711 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -117,8 +117,8 @@ typedef long mp_off_t; #define MICROPY_HELPER_LEXER_UNIX (1) #define MICROPY_VFS_POSIX (1) #define MICROPY_READER_POSIX (1) -#ifndef MICROPY_TRACKED_ALLOC -#define MICROPY_TRACKED_ALLOC (MICROPY_BLUETOOTH_BTSTACK) +#if MICROPY_PY_FFI || MICROPY_BLUETOOTH_BTSTACK +#define MICROPY_TRACKED_ALLOC (1) #endif // VFS stat functions should return time values relative to 1970/1/1