Added kore_buf no alloc functions.

kore_buf_create and kore_buf_free do use kore_malloc. But sometimes you
may embed a kore_buf inside a structure and would like a single way to
initialize / destroy it.
This commit is contained in:
Raphaël Monrouzeau 2016-07-05 16:48:58 +02:00
parent a07cf87b15
commit db3cf28d22
2 changed files with 20 additions and 2 deletions

View File

@ -621,9 +621,11 @@ void net_send_stream(struct connection *, void *,
void kore_buf_free(struct kore_buf *);
struct kore_buf *kore_buf_create(size_t);
void kore_buf_init(struct kore_buf *, size_t);
void kore_buf_append(struct kore_buf *, const void *, size_t);
u_int8_t *kore_buf_release(struct kore_buf *, size_t *);
void kore_buf_reset(struct kore_buf *);
void kore_buf_destroy(struct kore_buf *);
char *kore_buf_stringify(struct kore_buf *, size_t *);
void kore_buf_appendf(struct kore_buf *, const char *, ...);

View File

@ -27,11 +27,17 @@ kore_buf_create(size_t initial)
struct kore_buf *buf;
buf = kore_malloc(sizeof(*buf));
kore_buf_init(buf, initial);
return (buf);
}
void
kore_buf_init(struct kore_buf *buf, size_t initial)
{
buf->data = kore_malloc(initial);
buf->length = initial;
buf->offset = 0;
return (buf);
}
void
@ -126,6 +132,16 @@ kore_buf_free(struct kore_buf *buf)
kore_mem_free(buf);
}
void
kore_buf_destroy(struct kore_buf *buf)
{
if (buf->data)
kore_mem_free(buf->data);
buf->data = NULL;
buf->offset = 0;
buf->length = 0;
}
void
kore_buf_replace_string(struct kore_buf *b, char *src, void *dst, size_t len)
{