Skip to content

Commit 10593f6

Browse files
committed
Refactor timer helpers to support embedded list nodes
This change introduces helper macros to retrieve timer containers from embedded list nodes. Timer resources are now statically allocated from timer_pool, eliminating dynamic memory operations during timer usage. Timer allocation and release are tracked only via the timer pool bitmask. To avoid ambiguity, the suffix `_running_list` is added to explicitly indicate lists holding active timers.
1 parent 51d29a2 commit 10593f6

File tree

1 file changed

+29
-49
lines changed

1 file changed

+29
-49
lines changed

kernel/timer.c

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,27 @@ static struct {
3232
} timer_cache[4];
3333
static uint8_t timer_cache_index = 0;
3434

35-
/* Get a node from the pool, fall back to malloc if pool is empty */
36-
static list_node_t *get_timer_node(void)
35+
/* Get a timer from the pool */
36+
static timer_t *get_timer(void)
3737
{
3838
/* Find first free node in pool */
3939
for (int i = 0; i < TIMER_NODE_POOL_SIZE; i++) {
4040
if (pool_free_mask & (1 << i)) {
4141
pool_free_mask &= ~(1 << i);
42-
return &timer_node_pool[i];
42+
return &timer_pool[i];
4343
}
4444
}
45-
/* Pool exhausted, fall back to malloc */
46-
return malloc(sizeof(list_node_t));
45+
/* Pool exhausted */
46+
return NULL;
4747
}
4848

49-
/* Return a node to the pool, or free if it's not from pool */
50-
static void return_timer_node(list_node_t *node)
49+
/* Return the timer to the pool, mark only */
50+
static void return_timer(timer_t *timer)
5151
{
5252
/* Check if node is from our pool */
53-
if (node >= timer_node_pool &&
54-
node < timer_node_pool + TIMER_NODE_POOL_SIZE) {
55-
int index = node - timer_node_pool;
53+
if (timer >= timer_pool && timer < timer_pool + TIMER_NODE_POOL_SIZE) {
54+
int index = (timer - &timer_pool[0]);
5655
pool_free_mask |= (1 << index);
57-
} else {
58-
free(node);
5956
}
6057
}
6158

@@ -116,55 +113,40 @@ static int32_t timer_subsystem_init(void)
116113
return ERR_OK;
117114
}
118115

119-
/* Fast removal of timer from active list by data pointer */
120-
static void timer_remove_item_by_data(list_t *list, void *data)
116+
/* Fast removal of timer from active list */
117+
static void timer_remove_from_running_list(list_t *list, timer_t *t)
121118
{
122119
if (unlikely(!list || list_is_empty(list)))
123120
return;
124121

125-
list_node_t *prev = list->head;
126-
list_node_t *curr = prev->next;
127-
128-
while (curr != list->tail) {
129-
if (curr->data == data) {
130-
prev->next = curr->next;
131-
return_timer_node(curr);
132-
list->length--;
133-
return;
134-
}
135-
prev = curr;
136-
curr = curr->next;
137-
}
122+
list_remove(list, &t->t_running_node);
138123
}
139124

140-
/* Sorted insert with early termination for common cases */
141-
static int32_t timer_sorted_insert(timer_t *timer)
125+
/* Insert timer into the running timer list */
126+
static int32_t timer_sorted_insert_running_list(timer_t *timer)
142127
{
143-
list_node_t *new_node = get_timer_node();
144-
if (unlikely(!new_node))
128+
if (unlikely(!timer || timer->t_running_node.next))
145129
return ERR_FAIL;
146-
new_node->data = timer;
147-
148130
/* Fast path: if list is empty or timer should go at end */
149131
list_node_t *prev = kcb->timer_list->head;
150132
if (prev->next == kcb->timer_list->tail) {
151133
/* Empty list */
152-
new_node->next = kcb->timer_list->tail;
153-
prev->next = new_node;
134+
timer->t_running_node.next = kcb->timer_list->tail;
135+
prev->next = &timer->t_running_node;
154136
kcb->timer_list->length++;
155137
return ERR_OK;
156138
}
157139

158140
/* Find insertion point */
159141
while (prev->next != kcb->timer_list->tail) {
160-
timer_t *current_timer = (timer_t *) prev->next->data;
142+
timer_t *current_timer = timer_from_node(prev->next);
161143
if (timer->deadline_ticks < current_timer->deadline_ticks)
162144
break;
163145
prev = prev->next;
164146
}
165147

166-
new_node->next = prev->next;
167-
prev->next = new_node;
148+
timer->t_running_node.next = prev->next;
149+
prev->next = &timer->t_running_node;
168150
kcb->timer_list->length++;
169151
return ERR_OK;
170152
}
@@ -183,7 +165,7 @@ static timer_t *timer_find_by_id_fast(uint16_t id)
183165
/* Linear search for now - could be optimized to binary search if needed */
184166
list_node_t *node = all_timers_list->head->next;
185167
while (node != all_timers_list->tail) {
186-
timer_t *timer = (timer_t *) node->data;
168+
timer_t *timer = timer_from_node(node);
187169
if (timer->id == id) {
188170
cache_timer(id, timer);
189171
return timer;
@@ -204,7 +186,7 @@ static list_node_t *timer_find_node_by_id(uint16_t id)
204186

205187
list_node_t *node = all_timers_list->head->next;
206188
while (node != all_timers_list->tail) {
207-
if (((timer_t *) node->data)->id == id)
189+
if (timer_from_node(node)->id == id)
208190
return node;
209191
node = node->next;
210192
}
@@ -262,26 +244,24 @@ void _timer_tick_handler(void)
262244
}
263245

264246
/* Insert timer into sorted position in all_timers_list */
265-
static int32_t timer_insert_sorted_by_id(timer_t *timer)
247+
static void timer_insert_sorted_timer_list(timer_t *timer)
266248
{
267-
list_node_t *new_node = get_timer_node();
268-
if (unlikely(!new_node))
269-
return ERR_FAIL;
270-
new_node->data = timer;
249+
if (unlikely(!timer || timer->t_node.next))
250+
return;
271251

272252
/* Find insertion point to maintain ID sort order */
273253
list_node_t *prev = all_timers_list->head;
274254
while (prev->next != all_timers_list->tail) {
275-
timer_t *current = (timer_t *) prev->next->data;
255+
timer_t *current = timer_from_node(prev->next);
276256
if (timer->id < current->id)
277257
break;
278258
prev = prev->next;
279259
}
280260

281-
new_node->next = prev->next;
282-
prev->next = new_node;
261+
timer->t_node.next = prev->next;
262+
prev->next = &timer->t_node;
283263
all_timers_list->length++;
284-
return ERR_OK;
264+
return;
285265
}
286266

287267
int32_t mo_timer_create(void *(*callback)(void *arg),

0 commit comments

Comments
 (0)