From 3943026efdad0478874d478ffb3770c903a5cf2a Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Thu, 6 Mar 2025 19:55:47 +0200 Subject: [PATCH 1/2] mpprint: prefix pointer with `0x` --- py/mpprint.c | 1 + 1 file changed, 1 insertion(+) diff --git a/py/mpprint.c b/py/mpprint.c index 28dfa5020ac2f..a0eb4b89808be 100644 --- a/py/mpprint.c +++ b/py/mpprint.c @@ -536,6 +536,7 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) { } case 'p': case 'P': // don't bother to handle upcase for 'P' + chrs += mp_print_str(print, "0x"); // used by meminfo JSON dumps // Use unsigned long int to work on both ILP32 and LP64 systems chrs += mp_print_int(print, va_arg(args, unsigned long int), 0, 16, 'a', flags, fill, width); break; From e4d81eb3802b567960ae9405b33c0c9e6efe7f0d Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sun, 16 Mar 2025 10:48:07 +0200 Subject: [PATCH 2/2] gc: support user-defined OOM callback --- py/gc.c | 14 ++++++++++++++ py/gc.h | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/py/gc.c b/py/gc.c index 41be38e6c0a19..604334c95d5a9 100644 --- a/py/gc.c +++ b/py/gc.c @@ -455,6 +455,15 @@ void gc_info(gc_info_t *info) { GC_EXIT(); } +#if MICROPY_OOM_CALLBACK +static gc_oom_callback_t gc_oom_callback = NULL; + +void gc_set_oom_callback(gc_oom_callback_t func) +{ + gc_oom_callback = func; +} +#endif + void *gc_alloc(size_t n_bytes, unsigned int alloc_flags) { bool has_finaliser = alloc_flags & GC_ALLOC_FLAG_HAS_FINALISER; size_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK; @@ -504,6 +513,11 @@ void *gc_alloc(size_t n_bytes, unsigned int alloc_flags) { GC_EXIT(); // nothing found! if (collected) { + #if MICROPY_OOM_CALLBACK + if (gc_oom_callback) { + gc_oom_callback(); + } + #endif return NULL; } DEBUG_printf("gc_alloc(" UINT_FMT "): no free mem, triggering GC\n", n_bytes); diff --git a/py/gc.h b/py/gc.h index 93231fa56a64a..ed4c99fcb54be 100644 --- a/py/gc.h +++ b/py/gc.h @@ -73,4 +73,10 @@ void gc_dump_alloc_table(void); extern size_t alloc_count; #endif +#if MICROPY_OOM_CALLBACK +typedef void (*gc_oom_callback_t)(void); + +void gc_set_oom_callback(gc_oom_callback_t func); +#endif + #endif // MICROPY_INCLUDED_PY_GC_H