I have many data array defined in my code with the length only specified in the array initializer.
int a[] = {1, 2, 3, 4};
int b[] = {1, 2, 3, 4, 5};
For cache line coherence consideration, I want to padding them to cache line size (32). The following code can be used without scarifying the exact size information
struct {
int payload[4];
} __attribute__((aligned(32))) a = {
.payload = {1, 2, 3, 4}
};
struct {
int payload[5];
} __attribute__((aligned(32))) b = {
.payload = {1, 2, 3, 4, 5}
};
// sizeof(a.payload) == 4 * sizeof(int), sizeof(b.payload) == 5 * sizeof(int)
However, there are quite a bundle of such data, it is hard to manually rewrite them one by one. Is there a way to define a macro to handle this, like
#define ARRAY_INITIALIZER (array, initializer) ...
#define ARRAY_PAYLOAD(array) array.payload
ARRAY_INITIALIZER(a, {1, 2, 3, 4});
ARRAY_INITIALIZER(b, {1, 2, 3, 4, 5});
The most difficulty thing here is how to calculate the length of initializer here, don't get any direction for it, can anyone help here?
This initialization produces the aligned addresses either in C or C++:
#include <stdalign.h>
alignas(32) int a[] = {1, 2, 3, 4} ;
alignas(32) int b[] = {1, 2, 3, 4, 5};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With