Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@

#include <string>
#include <iostream>
#include <stdexcept>

// Default size of the buffer
constexpr uint32_t DEFAULT_BUFFER_SIZE = 2048;

// Mark value indicating no mark is set
constexpr uint32_t MARK_UNSET = UINT32_MAX;

class ByteBuffer
{
public:
Expand All @@ -29,7 +33,7 @@ class ByteBuffer
p_buffer_(other.p_buffer_)
{
other.p_buffer_ = nullptr;
other.mark_ = -1;
other.mark_ = MARK_UNSET;
other.limit_ = 0;
other.position_ = 0;
other.capacity_ = 0;
Expand All @@ -50,7 +54,7 @@ class ByteBuffer
name_ = std::move(other.name_);
p_buffer_ = other.p_buffer_;
other.p_buffer_ = nullptr;
other.mark_ = -1;
other.mark_ = MARK_UNSET;
other.limit_ = 0;
other.position_ = 0;
other.capacity_ = 0;
Expand All @@ -59,7 +63,7 @@ class ByteBuffer
}

ByteBuffer(uint32_t capacity = DEFAULT_BUFFER_SIZE, const char* name = "")
: mark_(-1),
: mark_(MARK_UNSET),
limit_(capacity),
position_(0),
capacity_(capacity),
Expand All @@ -69,7 +73,7 @@ class ByteBuffer
}

ByteBuffer(uint8_t* arr, uint32_t length, const char* name = "")
: mark_(-1),
: mark_(MARK_UNSET),
limit_(length),
position_(0),
capacity_(length),
Expand Down Expand Up @@ -287,15 +291,15 @@ class ByteBuffer
ByteBuffer& clear()
{
position_ = 0;
mark_ = -1;
mark_ = MARK_UNSET;
limit_ = capacity_;
return *this;
}
ByteBuffer& flip()
{
limit_ = position_;
position_ = 0;
mark_ = -1;
mark_ = MARK_UNSET;
return *this;
}
ByteBuffer& mark()
Expand All @@ -306,20 +310,20 @@ class ByteBuffer

ByteBuffer& discardMark()
{
mark_ = -1;
mark_ = MARK_UNSET;
return *this;
}
ByteBuffer& reset()
{
if (mark_ >= 0)
if (mark_ != MARK_UNSET)
position_ = mark_;

return *this;
}

ByteBuffer& rewind()
{
mark_ = -1;
mark_ = MARK_UNSET;
position_ = 0;

return *this;
Expand Down Expand Up @@ -374,7 +378,7 @@ class ByteBuffer
position_ = newLimit;

if (mark_ > newLimit)
mark_ = -1;
mark_ = MARK_UNSET;

return *this;
}
Expand Down Expand Up @@ -454,8 +458,7 @@ class ByteBuffer
uint8_t* pBuf = static_cast<uint8_t*>(realloc(p_buffer_, newSize));
if (!pBuf)
{
std::cout << "relloc failed!" << std::endl;
exit(1);
throw std::bad_alloc();
}

p_buffer_ = pBuf;
Expand All @@ -466,7 +469,7 @@ class ByteBuffer
const uint32_t BUFFER_SIZE_INCREASE = 2048;
std::string name_;
uint8_t* p_buffer_;
int32_t mark_;
uint32_t mark_;
uint32_t limit_;
uint32_t position_;
uint32_t capacity_;
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,11 @@ ctest --output-on-failure

1. **拷贝控制**:ByteBuffer 禁用了拷贝构造函数和拷贝赋值运算符,防止浅拷贝导致的双重释放问题。如需复制,请使用 `duplicate()` 方法或移动语义。

2. **内存对齐**:对于多字节类型的读写,某些架构可能要求内存对齐。在跨平台使用时需注意。
2. **内存对齐**:对于多字节类型的读写,某些架构(如 ARM)可能要求内存对齐。未对齐的访问可能导致性能下降或崩溃。建议在处理大量数据时确保数据对齐。

3. **字节序**:本实现使用主机字节序(host byte order)。如需网络传输,请自行处理字节序转换。
3. **字节序**:本实现使用主机字节序(host byte order)。如需网络传输,请自行处理字节序转换(如使用 `htons`/`ntohs` 等函数)。

4. **异常处理**:当内存分配失败时,会抛出 `std::bad_alloc` 异常。请确保在调用时进行适当的异常处理。

## 参考链接

Expand Down
Loading