I would like to do this with a macro:
typedef struct _TIMER_llist {
struct _TIMER_llist *next;
uint32_t time;
const int id;
} TIMER_llist;
TIMER_llist _timer_llists[] =
{
{ .id = 1, .next = &_timer_llists[1] },
{ .id = 2, .next = &_timer_llists[2] },
{ .id = 3, .next = &_timer_llists[3] },
{ .id = 4, .next = &_timer_llists[4] },
{ .id = 5, .next = &_timer_llists[5] },
{ .id = 6, .next = &_timer_llists[6] },
{ .id = 7, .next = &_timer_llists[7] },
{ .id = 8, .next = &_timer_llists[8] },
{ .id = 9, .next = &_timer_llists[9] },
{ .id = 10, .next = &_timer_llists[10] },
{ .id = 11, .next = &_timer_llists[11] },
{ .id = 12, .next = &_timer_llists[12] },
{ .id = 13, .next = 0 } };
//This won't work, because const .id
TIMER_llist _timer_llists[1];
void init() {
_timer_llists[0].id = 1;
_timer_llists[0].next = 0;
}
Instead of writing a line for each buffer entry I would like to use a
#define NUMBER_ENTRIES 13
as this gets really unhandy, if I want to do 64 entries or so...
Unfortunatly I don't have any experience with the C preprocessor, so I'm curious if:
- there is a better non macro solution
- there is an easy way with gcc to accomplish this macro operation
Besides, and that is more a theoretical aspect, is execution speed. AfaIk the example in the first post would be just a memcpy(.data[?], _timer_llists, sizeof(_timer_llists)), while a programmatical approach would require at least a counter, maybe an if statement for the last case, and a function overhead (ok, maybe inline). On the other hand this would save space in .data.
In this particular case I am using gcc-avr, but this question came to my mind repeatedly and I would like to have an universal approach.