diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..47158ea --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-01-19 - [Stack Allocation in drawstatusbar] +**Learning:** `drawstatusbar` in `bar.c` uses `malloc` for a temporary string buffer on every redraw. This is a hot path. The source string `stext` is a global fixed-size buffer of 1024 bytes. +**Action:** Replace `malloc` with a stack buffer `char text[1024]` to avoid heap allocation overhead and potential fragmentation. This is safe because the input size is bounded by the global `stext` size. diff --git a/bar.c b/bar.c index 8476144..37eec5e 100644 --- a/bar.c +++ b/bar.c @@ -53,15 +53,16 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { int len; int cmdcounter; short isCode = 0; - char *text; - char *p; + char text_buf[1024]; + char *text = text_buf; + char *p = text_buf; len = strlen(stext) + 1; - if (!(text = (char *)malloc(sizeof(char) * len))) { - die("malloc"); + if (len > sizeof(text_buf)) { + len = sizeof(text_buf); } - p = text; memcpy(text, stext, len); + text[sizeof(text_buf) - 1] = '\0'; /* compute width of the status text */ w = 0; @@ -194,7 +195,6 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } drw_setscheme(drw, statusscheme); - free(p); return ret; }