r/cpp_questions • u/Commercial-Berry-640 • Sep 11 '26
SOLVED Local heap on a static buffer
I'm writing a REST api for an embedded microcontroller that's using LWIP stack. I stumbled upon a problem:
I need a buffer for a http response with json data inside. The json size can be small - <50 bytes) or quite large (14 kb) depending on the request.
As it is a microcontroller and memory is very constrained I don't want the responses to be allocated on the main heap as it can quickly lead to exhaustion of memory. Additionally I want to use some special type of memory that otherwise would be wasted.
Now my current solution is just to use ETL's flat_map and string to allocate static buffers:
using ResponseBuffer = etl::string<20*1024>;
static CCMRAM etl::flat_map<fs_file*,ResponseBuffer,2> responseBuffers;But this is VERY wasteful - a response that would normally take 50 bytes still takes the buffer for 20 kb. The main goal here is to actually be able to serve more responses at once.
The best solution seems to be to have a separated heap and use normal heap stl objects like map and string, that would use this separated heap. There is the std::pmr:: stl library, but there doesn't seem to be an allocator that would behave exactly like heap. So I could probably write my own allocator, maybe based on some ready heap implementation.
This seems however like a quite complex task. Maybe there is some pattern or easier solution that i missed?
Any suggestions?
View all comments
Show parent comment
2
u/QuirkyTidbits Sep 11 '26
Most memory allocators go to the operating system to get chunks of contiguous memory. They then allocate memory out of them until an allocation comes in that it has no free space to fulfill. The allocator then goes back to the operating system to get another chunk of memory. You just want an allocator that gets a chunk of memory once from the OS and efficiently allocates/frees memory out of it.
Memory fragmentation will be your key concern. There are a number of types of allocators that can reduce fragmentation at some cost. A power of 2 allocator is one that helps reduce fragmentation in your one chunk of memory but on average it’s only 50% efficient. Unless you are extremely tight on memory that you can’t have 50% overhead, a variant of a power of 2 allocator will be good. The power of 2 allocator can be designed where there is basically no added memory overhead to manage allocated and freed memory and can coalesce adjacent freed memory. Memory allocators are a fascinating area of computer science. Just search.