diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2015-04-14 00:25:23 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2015-04-14 00:25:23 +0900 |
| commit | 692a674fe086b24fa462723688ced2722cfde3b9 (patch) | |
| tree | 9dcc8efc7e3ec92183ccbee611ce7b1934b071d0 /src/SocketRecvBuffer.h | |
| parent | 8ed8ac0fe8925ab8244e62fbf20de2472480a95f (diff) | |
SocketRecvBuffer: Eliminate memmove
Since we don't try to read into buffer if buffer is not empty, we
don't have to memmove things. This commit mostly rewrites
SocketRecvBuffer.
Diffstat (limited to 'src/SocketRecvBuffer.h')
| -rw-r--r-- | src/SocketRecvBuffer.h | 30 |
1 files changed, 13 insertions, 17 deletions
diff --git a/src/SocketRecvBuffer.h b/src/SocketRecvBuffer.h index 00d04bb7..ceee253e 100644 --- a/src/SocketRecvBuffer.h +++ b/src/SocketRecvBuffer.h @@ -38,6 +38,7 @@ #include "common.h" #include <memory> +#include <array> namespace aria2 { @@ -45,21 +46,17 @@ class SocketCore; class SocketRecvBuffer { public: - SocketRecvBuffer - (std::shared_ptr<SocketCore> socket, - size_t capacity = 16*1024); + SocketRecvBuffer(std::shared_ptr<SocketCore> socket); ~SocketRecvBuffer(); // Reads data from socket as much as capacity allows. Returns the // number of bytes read. ssize_t recv(); - // Shifts buffer by offset bytes. offset must satisfy offset <= - // getBufferLength(). - void shiftBuffer(size_t offset); // Truncates the contents of buffer to 0. - void clearBuffer() - { - bufLen_ = 0; - } + void truncateBuffer(); + // Drains first n bytes of data from buffer. It is an programmer's + // responsibility to ensure that n is smaller or equal to the + // buffered data. + void drain(size_t n); const std::shared_ptr<SocketCore>& getSocket() const { @@ -68,25 +65,24 @@ public: const unsigned char* getBuffer() const { - return buf_.get(); + return pos_; } size_t getBufferLength() const { - return bufLen_; + return last_ - pos_; } bool bufferEmpty() const { - return bufLen_ == 0; + return pos_ == last_; } - void pushBuffer(const unsigned char* data, size_t len); private: + std::array<unsigned char, 16384> buf_; std::shared_ptr<SocketCore> socket_; - size_t capacity_; - std::unique_ptr<unsigned char[]> buf_; - size_t bufLen_; + unsigned char *pos_; + unsigned char *last_; }; } // namespace aria2 |
