py/obj: Introduce mp_obj_malloc macro to allocate, and set object type.
This is to replace the following:
mp_foo_obj_t *self = m_new_obj(mp_foo_obj_t);
self->base.type = &mp_type_foo;
with:
mp_foo_obj_t *self = mp_obj_malloc(mp_foo_obj_t, &mp_type_foo);
Calling the function is less code than inlining setting the type
everywhere, adds up to ~100 bytes on PYBV11.
It also helps to avoid an easy mistake of forgetting to set the type.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
committed by
Damien George
parent
590de399f0
commit
709e8328d9
6
py/obj.h
6
py/obj.h
@@ -732,6 +732,12 @@ extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
|
||||
|
||||
// General API for objects
|
||||
|
||||
// Helper versions of m_new_obj when you need to immediately set base.type.
|
||||
// Implementing this as a call rather than inline saves 8 bytes per usage.
|
||||
#define mp_obj_malloc(struct_type, obj_type) ((struct_type *)mp_obj_malloc_helper(sizeof(struct_type), obj_type))
|
||||
#define mp_obj_malloc_var(struct_type, var_type, var_num, obj_type) ((struct_type *)mp_obj_malloc_helper(sizeof(struct_type) + sizeof(var_type) * (var_num), obj_type))
|
||||
void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type);
|
||||
|
||||
// These macros are derived from more primitive ones and are used to
|
||||
// check for more specific object types.
|
||||
// Note: these are kept as macros because inline functions sometimes use much
|
||||
|
||||
Reference in New Issue
Block a user