zephyr中net_buf的定义

来源:互联网 发布:单片机驱动发光二极管 编辑:程序博客网 时间:2024/05/19 02:43

1、相关数据结构说明:

数据结构如下:

struct net_buf {    union {        int _unused;        struct net_buf *frags;    };    uint8_t ref;    uint8_t flags;    struct net_buf_pool *pool;    union {        struct {            uint8_t *data;            uint16_t len;            uint16_t size;        };        struct net_buf_simple b;    };    uint8_t __buf[0] __net_buf_align;};struct net_buf_pool {    struct k_lifo free;  // LIFO to place the buffer into when free     const uint16_t buf_count;    uint16_t uninit_count;    const uint16_t buf_size;    const uint16_t user_data_size;    void (*const destroy)(struct net_buf *buf);    struct net_buf * const __bufs;};#define SYS_DLIST_STATIC_INIT(ptr_to_list) {{(ptr_to_list)}, {(ptr_to_list)}}#define K_LIFO_INITIALIZER(obj) \    { \    .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \    .list = NULL, \    _OBJECT_TRACING_INIT \    }#define NET_BUF_POOL_INITIALIZER(_pool, _bufs, _count, _size, _ud_size,      \                 _destroy)                                   \    {                                                                    \        .free = K_LIFO_INITIALIZER(_pool.free),                      \        .__bufs = (struct net_buf *)_bufs,                           \        .buf_count = _count,                                         \        .uninit_count = _count,                                      \        .buf_size = _size,                                           \        .user_data_size = _ud_size,                                  \        .destroy = _destroy,                                         \    }#define NET_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy)        \static struct {                                                      \        struct net_buf buf;                                          \        uint8_t data[_size] __net_buf_align;                         \        uint8_t ud[ROUND_UP(_ud_size, 4)] __net_buf_align;           \    } _net_buf_pool_##_name[_count] __noinit;                            \    static struct net_buf_pool _name =                                   \        NET_BUF_POOL_INITIALIZER(_name, _net_buf_pool_##_name,       \                     _count, _size, _ud_size, _destroy)

用以上的定义,来声明几个变量,用一个宏来搞定

NET_BUF_POOL_DEFINE(bufs_pool, 22, 74, 6, buf_destroy);

这个宏展开后如下

    static struct {                                                              struct net_buf buf;                                                  uint8_t data[74] __net_buf_align;                                uint8_t ud[ROUND_UP(6, 4)] __net_buf_align;               } _net_buf_pool_bufs_pool[22] __noinit;    static struct net_buf_pool bufs_pool =                                       {                                                                            .free = {                 .wait_q = {{(&bufs_pool.free.wait_q)}, {(&bufs_pool.free.wait_q)}},                .list = NULL,                 _OBJECT_TRACING_INIT //与这个结构没有关系,就先不展开这个宏定义了                },        .__bufs = (struct net_buf *)_net_buf_pool_bufs_pool,                                   .buf_count = 22,                                                 .uninit_count = 22,                                              .buf_size = 74,                                                   .user_data_size = 6,                                          .destroy = buf_destroy,                                             }

相当于定义了一个数组_net_buf_pool_bufs_pool[22] 和一个变量bufs_pool.

bufs_pool用来描述一个buffer池的,即这个buffer池有多少个buffer,用了多少个,首地址在什么地方,释放和销毁的函数是什么,每个buffer的大小等信息。

net_buf_pool_bufs_pool[22] 是具体的buffer池子,bufs_pool实际上是用来描述这个池子的。

2、补充:
(lifo在\kernel\lifo.c中定义和实现相关的函数)
补充罗列不太相关的结构定义,在前面有用到了,实际上是一个双向链表:

struct _dnode {    union {        struct _dnode *head; /* ptr to head of list (sys_dlist_t) */        struct _dnode *next; /* ptr to next node    (sys_dnode_t) */    };    union {        struct _dnode *tail; /* ptr to tail of list (sys_dlist_t) */        struct _dnode *prev; /* ptr to previous node (sys_dnode_t) */    };};typedef struct _dnode sys_dlist_t;typedef sys_dlist_t _wait_q_t;struct k_lifo {    _wait_q_t wait_q;    void *list;    _OBJECT_TRACING_NEXT_PTR(k_lifo);};
0 0