diff --git a/libc/src/stdio/gpu/file.h b/libc/src/stdio/gpu/file.h index 0ca1df8708a3f..77817e070680e 100644 --- a/libc/src/stdio/gpu/file.h +++ b/libc/src/stdio/gpu/file.h @@ -1,4 +1,4 @@ -//===--- GPU helper functions--------------------===// +//===--- GPU helper functions for file I/O using RPC ----------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -14,35 +14,17 @@ namespace __llvm_libc { namespace file { -LIBC_INLINE uint64_t write_to_stdout(const void *data, size_t size) { +template +LIBC_INLINE uint64_t write_impl(::FILE *file, const void *data, size_t size) { uint64_t ret = 0; - rpc::Client::Port port = rpc::client.open(); - port.send_n(data, size); - port.recv([&](rpc::Buffer *buffer) { - ret = reinterpret_cast(buffer->data)[0]; - }); - port.close(); - return ret; -} + rpc::Client::Port port = rpc::client.open(); -LIBC_INLINE uint64_t write_to_stderr(const void *data, size_t size) { - uint64_t ret = 0; - rpc::Client::Port port = rpc::client.open(); - port.send_n(data, size); - port.recv([&](rpc::Buffer *buffer) { - ret = reinterpret_cast(buffer->data)[0]; - }); - port.close(); - return ret; -} + if constexpr (opcode == RPC_WRITE_TO_STREAM) { + port.send([&](rpc::Buffer *buffer) { + buffer->data[0] = reinterpret_cast(file); + }); + } -LIBC_INLINE uint64_t write_to_stream(uintptr_t file, const void *data, - size_t size) { - uint64_t ret = 0; - rpc::Client::Port port = rpc::client.open(); - port.send([&](rpc::Buffer *buffer) { - reinterpret_cast(buffer->data)[0] = file; - }); port.send_n(data, size); port.recv([&](rpc::Buffer *buffer) { ret = reinterpret_cast(buffer->data)[0]; @@ -51,33 +33,24 @@ LIBC_INLINE uint64_t write_to_stream(uintptr_t file, const void *data, return ret; } -LIBC_INLINE uint64_t write(FILE *f, const void *data, size_t size) { +LIBC_INLINE uint64_t write(::FILE *f, const void *data, size_t size) { if (f == stdout) - return write_to_stdout(data, size); + return write_impl(f, data, size); else if (f == stderr) - return write_to_stderr(data, size); + return write_impl(f, data, size); else - return write_to_stream(reinterpret_cast(f), data, size); -} - -LIBC_INLINE uint64_t read_from_stdin(void *buf, size_t size) { - uint64_t ret = 0; - uint64_t recv_size; - rpc::Client::Port port = rpc::client.open(); - port.send([=](rpc::Buffer *buffer) { buffer->data[0] = size; }); - port.recv_n(&buf, &recv_size, [&](uint64_t) { return buf; }); - port.recv([&](rpc::Buffer *buffer) { ret = buffer->data[0]; }); - port.close(); - return ret; + return write_impl(f, data, size); } -LIBC_INLINE uint64_t read_from_stream(uintptr_t file, void *buf, size_t size) { +template +LIBC_INLINE uint64_t read_from_stream(::FILE *file, void *buf, size_t size) { uint64_t ret = 0; uint64_t recv_size; - rpc::Client::Port port = rpc::client.open(); + rpc::Client::Port port = rpc::client.open(); port.send([=](rpc::Buffer *buffer) { buffer->data[0] = size; - buffer->data[1] = file; + if constexpr (opcode == RPC_READ_FROM_STREAM) + buffer->data[1] = reinterpret_cast(file); }); port.recv_n(&buf, &recv_size, [&](uint64_t) { return buf; }); port.recv([&](rpc::Buffer *buffer) { ret = buffer->data[0]; }); @@ -85,11 +58,11 @@ LIBC_INLINE uint64_t read_from_stream(uintptr_t file, void *buf, size_t size) { return ret; } -LIBC_INLINE uint64_t read(FILE *f, void *data, size_t size) { +LIBC_INLINE uint64_t read(::FILE *f, void *data, size_t size) { if (f == stdin) - return read_from_stdin(data, size); + return read_from_stream(f, data, size); else - return read_from_stream(reinterpret_cast(f), data, size); + return read_from_stream(f, data, size); } } // namespace file