aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/curl/lib/llist.c
diff options
context:
space:
mode:
authorMaxim Yurchuk <maxim-yurchuk@ydb.tech>2024-10-04 17:24:16 +0300
committerGitHub <noreply@github.com>2024-10-04 17:24:16 +0300
commita1e4766748b5924d3879ab6a0259b28ec3c5d535 (patch)
tree4480150864228623d6c9101a4ba8c049bda9aa90 /contrib/libs/curl/lib/llist.c
parent6536467764bed7822214815ce92ed4dcd5bf409b (diff)
parenta46fe128b9c9c84438fc2aac337aeefdaecb99df (diff)
downloadydb-a1e4766748b5924d3879ab6a0259b28ec3c5d535.tar.gz
Merge pull request #10090 from ydb-platform/mergelibs-241004-1110
Library import 241004-1110
Diffstat (limited to 'contrib/libs/curl/lib/llist.c')
-rw-r--r--contrib/libs/curl/lib/llist.c212
1 files changed, 166 insertions, 46 deletions
diff --git a/contrib/libs/curl/lib/llist.c b/contrib/libs/curl/lib/llist.c
index 5b6b0336da..7e19cd5095 100644
--- a/contrib/libs/curl/lib/llist.c
+++ b/contrib/libs/curl/lib/llist.c
@@ -32,16 +32,34 @@
/* this must be the last include file */
#include "memdebug.h"
+#define LLISTINIT 0x100cc001 /* random pattern */
+#define NODEINIT 0x12344321 /* random pattern */
+#define NODEREM 0x54321012 /* random pattern */
+
+
+#ifdef DEBUGBUILD
+#define VERIFYNODE(x) verifynode(x)
+static struct Curl_llist_node *verifynode(struct Curl_llist_node *n)
+{
+ DEBUGASSERT(!n || (n->_init == NODEINIT));
+ return n;
+}
+#else
+#define VERIFYNODE(x) x
+#endif
/*
* @unittest: 1300
*/
void
Curl_llist_init(struct Curl_llist *l, Curl_llist_dtor dtor)
{
- l->size = 0;
- l->dtor = dtor;
- l->head = NULL;
- l->tail = NULL;
+ l->_size = 0;
+ l->_dtor = dtor;
+ l->_head = NULL;
+ l->_tail = NULL;
+#ifdef DEBUGBUILD
+ l->_init = LLISTINIT;
+#endif
}
/*
@@ -56,91 +74,193 @@ Curl_llist_init(struct Curl_llist *l, Curl_llist_dtor dtor)
* @unittest: 1300
*/
void
-Curl_llist_insert_next(struct Curl_llist *list, struct Curl_llist_element *e,
+Curl_llist_insert_next(struct Curl_llist *list,
+ struct Curl_llist_node *e, /* may be NULL */
const void *p,
- struct Curl_llist_element *ne)
+ struct Curl_llist_node *ne)
{
- ne->ptr = (void *) p;
- if(list->size == 0) {
- list->head = ne;
- list->head->prev = NULL;
- list->head->next = NULL;
- list->tail = ne;
+ DEBUGASSERT(list);
+ DEBUGASSERT(list->_init == LLISTINIT);
+ DEBUGASSERT(ne);
+
+#ifdef DEBUGBUILD
+ ne->_init = NODEINIT;
+#endif
+ ne->_ptr = (void *) p;
+ ne->_list = list;
+ if(list->_size == 0) {
+ list->_head = ne;
+ list->_head->_prev = NULL;
+ list->_head->_next = NULL;
+ list->_tail = ne;
}
else {
/* if 'e' is NULL here, we insert the new element first in the list */
- ne->next = e?e->next:list->head;
- ne->prev = e;
+ ne->_next = e?e->_next:list->_head;
+ ne->_prev = e;
if(!e) {
- list->head->prev = ne;
- list->head = ne;
+ list->_head->_prev = ne;
+ list->_head = ne;
}
- else if(e->next) {
- e->next->prev = ne;
+ else if(e->_next) {
+ e->_next->_prev = ne;
}
else {
- list->tail = ne;
+ list->_tail = ne;
}
if(e)
- e->next = ne;
+ e->_next = ne;
}
- ++list->size;
+ ++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_node *ne)
+{
+ DEBUGASSERT(list);
+ DEBUGASSERT(list->_init == LLISTINIT);
+ DEBUGASSERT(ne);
+ Curl_llist_insert_next(list, list->_tail, p, ne);
}
/*
* @unittest: 1300
*/
void
-Curl_llist_remove(struct Curl_llist *list, struct Curl_llist_element *e,
- void *user)
+Curl_node_uremove(struct Curl_llist_node *e, void *user)
{
void *ptr;
- if(!e || list->size == 0)
+ struct Curl_llist *list;
+ if(!e)
return;
- if(e == list->head) {
- list->head = e->next;
+ list = e->_list;
+ DEBUGASSERT(list);
+ DEBUGASSERT(list->_init == LLISTINIT);
+ DEBUGASSERT(list->_size);
+ DEBUGASSERT(e->_init == NODEINIT);
+ if(e == list->_head) {
+ list->_head = e->_next;
- if(!list->head)
- list->tail = NULL;
+ if(!list->_head)
+ list->_tail = NULL;
else
- e->next->prev = NULL;
+ e->_next->_prev = NULL;
}
else {
- if(e->prev)
- e->prev->next = e->next;
+ if(e->_prev)
+ e->_prev->_next = e->_next;
- if(!e->next)
- list->tail = e->prev;
+ if(!e->_next)
+ list->_tail = e->_prev;
else
- e->next->prev = e->prev;
+ e->_next->_prev = e->_prev;
}
- ptr = e->ptr;
+ ptr = e->_ptr;
- e->ptr = NULL;
- e->prev = NULL;
- e->next = NULL;
+ e->_list = NULL;
+ e->_ptr = NULL;
+ e->_prev = NULL;
+ e->_next = NULL;
+#ifdef DEBUGBUILD
+ e->_init = NODEREM; /* specific pattern on remove - not zero */
+#endif
- --list->size;
+ --list->_size;
/* call the dtor() last for when it actually frees the 'e' memory itself */
- if(list->dtor)
- list->dtor(user, ptr);
+ if(list->_dtor)
+ list->_dtor(user, ptr);
+}
+
+void Curl_node_remove(struct Curl_llist_node *e)
+{
+ Curl_node_uremove(e, NULL);
}
void
Curl_llist_destroy(struct Curl_llist *list, void *user)
{
if(list) {
- while(list->size > 0)
- Curl_llist_remove(list, list->tail, user);
+ DEBUGASSERT(list->_init == LLISTINIT);
+ while(list->_size > 0)
+ Curl_node_uremove(list->_tail, user);
}
}
-size_t
-Curl_llist_count(struct Curl_llist *list)
+/* Curl_llist_head() returns the first 'struct Curl_llist_node *', which
+ might be NULL */
+struct Curl_llist_node *Curl_llist_head(struct Curl_llist *list)
+{
+ DEBUGASSERT(list);
+ DEBUGASSERT(list->_init == LLISTINIT);
+ return VERIFYNODE(list->_head);
+}
+
+#ifdef UNITTESTS
+/* Curl_llist_tail() returns the last 'struct Curl_llist_node *', which
+ might be NULL */
+struct Curl_llist_node *Curl_llist_tail(struct Curl_llist *list)
+{
+ DEBUGASSERT(list);
+ DEBUGASSERT(list->_init == LLISTINIT);
+ return VERIFYNODE(list->_tail);
+}
+#endif
+
+/* Curl_llist_count() returns a size_t the number of nodes in the list */
+size_t Curl_llist_count(struct Curl_llist *list)
+{
+ DEBUGASSERT(list);
+ DEBUGASSERT(list->_init == LLISTINIT);
+ return list->_size;
+}
+
+/* Curl_node_elem() returns the custom data from a Curl_llist_node */
+void *Curl_node_elem(struct Curl_llist_node *n)
+{
+ DEBUGASSERT(n);
+ DEBUGASSERT(n->_init == NODEINIT);
+ return n->_ptr;
+}
+
+/* Curl_node_next() returns the next element in a list from a given
+ Curl_llist_node */
+struct Curl_llist_node *Curl_node_next(struct Curl_llist_node *n)
+{
+ DEBUGASSERT(n);
+ DEBUGASSERT(n->_init == NODEINIT);
+ return VERIFYNODE(n->_next);
+}
+
+#ifdef UNITTESTS
+
+/* Curl_node_prev() returns the previous element in a list from a given
+ Curl_llist_node */
+struct Curl_llist_node *Curl_node_prev(struct Curl_llist_node *n)
+{
+ DEBUGASSERT(n);
+ DEBUGASSERT(n->_init == NODEINIT);
+ return VERIFYNODE(n->_prev);
+}
+
+#endif
+
+struct Curl_llist *Curl_node_llist(struct Curl_llist_node *n)
{
- return list->size;
+ DEBUGASSERT(n);
+ DEBUGASSERT(!n->_list || n->_init == NODEINIT);
+ return n->_list;
}