From 4a06b57d270f6972d4f910a73a624a701a707d5c Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sat, 25 Apr 2020 10:43:42 -0700 Subject: Reduce footprint of :fd property. Querying the :fd stream property is equivalent to calling the stream-fd function. Streams have a C virtual function get_fd, so implementing :fd in their getprop functions is redundant functionality. The stream-getprop function can test for :fd and call stream-fd, so the stream implementations don't have to deal with the :fd property. Also, there are still places in the code base that are using stream_getprop to get the file descriptor, instead of calling stream_fd. If we fix all this, then fd_k remains referenced only in a very small number of places. * socket.c (dgram_get_prop): Don't handle :fd any more. * stream.c (unimpl_get_fd): Static function removed. (fill_stream_ops): Default the get_fd function to null_get_fd instead of unimpl_get_fd, so it doesn't throw. Even a stdio stream don't throw; when the file is closed, it returns nil. (stdio_get_prop): Don't handle :fd any more. (stream_get_prop): Handle :fd here. If the stream has a get_fd function that isn't null_get_fd, then call it. Only if the stream doesn't have a get_fd function, fall back on its get_prop function. * sysif.c (mkdir_wrap, poll_wrap, simulate_setuid_setgid): Call stream_fd instead of stream_get_prop. --- socket.c | 3 --- stream.c | 13 +++++-------- sysif.c | 6 +++--- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/socket.c b/socket.c index cda85e7e..9b213e4f 100644 --- a/socket.c +++ b/socket.c @@ -552,9 +552,6 @@ static val dgram_get_prop(val stream, val ind) { struct dgram_stream *d = coerce(struct dgram_stream *, stream->co.handle); - if (ind == fd_k) - return num(d->fd); - if (ind == name_k) { if (d->fd == -1) return lit("closed"); diff --git a/stream.c b/stream.c index 2f42ac3f..23dfc30e 100644 --- a/stream.c +++ b/stream.c @@ -217,11 +217,6 @@ static noreturn val unimpl_truncate(val stream, val len) unimpl(stream, lit("truncate-stream")); } -static noreturn val unimpl_get_fd(val stream) -{ - unimpl(stream, lit("fileno")); -} - static noreturn val unimpl_get_sock_family(val stream) { unimpl(stream, lit("sock-family")); @@ -421,7 +416,7 @@ void fill_stream_ops(struct strm_ops *ops) if (!ops->clear_error) ops->clear_error = null_clear_error; if (!ops->get_fd) - ops->get_fd = unimpl_get_fd; + ops->get_fd = null_get_fd; if (!ops->get_sock_family) ops->get_sock_family = unimpl_get_sock_family; if (!ops->get_sock_type) @@ -726,8 +721,6 @@ static val stdio_get_prop(val stream, val ind) return h->is_real_time ? t : nil; } else if (ind == name_k) { return h->descr; - } else if (ind == fd_k) { - return h->f ? num(fileno(h->f)) : nil; } else if (ind == byte_oriented_k) { return h->is_byte_oriented ? t : nil; } @@ -2896,6 +2889,10 @@ val stream_get_prop(val stream, val ind) val self = lit("stream-get-prop"); struct strm_ops *ops = coerce(struct strm_ops *, cobj_ops(self, stream, stream_s)); + + if (ind == fd_k && ops->get_fd != null_get_fd) + return ops->get_fd(stream); + return ops->get_prop(stream, ind); } diff --git a/sysif.c b/sysif.c index dcea7964..4b8f8b29 100644 --- a/sysif.c +++ b/sysif.c @@ -303,7 +303,7 @@ static val mkdir_wrap(val path, val mode) #if HAVE_CHMOD || HAVE_CHOWN || HAVE_SYS_STAT || HAVE_FILE_STAMP_CHANGE static int get_fd(val stream, val self) { - val fd_in = if3(integerp(stream), stream, stream_get_prop(stream, fd_k)); + val fd_in = if3(integerp(stream), stream, stream_fd(stream)); if (stream && !fd_in) uw_throwf(file_error_s, @@ -1326,7 +1326,7 @@ static val poll_wrap(val poll_list, val timeout_in) break; case COBJ: if (subtypep(obj->co.cls, stream_s)) { - val fdval = stream_get_prop(obj, fd_k); + val fdval = stream_fd(obj); if (!fdval) { free(pfd); uw_throwf(file_error_s, @@ -1553,7 +1553,7 @@ void simulate_setuid_setgid(val open_script) return; { - val fdv = stream_get_prop(open_script, fd_k); + val fdv = stream_fd(open_script); if (fdv) { struct stat stb; -- cgit v1.2.3