Approach for statically allocating in a "common library" in embedded systems

Viewed 80

We have a common C library for several embedded systems, which all use static (i.e. compile time) allocation.

So, this means we usually have something which boils down to this (if we don't want to change the common source file):

#define TMR_MAX 100
int actual_number_of_timers;
SoftTimer softTimers[TMR_MAX];

In other words, to avoid malloc'ing, lots of space is actually wasted.

Or we can do this, which is to have a new set of enums for each project:

enum TimerType
{
    TMR_KEYBOARD = 0,
    TMR_LCD,
    ...,
    TMR_MAX
};

SoftTimer softTimers[TMR_MAX];

This other case doesn't waste space, but it means each project changes the (say) timer.h file in order to define its own constants. Btw, the timer example is just an example of the principle.

So is there a smarter way to have something like compile-time "just-the-right-size" allocation which will still allow us to have a common "frozen" codebase with separate tests?

3 Answers
Related