Skip to content

Commit 80dd9be

Browse files
Johan Hedbergjhedberg
authored andcommitted
net: buf: Add net_buf_pull_mem() API
This is the same as net_buf_pull(), except that instead of returning the new buf->data it returns the old buf->data. This was recently discussed in github issue zephyrproject-rtos#12562. Signed-off-by: Johan Hedberg <[email protected]>
1 parent b2d13af commit 80dd9be

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

include/net/buf.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,19 @@ void net_buf_simple_push_u8(struct net_buf_simple *buf, u8_t val);
296296
*/
297297
void *net_buf_simple_pull(struct net_buf_simple *buf, size_t len);
298298

299+
/**
300+
* @brief Remove data from the beginning of the buffer.
301+
*
302+
* Removes data from the beginning of the buffer by modifying the data
303+
* pointer and buffer length.
304+
*
305+
* @param buf Buffer to update.
306+
* @param len Number of bytes to remove.
307+
*
308+
* @return Pointer to the old location of the buffer data.
309+
*/
310+
void *net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len);
311+
299312
/**
300313
* @brief Remove a 8-bit value from the beginning of the buffer
301314
*
@@ -1147,6 +1160,20 @@ static inline void *net_buf_user_data(const struct net_buf *buf)
11471160
*/
11481161
#define net_buf_pull(buf, len) net_buf_simple_pull(&(buf)->b, len)
11491162

1163+
/**
1164+
* @def net_buf_pull_mem
1165+
* @brief Remove data from the beginning of the buffer.
1166+
*
1167+
* Removes data from the beginning of the buffer by modifying the data
1168+
* pointer and buffer length.
1169+
*
1170+
* @param buf Buffer to update.
1171+
* @param len Number of bytes to remove.
1172+
*
1173+
* @return Pointer to the old beginning of the buffer data.
1174+
*/
1175+
#define net_buf_pull_mem(buf, len) net_buf_simple_pull_mem(&(buf)->b, len)
1176+
11501177
/**
11511178
* @def net_buf_pull_u8
11521179
* @brief Remove a 8-bit value from the beginning of the buffer

subsys/net/buf.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,20 @@ void *net_buf_simple_pull(struct net_buf_simple *buf, size_t len)
868868
return buf->data += len;
869869
}
870870

871+
void *net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len)
872+
{
873+
void *data = buf->data;
874+
875+
NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
876+
877+
NET_BUF_SIMPLE_ASSERT(buf->len >= len);
878+
879+
buf->len -= len;
880+
buf->data += len;
881+
882+
return data;
883+
}
884+
871885
u8_t net_buf_simple_pull_u8(struct net_buf_simple *buf)
872886
{
873887
u8_t val;

0 commit comments

Comments
 (0)