diff options
author | sfan5 <sfan5@live.de> | 2021-09-17 18:14:25 +0200 |
---|---|---|
committer | rubenwardy <rw@rubenwardy.com> | 2021-10-19 19:22:46 +0100 |
commit | 05b54a8d18051a3452c405f58bee5852f3d3a27b (patch) | |
tree | 961f7e79c3c64d4074e228ff0c415820eded32ea /src/util | |
parent | e5cfdd369ed96a7906a6ed6de7eb3878a10fc57c (diff) | |
download | minetest-05b54a8d18051a3452c405f58bee5852f3d3a27b.tar.gz minetest-05b54a8d18051a3452c405f58bee5852f3d3a27b.tar.bz2 minetest-05b54a8d18051a3452c405f58bee5852f3d3a27b.zip |
Shave off buffer copies in networking code (#11607)
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/container.h | 7 | ||||
-rw-r--r-- | src/util/pointer.h | 34 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/util/container.h b/src/util/container.h index 2ad2bbfc7..ea8c27bf8 100644 --- a/src/util/container.h +++ b/src/util/container.h @@ -143,6 +143,13 @@ public: m_signal.post(); } + void push_back(T &&t) + { + MutexAutoLock lock(m_mutex); + m_queue.push_back(std::move(t)); + m_signal.post(); + } + /* this version of pop_front returns a empty element of T on timeout. * Make sure default constructor of T creates a recognizable "empty" element */ diff --git a/src/util/pointer.h b/src/util/pointer.h index d29ec8739..7fc5de551 100644 --- a/src/util/pointer.h +++ b/src/util/pointer.h @@ -51,6 +51,19 @@ public: else data = NULL; } + Buffer(Buffer &&buffer) + { + m_size = buffer.m_size; + if(m_size != 0) + { + data = buffer.data; + buffer.data = nullptr; + buffer.m_size = 0; + } + else + data = nullptr; + } + // Copies whole buffer Buffer(const T *t, unsigned int size) { m_size = size; @@ -62,10 +75,12 @@ public: else data = NULL; } + ~Buffer() { drop(); } + Buffer& operator=(const Buffer &buffer) { if(this == &buffer) @@ -81,6 +96,23 @@ public: data = NULL; return *this; } + Buffer& operator=(Buffer &&buffer) + { + if(this == &buffer) + return *this; + drop(); + m_size = buffer.m_size; + if(m_size != 0) + { + data = buffer.data; + buffer.data = nullptr; + buffer.m_size = 0; + } + else + data = nullptr; + return *this; + } + T & operator[](unsigned int i) const { return data[i]; @@ -89,10 +121,12 @@ public: { return data; } + unsigned int getSize() const { return m_size; } + private: void drop() { |