diff --git a/py/malloc.c b/py/malloc.c index 45e939b6c2..1d18a9a643 100644 --- a/py/malloc.c +++ b/py/malloc.c @@ -53,6 +53,20 @@ void *m_malloc(int num_bytes) { return ptr; } +void *m_malloc_maybe(int num_bytes) { + void *ptr = malloc(num_bytes); + if (ptr == NULL) { + return NULL; + } +#if MICROPY_MEM_STATS + total_bytes_allocated += num_bytes; + current_bytes_allocated += num_bytes; + UPDATE_PEAK(); +#endif + DEBUG_printf("malloc %d : %p\n", num_bytes, ptr); + return ptr; +} + #if MICROPY_ENABLE_FINALISER void *m_malloc_with_finaliser(int num_bytes) { if (num_bytes == 0) { diff --git a/py/misc.h b/py/misc.h index 4112a6f0da..3f538b98eb 100644 --- a/py/misc.h +++ b/py/misc.h @@ -38,6 +38,7 @@ typedef unsigned int uint; #define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num))) void *m_malloc(int num_bytes); +void *m_malloc_maybe(int num_bytes); void *m_malloc_with_finaliser(int num_bytes); void *m_malloc0(int num_bytes); void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes); diff --git a/py/objexcept.c b/py/objexcept.c index 51e7a2615a..1ef3e31dc6 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -20,6 +20,9 @@ typedef struct _mp_obj_exception_t { // Instance of MemoryError exception - needed by mp_malloc_fail const mp_obj_exception_t mp_const_MemoryError_obj = {{&mp_type_MemoryError}, MP_OBJ_NULL, {{&mp_type_tuple}, 0}}; +// Local non-heap memory for allocating an exception when we run out of RAM +STATIC mp_obj_exception_t mp_emergency_exception_obj; + // Instance of GeneratorExit exception - needed by generator.close() // This would belong to objgenerator.c, but to keep mp_obj_exception_t // definition module-private so far, have it here. @@ -51,7 +54,13 @@ STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "%s does not take keyword arguments", mp_obj_get_type_str(type_in))); } - mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, n_args); + mp_obj_exception_t *o = m_malloc_maybe(sizeof(mp_obj_exception_t) + n_args * sizeof(mp_obj_t)); + if (o == NULL) { + // Couldn't allocate heap memory; use local data instead. + o = &mp_emergency_exception_obj; + // We can't store any args. + n_args = 0; + } o->base.type = type; o->traceback = MP_OBJ_NULL; o->args.base.type = &mp_type_tuple; @@ -196,25 +205,35 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char assert(exc_type->make_new == mp_obj_exception_make_new); // make exception object - mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, 1); - o->base.type = exc_type; - o->traceback = MP_OBJ_NULL; - o->args.base.type = &mp_type_tuple; - o->args.len = 1; - - if (fmt == NULL) { - // no message - assert(0); + mp_obj_exception_t *o = m_malloc_maybe(sizeof(mp_obj_exception_t) + 1 * sizeof(mp_obj_t)); + if (o == NULL) { + // Couldn't allocate heap memory; use local data instead. + // Unfortunately, we won't be able to format the string... + o = &mp_emergency_exception_obj; + o->base.type = exc_type; + o->traceback = MP_OBJ_NULL; + o->args.base.type = &mp_type_tuple; + o->args.len = 0; } else { - // render exception message and store as .args[0] - // TODO: optimize bufferbloat - vstr_t *vstr = vstr_new(); - va_list ap; - va_start(ap, fmt); - vstr_vprintf(vstr, fmt, ap); - va_end(ap); - o->args.items[0] = mp_obj_new_str((byte*)vstr->buf, vstr->len, false); - vstr_free(vstr); + o->base.type = exc_type; + o->traceback = MP_OBJ_NULL; + o->args.base.type = &mp_type_tuple; + o->args.len = 1; + + if (fmt == NULL) { + // no message + assert(0); + } else { + // render exception message and store as .args[0] + // TODO: optimize bufferbloat + vstr_t *vstr = vstr_new(); + va_list ap; + va_start(ap, fmt); + vstr_vprintf(vstr, fmt, ap); + va_end(ap); + o->args.items[0] = mp_obj_new_str((byte*)vstr->buf, vstr->len, false); + vstr_free(vstr); + } } return o; @@ -259,7 +278,7 @@ void mp_obj_exception_clear_traceback(mp_obj_t self_in) { void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) { // make sure self_in is an exception instance // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that) - if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) { + if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new && self_in != &mp_emergency_exception_obj) { mp_obj_exception_t *self = self_in; // for traceback, we are just using the list object for convenience, it's not really a list of Python objects