Lines Matching refs:pbuf

5  * Packets are built from the pbuf data structure. It supports dynamic
11 * list. This is called a "pbuf chain".
16 * So, a packet queue consists of one or more pbuf chains, each of
20 * The differences between a pbuf chain and a packet queue are very
23 * The last pbuf of a packet has a ->tot_len field that equals the
25 * pbuf of a packet has a ->next field other than NULL, more packets
28 * Therefore, looping through a pbuf of a single packet, has an
70 #include "lwip/pbuf.h"
82 #define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
176 * Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).
178 * The actual memory allocated for the pbuf is determined by the
179 * layer at which the pbuf is allocated and the requested size
183 * @param length size of the pbuf's payload
184 * @param type this parameter decides how and where the pbuf
187 * - PBUF_RAM: buffer memory for pbuf is allocated as one large
189 * - PBUF_ROM: no buffer memory is allocated for the pbuf, even for
191 * by allocating another pbuf and chain in to the front of
192 * the ROM pbuf. It is assumed that the memory used is really
196 * - PBUF_REF: no buffer memory is allocated for the pbuf, even for
197 * protocol headers. It is assumed that the pbuf is only
198 * being used in a single thread. If the pbuf gets queued,
200 * - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from
201 * the pbuf pool that is allocated during pbuf_init().
203 * @return the allocated pbuf. If multiple pbufs where allocated, this
204 * is the first pbuf of a pbuf chain.
206 struct pbuf *
209 struct pbuf *p, *q, *r;
232 LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);
238 /* allocate head of pbuf chain into p */
239 p = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
240 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));
248 /* make the payload pointer point 'offset' bytes into pbuf data memory */
250 LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",
252 /* the total length of the pbuf chain is the requested size */
254 /* set the length of the first pbuf in the chain */
256 LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
264 /* now allocate the tail of the pbuf chain */
266 /* remember first pbuf for linkage in next iteration */
272 q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
283 /* make previous pbuf point to this pbuf */
285 /* set total length of this pbuf and next in chain */
288 /* this pbuf length is pool size, unless smaller sized tail */
291 LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
293 LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
299 /* remember this pbuf for linkage in next iteration */
307 /* If pbuf is to be allocated in RAM, allocate memory for it. */
308 p = (struct pbuf*)mem_malloc(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF + offset) + LWIP_MEM_ALIGN_SIZE(length));
312 /* Set up internal structure of the pbuf. */
318 LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
321 /* pbuf references existing (non-volatile static constant) ROM payload? */
323 /* pbuf references existing (externally allocated) RAM payload? */
325 /* only allocate memory for the pbuf structure */
326 p = (struct pbuf *)memp_malloc(MEMP_PBUF);
352 /** Initialize a custom pbuf (already allocated).
355 * @param length size of the pbuf's payload
356 * @param type type of the pbuf (only used to treat the pbuf accordingly, as
358 * @param p pointer to the custom pbuf to initialize (already allocated)
366 struct pbuf*
391 LWIP_ASSERT("pbuf_alloced_custom: bad pbuf layer", 0);
400 p->pbuf.next = NULL;
402 p->pbuf.payload = (u8_t *)payload_mem + LWIP_MEM_ALIGN_SIZE(offset);
404 p->pbuf.payload = NULL;
406 p->pbuf.flags = PBUF_FLAG_IS_CUSTOM;
407 p->pbuf.len = p->pbuf.tot_len = length;
408 p->pbuf.type = type;
409 p->pbuf.ref = 1;
410 return &p->pbuf;
415 * Shrink a pbuf chain to a desired length.
417 * @param p pbuf to shrink.
418 * @param new_len desired new length of pbuf chain
421 * be skipped and left unchanged. The new last pbuf in the chain will be
424 * @note If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted.
427 * @note Despite its name, pbuf_realloc cannot grow the size of a pbuf (chain).
430 pbuf_realloc(struct pbuf *p, u16_t new_len)
432 struct pbuf *q;
448 /* the pbuf chain grows by (new_len - p->tot_len) bytes
455 /* should this pbuf be kept? */
457 /* decrease remaining length by pbuf length */
462 /* proceed to next pbuf in chain */
466 /* we have now reached the new last pbuf (in q) */
467 /* rem_len == desired length for pbuf q */
472 /* reallocate and adjust the length of the pbuf that will be split */
473 q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len);
476 /* adjust length fields for new last pbuf */
494 * (dis)appears in the pbuf payload.
498 * @param p pbuf to change the header size.
500 * increases the size of the pbuf. New space is on the front.
511 pbuf_header(struct pbuf *p, s16_t header_size_increment)
524 /* Check that we aren't going to move off the end of the pbuf */
531 /* Check that we've got the correct type of pbuf to work with */
534 /* Check that we aren't going to move off the beginning of the pbuf */
544 /* pbuf types containing payloads? */
558 /* pbuf types refering to external payloads? */
571 LWIP_ASSERT("bad pbuf type", 0);
574 /* modify pbuf length fields */
585 * Dereference a pbuf chain or queue and deallocate any no-longer-used
588 * Decrements the pbuf reference count. If it reaches zero, the pbuf is
591 * For a pbuf chain, this is repeated for each pbuf in the chain,
592 * up to the first pbuf which has a non-zero reference count after
596 * @param p The pbuf (chain) to be dereferenced.
602 * @note the reference counter of a pbuf equals the number of pointers
603 * that refer to the pbuf (or into the pbuf).
618 pbuf_free(struct pbuf *p)
621 struct pbuf *q;
651 /* decrease reference count (number of pointers to pbuf) */
654 /* this pbuf is no longer referenced to? */
656 /* remember next pbuf in chain for next iteration */
661 /* is this a custom pbuf? */
669 /* is this a pbuf from the pool? */
672 /* is this a ROM or RAM referencing pbuf? */
681 /* proceed to next pbuf */
683 /* p->ref > 0, this pbuf is still referenced to */
699 * @param p first pbuf of chain
704 pbuf_clen(struct pbuf *p)
717 * Increment the reference count of the pbuf.
719 * @param p pbuf to increase reference counter of
723 pbuf_ref(struct pbuf *p)
726 /* pbuf given? */
735 * Concatenate two pbufs (each may be a pbuf chain) and take over
736 * the caller's reference of the tail pbuf.
738 * @note The caller MAY NOT reference the tail pbuf afterwards.
745 pbuf_cat(struct pbuf *h, struct pbuf *t)
747 struct pbuf *p;
752 /* proceed to last pbuf of chain */
757 /* { p is last pbuf of first h chain, p->next == NULL } */
758 LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len);
760 /* add total length of second chain to last pbuf total of first chain */
762 /* chain last pbuf of head (p) with first of tail (t) */
770 * Chain two pbufs (or pbuf chains) together.
775 * @param h head pbuf (chain)
776 * @param t tail pbuf (chain)
781 * The ->next field of the last pbuf of the head chain is adjusted.
782 * The ->ref field of the first pbuf of the tail chain is adjusted.
786 pbuf_chain(struct pbuf *h, struct pbuf *t)
795 * Dechains the first pbuf from its succeeding pbufs in the chain.
798 * @param p pbuf to dechain
799 * @return remainder of the pbuf chain, or NULL if it was de-allocated.
802 struct pbuf *
803 pbuf_dechain(struct pbuf *p)
805 struct pbuf *q;
809 /* pbuf has successor in chain? */
815 /* decouple pbuf from remainder */
817 /* total length of pbuf p is its own length only */
844 * @param p_to pbuf destination of the copy
845 * @param p_from pbuf source of the copy
847 * @return ERR_OK if pbuf was copied
852 pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)
863 /* iterate through pbuf chain */
910 * @param buf the pbuf from which to copy data
918 pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
920 struct pbuf *p;
934 /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
956 * Copy application supplied data into a pbuf.
959 * @param buf pbuf to fill with data
963 * @return ERR_OK if successful, ERR_MEM if the pbuf is not big enough
966 pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
968 struct pbuf *p;
980 /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
982 LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL);
985 /* this pbuf cannot hold all remaining data */
998 * Creates a single pbuf out of a queue of pbufs.
1000 * @remark: Either the source pbuf 'p' is freed by this function or the original
1001 * pbuf 'p' is returned, therefore the caller has to check the result!
1003 * @param p the source pbuf
1004 * @param layer pbuf_layer of the new pbuf
1006 * @return a new, single pbuf (p->next is NULL)
1007 * or the old pbuf if allocation fails
1009 struct pbuf*
1010 pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
1012 struct pbuf *q;
1030 * Copies data into a single pbuf (*not* into a pbuf queue!) and updates
1033 * @param p the pbuf to copy data into
1035 * @param dataptr data to copy into the pbuf
1036 * @param len length of data to copy into the pbuf
1039 * within the (first) pbuf (no pbuf queues!)
1042 pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
1069 /** Get one byte from the specified position in a pbuf
1072 * @param p pbuf to parse
1077 pbuf_get_at(struct pbuf* p, u16_t offset)
1080 struct pbuf* q = p;
1082 /* get the correct pbuf */
1087 /* return requested data if pbuf is OK */
1094 /** Compare pbuf contents at specified offset with memory s2, both of length n
1096 * @param p pbuf to compare
1104 pbuf_memcmp(struct pbuf* p, u16_t offset, const void* s2, u16_t n)
1107 struct pbuf* q = p;
1109 /* get the correct pbuf */
1114 /* return requested data if pbuf is OK */
1129 /** Find occurrence of mem (with length mem_len) in pbuf p, starting at offset
1132 * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as
1140 pbuf_memfind(struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset)
1157 /** Find occurrence of substr with length substr_len in pbuf p, start at offset
1160 * the pbuf/source string!
1162 * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as
1168 pbuf_strstr(struct pbuf* p, const char* substr)