From dcdf8f2d1404c8a9b6fb91ae1b31c6b991a08017 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Tue, 8 Mar 2016 15:36:53 +0000
Subject: [PATCH] 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.
---
 py/objboundmeth.c | 33 ++++++++++++++++++---------------
 1 file 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
-- 
GitLab