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?
__attribute__((aligned(32)))be applied to the array as a single variable, but not a structure field?sizeofcan still get the actual size without the end paddingalignedto variable will only make it start aligned, say a array of 5 is start align on 32, the left 27 bytes are still available for other variables to allocate, we want it occupies the full 32 byte to avoid cache coherence issue