I'm trying to optimize boot-time and run-time of an embedded application, and to do that, I'd like to initialize some constant flexible array members at compile time. I've found a few posts that say you must use malloc, but theoretically, it should be possible to do this without...
As an example, say I have:
typedef struct _foo_t {
foo_t *next;
int num_bars;
bar_t bars[];
} foo_t __attribute((__packed__));
And I have literally have several million instances of foo_t. I then have a script to generate a header file with all the information. So I might have something like this:
const foo_t foo1 = {.next = &foo2, .num_bars = 2};
const bar_t foo1_bar1 = {...};
const bar_t foo1_bar2 = {...};
foo_t *const first_foo = &foo1;
But, the problem with this is that the compiler spec does not guarantee that &foo1_bar1
is not guaranteed to be at &foo1.bars[0]
. I'm wondering if there's any trickery anyone knows to force the fooX_barY members to be placed in the correct locations in memory.
Note that my goals are:
- reduce boot time by avoiding unnecessary mallocs
- reduce memory thrashing by having the bar's typically in the same cache pages as corresponding foos
- reduce memory requirements by not having pointers to bars
If anyone knows of any good tricks to do this, I'd love to hear them