Skip to content

Commit e8355eb

Browse files
jimmodpgeorge
authored andcommitted
py/obj: Add "full" and "empty" non-variable-length mp_obj_type_t.
This will always have the maximum/minimum size of a mp_obj_type_t representation and can be used as a member in other structs. Signed-off-by: Jim Mussared <[email protected]>
1 parent 5ddf671 commit e8355eb

File tree

15 files changed

+40
-21
lines changed

15 files changed

+40
-21
lines changed

examples/natmod/btree/btree_c.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ int mp_stream_posix_fsync(void *stream) {
9494
return res;
9595
}
9696

97-
mp_obj_type_t btree_type;
97+
mp_obj_full_type_t btree_type;
9898

9999
#include "extmod/modbtree.c"
100100

examples/natmod/framebuf/framebuf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ void *memset(void *s, int c, size_t n) {
88
}
99
#endif
1010

11-
mp_obj_type_t mp_type_framebuf;
11+
mp_obj_full_type_t mp_type_framebuf;
1212

1313
#include "extmod/modframebuf.c"
1414

examples/natmod/ure/ure.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ void *memmove(void *dest, const void *src, size_t n) {
3232
return mp_fun_table.memmove_(dest, src, n);
3333
}
3434

35-
mp_obj_type_t match_type;
36-
mp_obj_type_t re_type;
35+
mp_obj_full_type_t match_type;
36+
mp_obj_full_type_t re_type;
3737

3838
#include "extmod/modure.c"
3939

examples/natmod/uzlib/uzlib.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ void *memset(void *s, int c, size_t n) {
88
}
99
#endif
1010

11-
mp_obj_type_t decompio_type;
11+
mp_obj_full_type_t decompio_type;
1212

1313
#include "extmod/moduzlib.c"
1414

extmod/modbtree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void __dbpanic(DB *db) {
6767
}
6868

6969
STATIC mp_obj_btree_t *btree_new(DB *db, mp_obj_t stream) {
70-
mp_obj_btree_t *o = mp_obj_malloc(mp_obj_btree_t, &btree_type);
70+
mp_obj_btree_t *o = mp_obj_malloc(mp_obj_btree_t, (mp_obj_type_t *)&btree_type);
7171
o->stream = stream;
7272
o->db = db;
7373
o->start_key = mp_const_none;

extmod/modframebuf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
841841

842842
// this factory function is provided for backwards compatibility with old FrameBuffer1 class
843843
STATIC mp_obj_t legacy_framebuffer1(size_t n_args, const mp_obj_t *args_in) {
844-
mp_obj_framebuf_t *o = mp_obj_malloc(mp_obj_framebuf_t, &mp_type_framebuf);
844+
mp_obj_framebuf_t *o = mp_obj_malloc(mp_obj_framebuf_t, (mp_obj_type_t *)&mp_type_framebuf);
845845

846846
mp_buffer_info_t bufinfo;
847847
mp_get_buffer_raise(args_in[0], &bufinfo, MP_BUFFER_WRITE);

extmod/modnetwork.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ mp_obj_t mod_network_nic_ifconfig(struct netif *netif, size_t n_args, const mp_o
6262
struct _mod_network_socket_obj_t;
6363

6464
typedef struct _mod_network_nic_type_t {
65-
mp_obj_type_t base;
65+
// Ensure that this struct is big enough to hold any type size.
66+
mp_obj_full_type_t base;
6667

6768
// API for non-socket operations
6869
int (*gethostbyname)(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *ip_out);

extmod/modure.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ STATIC void re_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t
198198
STATIC mp_obj_t ure_exec(bool is_anchored, uint n_args, const mp_obj_t *args) {
199199
(void)n_args;
200200
mp_obj_re_t *self;
201-
if (mp_obj_is_type(args[0], &re_type)) {
201+
if (mp_obj_is_type(args[0], (mp_obj_type_t *)&re_type)) {
202202
self = MP_OBJ_TO_PTR(args[0]);
203203
} else {
204204
self = MP_OBJ_TO_PTR(mod_re_compile(1, args));
@@ -217,7 +217,7 @@ STATIC mp_obj_t ure_exec(bool is_anchored, uint n_args, const mp_obj_t *args) {
217217
return mp_const_none;
218218
}
219219

220-
match->base.type = &match_type;
220+
match->base.type = (mp_obj_type_t *)&match_type;
221221
match->num_matches = caps_num / 2; // caps_num counts start and end pointers
222222
match->str = args[1];
223223
return MP_OBJ_FROM_PTR(match);
@@ -282,7 +282,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_split_obj, 2, 3, re_split);
282282

283283
STATIC mp_obj_t re_sub_helper(size_t n_args, const mp_obj_t *args) {
284284
mp_obj_re_t *self;
285-
if (mp_obj_is_type(args[0], &re_type)) {
285+
if (mp_obj_is_type(args[0], (mp_obj_type_t *)&re_type)) {
286286
self = MP_OBJ_TO_PTR(args[0]);
287287
} else {
288288
self = MP_OBJ_TO_PTR(mod_re_compile(1, args));
@@ -305,7 +305,7 @@ STATIC mp_obj_t re_sub_helper(size_t n_args, const mp_obj_t *args) {
305305
vstr_t vstr_return;
306306
vstr_return.buf = NULL; // We'll init the vstr after the first match
307307
mp_obj_match_t *match = mp_local_alloc(sizeof(mp_obj_match_t) + caps_num * sizeof(char *));
308-
match->base.type = &match_type;
308+
match->base.type = (mp_obj_type_t *)&match_type;
309309
match->num_matches = caps_num / 2; // caps_num counts start and end pointers
310310
match->str = where;
311311

@@ -430,7 +430,7 @@ STATIC mp_obj_t mod_re_compile(size_t n_args, const mp_obj_t *args) {
430430
if (size == -1) {
431431
goto error;
432432
}
433-
mp_obj_re_t *o = mp_obj_malloc_var(mp_obj_re_t, char, size, &re_type);
433+
mp_obj_re_t *o = mp_obj_malloc_var(mp_obj_re_t, char, size, (mp_obj_type_t *)&re_type);
434434
#if MICROPY_PY_URE_DEBUG
435435
int flags = 0;
436436
if (n_args > 1) {

extmod/network_ninaw10.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ static const mp_rom_map_elem_t nina_locals_dict_table[] = {
774774

775775
static MP_DEFINE_CONST_DICT(nina_locals_dict, nina_locals_dict_table);
776776

777-
STATIC MP_DEFINE_CONST_OBJ_TYPE(
777+
STATIC MP_DEFINE_CONST_OBJ_FULL_TYPE(
778778
mod_network_nic_type_nina_base,
779779
MP_QSTR_nina,
780780
MP_TYPE_FLAG_NONE,

extmod/network_wiznet5k.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
10241024
locals_dict, &wiznet5k_locals_dict
10251025
);
10261026
#else // WIZNET5K_PROVIDED_STACK
1027-
STATIC MP_DEFINE_CONST_OBJ_TYPE(
1027+
STATIC MP_DEFINE_CONST_OBJ_FULL_TYPE(
10281028
mod_network_nic_type_wiznet5k_base,
10291029
MP_QSTR_WIZNET5K,
10301030
MP_TYPE_FLAG_NONE,

ports/cc3200/mods/modnetwork.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
DEFINE TYPES
3737
******************************************************************************/
3838
typedef struct _mod_network_nic_type_t {
39-
mp_obj_type_t base;
39+
// Ensure that this struct is big enough to hold any type size.
40+
mp_obj_full_type_t base;
4041
} mod_network_nic_type_t;
4142

4243
typedef struct _mod_network_socket_base_t {

ports/cc3200/mods/modwlan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,7 @@ STATIC const mp_rom_map_elem_t wlan_locals_dict_table[] = {
12851285
};
12861286
STATIC MP_DEFINE_CONST_DICT(wlan_locals_dict, wlan_locals_dict_table);
12871287

1288-
STATIC MP_DEFINE_CONST_OBJ_TYPE(
1288+
STATIC MP_DEFINE_CONST_OBJ_FULL_TYPE(
12891289
mod_network_nic_type_wlan_base,
12901290
MP_QSTR_WLAN,
12911291
MP_TYPE_FLAG_NONE,

py/obj.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,9 @@ typedef mp_int_t (*mp_buffer_fun_t)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_
562562
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
563563
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
564564

565+
// This struct will be updated to become a variable sized struct. In order to
566+
// use this as a member, or allocate dynamically, use the mp_obj_empty_type_t
567+
// or mp_obj_full_type_t structs below (which must be kept in sync).
565568
struct _mp_obj_type_t {
566569
// A type is an object so must start with this entry, which points to mp_type_type.
567570
mp_obj_base_t base;
@@ -632,6 +635,13 @@ struct _mp_obj_type_t {
632635
struct _mp_obj_dict_t *locals_dict;
633636
};
634637

638+
// Non-variable sized versions of mp_obj_type_t to be used as a member
639+
// in other structs or for dynamic allocation. The fields are exactly
640+
// as in mp_obj_type_t, but with a fixed size for the flexible array
641+
// members.
642+
typedef mp_obj_type_t mp_obj_empty_type_t;
643+
typedef mp_obj_type_t mp_obj_full_type_t;
644+
635645
#define MP_TYPE_NULL_MAKE_NEW (NULL)
636646

637647
// Implementation of MP_DEFINE_CONST_OBJ_TYPE for each number of arguments.
@@ -657,14 +667,15 @@ struct _mp_obj_type_t {
657667
// of the 30th argument (30 is 13*2 + 4).
658668
#define MP_DEFINE_CONST_OBJ_TYPE_NARGS(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, N, ...) MP_DEFINE_CONST_OBJ_TYPE_NARGS_##N
659669

660-
// This macro is used to define a object type in ROM.
670+
// These macros are used to define a object type in ROM.
661671
// Invoke as MP_DEFINE_CONST_OBJ_TYPE(_typename, _name, _flags, _make_new [, slot, func]*)
662-
// It uses the number of arguments to select which MP_DEFINE_CONST_OBJ_TYPE_*
672+
// They use the number of arguments to select which MP_DEFINE_CONST_OBJ_TYPE_*
663673
// macro to use based on the number of arguments. It works by shifting the
664674
// numeric values 12, 11, ... 0 by the number of arguments, such that the
665675
// 30th argument ends up being the number to use. The _INV values are
666676
// placeholders because the slot arguments come in pairs.
667677
#define MP_DEFINE_CONST_OBJ_TYPE(...) MP_DEFINE_CONST_OBJ_TYPE_EXPAND(MP_DEFINE_CONST_OBJ_TYPE_NARGS(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0)(mp_obj_type_t, __VA_ARGS__))
678+
#define MP_DEFINE_CONST_OBJ_FULL_TYPE(...) MP_DEFINE_CONST_OBJ_TYPE_EXPAND(MP_DEFINE_CONST_OBJ_TYPE_NARGS(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0)(mp_obj_full_type_t, __VA_ARGS__))
668679

669680
// Constant types, globally accessible
670681
extern const mp_obj_type_t mp_type_type;

py/objnamedtuple.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
#include "py/objtuple.h"
3030

3131
typedef struct _mp_obj_namedtuple_type_t {
32-
mp_obj_type_t base;
32+
// Must use the full-size version to avoid this being a variable sized member.
33+
// This means that named tuples use slightly more RAM than necessary, but
34+
// no worse than if we didn't have slots/split representation.
35+
mp_obj_full_type_t base;
3336
size_t n_fields;
3437
qstr fields[];
3538
} mp_obj_namedtuple_type_t;

py/objtype.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,10 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
11481148
#endif
11491149
}
11501150

1151-
mp_obj_type_t *o = m_new0(mp_obj_type_t, 1);
1151+
// Allocate a full-sized mp_obj_full_type_t instance (i.e. all slots / extended fields).
1152+
// Given that Python types use almost all the slots anyway, this doesn't cost anything
1153+
// extra.
1154+
mp_obj_type_t *o = (mp_obj_type_t *)m_new0(mp_obj_full_type_t, 1);
11521155
o->base.type = &mp_type_type;
11531156
o->flags = base_flags;
11541157
o->name = name;

0 commit comments

Comments
 (0)