Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Good C programming style

Reply
Thread Tools

Good C programming style

 
 
Skarmander
Guest
Posts: n/a
 
      10-11-2005
Chris Dollin wrote:
> Sensei wrote:
>
>
>>On 2005-10-10 15:31:40 +0200, Chris Dollin <> said:
>>
>>
>>>Sensei wrote:
>>>
>>>
>>>>So why I see so often something like:
>>>>
>>>>#define CEXCERPT do { \
>>>>some(); \
>>>>C_code(); \
>>>>here(); \
>>>>} while(0)
>>>>
>>>>Is it just some person thinking he would achieve more speed?
>>>
>>>No, it's some person arranging that their macro can be expanded
>>>as CEXCERPT;
>>>
>>>without confusion. (I would have thought, even more likely
>>>with a parameterised macro.)

>>
>>
>>I understood why he does not use a ; at the end, but why someone would
>>have such a big macro... is beyond my knowledge, something like the
>>xge_whatever stuff I found...

>
>
> How big is "big"?
>
> #define NEEDS(mill, n) \
> do { \
> if (vm->myHeap->available < (Size) (n)) \
> (FREEZE(), millRegenerate( mill ), MELT()); \
> } while (0)
>

#define INLINE inline

INLINE void needs(? mill, Size n) {
if (vm -> myHeap -> available < n) {
freeze();
millRegenerate(mill);
melt();
}
}

S.
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      10-11-2005
Skarmander wrote:

> Chris Dollin wrote:
>> Sensei wrote:
>>
>>
>>>On 2005-10-10 15:31:40 +0200, Chris Dollin <> said:
>>>
>>>
>>>>Sensei wrote:
>>>>
>>>>
>>>>>So why I see so often something like:
>>>>>
>>>>>#define CEXCERPT do { \
>>>>>some(); \
>>>>>C_code(); \
>>>>>here(); \
>>>>>} while(0)
>>>>>
>>>>>Is it just some person thinking he would achieve more speed?
>>>>
>>>>No, it's some person arranging that their macro can be expanded
>>>>as CEXCERPT;
>>>>
>>>>without confusion. (I would have thought, even more likely
>>>>with a parameterised macro.)
>>>
>>>
>>>I understood why he does not use a ; at the end, but why someone would
>>>have such a big macro... is beyond my knowledge, something like the
>>>xge_whatever stuff I found...

>>
>>
>> How big is "big"?
>>
>> #define NEEDS(mill, n) \
>> do { \
>> if (vm->myHeap->available < (Size) (n)) \
>> (FREEZE(), millRegenerate( mill ), MELT()); \
>> } while (0)
>>

> #define INLINE inline
>
> INLINE void needs(? mill, Size n) {
> if (vm -> myHeap -> available < n) {
> freeze();
> millRegenerate(mill);
> melt();
> }
> }


Won't work.

FREEZE() is a macro, MELT is a macro, both of them use variables
available where NEEDS is called - such as, for example, `vm`.

And `inline` wasn't routinely available when this code was written;
furthermore, failure to inline is likely to make the code run
significantly more slowly. Since this is part of the system core,
it would matter.

--
Chris "electric hedgehog" Dollin
"I know three kinds: hot, cool, and what-time-does-the-tune-start?"
 
Reply With Quote
 
 
 
 
Skarmander
Guest
Posts: n/a
 
      10-11-2005
Chris Dollin wrote:
> Skarmander wrote:
>
>
>>Chris Dollin wrote:
>>
>>>Sensei wrote:
>>>

<snip>
>>>>I understood why he does not use a ; at the end, but why someone would
>>>>have such a big macro... is beyond my knowledge, something like the
>>>>xge_whatever stuff I found...
>>>
>>>
>>>How big is "big"?
>>>
>>>#define NEEDS(mill, n) \
>>> do { \
>>> if (vm->myHeap->available < (Size) (n)) \
>>> (FREEZE(), millRegenerate( mill ), MELT()); \
>>> } while (0)
>>>

>>
>>#define INLINE inline
>>
>>INLINE void needs(? mill, Size n) {
>>if (vm -> myHeap -> available < n) {
>>freeze();
>>millRegenerate(mill);
>>melt();
>>}
>>}

>
>
> Won't work.
>
> FREEZE() is a macro, MELT is a macro, both of them use variables
> available where NEEDS is called - such as, for example, `vm`.


Pass as parameters or promote to global status. Probably the latter.
"Global" with respect to the smallest unit appropriate, of course.

> And `inline` wasn't routinely available when this code was written;
> furthermore, failure to inline is likely to make the code run
> significantly more slowly. Since this is part of the system core,
> it would matter.


Sure. If inlining really won't work and profiling shows you you do need
that performance, take the macro route.

S.
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      10-11-2005
Skarmander wrote:

> Chris Dollin wrote:
>> Skarmander wrote:
>>
>>
>>>Chris Dollin wrote:
>>>
>>>>Sensei wrote:
>>>>

> <snip>
>>>>>I understood why he does not use a ; at the end, but why someone would
>>>>>have such a big macro... is beyond my knowledge, something like the
>>>>>xge_whatever stuff I found...
>>>>
>>>>
>>>>How big is "big"?
>>>>
>>>>#define NEEDS(mill, n) \
>>>> do { \
>>>> if (vm->myHeap->available < (Size) (n)) \
>>>> (FREEZE(), millRegenerate( mill ), MELT()); \
>>>> } while (0)
>>>>
>>>
>>>#define INLINE inline
>>>
>>>INLINE void needs(? mill, Size n) {
>>>if (vm -> myHeap -> available < n) {
>>>freeze();
>>>millRegenerate(mill);
>>>melt();
>>>}
>>>}

>>
>>
>> Won't work.
>>
>> FREEZE() is a macro, MELT is a macro, both of them use variables
>> available where NEEDS is called - such as, for example, `vm`.

>
> Pass as parameters or promote to global status. Probably the latter.
> "Global" with respect to the smallest unit appropriate, of course.
>
>> And `inline` wasn't routinely available when this code was written;
>> furthermore, failure to inline is likely to make the code run
>> significantly more slowly. Since this is part of the system core,
>> it would matter.

>
> Sure. If inlining really won't work and profiling shows you you do need
> that performance, take the macro route.


Inlining really didn't work.

--
Chris "electric hedgehog" Dollin
Essare - to be. Essen - to be at Messe Essen for SPIEL 05.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Good idea or gimmick: Go-style OO-programming in C++ ? jeti789@web.de C++ 52 04-06-2013 03:30 AM
Good programming style Astley Le Jasper Python 5 09-15-2008 02:41 PM
good style guides for python-style documentation ? Fredrik Lundh Python 4 04-07-2006 06:19 AM
Could you tell me if this is good meta programming style? Vincent Foley Ruby 2 04-29-2005 10:31 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57