Skip to content
Draft
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-01-08 - Stack Allocation in Hot Path
**Learning:** In C window managers, status bars are redrawn very frequently (every second or more). Allocating strings on the heap (`malloc`) for temporary buffers in these hot paths adds unnecessary overhead and fragmentation.
**Action:** For bounded strings (like status text which is often limited to 1024 bytes), use stack allocation. It's faster, safer (no memory leaks), and removes failure paths (`die("malloc")`). Always check bounds with `strncpy`.
12 changes: 4 additions & 8 deletions bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,15 @@ int drawstatusbar(Monitor *m, int bh, char *stext) {
int i;
int w;
int x;
int len;
int cmdcounter;
short isCode = 0;
char *text;
char text_buf[1024];
char *text = text_buf;
char *p;

len = strlen(stext) + 1;
if (!(text = (char *)malloc(sizeof(char) * len))) {
die("malloc");
}
strncpy(text, stext, sizeof(text_buf));
text[sizeof(text_buf) - 1] = '\0';
p = text;
memcpy(text, stext, len);

/* compute width of the status text */
w = 0;
Expand Down Expand Up @@ -194,7 +191,6 @@ int drawstatusbar(Monitor *m, int bh, char *stext) {
}

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

return ret;
}
Expand Down