aboutsummaryrefslogtreecommitdiff
path: root/py/stream.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 /py/stream.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 'py/stream.c')
-rw-r--r--py/stream.c18
1 files changed, 11 insertions, 7 deletions
diff --git a/py/stream.c b/py/stream.c
index 453dee769..f51f634b6 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -105,13 +105,6 @@ const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) {
return stream_p;
}
-mp_obj_t mp_stream_close(mp_obj_t stream) {
- // TODO: Still consider using ioctl for close
- mp_obj_t dest[2];
- mp_load_method(stream, MP_QSTR_close, dest);
- return mp_call_method_n_kw(0, 0, dest);
-}
-
STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte flags) {
const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
@@ -434,6 +427,17 @@ mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
return MP_OBJ_STOP_ITERATION;
}
+mp_obj_t mp_stream_close(mp_obj_t stream) {
+ const mp_stream_p_t *stream_p = mp_get_stream_raise(stream, MP_STREAM_OP_IOCTL);
+ int error;
+ mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_CLOSE, 0, &error);
+ if (res == MP_STREAM_ERROR) {
+ mp_raise_OSError(error);
+ }
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_close_obj, mp_stream_close);
+
STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) {
const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);