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(buf,
s->buf_start, bufsz);
111 *buffer_length = bufsz;
130 LWDEBUGF(2,
"Entered bytebuffer_append_byte with value %d", val);
132 *(
s->writecursor)=val;
143 LWDEBUG(2,
"bytebuffer_append_bytebuffer");
179 return (
size_t)(
s->writecursor -
s->buf_start);
189 bytebuffer_create(
void)
191 LWDEBUG(2,
"Entered bytebuffer_create");
199 bytebuffer_create_with_size(
size_t size)
201 LWDEBUGF(2,
"Entered bytebuffer_create_with_size %d", size);
208 s->buf_start =
s->buf_static;
215 s->readcursor =
s->writecursor =
s->buf_start;
216 memset(
s->buf_start,0,
s->capacity);
217 LWDEBUGF(4,
"We create a buffer on %p of %d bytes",
s->buf_start,
s->capacity);
240 s->readcursor =
s->buf_start;
251 s->readcursor =
s->writecursor =
s->buf_start;
258 bytebuffer_append_bulk(
bytebuffer_t *
s,
void * start,
size_t size)
260 LWDEBUGF(2,
"bytebuffer_append_bulk with size %d",size);
262 memcpy(
s->writecursor, start, size);
263 s->writecursor += size;
271 bytebuffer_append_int(
bytebuffer_t *buf,
const int val,
int swap)
273 LWDEBUGF(2,
"Entered bytebuffer_append_int with value %d, swap = %d", val, swap);
276 char *iptr = (
char*)(&val);
288 LWDEBUG(4,
"Ok, let's do the swaping thing");
298 LWDEBUG(4,
"Ok, let's do the memcopying thing");
316 bytebuffer_append_double(
bytebuffer_t *buf,
const double val,
int swap)
318 LWDEBUGF(2,
"Entered bytebuffer_append_double with value %lf swap = %d", val, swap);
321 char *dptr = (
char*)(&val);
334 LWDEBUG(4,
"Ok, let's do the swapping thing");
344 LWDEBUG(4,
"Ok, let's do the memcopying thing");
349 LWDEBUG(4,
"Return from bytebuffer_append_double");
384 bytebuffer_merge(
bytebuffer_t **buff_array,
int nbuffers)
386 size_t total_size = 0, current_size, acc_size = 0;
388 for ( i = 0; i < nbuffers; i++ )
394 for ( i = 0; i < nbuffers; i++)
397 memcpy(
res->buf_start+acc_size, buff_array[i]->buf_start, current_size);
398 acc_size += current_size;
400 res->writecursor =
res->buf_start + total_size;
401 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.
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.
uint8_t * bytebuffer_get_buffer_copy(const bytebuffer_t *s, size_t *buffer_length)
Returns a copy of the internal 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)
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)