diff options
| author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-08-11 09:42:39 +0300 |
|---|---|---|
| committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-08-11 09:43:07 +0300 |
| commit | bfc2092dc55d1faab4a6d7e832005afd61d25c3c (patch) | |
| tree | 37b1449bd001755fc37fe3d23232658b0c82eca8 /py/objtype.c | |
| parent | 7d4a2f773cc6ce24a91e2d210378188f3371e8a6 (diff) | |
py/modsys: Initial implementation of sys.getsizeof().
Implemented as a new MP_UNARY_OP. This patch adds support lists, dicts and
instances.
Diffstat (limited to 'py/objtype.c')
| -rw-r--r-- | py/objtype.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/py/objtype.c b/py/objtype.c index a258915f3..87c0cc9e5 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -339,11 +339,27 @@ const qstr mp_unary_op_method_name[] = { [MP_UNARY_OP_NEGATIVE] = MP_QSTR___neg__, [MP_UNARY_OP_INVERT] = MP_QSTR___invert__, #endif + #if MICROPY_PY_SYS_GETSIZEOF + [MP_UNARY_OP_SIZEOF] = MP_QSTR_getsizeof, + #endif [MP_UNARY_OP_NOT] = MP_QSTR_, // don't need to implement this, used to make sure array has full size }; STATIC mp_obj_t instance_unary_op(mp_uint_t op, mp_obj_t self_in) { mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in); + + #if MICROPY_PY_SYS_GETSIZEOF + if (MP_UNLIKELY(op == MP_UNARY_OP_SIZEOF)) { + // TODO: This doesn't count inherited objects (self->subobj) + const mp_obj_type_t *native_base; + size_t num_native_bases = instance_count_native_bases(mp_obj_get_type(self_in), &native_base); + + size_t sz = sizeof(*self) + sizeof(*self->subobj) * num_native_bases + + sizeof(*self->members.table) * self->members.alloc; + return MP_OBJ_NEW_SMALL_INT(sz); + } + #endif + qstr op_name = mp_unary_op_method_name[op]; /* Still try to lookup native slot if (op_name == 0) { |
