8000 py/obj: Introduce mp_obj_malloc macro to allocate, and set object type. · sstobbe/micropython@709e832 · GitHub
[go: up one dir, main page]

Skip to content

Commit 709e832

Browse files
jimmodpgeorge
authored andcommitted
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>
1 parent 590de39 commit 709e832

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

py/dynruntime.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ static inline void *m_realloc_dyn(void *ptr, size_t new_num_bytes) {
126126
#define mp_obj_get_array(o, len, items) (mp_obj_get_array_dyn((o), (len), (items)))
127127
#define mp_obj_list_append(list, item) (mp_fun_table.list_append((list), (item)))
128128

129+
#define mp_obj_malloc_helper(n, t) (mp_obj_malloc_helper_dyn(n, t))
130+
129131
static inline mp_obj_t mp_obj_new_str_of_type_dyn(const mp_obj_type_t *type, const byte *data, size_t len) {
130132
if (type == &mp_type_str) {
131133
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) {
163165
return mp_fun_table.call_function_n_kw(mp_fun_table.load_name(MP_QSTR_len), 1, &o);
164166
}
165167

168+
static inline void *mp_obj_malloc_helper_dyn(size_t num_bytes, const mp_obj_type_t *type) {
169+
mp_obj_base_t *base = (mp_obj_base_t *)m_malloc(num_bytes);
170+
base->type = type;
171+
return base;
172+
}
173+
166174
/******************************************************************************/
167175
// General runtime functions
168176

py/obj.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
#include "py/stackctrl.h"
3838
#include "py/stream.h" // for mp_obj_print
3939

40+
// Allocates an object and also sets type, for mp_obj_malloc{,_var} macros.
41+
void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type) {
42+
mp_obj_base_t *base = (mp_obj_base_t *)m_malloc(num_bytes);
43+
base->type = type;
44+
return base;
45+
}
46+
4047
const mp_obj_type_t *MICROPY_WRAP_MP_OBJ_GET_TYPE(mp_obj_get_type)(mp_const_obj_t o_in) {
4148
#if MICROPY_OBJ_IMMEDIATE_OBJS && MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
4249

py/obj.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,12 @@ extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
732732

733733
// General API for objects
734734

735+
// Helper versions of m_new_obj when you need to immediately set base.type.
736+
// Implementing this as a call rather than inline saves 8 bytes per usage.
737+
#define mp_obj_malloc(struct_type, obj_type) ((struct_type *)mp_obj_malloc_helper(sizeof(struct_type), obj_type))
738+
#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))
739+
void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type);
740+
735741
// These macros are derived from more primitive ones and are used to
736742
// check for more specific object types.
737743
// Note: these are kept as macros because inline functions sometimes use much

0 commit comments

Comments
 (0)
0