Toolbox essentials

Well, I was reading ‘5 more essentials for your programming toolbox‘ and the first entry has my favourite – unfolded linked lists. It’s just that it’s broken in at least one way. You should embed the list of ‘void *’ pointers at the end of the entry using the magic of the zero length array at the end.

struct unrolled_listitem {
int num_elements;
struct unrolled_listitem *next;
void *pointers[0];
}

You get the size of a struct item from (sizeof(struct unrolled_listitem) + num_elements * sizeof(void *)). You need to min/max it to the size of a cache line.
The issue is that if you want to embed this structure in another one, you need to add padding after it to the max of the size of the structure that you are planning on allocating.