Skip to content

Commit 2411f42

Browse files
pfalcondpgeorge
authored andcommitted
extmod/moductypes: Make sizeof() accept "layout" parameter.
sizeof() can work in two ways: a) calculate size of already instantiated structure ("sizeof variable") - in this case we already no layout; b) size of structure decsription ("sizeof type"). In the latter case, LAYOUT_NATIVE was assumed, but there should possibility to calculate size for other layouts too. So, with this patch, there're now 2 forms: uctypes.sizeof(struct) uctypes.sizeof(struct_desc, layout)
1 parent 454cca6 commit 2411f42

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

extmod/moductypes.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ STATIC mp_uint_t uctypes_struct_size(mp_obj_t desc_in, int layout_type, mp_uint_
269269
return total_size;
270270
}
271271

272-
STATIC mp_obj_t uctypes_struct_sizeof(mp_obj_t obj_in) {
272+
STATIC mp_obj_t uctypes_struct_sizeof(size_t n_args, const mp_obj_t *args) {
273+
mp_obj_t obj_in = args[0];
273274
mp_uint_t max_field_size = 0;
274275
if (MP_OBJ_IS_TYPE(obj_in, &mp_type_bytearray)) {
275276
return mp_obj_len(obj_in);
@@ -278,15 +279,22 @@ STATIC mp_obj_t uctypes_struct_sizeof(mp_obj_t obj_in) {
278279
// We can apply sizeof either to structure definition (a dict)
279280
// or to instantiated structure
280281
if (MP_OBJ_IS_TYPE(obj_in, &uctypes_struct_type)) {
282+
if (n_args != 1) {
283+
mp_raise_TypeError(NULL);
284+
}
281285
// Extract structure definition
282286
mp_obj_uctypes_struct_t *obj = MP_OBJ_TO_PTR(obj_in);
283287
obj_in = obj->desc;
284288
layout_type = obj->flags;
289+
} else {
290+
if (n_args == 2) {
291+
layout_type = mp_obj_get_int(args[1]);
292+
}
285293
}
286294
mp_uint_t size = uctypes_struct_size(obj_in, layout_type, &max_field_size);
287295
return MP_OBJ_NEW_SMALL_INT(size);
288296
}
289-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_sizeof_obj, uctypes_struct_sizeof);
297+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(uctypes_struct_sizeof_obj, 1, 2, uctypes_struct_sizeof);
290298

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

0 commit comments

Comments
 (0)