41 s->buf_start =
s->buf_static;
48 s->readcursor =
s->writecursor =
s->buf_start;
49 memset(
s->buf_start, 0,
s->capacity);
58 if (
s->buf_start !=
s->buf_static )
74 LWDEBUGF(2,
"Entered bytebuffer_makeroom with space need of %d", size_to_add);
75 size_t current_write_size = (
s->writecursor -
s->buf_start);
76 size_t capacity =
s->capacity;
77 size_t required_size = current_write_size + size_to_add;
79 LWDEBUGF(2,
"capacity = %d and required size = %d",capacity ,required_size);
80 while (capacity < required_size)
83 if ( capacity >
s->capacity )
85 size_t current_read_size = (
s->readcursor -
s->buf_start);
86 LWDEBUGF(4,
"We need to realloc more memory. New capacity is %d", capacity);
87 if (
s->buf_start ==
s->buf_static )
90 memcpy(
s->buf_start,
s->buf_static,
s->capacity);
96 s->capacity = capacity;
97 s->writecursor =
s->buf_start + current_write_size;
98 s->readcursor =
s->buf_start + current_read_size;
109 memcpy(v->
data,
s->buf_start, bufsz);
129 LWDEBUGF(2,
"Entered bytebuffer_append_byte with value %d", val);
131 *(
s->writecursor)=val;
142 LWDEBUG(2,
"bytebuffer_append_bytebuffer");
178 return (
size_t)(
s->writecursor -
s->buf_start);
186 bytebuffer_get_buffer_copy(
const bytebuffer_t *
s,
size_t *buffer_length)
190 memcpy(buf,
s->buf_start, bufsz);
192 *buffer_length = bufsz;
200 bytebuffer_create(
void)
202 LWDEBUG(2,
"Entered bytebuffer_create");
210 bytebuffer_create_with_size(
size_t size)
212 LWDEBUGF(2,
"Entered bytebuffer_create_with_size %d", size);
219 s->buf_start =
s->buf_static;
226 s->readcursor =
s->writecursor =
s->buf_start;
227 memset(
s->buf_start,0,
s->capacity);
228 LWDEBUGF(4,
"We create a buffer on %p of %d bytes",
s->buf_start,
s->capacity);
251 s->readcursor =
s->buf_start;
262 s->readcursor =
s->writecursor =
s->buf_start;
269 bytebuffer_append_bulk(
bytebuffer_t *
s,
void * start,
size_t size)
271 LWDEBUGF(2,
"bytebuffer_append_bulk with size %d",size);
273 memcpy(
s->writecursor, start, size);
274 s->writecursor += size;
282 bytebuffer_append_int(
bytebuffer_t *buf,
const int val,
int swap)
284 LWDEBUGF(2,
"Entered bytebuffer_append_int with value %d, swap = %d", val, swap);
287 char *iptr = (
char*)(&val);
299 LWDEBUG(4,
"Ok, let's do the swaping thing");
309 LWDEBUG(4,
"Ok, let's do the memcopying thing");
327 bytebuffer_append_double(
bytebuffer_t *buf,
const double val,
int swap)
329 LWDEBUGF(2,
"Entered bytebuffer_append_double with value %lf swap = %d", val, swap);
332 char *dptr = (
char*)(&val);
345 LWDEBUG(4,
"Ok, let's do the swapping thing");
355 LWDEBUG(4,
"Ok, let's do the memcopying thing");
360 LWDEBUG(4,
"Return from bytebuffer_append_double");
395 bytebuffer_merge(
bytebuffer_t **buff_array,
int nbuffers)
397 size_t total_size = 0, current_size, acc_size = 0;
399 for ( i = 0; i < nbuffers; i++ )
405 for ( i = 0; i < nbuffers; i++)
408 memcpy(
res->buf_start+acc_size, buff_array[i]->buf_start, current_size);
409 acc_size += current_size;
411 res->writecursor =
res->buf_start + total_size;
412 res->readcursor =
res->buf_start;
void bytebuffer_destroy_buffer(bytebuffer_t *s)
Free the bytebuffer_t and all memory managed within it.
void bytebuffer_append_bytebuffer(bytebuffer_t *write_to, bytebuffer_t *write_from)
Writes a uint8_t value to the buffer.
size_t bytebuffer_getlength(const bytebuffer_t *s)
Returns the length of the current buffer.
void bytebuffer_append_byte(bytebuffer_t *s, const uint8_t val)
Writes a uint8_t value to the buffer.
lwvarlena_t * bytebuffer_get_buffer_varlena(const bytebuffer_t *s)
Returns a copy of the internal buffer.
void bytebuffer_append_uvarint(bytebuffer_t *b, const uint64_t val)
Writes a unsigned varInt to the buffer.
void bytebuffer_append_varint(bytebuffer_t *b, const int64_t val)
Writes a signed varInt to the buffer.
void bytebuffer_init_with_size(bytebuffer_t *s, size_t size)
Allocate just the internal buffer of an existing bytebuffer_t struct.
static void bytebuffer_makeroom(bytebuffer_t *s, size_t size_to_add)
If necessary, expand the bytebuffer_t internal buffer to accomodate the specified additional size.
const uint8_t * bytebuffer_get_buffer(const bytebuffer_t *s, size_t *buffer_length)
Returns a read-only reference to the internal buffer.
#define BYTEBUFFER_STATICSIZE
#define BYTEBUFFER_STARTSIZE
void * lwrealloc(void *mem, size_t size)
#define LWSIZE_SET(varsize, len)
void * lwalloc(size_t size)
#define WKB_DOUBLE_SIZE
Well-Known Binary (WKB) Output Variant Types.
#define LWDEBUG(level, msg)
#define LWDEBUGF(level, msg,...)
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
size_t varint_s64_encode_buf(int64_t val, uint8_t *buf)
size_t varint_u64_encode_buf(uint64_t val, uint8_t *buf)
int64_t varint_s64_decode(const uint8_t *the_start, const uint8_t *the_end, size_t *size)
uint64_t varint_u64_decode(const uint8_t *the_start, const uint8_t *the_end, size_t *size)