Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15318093
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类: LINUX

2009-11-23 13:37:07

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;
}
阅读(4970) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~