diff options
| author | Damien George | 2018-06-13 11:54:44 +1000 |
|---|---|---|
| committer | Damien George | 2018-06-18 12:35:56 +1000 |
| commit | 6abede2ca9e221b6aefcaccbda0c89e367507df1 (patch) | |
| tree | 86f1097930f15027bf294510d221da002f320798 /py/stream.h | |
| parent | 31cf49c672f6e141df2178e2ab0b6560244ea2e6 (diff) | |
py/stream: Introduce and use efficient mp_get_stream to access stream_p.
The existing mp_get_stream_raise() helper does explicit checks that the
input object is a real pointer object, has a non-NULL stream protocol, and
has the desired stream C method (read/write/ioctl). In most cases it is
not necessary to do these checks because it is guaranteed that the input
object has the stream protocol and desired C methods. For example, native
objects that use the stream wrappers (eg mp_stream_readinto_obj) in their
locals dict always have the stream protocol (or else they shouldn't have
these wrappers in their locals dict).
This patch introduces an efficient mp_get_stream() which doesn't do any
checks and just extracts the stream protocol struct. This should be used
in all cases where the argument object is known to be a stream. The
existing mp_get_stream_raise() should be used primarily to verify that an
object does have the correct stream protocol methods.
All uses of mp_get_stream_raise() in py/stream.c have been converted to use
mp_get_stream() because the argument is guaranteed to be a proper stream
object.
This patch improves efficiency of stream operations and reduces code size.
Diffstat (limited to 'py/stream.h')
| -rw-r--r-- | py/stream.h | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/py/stream.h b/py/stream.h index 3dec49a2e..7b953138c 100644 --- a/py/stream.h +++ b/py/stream.h @@ -90,6 +90,11 @@ MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj); #define MP_STREAM_OP_WRITE (2) #define MP_STREAM_OP_IOCTL (4) +// Object is assumed to have a non-NULL stream protocol with valid r/w/ioctl methods +static inline const mp_stream_p_t *mp_get_stream(mp_const_obj_t self) { + return (const mp_stream_p_t*)((const mp_obj_base_t*)MP_OBJ_TO_PTR(self))->type->protocol; +} + const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags); mp_obj_t mp_stream_close(mp_obj_t stream); |
