2021-03-29 20:45:21 +00:00
|
|
|
/*
|
|
|
|
* BIRD -- The BGP Monitoring Protocol (BMP)
|
|
|
|
*
|
|
|
|
* (c) 2020 Akamai Technologies, Inc. (Pawel Maslanka, pmaslank@akamai.com)
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "proto/bmp/buffer.h"
|
|
|
|
|
|
|
|
buffer
|
|
|
|
bmp_buffer_alloc(pool *ppool, const size_t n)
|
|
|
|
{
|
|
|
|
buffer buf;
|
|
|
|
buf.start = mb_alloc(ppool, n);
|
|
|
|
buf.pos = buf.start;
|
|
|
|
buf.end = buf.start + n;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bmp_buffer_free(buffer *buf)
|
|
|
|
{
|
|
|
|
mb_free(buf->start);
|
|
|
|
buf->start = buf->pos = buf->end = NULL;
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:32:53 +00:00
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2021-03-29 20:45:21 +00:00
|
|
|
static void
|
|
|
|
bmp_buffer_grow(buffer *buf, const size_t n)
|
|
|
|
{
|
2023-05-31 16:32:53 +00:00
|
|
|
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);
|
2021-03-29 20:45:21 +00:00
|
|
|
buf->pos = buf->start + pos;
|
2023-05-31 16:32:53 +00:00
|
|
|
buf->end = buf->start + size;
|
2021-03-29 20:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2023-05-30 13:52:01 +00:00
|
|
|
}
|