summaryrefslogtreecommitdiffstatshomepage
path: root/ports/zephyr/modusocket.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 /ports/zephyr/modusocket.c
parent8f11d0b532bd0203c875ad9e3e4efd8a7014ca15 (diff)
downloadmicropython-cf31d384f1d2ca09f817f154892eaa6860c10144.tar.gz
micropython-cf31d384f1d2ca09f817f154892eaa6860c10144.zip
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 'ports/zephyr/modusocket.c')
-rw-r--r--ports/zephyr/modusocket.c33
1 files changed, 22 insertions, 11 deletions
diff --git a/ports/zephyr/modusocket.c b/ports/zephyr/modusocket.c
index 95414a49b4..84d6fa7297 100644
--- a/ports/zephyr/modusocket.c
+++ b/ports/zephyr/modusocket.c
@@ -299,20 +299,31 @@ STATIC mp_obj_t socket_makefile(size_t n_args, const mp_obj_t *args) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_makefile_obj, 1, 3, socket_makefile);
-STATIC mp_obj_t socket_close(mp_obj_t self_in) {
- socket_obj_t *socket = self_in;
- if (socket->ctx != -1) {
- int res = zsock_close(socket->ctx);
- RAISE_SOCK_ERRNO(res);
- socket->ctx = -1;
+STATIC mp_uint_t sock_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
+ socket_obj_t *socket = o_in;
+ (void)arg;
+ switch (request) {
+ case MP_STREAM_CLOSE:
+ if (socket->ctx != -1) {
+ int res = zsock_close(socket->ctx);
+ RAISE_SOCK_ERRNO(res);
+ if (res == -1) {
+ *errcode = errno;
+ return MP_STREAM_ERROR;
+ }
+ socket->ctx = -1;
+ }
+ return 0;
+
+ default:
+ *errcode = MP_EINVAL;
+ return MP_STREAM_ERROR;
}
- return mp_const_none;
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);
STATIC const mp_rom_map_elem_t socket_locals_dict_table[] = {
- { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&socket_close_obj) },
- { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&socket_close_obj) },
+ { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_stream_close_obj) },
+ { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
{ MP_ROM_QSTR(MP_QSTR_bind), MP_ROM_PTR(&socket_bind_obj) },
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&socket_connect_obj) },
{ MP_ROM_QSTR(MP_QSTR_listen), MP_ROM_PTR(&socket_listen_obj) },
@@ -332,7 +343,7 @@ STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table);
STATIC const mp_stream_p_t socket_stream_p = {
.read = sock_read,
.write = sock_write,
- //.ioctl = sock_ioctl,
+ .ioctl = sock_ioctl,
};
STATIC const mp_obj_type_t socket_type = {