Skip to content

Commit 27e735f

Browse files
committed
py: Replace stream_p with *stream_p in mp_obj_type_t.
This is to reduce ROM usage. stream_p is used in file and socket types only (at the moment), so seems a good idea to make the protocol functions a pointer instead of the actual structure. It saves 308 bytes of ROM in the stmhal/ port, 928 in unix/.
1 parent 1752022 commit 27e735f

10 files changed

Lines changed: 30 additions & 26 deletions

File tree

py/obj.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ struct _mp_obj_type_t {
229229
// in mp_obj_type_t at the expense of extra pointer and extra dereference
230230
// when actually used.
231231
mp_buffer_p_t buffer_p;
232-
mp_stream_p_t stream_p;
232+
const mp_stream_p_t *stream_p;
233233

234234
// these are for dynamically created types (classes)
235235
mp_obj_t bases_tuple;

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Q(max)
111111
Q(min)
112112
Q(namedtuple)
113113
Q(next)
114+
Q(open)
114115
Q(ord)
115116
Q(path)
116117
Q(pow)

py/stream.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in);
1414

1515
STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
1616
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
17-
if (o->type->stream_p.read == NULL) {
17+
if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
1818
// CPython: io.UnsupportedOperation, OSError subclass
1919
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
2020
}
@@ -25,7 +25,7 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
2525
}
2626
byte *buf = m_new(byte, sz);
2727
int error;
28-
machine_int_t out_sz = o->type->stream_p.read(o, buf, sz, &error);
28+
machine_int_t out_sz = o->type->stream_p->read(o, buf, sz, &error);
2929
if (out_sz == -1) {
3030
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
3131
} else {
@@ -37,15 +37,15 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
3737

3838
STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
3939
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
40-
if (o->type->stream_p.write == NULL) {
40+
if (o->type->stream_p == NULL || o->type->stream_p->write == NULL) {
4141
// CPython: io.UnsupportedOperation, OSError subclass
4242
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
4343
}
4444

4545
uint sz;
4646
const char *buf = mp_obj_str_get_data(arg, &sz);
4747
int error;
48-
machine_int_t out_sz = o->type->stream_p.write(self_in, buf, sz, &error);
48+
machine_int_t out_sz = o->type->stream_p->write(self_in, buf, sz, &error);
4949
if (out_sz == -1) {
5050
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
5151
} else {
@@ -60,7 +60,7 @@ STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
6060
#define READ_SIZE 256
6161
STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
6262
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
63-
if (o->type->stream_p.read == NULL) {
63+
if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
6464
// CPython: io.UnsupportedOperation, OSError subclass
6565
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
6666
}
@@ -72,7 +72,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
7272
int error;
7373
int current_read = READ_SIZE;
7474
while (true) {
75-
machine_int_t out_sz = o->type->stream_p.read(self_in, p, current_read, &error);
75+
machine_int_t out_sz = o->type->stream_p->read(self_in, p, current_read, &error);
7676
if (out_sz == -1) {
7777
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
7878
}
@@ -101,7 +101,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
101101
// Unbuffered, inefficient implementation of readline() for raw I/O files.
102102
STATIC mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
103103
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
104-
if (o->type->stream_p.read == NULL) {
104+
if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
105105
// CPython: io.UnsupportedOperation, OSError subclass
106106
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
107107
}
@@ -126,7 +126,7 @@ STATIC mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
126126
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
127127
}
128128

129-
machine_int_t out_sz = o->type->stream_p.read(o, p, 1, &error);
129+
machine_int_t out_sz = o->type->stream_p->read(o, p, 1, &error);
130130
if (out_sz == -1) {
131131
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
132132
}

stm/qstrdefsport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ Q(Usart)
3636
Q(ADC)
3737
Q(ADC_all)
3838
Q(Audio)
39-
Q(open)
4039
Q(File)
4140
// Entries for sys.path
4241
Q(0:/)

stmhal/file.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,20 @@ STATIC const mp_map_elem_t file_locals_dict_table[] = {
6161
STATIC MP_DEFINE_CONST_DICT(file_locals_dict, file_locals_dict_table);
6262

6363
STATIC mp_obj_t file_obj_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args);
64-
static const mp_obj_type_t file_obj_type = {
64+
65+
STATIC const mp_stream_p_t file_obj_stream_p = {
66+
.read = file_read,
67+
.write = file_write,
68+
};
69+
70+
STATIC const mp_obj_type_t file_obj_type = {
6571
{ &mp_type_type },
6672
.name = MP_QSTR_File,
6773
.make_new = file_obj_make_new,
6874
.print = file_obj_print,
6975
.getiter = mp_identity,
7076
.iternext = mp_stream_unbuffered_iter,
71-
.stream_p = {
72-
.read = file_read,
73-
.write = file_write,
74-
},
77+
.stream_p = &file_obj_stream_p,
7578
.locals_dict = (mp_obj_t)&file_locals_dict,
7679
};
7780

stmhal/qstrdefsport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ Q(SDcard)
3333
Q(gpio)
3434
Q(gpio_in)
3535
Q(gpio_out)
36-
Q(open)
3736
Q(File)
3837
// Entries for sys.path
3938
Q(0:/)

unix/file.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,19 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = {
123123

124124
STATIC MP_DEFINE_CONST_DICT(rawfile_locals_dict, rawfile_locals_dict_table);
125125

126+
STATIC const mp_stream_p_t rawfile_stream_p = {
127+
.read = fdfile_read,
128+
.write = fdfile_write,
129+
};
130+
126131
STATIC const mp_obj_type_t rawfile_type = {
127132
{ &mp_type_type },
128133
.name = MP_QSTR_io_dot_FileIO,
129134
.print = fdfile_print,
130135
.make_new = fdfile_make_new,
131136
.getiter = mp_identity,
132137
.iternext = mp_stream_unbuffered_iter,
133-
.stream_p = {
134-
.read = fdfile_read,
135-
.write = fdfile_write,
136-
},
138+
.stream_p = &rawfile_stream_p,
137139
.locals_dict = (mp_obj_t)&rawfile_locals_dict,
138140
};
139141

unix/modsocket.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,19 @@ STATIC const mp_map_elem_t microsocket_locals_dict_table[] = {
240240

241241
STATIC MP_DEFINE_CONST_DICT(microsocket_locals_dict, microsocket_locals_dict_table);
242242

243+
STATIC const mp_stream_p_t microsocket_stream_p = {
244+
.read = socket_read,
245+
.write = socket_write,
246+
};
247+
243248
STATIC const mp_obj_type_t microsocket_type = {
244249
{ &mp_type_type },
245250
.name = MP_QSTR_socket,
246251
.print = socket_print,
247252
.make_new = socket_make_new,
248253
.getiter = NULL,
249254
.iternext = NULL,
250-
.stream_p = {
251-
.read = socket_read,
252-
.write = socket_write,
253-
},
255+
.stream_p = &microsocket_stream_p,
254256
.locals_dict = (mp_obj_t)&microsocket_locals_dict,
255257
};
256258

unix/mpconfigport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ typedef unsigned int machine_uint_t; // must be pointer size
3131
typedef void *machine_ptr_t; // must be of pointer size
3232
typedef const void *machine_const_ptr_t; // must be of pointer size
3333

34-
struct _mp_obj_fun_native_t;
3534
extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
3635
#define MICROPY_EXTRA_BUILTINS \
3736
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },

unix/qstrdefsport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Q(Test)
44

55
Q(argv)
6-
Q(open)
76
Q(stdin)
87
Q(stdout)
98
Q(stderr)

0 commit comments

Comments
 (0)