Skip to content

Commit 52f9dcf

Browse files
committed
Reduce flash use.
1 parent 2812b98 commit 52f9dcf

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

cores/esp8266/Schedule.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ struct recurrent_fn_t
2828
recurrent_fn_t(esp8266::polledTimeout::periodicFastUs interval) : callNow(interval) { }
2929
};
3030

31-
static recurrent_fn_t rFirst(0);
32-
static recurrent_fn_t* rLast = &rFirst;
31+
static recurrent_fn_t* rFirst = nullptr;
32+
static recurrent_fn_t* rLast = nullptr;
3333

3434
// Returns a pointer to an unused sched_fn_t,
3535
// or if none are available allocates a new one,
3636
// or nullptr if limit is reached
3737
IRAM_ATTR // called from ISR
38-
static scheduled_fn_t* get_fn_unsafe ()
38+
static scheduled_fn_t* get_fn_unsafe()
3939
{
4040
scheduled_fn_t* result = nullptr;
4141
// try to get an item from unused items list
@@ -54,15 +54,15 @@ static scheduled_fn_t* get_fn_unsafe ()
5454
return result;
5555
}
5656

57-
static void recycle_fn_unsafe (scheduled_fn_t* fn)
57+
static void recycle_fn_unsafe(scheduled_fn_t* fn)
5858
{
5959
fn->mFunc = nullptr; // special overload in c++ std lib
6060
fn->mNext = sUnused;
6161
sUnused = fn;
6262
}
6363

6464
IRAM_ATTR // (not only) called from ISR
65-
bool schedule_function (const std::function<void(void)>& fn)
65+
bool schedule_function(const std::function<void(void)>& fn)
6666
{
6767
if (!fn)
6868
return false;
@@ -98,18 +98,24 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
9898
return false;
9999

100100
item->mFunc = fn;
101-
item->mNext = nullptr;
102101
item->alarm = std::move(alarm);
103102

104103
esp8266::InterruptLock lockAllInterruptsInThisScope;
105104

106-
rLast->mNext = item;
105+
if (rLast)
106+
{
107+
rLast->mNext = item;
108+
}
109+
else
110+
{
111+
rFirst = item;
112+
}
107113
rLast = item;
108114

109115
return true;
110116
}
111117

112-
void run_scheduled_functions ()
118+
void run_scheduled_functions()
113119
{
114120
esp8266::polledTimeout::periodicFastMs yieldNow(100); // yield every 100ms
115121

@@ -147,7 +153,7 @@ void run_scheduled_recurrent_functions()
147153
// its purpose is that it is never called from an interrupt
148154
// (always on cont stack).
149155

150-
auto current = rFirst.mNext;
156+
auto current = rFirst;
151157
if (!current)
152158
return;
153159

@@ -164,7 +170,7 @@ void run_scheduled_recurrent_functions()
164170
fence = true;
165171
}
166172

167-
auto prev = &rFirst;
173+
recurrent_fn_t* prev = nullptr;
168174
// prevent scheduling of new functions during this run
169175
auto stop = rLast;
170176

@@ -187,7 +193,14 @@ void run_scheduled_recurrent_functions()
187193
rLast = prev;
188194

189195
current = current->mNext;
190-
prev->mNext = current;
196+
if (prev)
197+
{
198+
prev->mNext = current;
199+
}
200+
else
201+
{
202+
rFirst = current;
203+
}
191204

192205
delete(to_ditch);
193206
}

0 commit comments

Comments
 (0)