Skip to content

Commit

Permalink
mbuf: docs and setters/getters (#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredh authored Jan 27, 2023
1 parent 9543617 commit b6792f5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
40 changes: 39 additions & 1 deletion include/re_mbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@
#define MBUF_CHECK_END(mb)
#endif

/** Defines a memory buffer */
/**
* Defines a memory buffer.
*
* This is a dynamic and linear buffer for storing raw bytes.
* It is designed for network protocols, and supports automatic
* resizing of the buffer.
*
* - Writing to the buffer
* - Reading from the buffer
* - Automatic growing of buffer size
* - Print function for formatting printing
*/
struct mbuf {
uint8_t *buf; /**< Buffer memory */
size_t size; /**< Size of buffer */
Expand Down Expand Up @@ -67,6 +78,7 @@ int mbuf_printf(struct mbuf *mb, const char *fmt, ...);
int mbuf_write_pl_skip(struct mbuf *mb, const struct pl *pl,
const struct pl *skip);
int mbuf_fill(struct mbuf *mb, uint8_t c, size_t n);
void mbuf_set_posend(struct mbuf *mb, size_t pos, size_t end);
int mbuf_debug(struct re_printf *pf, const struct mbuf *mb);


Expand Down Expand Up @@ -168,3 +180,29 @@ static inline void mbuf_skip_to_end(struct mbuf *mb)
{
mb->pos = mb->end;
}


/**
* Get the current MBUF position
*
* @param mb Memory buffer
*
* @return Current position
*/
static inline size_t mbuf_pos(const struct mbuf *mb)
{
return mb ? mb->pos : 0;
}


/**
* Get the current MBUF end position
*
* @param mb Memory buffer
*
* @return Current end position
*/
static inline size_t mbuf_end(const struct mbuf *mb)
{
return mb ? mb->end : 0;
}
33 changes: 32 additions & 1 deletion src/mbuf/mbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ int mbuf_read_mem(struct mbuf *mb, uint8_t *buf, size_t size)
return EINVAL;

if (size > mbuf_get_left(mb)) {
DEBUG_WARNING("tried to read beyond mbuf end (%u > %u)\n",
DEBUG_WARNING("tried to read beyond mbuf end (%zu > %zu)\n",
size, mbuf_get_left(mb));
return EOVERFLOW;
}
Expand Down Expand Up @@ -618,6 +618,37 @@ int mbuf_fill(struct mbuf *mb, uint8_t c, size_t n)
}


/**
* Set absolute position and end position
*
* @param mb Memory buffer
* @param pos Position
* @param end End position
*/
void mbuf_set_posend(struct mbuf *mb, size_t pos, size_t end)
{
if (!mb)
return;

if (pos > end) {
DEBUG_WARNING("set_posend: pos %zu > end %zu\n",
pos, end);
return;
}
if (end > mb->size) {
DEBUG_WARNING("set_posend: end %zu > size %zu\n",
end, mb->size);
return;
}

mb->pos = pos;
mb->end = end;

MBUF_CHECK_POS(mb);
MBUF_CHECK_END(mb);
}


/**
* Debug the memory buffer
*
Expand Down

0 comments on commit b6792f5

Please sign in to comment.