Page 1 of 1
C preprocessor
Posted: Thu Sep 27, 2012 10:11 pm
by zeusk
In my OS, SPINLOCK_DECLARE depends on whether the build is SMP or not, when it's for UP, the macro expands to nothing so if i have something like
Code: Select all
static SPINLOCK_DECLARE(blah_blah);
Compiler gives me an error about expecting an identifier/type before ';' Is there a better way to fix this besides having to write a new macro like DECLARE_STATIC_SPINLOCK ?
Re: C preprocessor
Posted: Fri Sep 28, 2012 1:57 am
by iansjack
Isn't this what #ifdef/#endif sections are intended for?
Re: C preprocessor
Posted: Fri Sep 28, 2012 2:06 am
by bluemoon
zeusk wrote:Is there a better way to fix this besides having to write a new macro like DECLARE_STATIC_SPINLOCK ?
Yes, do not use macro, and properly define a data-type for it, and you get type-checking for free.
Code: Select all
typedef volatile uint32_t _SPINLOCK __attribute__ ((aligned(16)));
And you macro the function to {empty} instead - but wait, even for single processor a normal spinlock is
not empty.
EDIT: Usually, the multi-processor version runs just fine on uni-processor, and unless you care that extra nano-second you may just use MP version and reduce maintanence effort.
Re: C preprocessor
Posted: Fri Sep 28, 2012 2:57 am
by zeusk
bluemoon wrote:
Yes, do not use macro, and properly define a data-type for it, and you get type-checking for free.
Code: Select all
typedef volatile uint32_t _SPINLOCK __attribute__ ((aligned(16)));
And you macro the function to {empty} instead - but wait, even for single processor a normal spinlock is
not empty.
EDIT: Usually, the multi-processor version runs just fine on uni-processor, and unless you care that extra nano-second you may just use MP version and reduce maintanence effort.
I'm actually working on an embedded ARM kernel, which will run on high-end devices (1GHz QSD8650b with 576M Ram) to low-end devices (48MHz Cortex-M0 with 8K RAM, besides no MMU ... Good old 90s). I cannot waste those precious 4 bytes.
Re: C preprocessor
Posted: Fri Sep 28, 2012 4:27 am
by JamesM
zeusk wrote:In my OS, SPINLOCK_DECLARE depends on whether the build is SMP or not, when it's for UP, the macro expands to nothing so if i have something like
Code: Select all
static SPINLOCK_DECLARE(blah_blah);
Compiler gives me an error about expecting an identifier/type before ';' Is there a better way to fix this besides having to write a new macro like DECLARE_STATIC_SPINLOCK ?
Having a separate declarator for static spinlocks seems like the only real way to me.
Re: C preprocessor
Posted: Sat Sep 29, 2012 5:53 am
by evoex
I reckon this would be optimized away completely by most compilers (actually, I'm not sure how any compiler would make it do anything), and should work for both static and non-static:
Code: Select all
#define SPINLOCK_DECLARE(x) struct {} x
Hope that helps.