8000 Reduce stack usage of several components (#1093) · uPesy/arduino-pico@be34ed1 · GitHub
[go: up one dir, main page]

Skip to content

Commit be34ed1

Browse files
Reduce stack usage of several components (earlephilhower#1093)
Only 4K total stack, so allocating 400 bytes for a local C string or 600 bytes for a DHCP response is dangerous. Use static allocations instead on the heap.
1 parent 444bb24 commit be34ed1

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

libraries/SingleFileDrive/src/SingleFileDrive.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,30 +245,29 @@ int32_t SingleFileDrive::read10(uint32_t lba, uint32_t offset, void* buffer, uin
245245
}
246246

247247
uint32_t toread = bufsize;
248-
char buff[512];
249248
uint8_t *curbuff = (uint8_t *)buffer;
250249

251250
while (bufsize > 0) {
252251
if (lba == 0) {
253-
bootSector(buff);
252+
bootSector(_sectBuff);
254253
} else if ((lba == 128) || (lba == 256)) {
255-
fatSector(buff);
254+
fatSector(_sectBuff);
256255
} else if (lba == 384) {
257-
directorySector(buff);
256+
directorySector(_sectBuff);
258257
} else if (lba >= 640) {
259258
File f = LittleFS.open(_localFile, "r");
260259
f.seek((lba - 640) * 512);
261-
f.read((uint8_t*)buff, 512);
260+
f.read((uint8_t*)_sectBuff, 512);
262261
f.close();
263262
} else {
264-
memset(buff, 0, sizeof(buff));
263+
memset(_sectBuff, 0, sizeof(_sectBuff));
265264
}
266265

267266
uint32_t cplen = 512 - offset;
268267
if (bufsize < cplen) {
269268
cplen = bufsize;
270269
}
271-
memcpy(curbuff, buff + offset, cplen);
270+
memcpy(curbuff, _sectBuff + offset, cplen);
272271
curbuff += cplen;
273272
offset = 0;
274273
lba++;

libraries/SingleFileDrive/src/SingleFileDrive.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class SingleFileDrive {
5050
char *_localFile = nullptr;
5151
char *_dosFile = nullptr;
5252

53+
char _sectBuff[512]; // Read sector region
54+
5355
void (*_cbDelete)(uint32_t) = nullptr;
5456
uint32_t _cbDeleteData = 0;
5557

libraries/WebServer/examples/AdvancedWebServer/AdvancedWebServer.ino

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <WiFiClient.h>
3333
#include <WebServer.h>
3434
#include <LEAmDNS.h>
35+
#include <StreamString.h>
3536

3637
#ifndef STASSID
3738
#define STASSID "your-ssid"
@@ -48,14 +49,13 @@ const int led = LED_BUILTIN;
4849
void handleRoot() {
4950
static int cnt = 0;
5051
digitalWrite(led, 1);
51-
char temp[400];
5252
int sec = millis() / 1000;
5353
int min = sec / 60;
5454
int hr = min / 60;
5555

56-
snprintf(temp, 400,
57-
58-
"<html>\
56+
StreamString temp;
57+
temp.reserve(500); // Preallocate a large chunk to avoid memory fragmentation
58+
temp.printf("<html>\
5959
<head>\
6060
<meta http-equiv='refresh' content='5'/>\
6161
<title>Pico-W Demo</title>\
@@ -70,9 +70,7 @@ void handleRoot() {
7070
<p>Page Count: %d</p>\
7171
<img src=\"/test.svg\" />\
7272
</body>\
73-
</html>",
74-
75-
hr, min % 60, sec % 60, rp2040.getFreeHeap(), ++cnt);
73+
</html>", hr, min % 60, sec % 60, rp2040.getFreeHeap(), ++cnt);
7674
server.send(200, "text/html", temp);
7775
digitalWrite(led, 0);
7876
}

libraries/WiFi/src/dhcpserver/dhcpserver.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ static void dhcp_server_process(void *arg, struct udp_pcb *upcb, struct pbuf *p,
184184
(void)src_addr;
185185
(void)src_port;
186186

187-
// This is around 548 bytes
188-
dhcp_msg_t dhcp_msg;
187+
// This is around 548 bytes, so make static so it will live in the heap, not the stack (too large)
188+
static dhcp_msg_t dhcp_msg;
189189

190190
#define DHCP_MIN_SIZE (240 + 3)
191191
if (p->tot_len < DHCP_MIN_SIZE) {

0 commit comments

Comments
 (0)
0