summaryrefslogtreecommitdiff
path: root/extmod/modwebrepl.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-03-07 17:48:53 +1100
committerDamien George <damien.p.george@gmail.com>2018-04-10 13:41:32 +1000
commitcf31d384f1d2ca09f817f154892eaa6860c10144 (patch)
tree27b6dacefc471a394636e86ac787f6dd29f4a809 /extmod/modwebrepl.c
parent8f11d0b532bd0203c875ad9e3e4efd8a7014ca15 (diff)
py/stream: Switch stream close operation from method to ioctl.
This patch moves the implementation of stream closure from a dedicated method to the ioctl of the stream protocol, for each type that implements closing. The benefits of this are: 1. Rounds out the stream ioctl function, which already includes flush, seek and poll (among other things). 2. Makes calling mp_stream_close() on an object slightly more efficient because it now no longer needs to lookup the close method and call it, rather it just delegates straight to the ioctl function (if it exists). 3. Reduces code size and allows future types that implement the stream protocol to be smaller because they don't need a dedicated close method. Code size reduction is around 200 bytes smaller for x86 archs and around 30 bytes smaller for the bare-metal archs.
Diffstat (limited to 'extmod/modwebrepl.c')
-rw-r--r--extmod/modwebrepl.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/extmod/modwebrepl.c b/extmod/modwebrepl.c
index 42b30f5ea..983b5a10c 100644
--- a/extmod/modwebrepl.c
+++ b/extmod/modwebrepl.c
@@ -297,12 +297,20 @@ STATIC mp_uint_t webrepl_write(mp_obj_t self_in, const void *buf, mp_uint_t size
return stream_p->write(self->sock, buf, size, errcode);
}
-STATIC mp_obj_t webrepl_close(mp_obj_t self_in) {
- mp_obj_webrepl_t *self = MP_OBJ_TO_PTR(self_in);
- // TODO: This is a place to do cleanup
- return mp_stream_close(self->sock);
+STATIC mp_uint_t webrepl_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
+ mp_obj_webrepl_t *self = MP_OBJ_TO_PTR(o_in);
+ (void)arg;
+ switch (request) {
+ case MP_STREAM_CLOSE:
+ // TODO: This is a place to do cleanup
+ mp_stream_close(self->sock);
+ return 0;
+
+ default:
+ *errcode = MP_EINVAL;
+ return MP_STREAM_ERROR;
+ }
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(webrepl_close_obj, webrepl_close);
STATIC mp_obj_t webrepl_set_password(mp_obj_t passwd_in) {
size_t len;
@@ -319,13 +327,14 @@ STATIC const mp_rom_map_elem_t webrepl_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
- { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&webrepl_close_obj) },
+ { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
};
STATIC MP_DEFINE_CONST_DICT(webrepl_locals_dict, webrepl_locals_dict_table);
STATIC const mp_stream_p_t webrepl_stream_p = {
.read = webrepl_read,
.write = webrepl_write,
+ .ioctl = webrepl_ioctl,
};
STATIC const mp_obj_type_t webrepl_type = {