日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LwIP之netbuf

發(fā)布時間:2025/3/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LwIP之netbuf 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?netbuf是應(yīng)用程序和協(xié)議棧內(nèi)核交互的一種數(shù)據(jù)結(jié)構(gòu)

netbuf并不復(fù)雜,下面是實現(xiàn)代碼

/* 創(chuàng)建netbuf */ struct netbuf *netbuf_new(void) {struct netbuf *buf;/* 為netbuf申請內(nèi)存空間 */buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);if(buf != NULL) {/* 清空參數(shù) */buf->p = NULL;buf->ptr = NULL;ip_addr_set_any(&buf->addr);buf->port = 0;return buf;} else {return NULL;} }/* 刪除netbuf */ void netbuf_delete(struct netbuf *buf) {if(buf != NULL) {/* 先釋放pbuf */if(buf->p != NULL) {pbuf_free(buf->p);buf->p = buf->ptr = NULL;}/* 再釋放netbuf */memp_free(MEMP_NETBUF, buf);} }/* 為netbuf申請數(shù)據(jù)(pbuf)空間 */ void *netbuf_alloc(struct netbuf *buf, u16_t size) {/* 為netbuf申請數(shù)據(jù)(pbuf)空間 */if(buf->p != NULL) {pbuf_free(buf->p);}buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);if(buf->p == NULL) {return NULL;}/* ptr指針初始化指向第一個pbuf */buf->ptr = buf->p;/* 返回數(shù)據(jù)有效數(shù)據(jù)指針 */return buf->p->payload; }/* 釋放netbuf的數(shù)據(jù)(pbuf)空間 */ void netbuf_free(struct netbuf *buf) {if(buf->p != NULL) {pbuf_free(buf->p);}buf->p = buf->ptr = NULL; }/* 為netbuf申請PBUF_REF型pbuf內(nèi)存,指向已存在RAM */ err_t netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size) {if(buf->p != NULL) {pbuf_free(buf->p);}buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);if(buf->p == NULL) {buf->ptr = NULL;return ERR_MEM;}buf->p->payload = (void *)dataptr;buf->p->len = buf->p->tot_len = size;buf->ptr = buf->p;return ERR_OK; }/* 將兩個netbuf拼接起來 */ void netbuf_chain(struct netbuf *head, struct netbuf *tail) {pbuf_cat(head->p, tail->p);head->ptr = head->p;memp_free(MEMP_NETBUF, tail); }/* 獲取netbuf有效數(shù)據(jù)指針和數(shù)據(jù)長度 */ err_t netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len) {if(buf->ptr == NULL) {return ERR_BUF;}*dataptr = buf->ptr->payload;*len = buf->ptr->len;return ERR_OK; }/* 向后偏移netbuf的pbuf偏移指針 */ s8_t netbuf_next(struct netbuf *buf) {if(buf->ptr->next == NULL) {return -1;}buf->ptr = buf->ptr->next;if(buf->ptr->next == NULL) {return 1;}return 0; }/* netbuf的pbuf偏移指針指向第一個pbuf */ void netbuf_first(struct netbuf *buf) {buf->ptr = buf->p; }

?

總結(jié)

以上是生活随笔為你收集整理的LwIP之netbuf的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。