From: Stefan Eissing Date: Wed, 10 Apr 2024 09:51:08 +0000 (+0200) Subject: llist: add Curl_llist_append() X-Git-Tag: curl-8_8_0~250 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c296abd42dc9d1af050818b1cd23f9a8a9e5cbc5;p=thirdparty%2Fcurl.git llist: add Curl_llist_append() - use for better readability in all places where the "insert_next" actually performs an append to the list - add some tests in unit1300 Closes #13336 --- diff --git a/lib/altsvc.c b/lib/altsvc.c index c12d7bda19..4fc4fe60f0 100644 --- a/lib/altsvc.c +++ b/lib/altsvc.c @@ -191,7 +191,7 @@ static CURLcode altsvc_add(struct altsvcinfo *asi, char *line) as->expires = expires; as->prio = prio; as->persist = persist ? 1 : 0; - Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node); + Curl_llist_append(&asi->list, as, &as->node); } } @@ -643,7 +643,7 @@ CURLcode Curl_altsvc_parse(struct Curl_easy *data, account. [See RFC 7838 section 3.1] */ as->expires = maxage + time(NULL); as->persist = persist; - Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node); + Curl_llist_append(&asi->list, as, &as->node); infof(data, "Added alt-svc: %s:%d over %s", dsthost, dstport, Curl_alpnid2str(dstalpnid)); } diff --git a/lib/conncache.c b/lib/conncache.c index 0d26090858..11dd8e8ca9 100644 --- a/lib/conncache.c +++ b/lib/conncache.c @@ -68,8 +68,7 @@ static void bundle_destroy(struct connectbundle *bundle) static void bundle_add_conn(struct connectbundle *bundle, struct connectdata *conn) { - Curl_llist_insert_next(&bundle->conn_list, bundle->conn_list.tail, conn, - &conn->bundle_node); + Curl_llist_append(&bundle->conn_list, conn, &conn->bundle_node); conn->bundle = bundle; bundle->num_connections++; } diff --git a/lib/ftplistparser.c b/lib/ftplistparser.c index 82f1ea00d3..448f3a43ab 100644 --- a/lib/ftplistparser.c +++ b/lib/ftplistparser.c @@ -349,7 +349,7 @@ static CURLcode ftp_pl_insert_finfo(struct Curl_easy *data, Curl_set_in_callback(data, false); if(add) { - Curl_llist_insert_next(llist, llist->tail, finfo, &infop->list); + Curl_llist_append(llist, finfo, &infop->list); } else { Curl_fileinfo_cleanup(infop); diff --git a/lib/hash.c b/lib/hash.c index 30f28e2352..772c03b5ca 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -132,7 +132,7 @@ Curl_hash_add(struct Curl_hash *h, void *key, size_t key_len, void *p) he = mk_hash_element(key, key_len, p); if(he) { - Curl_llist_insert_next(l, l->tail, he, &he->list); + Curl_llist_append(l, he, &he->list); ++h->size; return p; /* return the new entry */ } diff --git a/lib/headers.c b/lib/headers.c index ea8a7aa98d..ef947492de 100644 --- a/lib/headers.c +++ b/lib/headers.c @@ -264,8 +264,7 @@ static CURLcode unfold_value(struct Curl_easy *data, const char *value, newhs->value[olen + vlen] = 0; /* null-terminate at newline */ /* insert this node into the list of headers */ - Curl_llist_insert_next(&data->state.httphdrs, data->state.httphdrs.tail, - newhs, &newhs->node); + Curl_llist_append(&data->state.httphdrs, newhs, &newhs->node); data->state.prevhead = newhs; return CURLE_OK; } @@ -328,8 +327,7 @@ CURLcode Curl_headers_push(struct Curl_easy *data, const char *header, hs->request = data->state.requests; /* insert this node into the list of headers */ - Curl_llist_insert_next(&data->state.httphdrs, data->state.httphdrs.tail, - hs, &hs->node); + Curl_llist_append(&data->state.httphdrs, hs, &hs->node); data->state.prevhead = hs; } else diff --git a/lib/hsts.c b/lib/hsts.c index 607755e6ba..d006e68869 100644 --- a/lib/hsts.c +++ b/lib/hsts.c @@ -140,7 +140,7 @@ static CURLcode hsts_create(struct hsts *h, sts->host = duphost; sts->expires = expires; sts->includeSubDomains = subdomains; - Curl_llist_insert_next(&h->list, h->list.tail, sts, &sts->node); + Curl_llist_append(&h->list, sts, &sts->node); } return CURLE_OK; } diff --git a/lib/llist.c b/lib/llist.c index 5b6b0336da..716f0cd576 100644 --- a/lib/llist.c +++ b/lib/llist.c @@ -88,6 +88,22 @@ Curl_llist_insert_next(struct Curl_llist *list, struct Curl_llist_element *e, ++list->size; } +/* + * Curl_llist_append() + * + * Adds a new list element to the end of the list. + * + * The 'ne' argument should be a pointer into the object to store. + * + * @unittest: 1300 + */ +void +Curl_llist_append(struct Curl_llist *list, const void *p, + struct Curl_llist_element *ne) +{ + Curl_llist_insert_next(list, list->tail, p, ne); +} + /* * @unittest: 1300 */ diff --git a/lib/llist.h b/lib/llist.h index 320580e33c..d75582fa97 100644 --- a/lib/llist.h +++ b/lib/llist.h @@ -45,6 +45,8 @@ struct Curl_llist { void Curl_llist_init(struct Curl_llist *, Curl_llist_dtor); void Curl_llist_insert_next(struct Curl_llist *, struct Curl_llist_element *, const void *, struct Curl_llist_element *node); +void Curl_llist_append(struct Curl_llist *, + const void *, struct Curl_llist_element *node); void Curl_llist_remove(struct Curl_llist *, struct Curl_llist_element *, void *); size_t Curl_llist_count(struct Curl_llist *); diff --git a/lib/multi.c b/lib/multi.c index ee2b9cbde1..55c6dfc8b9 100644 --- a/lib/multi.c +++ b/lib/multi.c @@ -374,8 +374,7 @@ static void sh_init(struct Curl_hash *hash, int hashsize) */ static void multi_addmsg(struct Curl_multi *multi, struct Curl_message *msg) { - Curl_llist_insert_next(&multi->msglist, multi->msglist.tail, msg, - &msg->list); + Curl_llist_append(&multi->msglist, msg, &msg->list); } struct Curl_multi *Curl_multi_handle(int hashsize, /* socket hash */ @@ -1002,8 +1001,7 @@ void Curl_attach_connection(struct Curl_easy *data, DEBUGASSERT(!data->conn); DEBUGASSERT(conn); data->conn = conn; - Curl_llist_insert_next(&conn->easyq, conn->easyq.tail, data, - &data->conn_queue); + Curl_llist_append(&conn->easyq, data, &data->conn_queue); if(conn->handler && conn->handler->attach) conn->handler->attach(data, conn); Curl_conn_ev_data_attach(conn, data); @@ -1996,8 +1994,7 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi, multistate(data, MSTATE_PENDING); /* add this handle to the list of connect-pending handles */ - Curl_llist_insert_next(&multi->pending, multi->pending.tail, data, - &data->connect_queue); + Curl_llist_append(&multi->pending, data, &data->connect_queue); /* unlink from the main list */ unlink_easy(multi, data); result = CURLE_OK; @@ -2720,8 +2717,7 @@ statemachine_end: multistate(data, MSTATE_MSGSENT); /* add this handle to the list of msgsent handles */ - Curl_llist_insert_next(&multi->msgsent, multi->msgsent.tail, data, - &data->connect_queue); + Curl_llist_append(&multi->msgsent, data, &data->connect_queue); /* unlink from the main list */ unlink_easy(multi, data); return CURLM_OK; diff --git a/lib/multihandle.h b/lib/multihandle.h index 3156c83ad3..122f8f6a7c 100644 --- a/lib/multihandle.h +++ b/lib/multihandle.h @@ -179,7 +179,7 @@ struct Curl_multi { BIT(dead); /* a callback returned error, everything needs to crash and burn */ BIT(xfer_buf_borrowed); /* xfer_buf is currently being borrowed */ - BIT(xfer_ulbuf_borrowed); /* xfer_buf is currently being borrowed */ + BIT(xfer_ulbuf_borrowed); /* xfer_ulbuf is currently being borrowed */ #ifdef DEBUGBUILD BIT(warned); /* true after user warned of DEBUGBUILD */ #endif diff --git a/tests/unit/unit1300.c b/tests/unit/unit1300.c index 66fb5d02d5..d94cfe1787 100644 --- a/tests/unit/unit1300.c +++ b/tests/unit/unit1300.c @@ -217,6 +217,54 @@ UNITTEST_START fail_unless(llist.tail == NULL, "llist tail is not NULL while the llist is empty"); + /** + * testing Curl_llist_append + * case 1: + * list is empty + * @assumptions: + * 1: the element next to head should be our newly created element + * 2: the list tail should different from newly created element + */ + Curl_llist_append(&llist, &unusedData_case1, &case1_list); + fail_unless(Curl_llist_count(&llist) == 1, + "List size should be 1 after appending a new element"); + /* test that the list head data holds my unusedData */ + fail_unless(llist.head->ptr == &unusedData_case1, + "head ptr should be first entry"); + /* same goes for the list tail */ + fail_unless(llist.tail == llist.head, + "tail and head should be the same"); + + /** + * testing Curl_llist_append + * case 2: + * list is not empty + * @assumptions: + * 1: the list head-next should be the newly created element + * 2: the list tail should be the newly created element + */ + Curl_llist_append(&llist, &unusedData_case2, &case2_list); + fail_unless(llist.head->next->ptr == &unusedData_case2, + "the node next to head is not getting set correctly"); + fail_unless(llist.tail->ptr == &unusedData_case2, + "the list tail is not getting set correctly"); + + /** + * testing Curl_llist_append + * case 3: + * list is has 2 members + * @assumptions: + * 1: the list head-next should remain the same + * 2: the list tail should be the newly created element + */ + Curl_llist_append(&llist, &unusedData_case3, &case3_list); + fail_unless(llist.head->next->ptr == &unusedData_case2, + "the node next to head did not stay the same"); + fail_unless(llist.tail->ptr == &unusedData_case3, + "the list tail is not getting set correctly"); + + + Curl_llist_destroy(&llist, NULL); Curl_llist_destroy(&llist_destination, NULL); }