[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] C Preprocessor question
From: |
Richard Urwin |
Subject: |
Re: [avr-gcc-list] C Preprocessor question |
Date: |
Sun, 11 Sep 2005 11:04:00 +0100 |
User-agent: |
KMail/1.7.2 |
On Sunday 11 Sep 2005 01:17, Larry Barello wrote:
> Can I define a macro that defines some stuff? I cannot get the
> following to work
>
> #define mymacro(foo, somenumber) \
> #define foo (*(pFifo)foo##bar) \
> Static uint8_t foo##bar[somenumber + sizeof(Fifo)]
>
> The goal (maybe there is a better way...) is to have a variable
> length buffer with some defined structure in the front. A pointer to
> the struct is passed around various routines.
>
> I could use a union:
>
> #define mymacro(foo, somenumber) \
> Union {uint8_t buf[somenumber + sizeof(Fifo)]; Fifo fifo;} foo
>
> And the user would have to use "&foo.fifo" as the reference, but I
> would prefer to just type in "&foo" when calling routines.
What is wrong with:
#define mymacro (foo, somenumber) \
struct {Fifo fifo; uint8_t buf[somenumber]} foo;
Or even use C++ templates.
What I tend to use is
struct {
uint_8 *In, *Out, buf[0];
} foo
and then malloc(buffersize+sizeof(foo))
but that doesn't work for static variables of course. (and [0] is gcc
specific)
I have also seen
#define FOO foo.fifo
But, IMHO, that's ugly and hard to maintain.
The C preprocessor is only single pass, which is why your code does not
work. You could fiddle with your make file to call the preprocessor
twice, and your code should then work.
Another possibility to play with:
#define mymacro (foo, somenumber) \
struct {Fifo fifo; uint8_t buf[somenumber]} foo;\
const uint8_t *foo##ptr = (uint8_t*)&foo;
but I haven't tested that, I don't know the ## syntax, and it depends on
gcc optimising out the constant.
--
Richard Urwin