C preprocessor

Programming, for all ages and all languages.
Post Reply
zeusk
Posts: 16
Joined: Fri Sep 14, 2012 1:09 pm

C preprocessor

Post 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 ?
User avatar
iansjack
Member
Member
Posts: 4686
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: C preprocessor

Post by iansjack »

Isn't this what #ifdef/#endif sections are intended for?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: C preprocessor

Post 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.
zeusk
Posts: 16
Joined: Fri Sep 14, 2012 1:09 pm

Re: C preprocessor

Post 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.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: C preprocessor

Post 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.
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Re: C preprocessor

Post 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.
Post Reply