summaryrefslogtreecommitdiff
path: root/py/objboundmeth.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-03-08 15:36:53 +0000
committerDamien George <damien.p.george@gmail.com>2016-03-08 15:36:53 +0000
commitdcdf8f2d1404c8a9b6fb91ae1b31c6b991a08017 (patch)
tree21121593088e225a73c32f9bd359540c92128a46 /py/objboundmeth.c
parentbb293e6bcfbaad6d4fa247c56d62a8af55fd3068 (diff)
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.
Diffstat (limited to 'py/objboundmeth.c')
-rw-r--r--py/objboundmeth.c33
1 files changed, 18 insertions, 15 deletions
diff --git a/py/objboundmeth.c b/py/objboundmeth.c
index c0e75eae9..e32caba33 100644
--- a/py/objboundmeth.c
+++ b/py/objboundmeth.c
@@ -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
- mp_uint_t n_total = n_args + 2 * n_kw;
- if (n_total <= 4) {
- // use stack to allocate temporary args array
- mp_obj_t args2[5];
- args2[0] = self->self;
- memcpy(args2 + 1, args, n_total * sizeof(mp_obj_t));
- return mp_call_function_n_kw(self->meth, n_args + 1, n_kw, &args2[0]);
- } 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;
+ size_t n_total = n_args + 2 * n_kw;
+ mp_obj_t *args2 = NULL;
+ mp_obj_t *free_args2 = NULL;
+ if (n_total > 4) {
+ // try to use heap to allocate temporary args array
+ args2 = m_new_maybe(mp_obj_t, 1 + n_total);
+ free_args2 = args2;
}
+ 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