Make some pbuf functions take const pbuf pointers

This commit is contained in:
goldsimon 2016-10-11 09:23:45 +02:00
parent c9cfbe27c1
commit 697be5c2c3
2 changed files with 38 additions and 31 deletions

View File

@ -782,7 +782,7 @@ pbuf_free(struct pbuf *p)
* @return the number of pbufs in a chain * @return the number of pbufs in a chain
*/ */
u16_t u16_t
pbuf_clen(struct pbuf *p) pbuf_clen(const struct pbuf *p)
{ {
u16_t len; u16_t len;
@ -929,7 +929,7 @@ pbuf_dechain(struct pbuf *p)
* enough to hold p_from * enough to hold p_from
*/ */
err_t err_t
pbuf_copy(struct pbuf *p_to, struct pbuf *p_from) pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
{ {
u16_t offset_to=0, offset_from=0, len; u16_t offset_to=0, offset_from=0, len;
@ -996,9 +996,9 @@ pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)
* @return the number of bytes copied, or 0 on failure * @return the number of bytes copied, or 0 on failure
*/ */
u16_t u16_t
pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset) pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
{ {
struct pbuf *p; const struct pbuf *p;
u16_t left; u16_t left;
u16_t buf_copy_len; u16_t buf_copy_len;
u16_t copied_total = 0; u16_t copied_total = 0;
@ -1084,6 +1084,24 @@ void pbuf_split_64k(struct pbuf *p, struct pbuf **rest)
} }
#endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */ #endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
/* Actual implementation of pbuf_skip() but returning const pointer... */
static const struct pbuf*
pbuf_skip_const(const struct pbuf* in, u16_t in_offset, u16_t* out_offset)
{
u16_t offset_left = in_offset;
const struct pbuf* q = in;
/* get the correct pbuf */
while ((q != NULL) && (q->len <= offset_left)) {
offset_left -= q->len;
q = q->next;
}
if (out_offset != NULL) {
*out_offset = offset_left;
}
return q;
}
/** /**
* @ingroup pbuf * @ingroup pbuf
* Skip a number of bytes at the start of a pbuf * Skip a number of bytes at the start of a pbuf
@ -1096,18 +1114,7 @@ void pbuf_split_64k(struct pbuf *p, struct pbuf **rest)
struct pbuf* struct pbuf*
pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset) pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset)
{ {
u16_t offset_left = in_offset; return (struct pbuf*)(size_t)pbuf_skip_const(in, in_offset, out_offset);
struct pbuf* q = in;
/* get the correct pbuf */
while ((q != NULL) && (q->len <= offset_left)) {
offset_left -= q->len;
q = q->next;
}
if (out_offset != NULL) {
*out_offset = offset_left;
}
return q;
} }
/** /**
@ -1271,7 +1278,7 @@ pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
* @return byte at an offset into p OR ZERO IF 'offset' >= p->tot_len * @return byte at an offset into p OR ZERO IF 'offset' >= p->tot_len
*/ */
u8_t u8_t
pbuf_get_at(struct pbuf* p, u16_t offset) pbuf_get_at(const struct pbuf* p, u16_t offset)
{ {
int ret = pbuf_try_get_at(p, offset); int ret = pbuf_try_get_at(p, offset);
if (ret >= 0) { if (ret >= 0) {
@ -1289,10 +1296,10 @@ pbuf_get_at(struct pbuf* p, u16_t offset)
* @return byte at an offset into p [0..0xFF] OR negative if 'offset' >= p->tot_len * @return byte at an offset into p [0..0xFF] OR negative if 'offset' >= p->tot_len
*/ */
int int
pbuf_try_get_at(struct pbuf* p, u16_t offset) pbuf_try_get_at(const struct pbuf* p, u16_t offset)
{ {
u16_t q_idx; u16_t q_idx;
struct pbuf* q = pbuf_skip(p, offset, &q_idx); const struct pbuf* q = pbuf_skip_const(p, offset, &q_idx);
/* return requested data if pbuf is OK */ /* return requested data if pbuf is OK */
if ((q != NULL) && (q->len > q_idx)) { if ((q != NULL) && (q->len > q_idx)) {
@ -1334,10 +1341,10 @@ pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data)
* (0xffff if p is too short, diffoffset+1 otherwise) * (0xffff if p is too short, diffoffset+1 otherwise)
*/ */
u16_t u16_t
pbuf_memcmp(struct pbuf* p, u16_t offset, const void* s2, u16_t n) pbuf_memcmp(const struct pbuf* p, u16_t offset, const void* s2, u16_t n)
{ {
u16_t start = offset; u16_t start = offset;
struct pbuf* q = p; const struct pbuf* q = p;
u16_t i; u16_t i;
/* pbuf long enough to perform check? */ /* pbuf long enough to perform check? */
@ -1376,7 +1383,7 @@ pbuf_memcmp(struct pbuf* p, u16_t offset, const void* s2, u16_t n)
* @return 0xFFFF if substr was not found in p or the index where it was found * @return 0xFFFF if substr was not found in p or the index where it was found
*/ */
u16_t u16_t
pbuf_memfind(struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset) pbuf_memfind(const struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset)
{ {
u16_t i; u16_t i;
u16_t max = p->tot_len - mem_len; u16_t max = p->tot_len - mem_len;
@ -1403,7 +1410,7 @@ pbuf_memfind(struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset)
* @return 0xFFFF if substr was not found in p or the index where it was found * @return 0xFFFF if substr was not found in p or the index where it was found
*/ */
u16_t u16_t
pbuf_strstr(struct pbuf* p, const char* substr) pbuf_strstr(const struct pbuf* p, const char* substr)
{ {
size_t substr_len; size_t substr_len;
if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) { if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) {

View File

@ -231,12 +231,12 @@ u8_t pbuf_header(struct pbuf *p, s16_t header_size);
u8_t pbuf_header_force(struct pbuf *p, s16_t header_size); u8_t pbuf_header_force(struct pbuf *p, s16_t header_size);
void pbuf_ref(struct pbuf *p); void pbuf_ref(struct pbuf *p);
u8_t pbuf_free(struct pbuf *p); u8_t pbuf_free(struct pbuf *p);
u16_t pbuf_clen(struct pbuf *p); u16_t pbuf_clen(const struct pbuf *p);
void pbuf_cat(struct pbuf *head, struct pbuf *tail); void pbuf_cat(struct pbuf *head, struct pbuf *tail);
void pbuf_chain(struct pbuf *head, struct pbuf *tail); void pbuf_chain(struct pbuf *head, struct pbuf *tail);
struct pbuf *pbuf_dechain(struct pbuf *p); struct pbuf *pbuf_dechain(struct pbuf *p);
err_t pbuf_copy(struct pbuf *p_to, struct pbuf *p_from); err_t pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from);
u16_t pbuf_copy_partial(struct pbuf *p, void *dataptr, u16_t len, u16_t offset); u16_t pbuf_copy_partial(const struct pbuf *p, void *dataptr, u16_t len, u16_t offset);
err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len); err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len);
err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset); err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset);
struct pbuf *pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset); struct pbuf *pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset);
@ -249,12 +249,12 @@ err_t pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
void pbuf_split_64k(struct pbuf *p, struct pbuf **rest); void pbuf_split_64k(struct pbuf *p, struct pbuf **rest);
#endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */ #endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
u8_t pbuf_get_at(struct pbuf* p, u16_t offset); u8_t pbuf_get_at(const struct pbuf* p, u16_t offset);
int pbuf_try_get_at(struct pbuf* p, u16_t offset); int pbuf_try_get_at(const struct pbuf* p, u16_t offset);
void pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data); void pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data);
u16_t pbuf_memcmp(struct pbuf* p, u16_t offset, const void* s2, u16_t n); u16_t pbuf_memcmp(const struct pbuf* p, u16_t offset, const void* s2, u16_t n);
u16_t pbuf_memfind(struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset); u16_t pbuf_memfind(const struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset);
u16_t pbuf_strstr(struct pbuf* p, const char* substr); u16_t pbuf_strstr(const struct pbuf* p, const char* substr);
#ifdef __cplusplus #ifdef __cplusplus
} }