You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// buffer which should be faster than std::vector<uint8_t> when resizing a lot because it does not do byte initialization when resizingclassuint8_fast_buffer
{
public:uint8_fast_buffer(constsize_t initial_size)
{
// .. i don't really like the idea of buf being nullptr, this avoids that issue.this->internal_reserve(std::max(initial_size,(decltype(initial_size))1));
this->buf_size=initial_size;
}
~uint8_fast_buffer() noexcept
{
free(this->buf);
}
size_tsize(void) noexcept
{
returnthis->buf_size;
}
voidreserve(constsize_t reserve_size)
{
if(reserve_size > this->buf_cap)
{
this->internal_reserve(reserve_size);
}
}
// this function is supposed to be very fast when newlen is smaller than the biggest it has ever been before.voidresize(constsize_t newlen)
{
if(__builtin_expect(newlen > this->buf_cap,0))
{
this->internal_reserve(newlen);
}
this->buf_size=newlen;
}
voidappend(constuint8_t *data, constsize_t len)
{
constsize_t pos=this->size();
constsize_t new_pos=this->size()+len;
this->resize(new_pos);
memcpy(&this->buf[pos],data,len);
}
voidreset(void) noexcept
{
this->buf_size=0;
}
boolempty(void) noexcept
{
return (this->buf_size==0);
}
uint8_t* data(void) noexcept
{
returnthis->buf;
}
uint8_t& at(constsize_t pos)
{
if(__builtin_expect(pos >= this->size(),0))
{
throwstd::out_of_range(std::to_string(pos)+std::string(" >= ")+std::to_string(this->size()));
}
returnthis->buf[pos];
}
uint8_t& operator[](constsize_t pos) noexcept
{
returnthis->buf[pos];
}
private:voidinternal_reserve(constsize_t reserve_size)
{
uint8_t *newbuf=(uint8_t*)realloc(this->buf,reserve_size);
if(__builtin_expect(newbuf == nullptr,0))
{
throwstd::bad_alloc();
}
this->buf_cap=reserve_size;
this->buf=newbuf;
}
size_t buf_size=0;
size_t buf_cap=0;
uint8_t *buf=nullptr;
};
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: