Skip to content

Commit

Permalink
moductypes: add layout parameter to uctypes.sizeof
Browse files Browse the repository at this point in the history
  • Loading branch information
jpochyla committed Sep 6, 2017
1 parent beeb748 commit 6c5f953
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions extmod/moductypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,24 +262,31 @@ STATIC mp_uint_t uctypes_struct_size(mp_obj_t desc_in, int layout_type, mp_uint_
return total_size;
}

STATIC mp_obj_t uctypes_struct_sizeof(mp_obj_t obj_in) {
mp_uint_t max_field_size = 0;
STATIC mp_obj_t uctypes_struct_sizeof(size_t n_args, const mp_obj_t *args) {
mp_obj_t obj_in = args[0];
if (MP_OBJ_IS_TYPE(obj_in, &mp_type_bytearray)) {
return mp_obj_len(obj_in);
}
int layout_type = LAYOUT_NATIVE;
// Second argument can specify the layout type, native is a default
int layout_type;
if (n_args == 2) {
layout_type = mp_obj_get_int(args[1]);
} else {
layout_type = LAYOUT_NATIVE;
}
// We can apply sizeof either to structure definition (a dict)
// or to instantiated structure
// or to instantiated structure, which overrides the layout type
if (MP_OBJ_IS_TYPE(obj_in, &uctypes_struct_type)) {
// Extract structure definition
mp_obj_uctypes_struct_t *obj = MP_OBJ_TO_PTR(obj_in);
obj_in = obj->desc;
layout_type = obj->flags;
}
mp_uint_t max_field_size = 0;
mp_uint_t size = uctypes_struct_size(obj_in, layout_type, &max_field_size);
return MP_OBJ_NEW_SMALL_INT(size);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_sizeof_obj, uctypes_struct_sizeof);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(uctypes_struct_sizeof_obj, 1, 2, uctypes_struct_sizeof);

static inline mp_obj_t get_unaligned(uint val_type, byte *p, int big_endian) {
char struct_type = big_endian ? '>' : '<';
Expand Down

0 comments on commit 6c5f953

Please sign in to comment.