summaryrefslogtreecommitdiff
path: root/src/adlist.c
diff options
context:
space:
mode:
authorAdi Pinsky <adipins@amazon.com>2022-09-15 06:39:47 +0300
committerGitHub <noreply@github.com>2022-09-14 22:39:47 -0500
commitd144dc927a6ea10cd5dcbdb2eacd58e929dedcfe (patch)
tree17b5f3f90fdab39d8a61cfb6a75fc349b1802101 /src/adlist.c
parent42e4241ecee472fc2341660e29652f0e2a485453 (diff)
downloadredis-d144dc927a6ea10cd5dcbdb2eacd58e929dedcfe.tar.gz
Adds listnode to client struct for clients_pending_write list (#11220)
Diffstat (limited to 'src/adlist.c')
-rw-r--r--src/adlist.c35
1 files changed, 31 insertions, 4 deletions
diff --git a/src/adlist.c b/src/adlist.c
index 2e811e02e..6bde46132 100644
--- a/src/adlist.c
+++ b/src/adlist.c
@@ -93,6 +93,14 @@ list *listAddNodeHead(list *list, void *value)
if ((node = zmalloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
+ listLinkNodeHead(list, node);
+ return list;
+}
+
+/*
+ * Add a node that has already been allocated to the head of list
+ */
+void listLinkNodeHead(list* list, listNode *node) {
if (list->len == 0) {
list->head = list->tail = node;
node->prev = node->next = NULL;
@@ -103,7 +111,6 @@ list *listAddNodeHead(list *list, void *value)
list->head = node;
}
list->len++;
- return list;
}
/* Add a new node to the list, to tail, containing the specified 'value'
@@ -162,11 +169,20 @@ list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
}
/* Remove the specified node from the specified list.
- * It's up to the caller to free the private value of the node.
+ * The node is freed. If free callback is provided the value is freed as well.
*
* This function can't fail. */
void listDelNode(list *list, listNode *node)
{
+ listUnlinkNode(list, node);
+ if (list->free) list->free(node->value);
+ zfree(node);
+}
+
+/*
+ * Remove the specified node from the list without freeing it.
+ */
+void listUnlinkNode(list *list, listNode *node) {
if (node->prev)
node->prev->next = node->next;
else
@@ -175,8 +191,10 @@ void listDelNode(list *list, listNode *node)
node->next->prev = node->prev;
else
list->tail = node->prev;
- if (list->free) list->free(node->value);
- zfree(node);
+
+ node->next = NULL;
+ node->prev = NULL;
+
list->len--;
}
@@ -381,3 +399,12 @@ void listJoin(list *l, list *o) {
o->head = o->tail = NULL;
o->len = 0;
}
+
+/* Initializes the node's value and sets its pointers
+ * so that it is initially not a member of any list.
+ */
+void listInitNode(listNode *node, void *value) {
+ node->prev = NULL;
+ node->next = NULL;
+ node->value = value;
+}