Skip to content

Commit 9796625

Browse files
agattidpgeorge
andcommitted
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 <[email protected]> Signed-off-by: Alessandro Gatti <[email protected]>
1 parent 2b5feb9 commit 9796625

File tree

3 files changed

+4
-22
lines changed

3 files changed

+4
-22
lines changed

ports/unix/alloc.c

-19
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,6 @@ void mp_unix_mark_exec(void) {
8585
}
8686
}
8787

88-
#if MICROPY_FORCE_PLAT_ALLOC_EXEC
89-
// Provide implementation of libffi ffi_closure_* functions in terms
90-
// of the functions above. On a normal Linux system, this save a lot
91-
// of code size.
92-
void *ffi_closure_alloc(size_t size, void **code);
93-
void ffi_closure_free(void *ptr);
94-
95-
void *ffi_closure_alloc(size_t size, void **code) {
96-
size_t dummy;
97-
mp_unix_alloc_exec(size, code, &dummy);
98-
return *code;
99-
}
100-
101-
void ffi_closure_free(void *ptr) {
102-
(void)ptr;
103-
// TODO
104-
}
105-
#endif
106-
10788
MP_REGISTER_ROOT_POINTER(void *mmap_region_head);
10889

10990
#endif // MICROPY_EMIT_NATIVE || (MICROPY_PY_FFI && MICROPY_FORCE_PLAT_ALLOC_EXEC)

ports/unix/modffi.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ static mp_obj_t mod_ffi_callback(size_t n_args, const mp_obj_t *pos_args, mp_map
334334
const char *rettype = mp_obj_str_get_str(rettype_in);
335335

336336
mp_int_t nparams = MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(paramtypes_in));
337-
mp_obj_fficallback_t *o = mp_obj_malloc_var(mp_obj_fficallback_t, params, ffi_type *, nparams, &fficallback_type);
337+
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));
338+
o->base.type = &fficallback_type;
338339

339340
o->clo = ffi_closure_alloc(sizeof(ffi_closure), &o->func);
340341

ports/unix/mpconfigport.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ typedef long mp_off_t;
117117
#define MICROPY_HELPER_LEXER_UNIX (1)
118118
#define MICROPY_VFS_POSIX (1)
119119
#define MICROPY_READER_POSIX (1)
120-
#ifndef MICROPY_TRACKED_ALLOC
121-
#define MICROPY_TRACKED_ALLOC (MICROPY_BLUETOOTH_BTSTACK)
120+
#if MICROPY_PY_FFI || MICROPY_BLUETOOTH_BTSTACK
121+
#define MICROPY_TRACKED_ALLOC (1)
122122
#endif
123123

124124
// VFS stat functions should return time values relative to 1970/1/1

0 commit comments

Comments
 (0)