Skip to content

Commit eb30046

Browse files
committed
Add list helpers without memory operations
Previously, list_pushback() and list_remove() were the only list APIs available for data insertion into and removal from the list by malloc a new and free target linkage node. The memory overhead becomes a critical issue for rapid API usage. This change adds insertion and removal helpers that operate on linkage nodes instead of calling malloc/free, simplifying linkage node reuse and reducing allocation overhead.
1 parent fc2339c commit eb30046

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

include/lib/list.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ static inline list_node_t *list_cnext(const list_t *list,
7878

7979
/* Push and pop */
8080

81+
/* Pushback list node into list without new node memory allocation */
82+
static inline void list_pushback_node(list_t *list, list_node_t *target)
83+
{
84+
if (unlikely(!list || !target || target->next))
85+
return;
86+
87+
target->next = list->tail;
88+
89+
/* Insert before tail sentinel */
90+
list_node_t *prev = list->head;
91+
while (prev->next != list->tail)
92+
prev = prev->next;
93+
94+
prev->next = target;
95+
list->length++;
96+
return;
97+
}
98+
8199
static inline list_node_t *list_pushback(list_t *list, void *data)
82100
{
83101
if (unlikely(!list))
@@ -114,6 +132,25 @@ static inline void *list_pop(list_t *list)
114132
return data;
115133
}
116134

135+
/* Remove a node from list without freeing */
136+
static inline void list_remove_node(list_t *list, list_node_t *target)
137+
{
138+
if (unlikely(!list || !target || list_is_empty(list)))
139+
return;
140+
141+
list_node_t *prev = list->head;
142+
while (prev->next != list->tail && prev->next != target)
143+
prev = prev->next;
144+
145+
if (unlikely(prev->next != target))
146+
return; /* node not found */
147+
148+
prev->next = target->next;
149+
target->next = NULL;
150+
list->length--;
151+
return;
152+
}
153+
117154
/* Remove a specific node; returns its data */
118155
static inline void *list_remove(list_t *list, list_node_t *target)
119156
{

0 commit comments

Comments
 (0)