关于lwip中pbuf_alloc()内存申请函数

简介: 1. PBUF_RAM一次性分配size大小的连续内存2. RBUF_ROM只需要分配小小的管理pbuf的控制管理内存3. PBUF_ROOL分配一个链表,链表上每个元素所管理的内存最大不超过PBUF_POOL_BUFSIZE,它更像linux   中的kmem_alloc内存高速缓...
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;

    p->flags = PBUF_FLAG_POOL;
    
    /* Allocate the tail of the pbuf chain. */
    r = p;
    rsize = size - p->len; // 第1个pbuf所能存储数据大小为p->len,这里计算还需要多少存储空间存储剩下的数据.
    while(rsize > 0) {     // 构成数据缓存链表,每个链表元素所能存储的最大数据量为PBUF_POOL_BUFSIZE个字节[luther.gliethttp]
      q = pbuf_pool_alloc();
      if(q == NULL) {
    DEBUGF(PBUF_DEBUG, ("pbuf_alloc: Out of pbufs in pool,\n"));
#ifdef PBUF_STATS
        ++stats.pbuf.err;
#endif /* PBUF_STATS */
        pbuf_pool_free(p);
        return NULL;
      }
      q->next = NULL;
      r->next = q;
      q->len = rsize > PBUF_POOL_BUFSIZE? PBUF_POOL_BUFSIZE: rsize;
      q->flags = PBUF_FLAG_POOL;
      q->payload = (void *)((u8_t *)q + sizeof(struct pbuf));
      r = q;
      q->ref = 1;
      q = q->next;
      rsize -= PBUF_POOL_BUFSIZE;
    }
    r->next = NULL;

    ASSERT("pbuf_alloc: pbuf->payload properly aligned",
       ((u32_t)p->payload % MEM_ALIGNMENT) == 0);
    break;
  case PBUF_RAM:
    /* If pbuf is to be allocated in RAM, allocate memory for it. */
// PBUF_RAM类型内存,那么一次性申请size大小的连续内存
    p = mem_malloc(MEM_ALIGN_SIZE(sizeof(struct pbuf) + size + offset));
    if(p == NULL) {
      return NULL;
    }
    /* Set up internal structure of the pbuf. */
    p->payload = MEM_ALIGN((void *)((u8_t *)p + sizeof(struct pbuf) + offset));
    p->len = p->tot_len = size;
    p->next = NULL;
    p->flags = PBUF_FLAG_RAM;

    ASSERT("pbuf_alloc: pbuf->payload properly aligned",
       ((u32_t)p->payload % MEM_ALIGNMENT) == 0);
    break;
  case PBUF_ROM:
    /* If the pbuf should point to ROM, we only need to allocate
       memory for the pbuf structure. */
// PBUF_ROM类型内存,那么只需要申请pbufs头部控制结构体所需内存即可[luther.gliethttp]
    p = memp_mallocp(MEMP_PBUF);
    if(p == NULL) {
      return NULL;
    }
    p->payload = NULL;
    p->len = p->tot_len = size;
    p->next = NULL;
    p->flags = PBUF_FLAG_ROM;
    break;
  default:
    ASSERT("pbuf_alloc: erroneous flag", 0);
    return NULL;
  }
  p->ref = 1;
  return p;

相关文章
|
3月前
10分钟让你学会内存函数:memcpy,memmove,memset,memcmp的用法及模拟实现。
10分钟让你学会内存函数:memcpy,memmove,memset,memcmp的用法及模拟实现。
49 2
|
3月前
|
存储 C语言
C语言学习记录——动态内存函数介绍(malloc、free、calloc、realloc)
C语言学习记录——动态内存函数介绍(malloc、free、calloc、realloc)
75 1
|
3月前
|
C语言
【C语言】:动态内存管理函数malloc,calloc,realloc和free的介绍的介绍
【C语言】:动态内存管理函数malloc,calloc,realloc和free的介绍的介绍
46 0
|
1月前
|
C语言
【C语言篇】字符和字符串以及内存函数详细介绍与模拟实现(下篇)
perror函数打印完参数部分的字符串后,再打印⼀个冒号和⼀个空格,再打印错误信息。
|
1月前
|
存储 安全 编译器
【C语言篇】字符和字符串以及内存函数的详细介绍与模拟实现(上篇)
当然可以用scanf和printf输入输出,这里在之前【C语言篇】scanf和printf万字超详细介绍(基本加拓展用法)已经讲过了,这里就不再赘述,主要介绍只针对字符的函数.
|
1月前
【C初阶】内存函数:memcpy+memmove+memset+memcmp
【C初阶】内存函数:memcpy+memmove+memset+memcmp
|
2月前
|
存储 缓存 C语言
【C语言】字符函数,字符串函数,内存函数
C语言中的字符串函数和内存函数
33 0
【C语言】字符函数,字符串函数,内存函数
|
3月前
Unsafe申请堆外内存
Unsafe申请堆外内存
|
3月前
|
C语言
字符串和内存函数(1)
字符串和内存函数(1)
35 7
|
3月前
|
C语言
【C语言】:4大内存函数
【C语言】:4大内存函数
22 2

热门文章

最新文章