Skip to content

Commit

Permalink
core: Replace std::unique_ptr<char[]> with Array<char> in `Networ…
Browse files Browse the repository at this point in the history
…kReader`. (y-scope#531)
  • Loading branch information
LinZhihao-723 authored Sep 9, 2024
1 parent decdc2d commit cb43459
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
10 changes: 6 additions & 4 deletions components/core/src/clp/NetworkReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ NetworkReader::NetworkReader(
m_buffer_pool_size{std::max(cMinBufferPoolSize, buffer_pool_size)},
m_buffer_size{std::max(cMinBufferSize, buffer_size)} {
for (size_t i = 0; i < m_buffer_pool_size; ++i) {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
m_buffer_pool.emplace_back(std::make_unique<char[]>(m_buffer_size));
m_buffer_pool.emplace_back(m_buffer_size);
}
m_downloader_thread = std::make_unique<DownloaderThread>(*this, offset, disable_caching);
m_downloader_thread->start();
Expand Down Expand Up @@ -248,7 +247,10 @@ auto NetworkReader::acquire_empty_buffer() -> void {
return;
}
}
m_curr_downloader_buf.emplace(m_buffer_pool.at(m_curr_downloader_buf_idx).get(), m_buffer_size);
m_curr_downloader_buf.emplace(
m_buffer_pool.at(m_curr_downloader_buf_idx).data(),
m_buffer_size
);
}

auto NetworkReader::release_empty_buffer() -> void {
Expand All @@ -264,7 +266,7 @@ auto NetworkReader::enqueue_filled_buffer() -> void {
}
std::unique_lock<std::mutex> const buffer_resource_lock{m_buffer_resource_mutex};
m_filled_buffer_queue.emplace(
m_buffer_pool.at(m_curr_downloader_buf_idx).get(),
m_buffer_pool.at(m_curr_downloader_buf_idx).data(),
m_buffer_size - m_curr_downloader_buf.value().size()
);

Expand Down
6 changes: 3 additions & 3 deletions components/core/src/clp/NetworkReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <curl/curl.h>

#include "Array.hpp"
#include "CurlDownloadHandler.hpp"
#include "CurlGlobalInstance.hpp"
#include "ErrorCode.hpp"
Expand Down Expand Up @@ -106,7 +107,7 @@ class NetworkReader : public ReaderInterface {
);

// Destructor
virtual ~NetworkReader();
~NetworkReader() override;

// Copy/Move Constructors
// These are disabled since this class' synchronization primitives are non-copyable and
Expand Down Expand Up @@ -330,8 +331,7 @@ class NetworkReader : public ReaderInterface {
size_t m_buffer_size{cDefaultBufferSize};
size_t m_curr_downloader_buf_idx{0};

// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
std::vector<std::unique_ptr<char[]>> m_buffer_pool;
std::vector<Array<char>> m_buffer_pool;
std::queue<BufferView> m_filled_buffer_queue;
std::optional<BufferView> m_curr_downloader_buf;
std::optional<BufferView> m_curr_reader_buf;
Expand Down
8 changes: 4 additions & 4 deletions components/core/tests/test-NetworkReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <Catch2/single_include/catch2/catch.hpp>
#include <curl/curl.h>

#include "../src/clp/Array.hpp"
#include "../src/clp/CurlDownloadHandler.hpp"
#include "../src/clp/CurlGlobalInstance.hpp"
#include "../src/clp/ErrorCode.hpp"
Expand Down Expand Up @@ -65,12 +66,11 @@ auto get_test_input_path_relative_to_tests_dir() -> std::filesystem::path {

auto get_content(clp::ReaderInterface& reader, size_t read_buf_size) -> std::vector<char> {
std::vector<char> buf;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
auto const read_buf{std::make_unique<char[]>(read_buf_size)};
clp::Array<char> read_buf{read_buf_size};
for (bool has_more_content{true}; has_more_content;) {
size_t num_bytes_read{};
has_more_content = reader.read(read_buf.get(), read_buf_size, num_bytes_read);
std::string_view const view{read_buf.get(), num_bytes_read};
has_more_content = reader.read(read_buf.data(), read_buf_size, num_bytes_read);
std::string_view const view{read_buf.data(), num_bytes_read};
buf.insert(buf.cend(), view.cbegin(), view.cend());
}
return buf;
Expand Down

0 comments on commit cb43459

Please sign in to comment.