Skip to content

Commit b3c9272

Browse files
committed
Add am_timer_event_allocate()
1 parent 64e9463 commit b3c9272

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

libs/timer/timer.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,13 @@ void am_timer_tick(int domain) {
150150
}
151151
}
152152
}
153+
154+
struct am_event_timer *am_timer_event_allocate(int id, int size, int domain) {
155+
AM_ASSERT(size >= (int)sizeof(struct am_event_timer));
156+
struct am_event *e = am_event_allocate(id, size, /*margin=*/0);
157+
AM_DISABLE_WARNING(AM_W_CAST_ALIGN)
158+
struct am_event_timer *te = (struct am_event_timer *)e;
159+
AM_ENABLE_WARNING(AM_W_CAST_ALIGN)
160+
am_timer_event_ctor(te, id, domain);
161+
return te;
162+
}

libs/timer/timer.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,37 @@ AM_ASSERT_STATIC(AM_TICK_DOMAIN_MAX < (1U << AM_EVENT_TICK_DOMAIN_BITS));
8181

8282
/**
8383
* Timer constructor.
84+
*
8485
* @param cfg timer module configuration
8586
* The timer module makes an internal copy of the configuration.
8687
*/
8788
void am_timer_ctor(const struct am_timer_cfg *cfg);
8889

8990
/**
9091
* Timer event constructor.
92+
*
9193
* @param event the timer event to construct
9294
* @param id the timer event identifier
9395
* @param domain tick domain the event belongs to
9496
*/
9597
void am_timer_event_ctor(struct am_event_timer *event, int id, int domain);
9698

99+
/**
100+
* Allocate and construct timer event.
101+
* Cannot fail. Cannot be freed. Never garbage collected.
102+
* The returned timer event is fully constructed.
103+
* No need to call am_timer_event_ctor() for it.
104+
* Provides an alternative way to reserve memory for timer events in
105+
* addition to the static allocation in user code.
106+
* Allocation of timer event using this API is preferred as it
107+
* improves cache locality of timer event structures.
108+
*
109+
* @param id the timer event id
110+
* @param size the timer event size [bytes]
111+
* @param domain the clock domain [0-AM_TICK_DOMAIN_MAX[
112+
*/
113+
struct am_event_timer *am_timer_event_allocate(int id, int size, int domain);
114+
97115
/**
98116
* Tick timer.
99117
*
@@ -120,6 +138,7 @@ void am_timer_arm(
120138

121139
/**
122140
* Disarm timer.
141+
*
123142
* @param event the timer to disarm
124143
* @retval true the timer was armed
125144
* @retval false the timer was not armed
@@ -128,6 +147,7 @@ bool am_timer_disarm(struct am_event_timer *event);
128147

129148
/**
130149
* Check if timer is armed.
150+
*
131151
* @param event the timer to check
132152
* @retval true the timer is armed
133153
* @retval false the timer is not armed

0 commit comments

Comments
 (0)