Skip to content

Instantly share code, notes, and snippets.

@robertodauria
Created September 4, 2019 16:16
Show Gist options
  • Save robertodauria/6bb950515421c6ba7e1972306bbbf390 to your computer and use it in GitHub Desktop.
Save robertodauria/6bb950515421c6ba7e1972306bbbf390 to your computer and use it in GitHub Desktop.
diff --git a/libndt.hpp b/libndt.hpp
index baa203a..b4d8338 100644
--- a/libndt.hpp
+++ b/libndt.hpp
@@ -1638,21 +1638,22 @@ bool Client::run_download() noexcept {
]() noexcept {
// TODO(bassosimone): allocate on heap and keep safe using unique_ptr
// because with musl libc the stack is 80KB by default.
- char buf[131072];
+ constexpr size_t ndt_bufsize = 131072;
+ std::unique_ptr<char[]> buf(new char[ndt_bufsize]);
for (;;) {
auto err = Err::none;
Size n = 0;
if (ws) {
uint8_t op = 0;
err = const_this->ws_recvmsg(
- fd, &op, (uint8_t *)buf, sizeof (buf), &n);
+ fd, &op, (uint8_t *)buf.get(), ndt_bufsize, &n);
if (err == Err::none && op != ws_opcode_binary) {
LIBNDT_EMIT_WARNING_EX(const_this,
"run_download: unexpected opcode: " << (unsigned int)op);
break;
}
} else {
- err = const_this->netx_recv(fd, buf, sizeof(buf), &n);
+ err = const_this->netx_recv(fd, buf.get(), ndt_bufsize, &n);
}
if (err != Err::none) {
if (err != Err::eof) {
@@ -1820,17 +1821,18 @@ bool Client::run_upload() noexcept {
]() noexcept {
// TODO(bassosimone): allocate on heap and keep safe using unique_ptr
// because with musl libc the stack is 80KB by default.
- char buf[131072];
+ constexpr size_t ndt_bufsize = 131072;
+ std::unique_ptr<char[]> buf(new char[ndt_bufsize]);
{
auto start = std::chrono::steady_clock::now();
- random_printable_fill(buf, sizeof(buf));
+ random_printable_fill(buf.get(), ndt_bufsize);
auto now = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed = now - start;
LIBNDT_EMIT_DEBUG_EX(const_this,
"run_upload: time to fill random buffer: " << elapsed.count());
}
std::string frame = const_this->ws_prepare_frame(
- ws_opcode_binary | ws_fin_flag, (uint8_t *)buf, sizeof (buf));
+ ws_opcode_binary | ws_fin_flag, (uint8_t *)buf.get(), ndt_bufsize);
for (;;) {
Size n = 0;
auto err = Err::none;
@@ -1840,7 +1842,7 @@ bool Client::run_upload() noexcept {
n = frame.size();
}
} else {
- err = const_this->netx_send(fd, buf, sizeof(buf), &n);
+ err = const_this->netx_send(fd, buf.get(), ndt_bufsize, &n);
}
if (err != Err::none) {
if (err != Err::broken_pipe) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment