Skip to content

Commit

Permalink
buffer: use malloc/free to replace fixed array
Browse files Browse the repository at this point in the history
Signed-off-by: Date Huang <[email protected]>
  • Loading branch information
tjjh89017 committed Oct 24, 2024
1 parent 6c955e0 commit 1b73fa9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
26 changes: 12 additions & 14 deletions buffer_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,34 @@ void watermark_callback(std::vector<std::weak_ptr<libtorrent::disk_observer>> co

buffer_pool::buffer_pool(libtorrent::io_context &ioc) :
m_ios(ioc),
m_size(0),
m_exceeded_max_size(false)
{
m_buffer = new char[MAX_BUFFER_POOL_SIZE];

// put into deque
for (int i = 0; i < MAX_BUFFER_POOL_SIZE; i += DEFAULT_BLOCK_SIZE) {
m_deque.push_back(m_buffer + i);
}
}

buffer_pool::~buffer_pool()
{
m_deque.clear();
delete m_buffer;
}

char *buffer_pool::allocate_buffer_impl(std::unique_lock<std::mutex> &l)
{
// no memory
if (m_deque.empty()) {
if (m_size >= BUFFER_COUNT) {
m_exceeded_max_size = true;
return nullptr;
}

// reach high watermak, but still has some buffer to use
if (BUFFER_COUNT - m_deque.size() > HIGH_WATERMARK) {
if (m_size > HIGH_WATERMARK) {
m_exceeded_max_size = true;
}

char *buf = m_deque.front();
m_deque.pop_front();
char *buf = (char*)malloc(DEFAULT_BLOCK_SIZE);
if (!buf) {
m_exceeded_max_size = true;
return nullptr;
}
m_size++;
return buf;
}

Expand Down Expand Up @@ -73,13 +70,14 @@ char *buffer_pool::allocate_buffer(bool &exceeded, std::shared_ptr<libtorrent::d
void buffer_pool::free_disk_buffer(char *buf)
{
std::unique_lock<std::mutex> l(m_pool_mutex);
m_deque.push_back(buf);
free(buf);
m_size--;
check_buffer_level(l);
}

void buffer_pool::check_buffer_level(std::unique_lock<std::mutex> &l)
{
if (!m_exceeded_max_size || BUFFER_COUNT - m_deque.size() > LOW_WATERMARK) {
if (!m_exceeded_max_size || m_size > LOW_WATERMARK) {
// still high usgae
return;
}
Expand Down
5 changes: 2 additions & 3 deletions buffer_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <boost/core/noncopyable.hpp>
#include <libtorrent/libtorrent.hpp>

// 16 MB
// 256 MB
#define MAX_BUFFER_POOL_SIZE (16ULL * 1024 * 1024)
// 16 KB
#define DEFAULT_BLOCK_SIZE (16 * 1024)
Expand Down Expand Up @@ -36,8 +36,7 @@ class buffer_pool : public libtorrent::buffer_allocator_interface, boost::noncop
private:
libtorrent::io_context &m_ios;
std::mutex m_pool_mutex;
char *m_buffer;
std::deque<char *> m_deque;
int m_size;
bool m_exceeded_max_size;
std::vector<std::weak_ptr<libtorrent::disk_observer>> m_observers;
};
Expand Down

0 comments on commit 1b73fa9

Please sign in to comment.