mirror of
https://github.com/pumpitupdev/pumptools.git
synced 2026-09-28 17:58:01 +03:00
util/iobuf: Add functions to safely and easly alloc/free iobufs
This commit is contained in:
+78
-2
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user