10000 py/obj: Introduce mp_obj_malloc_with_finaliser to allocate and set type. · micropython/micropython@4133c03 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4133c03

Browse files
committed
py/obj: Introduce mp_obj_malloc_with_finaliser to allocate and set type.
Following 709e832. 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>
1 parent 2423493 commit 4133c03

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

py/obj.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ MP_NOINLINE void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *ty
4444
return base;
4545
}
4646

47+
#if MICROPY_ENABLE_FINALISER
48+
// Allocates an object and also sets type, for mp_obj_malloc{,_var}_with_finaliser macros.
49+
MP_NOINLINE void *mp_obj_malloc_with_finaliser_helper(size_t num_bytes, const mp_obj_type_t *type) {
50+
mp_obj_base_t *base = (mp_obj_base_t *)m_malloc_with_finaliser(num_bytes);
51+
base->type = type;
52+
return base;
53+
}
54+
#endif
55+
4756
const mp_obj_type_t *MICROPY_WRAP_MP_OBJ_GET_TYPE(mp_obj_get_type)(mp_const_obj_t o_in) {
4857
#if MICROPY_OBJ_IMMEDIATE_OBJS && MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
4958

py/obj.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,16 @@ extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
916916
#define mp_obj_malloc_var(struct_type, var_field, var_type, var_num, obj_type) ((struct_type *)mp_obj_malloc_helper(offsetof(struct_type, var_field) + sizeof(var_type) * (var_num), obj_type))
917917
void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type);
918918

919+
// Object allocation macros for allocating objects that have a finaliser.
920+
#if MICROPY_ENABLE_FINALISER
921+
#define mp_obj_malloc_with_finaliser(struct_type, obj_type) ((struct_type *)mp_obj_malloc_with_finaliser_helper(sizeof(struct_type), obj_type))
922+
#define mp_obj_malloc_var_with_finaliser(struct_type, var_type, var_num, obj_type) ((struct_type *)mp_obj_malloc_with_finaliser_helper(sizeof(struct_type) + sizeof(var_type) * (var_num), obj_type))
923+
void *mp_obj_malloc_with_finaliser_helper(size_t num_bytes, const mp_obj_type_t *type);
924+
#else
925+
#define mp_obj_malloc_with_finaliser(struct_type, obj_type) mp_obj_malloc(struct_type, obj_type)
926+
#define mp_obj_malloc_var_with_finaliser(struct_type, var_type, var_num, obj_type) mp_obj_malloc_var(struct_type, var_type, var_num, obj_type)
927+
#endif
928+
919929
// These macros are derived from more primitive ones and are used to
920930
// check for more specific object types.
921931
// Note: these are kept as macros because inline functions sometimes use much

0 commit comments

Comments
 (0)
0