Skip to content

Commit aafbb6c

Browse files
committed
Refactor list node to support embedded usage
Remove the data member from list_node to support an embedded-node design. List nodes are now embedded directly into container structures, eliminating the need to manage list node lifecycles during pushback or remove operations. The lifecycle is instead owned by the container itself. This change updates existing list helpers to align with the embedded design and reduces unnecessary memory operations during list manipulation.
1 parent 5d0291f commit aafbb6c

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

include/lib/list.h

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
/* List node */
1616
typedef struct list_node {
1717
struct list_node *next;
18-
void *data;
1918
} list_node_t;
2019

2120
/* Public list descriptor */
@@ -45,10 +44,8 @@ static inline list_t *list_create(void)
4544
}
4645

4746
head->next = tail;
48-
head->data = NULL;
4947

5048
tail->next = NULL;
51-
tail->data = NULL;
5249

5350
list->head = head;
5451
list->tail = tail;
@@ -78,16 +75,11 @@ static inline list_node_t *list_cnext(const list_t *list,
7875

7976
/* Push and pop */
8077

81-
static inline list_node_t *list_pushback(list_t *list, void *data)
78+
static inline list_node_t *list_pushback(list_t *list, list_node_t *node)
8279
{
83-
if (unlikely(!list))
84-
return NULL;
85-
86-
list_node_t *node = malloc(sizeof(*node));
87-
if (unlikely(!node))
80+
if (unlikely(!list || !node || node->next))
8881
return NULL;
8982

90-
node->data = data;
9183
node->next = list->tail;
9284

9385
/* Insert before tail sentinel */
@@ -100,22 +92,21 @@ static inline list_node_t *list_pushback(list_t *list, void *data)
10092
return node;
10193
}
10294

103-
static inline void *list_pop(list_t *list)
95+
static inline list_node_t *list_pop(list_t *list)
10496
{
10597
if (unlikely(list_is_empty(list)))
10698
return NULL;
10799

108100
list_node_t *first = list->head->next;
109101
list->head->next = first->next;
102+
first->next = NULL;
110103

111-
void *data = first->data;
112-
free(first);
113104
list->length--;
114-
return data;
105+
return first;
115106
}
116107

117-
/* Remove a specific node; returns its data */
118-
static inline void *list_remove(list_t *list, list_node_t *target)
108+
/* Remove a specific node form the list */
109+
static inline list_node_t *list_remove(list_t *list, list_node_t *target)
119110
{
120111
if (unlikely(!list || !target || list_is_empty(list)))
121112
return NULL;
@@ -128,10 +119,9 @@ static inline void *list_remove(list_t *list, list_node_t *target)
128119
return NULL; /* node not found */
129120

130121
prev->next = target->next;
131-
void *data = target->data;
132-
free(target);
122+
target->next = NULL;
133123
list->length--;
134-
return data;
124+
return target;
135125
}
136126

137127
/* Iteration */

0 commit comments

Comments
 (0)