Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2026-02-01 - Stack Allocation for Status Bar
**Learning:** `drawstatusbar` in `bar.c` is a hot path called frequently. It was using `malloc` for a string buffer (`stext`) that is globally bounded to 1024 bytes.
**Action:** Use stack allocation (`char buf[1024]`) for strings <= 1024 bytes to avoid malloc overhead and fragmentation. Add a fallback to `malloc` for safety.
16 changes: 11 additions & 5 deletions bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ int drawstatusbar(Monitor *m, int bh, char *stext) {
int cmdcounter;
short isCode = 0;
char *text;
char *p;
char *p = NULL;
char buf[1024];

len = strlen(stext) + 1;
if (!(text = (char *)malloc(sizeof(char) * len))) {
die("malloc");
if (len > sizeof(buf)) {
if (!(text = (char *)malloc(sizeof(char) * len))) {
die("malloc");
}
p = text;
} else {
text = buf;
}
p = text;
memcpy(text, stext, len);

/* compute width of the status text */
Expand Down Expand Up @@ -194,7 +199,8 @@ int drawstatusbar(Monitor *m, int bh, char *stext) {
}

drw_setscheme(drw, statusscheme);
free(p);
if (p)
free(p);

return ret;
}
Expand Down