1. PBUF_RAM一次性分配size大小的连续内存 2. RBUF_ROM只需要分配小小的管理pbuf的控制管理内存 3. PBUF_ROOL分配一个链表,链表上每个元素所管理的内存最大不超过PBUF_POOL_BUFSIZE,它更像linux 中的kmem_alloc内存高速缓存机制,所以它也更适合在网卡驱动irq中断中为刚刚到来的网络数据包申请存储空间
/*-----------------------------------------------------------------------------------*/
/* pbuf_alloc():
*
* Allocates a pbuf at protocol layer l. The actual memory allocated
* for the pbuf is determined by the layer at which the pbuf is
* allocated and the requested size (from the size parameter). The
* flag parameter decides how and where the pbuf should be allocated
* as follows:
*
* * PBUF_RAM: buffer memory for pbuf is allocated as one large
* chunk. This includes protocol headers as well.
* * RBUF_ROM: no buffer memory is allocated for the pbuf, even for
* protocol headers. Additional headers must be prepended
* by allocating another pbuf and chain in to the front of
* the ROM pbuf.
* * PBUF_ROOL: the pbuf is allocated as a pbuf chain, with pbufs from
* the pbuf pool that is allocated during pbuf_init().
*/
/*-----------------------------------------------------------------------------------*/
struct pbuf *
pbuf_alloc(pbuf_layer l, u16_t size, pbuf_flag flag)
{
struct pbuf *p, *q, *r;
u16_t offset;
s32_t rsize;
offset = 0;
switch(l) {
case PBUF_TRANSPORT: // 如果为传输层申请pbuf,那么有效数据的偏移位置为PBUF_TRANSPORT_HLEN[luther.gliethttp]
offset += PBUF_TRANSPORT_HLEN;
/* FALLTHROUGH */
case PBUF_IP: // 如果为ip层申请pbuf,那么有效数据的偏移位置为PBUF_IP_HLEN+PBUF_LINK_HLEN
offset += PBUF_IP_HLEN;
offset += PBUF_LINK_HLEN;
/* FALLTHROUGH */
case PBUF_LINK: // 如果是链路层申请pbuf内存,那么数据偏移位置就是0
break;
case PBUF_RAW:
break;
default:
ASSERT("pbuf_alloc: bad pbuf layer", 0);
return NULL;
}
switch(flag) {
case PBUF_POOL:
/* Allocate head of pbuf chain into p. */
p = pbuf_pool_alloc(); // 为PBUF_POOL类型,那么需要多申请pbuf的head控制头部所需内存[luther.gliethttp]
if(p == NULL) {
#ifdef PBUF_STATS
++stats.pbuf.err;
#endif /* PBUF_STATS */
return NULL;
}
p->next = NULL;
/* Set the payload pointer so that it points offset bytes into
pbuf data memory. */
p->payload = MEM_ALIGN((void *)((u8_t *)p + (sizeof(struct pbuf) + offset))); // 以offset为基准,登记有效数据存储的起始偏移位置到p->payload[luther.gliethttp]
/* The total length of the pbuf is the requested size. */
p->tot_len = size; // pbuf链表上有效数据总大小
/* Set the length of the first pbuf is the chain. */
// pbuf链表上每个元素所能存储的最大数据为PBUF_POOL_BUFSIZE,如果超过该值,那么就会
// 使用链表方式,链接其很多个pbuf,直到申请的size数据全部能够正常存储为止[luther.gliethttp]
p->len = size > PBUF_POOL_BUFSIZE - offset? PBUF_POOL_BUFSIZE - offset: size;