Skip to content

Commit 42af9f0

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 b4c2d10 commit 42af9f0

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
@@ -100,6 +100,24 @@ static inline list_node_t *list_pushback(list_t *list, void *data)
100100
return node;
101101
}
102102

103+
/* Pushback list node into list */
104+
static inline void list_pushback_node(list_t *list, list_node_t *target)
105+
{
106+
if (unlikely(!list || !target || target->next))
107+
return;
108+
109+
target->next = list->tail;
110+
111+
/* Insert before tail sentinel */
112+
list_node_t *prev = list->head;
113+
while (prev->next != list->tail)
114+
prev = prev->next;
115+
116+
prev->next = target;
117+
list->length++;
118+
return;
119+
}
120+
103121
static inline void *list_pop(list_t *list)
104122
{
105123
if (unlikely(list_is_empty(list)))
@@ -134,6 +152,25 @@ static inline void *list_remove(list_t *list, list_node_t *target)
134152
return data;
135153
}
136154

155+
/* Remove a node from list without freeing */
156+
static inline void list_remove_node(list_t *list, list_node_t *target)
157+
{
158+
if (unlikely(!list || !target || list_is_empty(list)))
159+
return;
160+
161+
list_node_t *prev = list->head;
162+
while (prev->next != list->tail && prev->next != target)
163+
prev = prev->next;
164+
165+
if (unlikely(prev->next != target))
166+
return; /* node not found */
167+
168+
prev->next = target->next;
169+
target->next = NULL;
170+
list->length--;
171+
return;
172+
}
173+
137174
/* Iteration */
138175

139176
/* Callback should return non-NULL to stop early, NULL to continue */

0 commit comments

Comments
 (0)