|
13 | 13 |
|
14 | 14 | namespace LIBC_NAMESPACE_DECL {
|
15 | 15 |
|
16 |
| -// This is intended to be provided by the vendor. |
| 16 | +// These are intended to be provided by the vendor. |
| 17 | +// |
| 18 | +// The signature of these types and functions intentionally match `fopencookie` |
| 19 | +// which allows the following: |
| 20 | +// |
| 21 | +// ``` |
| 22 | +// struct __llvm_libc_stdio_cookie { ... }; |
| 23 | +// ... |
| 24 | +// struct __llvm_libc_stdio_cookie __llvm_libc_stdin_cookie; |
| 25 | +// cookie_io_functions_t stdin_func = { .read = __llvm_libc_stdio_read }; |
| 26 | +// FILE *stdin = fopencookie(&__llvm_libc_stdin_cookie, "r", stdin_func); |
| 27 | +// ... |
| 28 | +// struct __llvm_libc_stdio_cookie __llvm_libc_stdout_cookie; |
| 29 | +// cookie_io_functions_t stdout_func = { .write = __llvm_libc_stdio_write }; |
| 30 | +// FILE *stdout = fopencookie(&__llvm_libc_stdout_cookie, "w", stdout_func); |
| 31 | +// ... |
| 32 | +// struct __llvm_libc_stdio_cookie __llvm_libc_stderr_cookie; |
| 33 | +// cookie_io_functions_t stderr_func = { .write = __llvm_libc_stdio_write }; |
| 34 | +// FILE *stderr = fopencookie(&__llvm_libc_stderr_cookie, "w", stderr_func); |
| 35 | +// ``` |
| 36 | +// |
| 37 | +// At the same time, implementation of functions like `printf` and `scanf` can |
| 38 | +// use `__llvm_libc_stdio_read` and `__llvm_libc_stdio_write` directly to avoid |
| 39 | +// the extra indirection. |
| 40 | +// |
| 41 | +// All three symbols `__llvm_libc_stdin_cookie`, `__llvm_libc_stdout_cookie`, |
| 42 | +// and `__llvm_libc_stderr_cookie` must be provided, even if they don't point |
| 43 | +// at anything. |
17 | 44 |
|
18 |
| -extern struct __llvm_libc_stdin __llvm_libc_stdin; |
19 |
| -extern "C" ssize_t __llvm_libc_stdin_read(void *cookie, char *buf, size_t size); |
| 45 | +struct __llvm_libc_stdio_cookie; |
20 | 46 |
|
21 |
| -extern "C" void __llvm_libc_log_write(const char *msg, size_t len); |
| 47 | +extern struct __llvm_libc_stdio_cookie __llvm_libc_stdin_cookie; |
| 48 | +extern struct __llvm_libc_stdio_cookie __llvm_libc_stdout_cookie; |
| 49 | +extern struct __llvm_libc_stdio_cookie __llvm_libc_stderr_cookie; |
| 50 | + |
| 51 | +extern "C" ssize_t __llvm_libc_stdio_read(void *cookie, char *buf, size_t size); |
| 52 | +extern "C" ssize_t __llvm_libc_stdio_write(void *cookie, const char *buf, |
| 53 | + size_t size); |
22 | 54 |
|
23 | 55 | ssize_t read_from_stdin(char *buf, size_t size) {
|
24 |
| - return __llvm_libc_stdin_read(reinterpret_cast<void *>(&__llvm_libc_stdin), |
| 56 | + return __llvm_libc_stdio_read(static_cast<void *>(&__llvm_libc_stdin_cookie), |
25 | 57 | buf, size);
|
26 | 58 | }
|
27 | 59 |
|
| 60 | +void write_to_stdout(cpp::string_view msg) { |
| 61 | + __llvm_libc_stdio_write(static_cast<void *>(&__llvm_libc_stdout_cookie), |
| 62 | + msg.data(), msg.size()); |
| 63 | +} |
| 64 | + |
28 | 65 | void write_to_stderr(cpp::string_view msg) {
|
29 |
| - __llvm_libc_log_write(msg.data(), msg.size()); |
| 66 | + __llvm_libc_stdio_write(static_cast<void *>(&__llvm_libc_stderr_cookie), |
| 67 | + msg.data(), msg.size()); |
30 | 68 | }
|
31 | 69 |
|
32 | 70 | } // namespace LIBC_NAMESPACE_DECL
|
0 commit comments