diff --git a/src/main/util/iobuf.c b/src/main/util/iobuf.c index b8bd76e..6bc89d2 100644 --- a/src/main/util/iobuf.c +++ b/src/main/util/iobuf.c @@ -1,7 +1,83 @@ -#include "util/iobuf.h" - #include +#include "util/iobuf.h" +#include "util/log.h" +#include "util/mem.h" + +void util_iobuf_alloc(struct util_iobuf* iobuf, size_t size) +{ + log_assert(iobuf); + + iobuf->bytes = util_xmalloc(size); + iobuf->nbytes = size; + iobuf->pos = 0; +} + +void util_iobuf_alloc2(struct util_iobuf** iobuf, size_t size) +{ + log_assert(iobuf); + + iobuf = util_xmalloc(sizeof(struct util_iobuf)); + + (*iobuf)->bytes = util_xmalloc(size); + (*iobuf)->nbytes = size; + (*iobuf)->pos = 0; +} + +void util_const_iobuf_alloc(struct util_const_iobuf* iobuf, size_t size) +{ + log_assert(iobuf); + + iobuf->bytes = util_xmalloc(size); + iobuf->nbytes = size; + iobuf->pos = 0; +} + +void util_const_iobuf_alloc2(struct util_const_iobuf** iobuf, size_t size) +{ + log_assert(iobuf); + + iobuf = util_xmalloc(sizeof(struct util_iobuf)); + + (*iobuf)->bytes = util_xmalloc(size); + (*iobuf)->nbytes = size; + (*iobuf)->pos = 0; +} + +void util_iobuf_free(struct util_iobuf* iobuf) +{ + log_assert(iobuf); + + util_xfree((void**) &iobuf->bytes); + iobuf->nbytes = 0; + iobuf->pos = 0; +} + +void util_iobuf_free2(struct util_iobuf** iobuf) +{ + log_assert(iobuf); + + util_xfree((void**) &(*iobuf)->bytes); + util_xfree((void**) &iobuf); +} + +void util_const_iobuf_free(struct util_const_iobuf* iobuf) +{ + log_assert(iobuf); + + util_xfree((void**) &iobuf->bytes); + iobuf->nbytes = 0; + iobuf->pos = 0; +} + +void util_const_iobuf_free2(struct util_const_iobuf** iobuf) +{ + log_assert(iobuf); + + util_xfree((void**) &(*iobuf)->bytes); + util_xfree((void**) &iobuf); +} + size_t util_iobuf_move(struct util_iobuf* dest, struct util_const_iobuf* src) { size_t dest_avail; diff --git a/src/main/util/iobuf.h b/src/main/util/iobuf.h index 4086482..b2a8526 100644 --- a/src/main/util/iobuf.h +++ b/src/main/util/iobuf.h @@ -22,6 +22,66 @@ struct util_const_iobuf { size_t pos; }; +/** + * Allocate and init a fresh iobuf struct + * + * @param iobuf Pointer to an already allocated struct, e.g. on the stack + * @param size Size of the buffer + */ +void util_iobuf_alloc(struct util_iobuf* iobuf, size_t size); + +/** + * Allocate and init a fresh iobuf struct + * + * @param iobuf Pointer to a pointer, e.g. the resulting struct will be allcoated on the heap + * @param size Size of the buffer + */ +void util_iobuf_alloc2(struct util_iobuf** iobuf, size_t size); + +/** + * Allocate and init a fresh const iobuf struct + * + * @param iobuf Pointer to an already allocated struct, e.g. on the stack + * @param size Size of the buffer + */ +void util_const_iobuf_alloc(struct util_const_iobuf* iobuf, size_t size); + +/** + * Allocate and init a fresh const iobuf struct + * + * @param iobuf Pointer to a pointer, e.g. the resulting struct will be allcoated on the heap + * @param size Size of the buffer + */ +void util_const_iobuf_alloc2(struct util_const_iobuf** iobuf, size_t size); + +/** + * Free an allocated iobuf struct + * + * @param iobuf Pointer to allocated iobuf struct, e.g. on stack + */ +void util_iobuf_free(struct util_iobuf* iobuf); + +/** + * Free an allocated iobuf struct + * + * @param iobuf Pointer to a pointer of an iobuf struct, e.g. iobuf on heap + */ +void util_iobuf_free2(struct util_iobuf** iobuf); + +/** + * Free an allocated const iobuf struct + * + * @param iobuf Pointer to allocated const iobuf struct, e.g. on stack + */ +void util_const_iobuf_free(struct util_const_iobuf* iobuf); + +/** + * Free an allocated const iobuf struct + * + * @param iobuf Pointer to a pointer of a const iobuf struct, e.g. iobuf on heap + */ +void util_const_iobuf_free2(struct util_const_iobuf** iobuf); + /** * Move the contents of one iobuffer to another one (data, size and pos) *