The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.
One of the problems with stack allocation is on platforms like the ESP32 and most of the arduinos, they don't give you a lot of stack space. I know usually one grows up and the other grows down but I run out of stack declaring 2kB blocks sometimes so there might be some kind of artificial limit.
I see you're using both #pragma once and #ifdef include guards. Is that really necessary? GCC supports both, going back to at least version 4.8, so the #pragma doesn't even need to be wrapped in an #ifdef _MSC_VER. But maybe you know something I don't, or maybe you're using some other compiler that doesn't understand the #pragma?
Also, picking nits, since I have nothing better to offer, I see that this is a memory pool for contrained memory environments. That must be a constrained, contained memory situation, correct?
1. Identifiers starting with underscores are reserved. If you use them then your program is non-conforming for no good reason.
2. The comparison against capacity in both the static and dynamic classes result in never being able to use the last byte of the pool: The "used()>=capacity" should be "used()>capacity". To test it instantiate a pool of 10 bytes and allocate 6. The (capacity() - used()) is then 4, but a further allocation of 4 fails. A further allocation of 3, on the other hand, succeeds and (capacity() - used()) is then 1.
3. The static pool could benefit from a #warning directive when C is too large. Right now a 8MB C when instantiating it (1024 * 1024 * 8) would almost certainly overflow the stack, and 8MB is not a lot of memory.
1. I thought they were only reserved for globals. I stand corrected.
2. Good catch.
3. The static pool can and often is declared as a global, making it heap/not stack, which is where it's primarily designed to go. DynamicMemoryPool is probably a better choice if you need a locally scoped pool because it always allocates from the heap.
1) forgive me for asking, but why? edit: whoops that was an error. if anything it was supposed to be inside the file's namespace
2) MemoryPool is an interface - a pure abstract base. what is the purpose of a virtual destructor in such a contract as it holds no resources? - never mind. I was thinking about the call chain backwards. derived classes need to have their destructor called if the base goes out of scope. i forgot. I'm rusty.
3) I'm targeting C++11 for now because reasons having to do with the platforms this is primarily for.
1) you don't want to introduce bunch of names from std into client's scope, can cause all kind of nasty problems for users. C++ name lookup is complex as it is.
2) if I got a pointer to MemoryPool and tried to delete the referenced object, I would invoke undefined behavior, even thought virtual methods are strongly suggesting me that I should be able to do it
Some more points, since you said C++ is love:
4) virtual void* alloc(const size_t size)=0; - const is needless
5) if(!TCapacity) will give you a warning (on /W4 maybe) if TCapacity is 0, but
6) the bigger problem is uint8_t m_heap[TCapacity], since zero-sized arrays is not standard C++
So I would either go with static_assert and ensure that 0 is not valid value or make specialization for that cas.
Last Visit: 31-Dec-99 18:00 Last Update: 16-Jun-21 4:32