36 LWDEBUG(2,
"Entered bytebuffer_create");
46 LWDEBUGF(2,
"Entered bytebuffer_create_with_size %d", size);
53 s->buf_start =
s->buf_static;
60 s->readcursor =
s->writecursor =
s->buf_start;
61 memset(
s->buf_start,0,
s->capacity);
62 LWDEBUGF(4,
"We create a buffer on %p of %d bytes",
s->buf_start,
s->capacity);
76 s->buf_start =
s->buf_static;
83 s->readcursor =
s->writecursor =
s->buf_start;
84 memset(
s->buf_start, 0,
s->capacity);
106 if (
s->buf_start !=
s->buf_static )
121 s->readcursor =
s->buf_start;
132 s->readcursor =
s->writecursor =
s->buf_start;
142 LWDEBUGF(2,
"Entered bytebuffer_makeroom with space need of %d", size_to_add);
143 size_t current_write_size = (
s->writecursor -
s->buf_start);
144 size_t capacity =
s->capacity;
145 size_t required_size = current_write_size + size_to_add;
147 LWDEBUGF(2,
"capacity = %d and required size = %d",capacity ,required_size);
148 while (capacity < required_size)
151 if ( capacity >
s->capacity )
153 size_t current_read_size = (
s->readcursor -
s->buf_start);
154 LWDEBUGF(4,
"We need to realloc more memory. New capacity is %d", capacity);
155 if (
s->buf_start ==
s->buf_static )
158 memcpy(
s->buf_start,
s->buf_static,
s->capacity);
164 s->capacity = capacity;
165 s->writecursor =
s->buf_start + current_write_size;
166 s->readcursor =
s->buf_start + current_read_size;
177 memcpy(buf,
s->buf_start, bufsz);
179 *buffer_length = bufsz;
199 LWDEBUGF(2,
"Entered bytebuffer_append_byte with value %d", val);
201 *(
s->writecursor)=val;
213 LWDEBUGF(2,
"bytebuffer_append_bulk with size %d",size);
215 memcpy(
s->writecursor, start, size);
216 s->writecursor += size;
226 LWDEBUG(2,
"bytebuffer_append_bytebuffer");
264 LWDEBUGF(2,
"Entered bytebuffer_append_int with value %d, swap = %d", val, swap);
267 char *iptr = (
char*)(&val);
279 LWDEBUG(4,
"Ok, let's do the swaping thing");
289 LWDEBUG(4,
"Ok, let's do the memcopying thing");
309 LWDEBUGF(2,
"Entered bytebuffer_append_double with value %lf swap = %d", val, swap);
312 char *dptr = (
char*)(&val);
325 LWDEBUG(4,
"Ok, let's do the swapping thing");
335 LWDEBUG(4,
"Ok, let's do the memcopying thing");
340 LWDEBUG(4,
"Return from bytebuffer_append_double");
375 return (
size_t) (
s->writecursor -
s->buf_start);
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.
uint64_t bytebuffer_read_uvarint(bytebuffer_t *b)
Reads a unsigned varInt from the buffer.
void bytebuffer_append_double(bytebuffer_t *buf, const double val, int swap)
Writes a float64 to the buffer.
void bytebuffer_append_bytebuffer(bytebuffer_t *write_to, bytebuffer_t *write_from)
Writes a uint8_t value to the buffer.
bytebuffer_t * bytebuffer_create_with_size(size_t size)
Allocate a new bytebuffer_t.
bytebuffer_t * bytebuffer_merge(bytebuffer_t **buff_array, int nbuffers)
Returns a new bytebuffer were both ingoing bytebuffers is merged.
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_destroy(bytebuffer_t *s)
Free the bytebuffer_t and all memory managed within it.
int64_t bytebuffer_read_varint(bytebuffer_t *b)
Reads a signed varInt from the buffer.
void bytebuffer_append_varint(bytebuffer_t *b, const int64_t val)
Writes a signed varInt to the buffer.
void bytebuffer_reset_reading(bytebuffer_t *s)
Set the read cursor to the beginning.
void bytebuffer_append_int(bytebuffer_t *buf, const int val, int swap)
void bytebuffer_clear(bytebuffer_t *s)
Reset the bytebuffer_t.
void bytebuffer_append_bulk(bytebuffer_t *s, void *start, size_t size)
Writes a uint8_t value 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.
bytebuffer_t * bytebuffer_create(void)
Allocate a new bytebuffer_t.
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)