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 <damien@micropython.org>
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit is contained in:
Alessandro Gatti 2024-09-22 15:39:24 +02:00 committed by Damien George
parent 2b5feb9121
commit 9796625457
3 changed files with 4 additions and 22 deletions

View File

@ -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)

View File

@ -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);

View File

@ -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