py/objboundmeth: Allocate arg state on stack if heap alloc fails.

If the heap is locked, or memory allocation fails, then calling a bound
method will still succeed by allocating the argument state on the stack.

The new code also allocates less stack than before if less than 4
arguments are passed.  It's also a tiny bit smaller in code size.

This was done as part of the ESA project.
This commit is contained in:
Damien George 2016-03-08 15:36:53 +00:00
parent bb293e6bcf
commit dcdf8f2d14

View File

@ -52,22 +52,25 @@ STATIC mp_obj_t bound_meth_call(mp_obj_t self_in, size_t n_args, size_t n_kw, co
// need to insert self->self before all other args and then call self->meth // need to insert self->self before all other args and then call self->meth
mp_uint_t n_total = n_args + 2 * n_kw; size_t n_total = n_args + 2 * n_kw;
if (n_total <= 4) { mp_obj_t *args2 = NULL;
// use stack to allocate temporary args array mp_obj_t *free_args2 = NULL;
mp_obj_t args2[5]; if (n_total > 4) {
args2[0] = self->self; // try to use heap to allocate temporary args array
memcpy(args2 + 1, args, n_total * sizeof(mp_obj_t)); args2 = m_new_maybe(mp_obj_t, 1 + n_total);
return mp_call_function_n_kw(self->meth, n_args + 1, n_kw, &args2[0]); free_args2 = args2;
} else {
// use heap to allocate temporary args array
mp_obj_t *args2 = m_new(mp_obj_t, 1 + n_total);
args2[0] = self->self;
memcpy(args2 + 1, args, n_total * sizeof(mp_obj_t));
mp_obj_t res = mp_call_function_n_kw(self->meth, n_args + 1, n_kw, &args2[0]);
m_del(mp_obj_t, args2, 1 + n_total);
return res;
} }
if (args2 == NULL) {
// (fallback to) use stack to allocate temporary args array
args2 = alloca(sizeof(mp_obj_t) * (1 + n_total));
}
args2[0] = self->self;
memcpy(args2 + 1, args, n_total * sizeof(mp_obj_t));
mp_obj_t res = mp_call_function_n_kw(self->meth, n_args + 1, n_kw, &args2[0]);
if (free_args2 != NULL) {
m_del(mp_obj_t, free_args2, 1 + n_total);
}
return res;
} }
#if MICROPY_PY_FUNCTION_ATTRS #if MICROPY_PY_FUNCTION_ATTRS