Skip to content

Commit 51d29a2

Browse files
committed
Refactor timer management to use embedded list nodes
Previously, timer management relied on list nodes provided by timer_node_pool. Timer creation and cancellation could trigger allocation or deallocation of timer or list nodes when no pool entries were available. This change pre-allocates timers and embeds list nodes directly within timer structures, reducing memory operations during timer lifecycle transitions and simplifying timer management.
1 parent aafbb6c commit 51d29a2

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

include/sys/timer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ typedef struct {
3939
/* Callback Configuration */
4040
void *(*callback)(void *arg); /* Function to execute upon timer expiry */
4141
void *arg; /* User-defined argument passed to callback */
42+
43+
/* Embedded node for timer */
44+
list_node_t t_node; /* All timer list node*/
45+
list_node_t t_running_node; /* Running timer list node */
4246
} timer_t;
4347

4448
/* Timer Management Functions */

kernel/timer.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
#include "private/error.h"
1717
#include "private/utils.h"
1818

19-
/* Pre-allocated node pool for reduced malloc/free overhead */
19+
/* Pre-allocated timer pool for reduced malloc/free overhead */
2020
#define TIMER_NODE_POOL_SIZE 16
21-
static list_node_t timer_node_pool[TIMER_NODE_POOL_SIZE];
22-
static uint16_t pool_free_mask = 0xFFFF; /* Bitmask for free nodes */
21+
static timer_t timer_pool[TIMER_NODE_POOL_SIZE];
22+
static uint16_t pool_free_mask = 0xFFFF; /* Bitmask for free timers */
2323

2424
/* Master list of all created timers, kept sorted by ID for faster lookup */
2525
static list_t *all_timers_list = NULL;
@@ -105,10 +105,10 @@ static int32_t timer_subsystem_init(void)
105105
return ERR_FAIL;
106106
}
107107

108-
/* Initialize node pool */
108+
/* Initialize timer pool */
109+
109110
for (int i = 0; i < TIMER_NODE_POOL_SIZE; i++) {
110-
timer_node_pool[i].data = NULL;
111-
timer_node_pool[i].next = NULL;
111+
memset(&timer_pool[i], 0, sizeof(timer_t));
112112
}
113113

114114
timer_initialized = true;

0 commit comments

Comments
 (0)