Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uint8_fast_buffer #10

Open
divinity76 opened this issue Jul 6, 2019 · 0 comments
Open

uint8_fast_buffer #10

divinity76 opened this issue Jul 6, 2019 · 0 comments
Labels
enhancement New feature or request

Comments

@divinity76
Copy link
Owner

divinity76 commented Jul 6, 2019

// buffer which should be faster than std::vector<uint8_t> when resizing a lot because it does not do byte initialization when resizing
class uint8_fast_buffer
{
public:
    uint8_fast_buffer(const size_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_t size(void) noexcept
    {
        return this->buf_size;
    }
    void reserve(const size_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.
    void resize(const size_t newlen)
    {
        if(__builtin_expect(newlen > this->buf_cap,0))
        {
            this->internal_reserve(newlen);
        }
        this->buf_size=newlen;
    }
    void append(const uint8_t *data, const size_t len)
    {
        const size_t pos=this->size();
        const size_t new_pos=this->size()+len;
        this->resize(new_pos);
        memcpy(&this->buf[pos],data,len);
    }
    void reset(void) noexcept
    {
        this->buf_size=0;
    }
    bool empty(void) noexcept
    {
        return (this->buf_size==0);
    }
    uint8_t* data(void) noexcept
    {
        return this->buf;
    }
    uint8_t& at(const size_t pos)
    {
        if(__builtin_expect(pos >= this->size(),0))
        {
            throw std::out_of_range(std::to_string(pos)+std::string(" >= ")+std::to_string(this->size()));
        }
        return this->buf[pos];
    }
    uint8_t& operator[](const size_t pos) noexcept
    {
        return this->buf[pos];
    }
private:
    void internal_reserve(const size_t reserve_size)
    {
        uint8_t *newbuf=(uint8_t*)realloc(this->buf,reserve_size);
        if(__builtin_expect(newbuf == nullptr,0))
        {
            throw std::bad_alloc();
        }
        this->buf_cap=reserve_size;
        this->buf=newbuf;
    }
    size_t buf_size=0;
    size_t buf_cap=0;
    uint8_t *buf=nullptr;
};
@divinity76 divinity76 added the enhancement New feature or request label Jul 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant