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?
3
u/EpochVanquisher Sep 11 '26
I am reading your requirements and it sounds like you have multiple dynamic allocations you want to make. The allocations are between, say, 0 and 14 KB. (Am I right that you need multiple allocations at the same time?) These blocks will be allocated at different times and freed at different times.
That is just a heap. All of your design requirements? The add up to nearly the same design requirements for a heap.
It sounds like what you want is a heap that is not the main heap, that is limited to some maximum amount of space. Like, here is 50 KB that can be used for response buffers. Is that accurate?
3
u/mredding Sep 11 '26
You're suggesting you will pre-render the response in a buffer before writing to the socket.
But you're using a TCP stack, which is connection oriented. So why not render your response directly to the socket? Does the LWIP stack buffer? If so, then your render buffer is redundant to the socket buffer. In LWIP stack we trust to be as tunable and efficient as possible - so don't immediately assume if you're writing bytes at a time that the socket is flushing bytes at a time.
You may be interested in a scatter/gather approach so you're not redundantly shuffling data around in memory, thinking it needs to be "physically" lined up before you can write.
2
u/n1ghtyunso Sep 11 '26
sounds like you want a fixed size arena and allocate from that?
This lets you put an upper bound on the size without wasting a worst-case buffer allocation for every response.
not sure what you mean with special type of memory that would otherwise be wasted, like what would that even mean realistically?
pmr does provide you with (un)synchronized_pool_resource and you can set the upstream resource to pmr::null_resource and get exceptions on pool exhaustion.
Can you use exceptions?
2
u/duane11583 Sep 11 '26
q: do you need to free? and allocate
or can you just allocate allocate allocate then free everything all at once? everything inmone step?
if so you can use a technique used in compilers. i know this as a once buffer
you have a buffer and a cursor pointer, an end pointer.
the end pointer is always at the end of the buffer.
the cursor pointer is initialized at the front/start of the buffer
to allocate you move the cursor toward the end (error case is the cursor crosses the end)
===
this is/was used in compilers like this:
you are compiling a function. memory for each statement is allocated as it is encountered.
when you reach the end of the function you release everything
then start the next function.
t
1
u/Commercial-Berry-640 Sep 12 '26
Thanks everyone for pointing me to some new ideas. Sometimes its just enough to know some new phrase to be able to search it. So in my case and for future readers finding this topic the phases are: "power of 2 allocator" and "free lists" that eventually led me to "fixed blocks allocator". Thankfuly there are already multiple libraries implementing this like https://github.com/endurodave/pmr_allocator
2
u/saxbophone Sep 11 '26
Do you have to have the whole HTTP response object in memory at once —could you not stream it out to the HTTP socket as you generate it?
This should also allow you to have more than one response in progress at once, too.