summaryrefslogtreecommitdiff
path: root/py/obj.c
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2024-02-16 10:58:36 +1100
committerDamien George <damien@micropython.org>2024-02-20 10:32:51 +1100
commit4133c0304011ff7be23f47516aac20ed54505e7a (patch)
tree9bc8d25666bbf5b0e1226dd5ad3b94ef8ea9bd46 /py/obj.c
parent24234937747e6d7fd920d21358fb26b826398e1a (diff)
py/obj: Introduce mp_obj_malloc_with_finaliser to allocate and set type.
Following 709e8328d9812a2e02da6fba65a03f9a873d60a2. Using this helps to reduce code size. And it ensure that the type is always set as soon as the object is allocated, which is important for the GC to function correctly. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py/obj.c')
-rw-r--r--py/obj.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/py/obj.c b/py/obj.c
index 5e01198b6..e43451dad 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -44,6 +44,15 @@ MP_NOINLINE void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *ty
return base;
}
+#if MICROPY_ENABLE_FINALISER
+// Allocates an object and also sets type, for mp_obj_malloc{,_var}_with_finaliser macros.
+MP_NOINLINE void *mp_obj_malloc_with_finaliser_helper(size_t num_bytes, const mp_obj_type_t *type) {
+ mp_obj_base_t *base = (mp_obj_base_t *)m_malloc_with_finaliser(num_bytes);
+ base->type = type;
+ return base;
+}
+#endif
+
const mp_obj_type_t *MICROPY_WRAP_MP_OBJ_GET_TYPE(mp_obj_get_type)(mp_const_obj_t o_in) {
#if MICROPY_OBJ_IMMEDIATE_OBJS && MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A