From 5e2b1e2d0d11c911b6f91142b73e0b6ae8df9bb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Tue, 5 May 2015 00:06:28 +0200 Subject: [PATCH] Add mbuf enqueue/dequeue functions --- include/ucore/mbuf.h | 14 +++++++++++++- src/mbuf.c | 18 ++++++++++++++++++ test/test_mbuf.c | 41 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/include/ucore/mbuf.h b/include/ucore/mbuf.h index 9272568..7939504 100644 --- a/include/ucore/mbuf.h +++ b/include/ucore/mbuf.h @@ -181,7 +181,7 @@ uint8_t *uc_mbuf_put(struct MBuf *mbuf, uint32_t size); * data in the buffer, retreating the head pointer by size butes * You need to insert the size bytes in the returned pointer. * - * @oaram size bytes to prepend to the buffer +w* @oaram size bytes to prepend to the buffer * @return NULL if the mbuf does not have enough headroom */ uint8_t *uc_mbuf_push(struct MBuf *mbuf, uint32_t size); @@ -214,4 +214,16 @@ static UC_INLINE uint8_t *uc_mbuf_head(struct MBuf* mbuf) return mbuf->head; } +/** Add the mbuf at the end of the tailq. + * This uses the tailQ entry member of the mbuf. + */ +void uc_mbuf_enqueue(struct MBuf *mbuf, struct TailQ *head); + +/** Remove and return the mbuf at the start of the tailq. + * @return the first mbuf in the queue, or NULL if ti isempty. + */ +struct MBuf *uc_mbuf_dequeue(struct TailQ *head); + + + #endif diff --git a/src/mbuf.c b/src/mbuf.c index 7fa8abc..4b729fb 100644 --- a/src/mbuf.c +++ b/src/mbuf.c @@ -203,3 +203,21 @@ int uc_mbuf_extend_tailroom(struct MBuf *mbuf, uint32_t len) return 0; } + +void uc_mbuf_enqueue(struct MBuf *mbuf, struct TailQ *head) +{ + uc_tailq_insert_tail(head, &mbuf->entry); +} + +struct MBuf *uc_mbuf_dequeue(struct TailQ *head) +{ + struct TailQ *q; + + if (uc_tailq_empty(head)) { + return NULL; + } + q = UC_TAILQ_FIRST(head); + uc_tailq_remove(q); + + return UC_TAILQ_CONTAINER(q, struct MBuf, entry); +} diff --git a/test/test_mbuf.c b/test/test_mbuf.c index be2df35..9b7464f 100644 --- a/test/test_mbuf.c +++ b/test/test_mbuf.c @@ -297,11 +297,49 @@ START_TEST (test_mbuf_extend_tailroom_wrong_type) END_TEST +START_TEST (test_mbuf_queue) + struct MBuf *m1; + struct MBuf *m2; + struct MBuf *q; + uint8_t *data1; + UC_TAILQ_HEAD(head); + + m1 = uc_mbuf_new_inline(10); + fail_if(m1 == NULL); + + data1 = uc_mbuf_put(m1, 10); + fail_if(data1 == NULL); + + strcpy((char*)data1, "123456789"); + + m2 = uc_mbuf_new_inline(10); + fail_if(m2 == NULL); + + data1 = uc_mbuf_put(m2, 10); + fail_if(data1 == NULL); + + strcpy((char*)data1, "987654321"); + + uc_mbuf_enqueue(m1, &head); + uc_mbuf_enqueue(m2, &head); + + q = uc_mbuf_dequeue(&head); + fail_if(strcmp((char*)uc_mbuf_head(q), "123456789") != 0); + + q = uc_mbuf_dequeue(&head); + fail_if(strcmp((char*)uc_mbuf_head(q), "987654321") != 0); + + fail_if(!uc_tailq_empty(&head)); + + uc_mbuf_free(m1); + uc_mbuf_free(m2); + +END_TEST Suite *mbuf_suite(void) { Suite *s = suite_create("mbuf"); - TCase *tc = tcase_create("mbuf tests"); + TCase *tc = tcase_create("mbuf tests"); tcase_add_test(tc, test_mbuf_new); tcase_add_test(tc, test_mbuf_new_inline); tcase_add_test(tc, test_mbuf_new_headroom); @@ -316,6 +354,7 @@ Suite *mbuf_suite(void) tcase_add_test(tc, test_mbuf_static); tcase_add_test(tc, test_mbuf_extend_tailroom); tcase_add_test(tc, test_mbuf_extend_tailroom_wrong_type); + tcase_add_test(tc, test_mbuf_queue); suite_add_tcase(s, tc);