diff options
Diffstat (limited to 'proto/bmp/buffer.c')
-rw-r--r-- | proto/bmp/buffer.c | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/proto/bmp/buffer.c b/proto/bmp/buffer.c index f471e08a..be9dd698 100644 --- a/proto/bmp/buffer.c +++ b/proto/bmp/buffer.c @@ -15,7 +15,6 @@ bmp_buffer_alloc(pool *ppool, const size_t n) buf.start = mb_alloc(ppool, n); buf.pos = buf.start; buf.end = buf.start + n; - return buf; } @@ -26,33 +25,41 @@ bmp_buffer_free(buffer *buf) buf->start = buf->pos = buf->end = NULL; } +/** + * @brief bmp_buffer_grow + * @param buf - buffer to grow + * @param n - required amount of available space + * Resize buffer in a way that there is at least @n bytes of available space. + */ static void bmp_buffer_grow(buffer *buf, const size_t n) { - const size_t pos = bmp_buffer_pos(buf); - buf->start = mb_realloc(buf->start, n); + size_t pos = bmp_buffer_pos(buf); + size_t size = bmp_buffer_size(buf); + size_t req = pos + n; + + while (size < req) + size = size * 3 / 2; + + buf->start = mb_realloc(buf->start, size); buf->pos = buf->start + pos; - buf->end = buf->start + n; + buf->end = buf->start + size; } void bmp_buffer_need(buffer *buf, const size_t n) { if (bmp_buffer_avail(buf) < n) - { bmp_buffer_grow(buf, n); - } } void bmp_put_data(buffer *buf, const void *src, const size_t n) { if (!n) - { return; - } bmp_buffer_need(buf, n); memcpy(buf->pos, src, n); buf->pos += n; -}
\ No newline at end of file +} |