mirror of
https://github.com/curl/curl.git
synced 2026-08-24 22:13:33 +03:00
llist: replace Curl_llist_alloc with Curl_llist_init
No longer allocate the curl_llist head struct for lists separately. Removes 17 (15%) tiny allocations in a normal "curl localhost" invoke. closes #1381
This commit is contained in:
parent
a68ca63d73
commit
e60fe20fdf
18 changed files with 214 additions and 310 deletions
|
|
@ -23,9 +23,9 @@
|
|||
|
||||
#include "llist.h"
|
||||
|
||||
static struct curl_llist *llist;
|
||||
static struct curl_llist llist;
|
||||
|
||||
static struct curl_llist *llist_destination;
|
||||
static struct curl_llist llist_destination;
|
||||
|
||||
static void test_curl_llist_dtor(void *key, void *value)
|
||||
{
|
||||
|
|
@ -36,22 +36,15 @@ static void test_curl_llist_dtor(void *key, void *value)
|
|||
|
||||
static CURLcode unit_setup(void)
|
||||
{
|
||||
llist = Curl_llist_alloc(test_curl_llist_dtor);
|
||||
if(!llist)
|
||||
return CURLE_OUT_OF_MEMORY;
|
||||
llist_destination = Curl_llist_alloc(test_curl_llist_dtor);
|
||||
if(!llist_destination) {
|
||||
Curl_llist_destroy(llist, NULL);
|
||||
return CURLE_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
Curl_llist_init(&llist, test_curl_llist_dtor);
|
||||
Curl_llist_init(&llist_destination, test_curl_llist_dtor);
|
||||
return CURLE_OK;
|
||||
}
|
||||
|
||||
static void unit_stop(void)
|
||||
{
|
||||
Curl_llist_destroy(llist, NULL);
|
||||
Curl_llist_destroy(llist_destination, NULL);
|
||||
Curl_llist_destroy(&llist, NULL);
|
||||
Curl_llist_destroy(&llist_destination, NULL);
|
||||
}
|
||||
|
||||
UNITTEST_START
|
||||
|
|
@ -62,7 +55,7 @@ UNITTEST_START
|
|||
struct curl_llist_element *element_next;
|
||||
struct curl_llist_element *element_prev;
|
||||
struct curl_llist_element *to_remove;
|
||||
size_t llist_size = Curl_llist_count(llist);
|
||||
size_t llist_size = Curl_llist_count(&llist);
|
||||
int curlErrCode = 0;
|
||||
|
||||
/**
|
||||
|
|
@ -76,10 +69,10 @@ UNITTEST_START
|
|||
* 4: list dtor will be NULL
|
||||
*/
|
||||
|
||||
fail_unless(llist->size == 0, "list initial size should be zero");
|
||||
fail_unless(llist->head == NULL, "list head should initiate to NULL");
|
||||
fail_unless(llist->tail == NULL, "list tail should intiate to NULL");
|
||||
fail_unless(llist->dtor == test_curl_llist_dtor,
|
||||
fail_unless(llist.size == 0, "list initial size should be zero");
|
||||
fail_unless(llist.head == NULL, "list head should initiate to NULL");
|
||||
fail_unless(llist.tail == NULL, "list tail should intiate to NULL");
|
||||
fail_unless(llist.dtor == test_curl_llist_dtor,
|
||||
"list dtor shold initiate to test_curl_llist_dtor");
|
||||
|
||||
/**
|
||||
|
|
@ -92,15 +85,15 @@ UNITTEST_START
|
|||
* 3: list tail will be the same as list head
|
||||
*/
|
||||
|
||||
curlErrCode = Curl_llist_insert_next(llist, llist->head, &unusedData_case1);
|
||||
curlErrCode = Curl_llist_insert_next(&llist, llist.head, &unusedData_case1);
|
||||
if(curlErrCode == 1) {
|
||||
fail_unless(Curl_llist_count(llist) == 1,
|
||||
fail_unless(Curl_llist_count(&llist) == 1,
|
||||
"List size should be 1 after adding a new element");
|
||||
/*test that the list head data holds my unusedData */
|
||||
fail_unless(llist->head->ptr == &unusedData_case1,
|
||||
fail_unless(llist.head->ptr == &unusedData_case1,
|
||||
"List size should be 1 after adding a new element");
|
||||
/*same goes for the list tail */
|
||||
fail_unless(llist->tail == llist->head,
|
||||
fail_unless(llist.tail == llist.head,
|
||||
"List size should be 1 after adding a new element");
|
||||
|
||||
/**
|
||||
|
|
@ -112,12 +105,12 @@ UNITTEST_START
|
|||
* 2: the list tail should be our newly created element
|
||||
*/
|
||||
|
||||
curlErrCode = Curl_llist_insert_next(llist, llist->head,
|
||||
curlErrCode = Curl_llist_insert_next(&llist, llist.head,
|
||||
&unusedData_case3);
|
||||
if(curlErrCode == 1) {
|
||||
fail_unless(llist->head->next->ptr == &unusedData_case3,
|
||||
fail_unless(llist.head->next->ptr == &unusedData_case3,
|
||||
"the node next to head is not getting set correctly");
|
||||
fail_unless(llist->tail->ptr == &unusedData_case3,
|
||||
fail_unless(llist.tail->ptr == &unusedData_case3,
|
||||
"the list tail is not getting set correctly");
|
||||
}
|
||||
else {
|
||||
|
|
@ -134,13 +127,13 @@ UNITTEST_START
|
|||
* 2: the list tail should different from newly created element
|
||||
*/
|
||||
|
||||
curlErrCode = Curl_llist_insert_next(llist, llist->head,
|
||||
curlErrCode = Curl_llist_insert_next(&llist, llist.head,
|
||||
&unusedData_case2);
|
||||
if(curlErrCode == 1) {
|
||||
fail_unless(llist->head->next->ptr == &unusedData_case2,
|
||||
fail_unless(llist.head->next->ptr == &unusedData_case2,
|
||||
"the node next to head is not getting set correctly");
|
||||
/* better safe than sorry, check that the tail isn't corrupted */
|
||||
fail_unless(llist->tail->ptr != &unusedData_case2,
|
||||
fail_unless(llist.tail->ptr != &unusedData_case2,
|
||||
"the list tail is not getting set correctly");
|
||||
}
|
||||
else {
|
||||
|
|
@ -165,19 +158,19 @@ UNITTEST_START
|
|||
* 3: "new" head's previous will be NULL
|
||||
*/
|
||||
|
||||
head=llist->head;
|
||||
abort_unless(head, "llist->head is NULL");
|
||||
head=llist.head;
|
||||
abort_unless(head, "llist.head is NULL");
|
||||
element_next = head->next;
|
||||
llist_size = Curl_llist_count(llist);
|
||||
llist_size = Curl_llist_count(&llist);
|
||||
|
||||
Curl_llist_remove(llist, llist->head, NULL);
|
||||
Curl_llist_remove(&llist, llist.head, NULL);
|
||||
|
||||
fail_unless(Curl_llist_count(llist) == (llist_size-1),
|
||||
fail_unless(Curl_llist_count(&llist) == (llist_size-1),
|
||||
"llist size not decremented as expected");
|
||||
fail_unless(llist->head == element_next,
|
||||
fail_unless(llist.head == element_next,
|
||||
"llist new head not modified properly");
|
||||
abort_unless(llist->head, "llist->head is NULL");
|
||||
fail_unless(llist->head->prev == NULL,
|
||||
abort_unless(llist.head, "llist.head is NULL");
|
||||
fail_unless(llist.head->prev == NULL,
|
||||
"new head previous not set to null");
|
||||
|
||||
/**
|
||||
|
|
@ -190,13 +183,13 @@ UNITTEST_START
|
|||
* 2: element->previous->next will be element->next
|
||||
* 3: element->next->previous will be element->previous
|
||||
*/
|
||||
Curl_llist_insert_next(llist, llist->head, &unusedData_case3);
|
||||
llist_size = Curl_llist_count(llist);
|
||||
to_remove = llist->head->next;
|
||||
Curl_llist_insert_next(&llist, llist.head, &unusedData_case3);
|
||||
llist_size = Curl_llist_count(&llist);
|
||||
to_remove = llist.head->next;
|
||||
abort_unless(to_remove, "to_remove is NULL");
|
||||
element_next = to_remove->next;
|
||||
element_prev = to_remove->prev;
|
||||
Curl_llist_remove(llist, to_remove, NULL);
|
||||
Curl_llist_remove(&llist, to_remove, NULL);
|
||||
fail_unless(element_prev->next == element_next,
|
||||
"element previous->next is not being adjusted");
|
||||
abort_unless(element_next, "element_next is NULL");
|
||||
|
|
@ -213,10 +206,10 @@ UNITTEST_START
|
|||
* 4: list->tail will be tail->previous
|
||||
*/
|
||||
|
||||
to_remove = llist->tail;
|
||||
to_remove = llist.tail;
|
||||
element_prev = to_remove->prev;
|
||||
Curl_llist_remove(llist, to_remove, NULL);
|
||||
fail_unless(llist->tail == element_prev,
|
||||
Curl_llist_remove(&llist, to_remove, NULL);
|
||||
fail_unless(llist.tail == element_prev,
|
||||
"llist tail is not being adjusted when removing tail");
|
||||
|
||||
/**
|
||||
|
|
@ -228,11 +221,11 @@ UNITTEST_START
|
|||
* 3: list tail will be null
|
||||
*/
|
||||
|
||||
to_remove = llist->head;
|
||||
Curl_llist_remove(llist, to_remove, NULL);
|
||||
fail_unless(llist->head == NULL,
|
||||
to_remove = llist.head;
|
||||
Curl_llist_remove(&llist, to_remove, NULL);
|
||||
fail_unless(llist.head == NULL,
|
||||
"llist head is not NULL while the llist is empty");
|
||||
fail_unless(llist->tail == NULL,
|
||||
fail_unless(llist.tail == NULL,
|
||||
"llist tail is not NULL while the llist is empty");
|
||||
|
||||
/* @testing Curl_llist_move(struct curl_llist *,
|
||||
|
|
@ -255,36 +248,36 @@ UNITTEST_START
|
|||
* add one element to the list
|
||||
*/
|
||||
|
||||
curlErrCode = Curl_llist_insert_next(llist, llist->head, &unusedData_case1);
|
||||
curlErrCode = Curl_llist_insert_next(&llist, llist.head, &unusedData_case1);
|
||||
/* necessary assertions */
|
||||
|
||||
abort_unless(curlErrCode == 1,
|
||||
"Curl_llist_insert_next returned an error, Can't move on with test");
|
||||
abort_unless(Curl_llist_count(llist) == 1,
|
||||
abort_unless(Curl_llist_count(&llist) == 1,
|
||||
"Number of list elements is not as expected, Aborting");
|
||||
abort_unless(Curl_llist_count(llist_destination) == 0,
|
||||
abort_unless(Curl_llist_count(&llist_destination) == 0,
|
||||
"Number of list elements is not as expected, Aborting");
|
||||
|
||||
/*actual testing code*/
|
||||
curlErrCode = Curl_llist_move(llist, llist->head, llist_destination, NULL);
|
||||
curlErrCode = Curl_llist_move(&llist, llist.head, &llist_destination, NULL);
|
||||
abort_unless(curlErrCode == 1,
|
||||
"Curl_llist_move returned an error, Can't move on with test");
|
||||
fail_unless(Curl_llist_count(llist) == 0,
|
||||
fail_unless(Curl_llist_count(&llist) == 0,
|
||||
"moving element from llist didn't decrement the size");
|
||||
|
||||
fail_unless(Curl_llist_count(llist_destination) == 1,
|
||||
fail_unless(Curl_llist_count(&llist_destination) == 1,
|
||||
"moving element to llist_destination didn't increment the size");
|
||||
|
||||
fail_unless(llist->head == NULL,
|
||||
fail_unless(llist.head == NULL,
|
||||
"llist head not set to null after moving the head");
|
||||
|
||||
fail_unless(llist_destination->head != NULL,
|
||||
fail_unless(llist_destination.head != NULL,
|
||||
"llist_destination head set to null after moving an element");
|
||||
|
||||
fail_unless(llist_destination->tail != NULL,
|
||||
fail_unless(llist_destination.tail != NULL,
|
||||
"llist_destination tail set to null after moving an element");
|
||||
|
||||
fail_unless(llist_destination->tail == llist_destination->tail,
|
||||
fail_unless(llist_destination.tail == llist_destination.tail,
|
||||
"llist_destination tail doesn't equal llist_destination head");
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue