util/iobuf: Add functions to safely and easly alloc/free iobufs

This commit is contained in:
icex2
2021-01-17 00:52:09 +01:00
parent 2871238427
commit c84ff4463f
2 changed files with 138 additions and 2 deletions
+78 -2
View File
@@ -1,7 +1,83 @@
#include "util/iobuf.h"
#include <string.h>
#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;
+60
View File
@@ -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)
*