From ef477df91db427ddf332e7b170e18040e71ee23f Mon Sep 17 00:00:00 2001 From: wangqiang Date: Sat, 4 Apr 2026 00:35:50 +0800 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=20std::vecto?= =?UTF-8?q?r=20=E7=AE=A1=E7=90=86=E5=86=85=E5=AD=98=E5=B9=B6=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 问题 1.2: equals() 方法添加 const 限定符 - 问题 2.1: 使用 std::vector 替代 calloc/free 进行内存管理 - 问题 2.3: compact() 方法使用 memmove 替代循环逐字节复制 主要改动: - 移除 p_buffer_ 裸指针,改用 buffer_ (std::vector) - 析构函数改为 default - 移动语义使用 std::move 管理 vector - checkSize 使用 vector::resize 替代 realloc - getBytes 使用 buffer_.data() 访问底层数据 --- ByteBuffer.h | 80 ++++++++++++++++++---------------------------------- 1 file changed, 27 insertions(+), 53 deletions(-) diff --git a/ByteBuffer.h b/ByteBuffer.h index c16bac2..d314fbc 100644 --- a/ByteBuffer.h +++ b/ByteBuffer.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -29,9 +30,8 @@ class ByteBuffer position_(other.position_), capacity_(other.capacity_), name_(std::move(other.name_)), - p_buffer_(other.p_buffer_) + buffer_(std::move(other.buffer_)) { - other.p_buffer_ = nullptr; other.mark_ = MARK_UNSET; other.limit_ = 0; other.position_ = 0; @@ -42,17 +42,12 @@ class ByteBuffer { if (this != &other) { - if (p_buffer_) - { - free(p_buffer_); - } mark_ = other.mark_; limit_ = other.limit_; position_ = other.position_; capacity_ = other.capacity_; name_ = std::move(other.name_); - p_buffer_ = other.p_buffer_; - other.p_buffer_ = nullptr; + buffer_ = std::move(other.buffer_); other.mark_ = MARK_UNSET; other.limit_ = 0; other.position_ = 0; @@ -66,9 +61,9 @@ class ByteBuffer limit_(capacity), position_(0), capacity_(capacity), - name_(name) + name_(name), + buffer_(capacity, 0) { - p_buffer_ = static_cast(calloc(capacity_, sizeof(uint8_t))); } ByteBuffer(uint8_t* arr, uint32_t length, const char* name = "") @@ -76,22 +71,14 @@ class ByteBuffer limit_(length), position_(0), capacity_(length), - name_(name) + name_(name), + buffer_(length, 0) { - p_buffer_ = static_cast(calloc(capacity_, sizeof(uint8_t))); - putBytes(arr, capacity_); clear(); } - ~ByteBuffer() - { - if (p_buffer_) - { - free(p_buffer_); - p_buffer_ = nullptr; - } - } + ~ByteBuffer() = default; // Write Methods ByteBuffer& put(ByteBuffer* bb) @@ -198,18 +185,18 @@ class ByteBuffer } void getBytes(uint8_t* buf, uint32_t len) { - if (!p_buffer_ || position_ + len > limit_) + if (buffer_.empty() || position_ + len > limit_) return; - memcpy(buf, p_buffer_ + position_, len); + memcpy(buf, buffer_.data() + position_, len); position_ += len; } void getBytes(uint32_t index, uint8_t* buf, uint32_t len) const { - if (!p_buffer_ || index + len > limit_) + if (buffer_.empty() || index + len > limit_) return; - memcpy(buf, p_buffer_ + index, len); + memcpy(buf, buffer_.data() + index, len); } char getChar() { @@ -260,7 +247,7 @@ class ByteBuffer return read(index); } - bool equals(ByteBuffer* other) + bool equals(const ByteBuffer* other) const { uint32_t len = limit(); if (len != other->limit()) @@ -330,20 +317,16 @@ class ByteBuffer ByteBuffer& compact() { - do + if (position_ >= limit_) + { + position_ = 0; + } + else { - if (position_ >= limit_) - { - position_ = 0; - break; - } - - for (uint32_t i = 0; i < limit_ - position_; i++) - { - p_buffer_[i] = p_buffer_[position_ + i]; - } - position_ = limit_ - position_; - } while (0); + uint32_t remaining = limit_ - position_; + memmove(buffer_.data(), &buffer_[position_], remaining); + position_ = remaining; + } limit_ = capacity_; return *this; @@ -401,11 +384,11 @@ class ByteBuffer template T read(uint32_t index) const { - if (!p_buffer_ || index + sizeof(T) > limit_) + if (index + sizeof(T) > limit_) return 0; T data; - memcpy(&data, &p_buffer_[index], sizeof(T)); + memcpy(&data, &buffer_[index], sizeof(T)); return data; } @@ -421,13 +404,10 @@ class ByteBuffer template void append(T data) { - if (!p_buffer_) - return; - uint32_t s = sizeof(data); checkSize(s); - memcpy(&p_buffer_[position_], reinterpret_cast(&data), s); + memcpy(&buffer_[position_], reinterpret_cast(&data), s); position_ += s; } @@ -454,20 +434,14 @@ class ByteBuffer uint32_t newSize = capacity_ + (increase + BUFFER_SIZE_INCREASE - 1) / BUFFER_SIZE_INCREASE * BUFFER_SIZE_INCREASE; - uint8_t* pBuf = static_cast(realloc(p_buffer_, newSize)); - if (!pBuf) - { - throw std::bad_alloc(); - } - - p_buffer_ = pBuf; + buffer_.resize(newSize, 0); capacity_ = newSize; } private: const uint32_t BUFFER_SIZE_INCREASE = 2048; std::string name_; - uint8_t* p_buffer_; + std::vector buffer_; uint32_t mark_; uint32_t limit_; uint32_t position_; From f3eca608667e75ad88309ee2e4e2c00018552ca3 Mon Sep 17 00:00:00 2001 From: wangqiang Date: Sat, 4 Apr 2026 00:39:51 +0800 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=20std::copy?= =?UTF-8?q?=20=E6=9B=BF=E4=BB=A3=20memcpy/memmove?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ByteBuffer.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ByteBuffer.h b/ByteBuffer.h index d314fbc..46c26dc 100644 --- a/ByteBuffer.h +++ b/ByteBuffer.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -188,7 +189,7 @@ class ByteBuffer if (buffer_.empty() || position_ + len > limit_) return; - memcpy(buf, buffer_.data() + position_, len); + std::copy(buffer_.begin() + position_, buffer_.begin() + position_ + len, buf); position_ += len; } void getBytes(uint32_t index, uint8_t* buf, uint32_t len) const @@ -196,7 +197,7 @@ class ByteBuffer if (buffer_.empty() || index + len > limit_) return; - memcpy(buf, buffer_.data() + index, len); + std::copy(buffer_.begin() + index, buffer_.begin() + index + len, buf); } char getChar() { @@ -324,7 +325,7 @@ class ByteBuffer else { uint32_t remaining = limit_ - position_; - memmove(buffer_.data(), &buffer_[position_], remaining); + std::copy(buffer_.begin() + position_, buffer_.begin() + limit_, buffer_.begin()); position_ = remaining; }