mirror of
https://github.com/micropython/micropython.git
synced 2026-01-06 04:00:28 +01:00
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
@@ -126,6 +126,8 @@ static inline void *m_realloc_dyn(void *ptr, size_t new_num_bytes) {
|
||||
#define mp_obj_get_array(o, len, items) (mp_obj_get_array_dyn((o), (len), (items)))
|
||||
#define mp_obj_list_append(list, item) (mp_fun_table.list_append((list), (item)))
|
||||
|
||||
#define mp_obj_malloc_helper(n, t) (mp_obj_malloc_helper_dyn(n, t))
|
||||
|
||||
static inline mp_obj_t mp_obj_new_str_of_type_dyn(const mp_obj_type_t *type, const byte *data, size_t len) {
|
||||
if (type == &mp_type_str) {
|
||||
return mp_obj_new_str((const char *)data, len);
|
||||
@@ -163,6 +165,12 @@ static inline mp_obj_t mp_obj_len_dyn(mp_obj_t o) {
|
||||
return mp_fun_table.call_function_n_kw(mp_fun_table.load_name(MP_QSTR_len), 1, &o);
|
||||
}
|
||||
|
||||
static inline void *mp_obj_malloc_helper_dyn(size_t num_bytes, const mp_obj_type_t *type) {
|
||||
mp_obj_base_t *base = (mp_obj_base_t *)m_malloc(num_bytes);
|
||||
base->type = type;
|
||||
return base;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
// General runtime functions
|
||||
|
||||
|
||||
Reference in New Issue
Block a user