kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* Linux-wireless: why use macro to call functions
@ 2013-12-23  6:05 Fred Chou
  2013-12-23  7:25 ` Daniel Baluta
  0 siblings, 1 reply; 8+ messages in thread
From: Fred Chou @ 2013-12-23  6:05 UTC (permalink / raw)
  To: kernelnewbies

Hi,

I was studying the Linux wireless subsystem code and noticed this code (in
ieee80211_rx_handlers):

It first defines the macro:

#define CALL_RXH(rxh) \
 do {                            \
             res = rxh(rx);          \
             if (res != RX_CONTINUE) \
                     goto rxh_next;  \
     } while (0);

Then the macro is used to call a series of functions:

CALL_RXH(ieee80211_rx_h_check_more_data)
CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll)
CALL_RXH(ieee80211_rx_h_sta_process)
CALL_RXH(ieee80211_rx_h_decrypt)
CALL_RXH(ieee80211_rx_h_defragment)
CALL_RXH(ieee80211_rx_h_michael_mic_verify)


My question is, why not just call the functions directly like:

ieee80211_rx_h_check_more_data(rx);
ieee80211_rx_h_uapsd_and_pspoll(rx);

...

Is it just for the sake of outlining the code for easy reading? Appreciate
if anyone can explain. Thanks!


Regards,
Fred
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20131223/5bc84a1b/attachment.html 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Linux-wireless: why use macro to call functions
  2013-12-23  6:05 Fred Chou
@ 2013-12-23  7:25 ` Daniel Baluta
  0 siblings, 0 replies; 8+ messages in thread
From: Daniel Baluta @ 2013-12-23  7:25 UTC (permalink / raw)
  To: kernelnewbies

On Mon, Dec 23, 2013 at 8:05 AM, Fred Chou <fred.chou.nd@gmail.com> wrote:
> Hi,
>
> I was studying the Linux wireless subsystem code and noticed this code (in
> ieee80211_rx_handlers):
>
> It first defines the macro:
>
> #define CALL_RXH(rxh) \
>  do {                            \
>              res = rxh(rx);          \
>              if (res != RX_CONTINUE) \
>                      goto rxh_next;  \
>      } while (0);
>
> Then the macro is used to call a series of functions:
>
> CALL_RXH(ieee80211_rx_h_check_more_data)
> CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll)
> CALL_RXH(ieee80211_rx_h_sta_process)
> CALL_RXH(ieee80211_rx_h_decrypt)
> CALL_RXH(ieee80211_rx_h_defragment)
> CALL_RXH(ieee80211_rx_h_michael_mic_verify)
>
>
> My question is, why not just call the functions directly like:
>
> ieee80211_rx_h_check_more_data(rx);
> ieee80211_rx_h_uapsd_and_pspoll(rx);
>
> ...
>
> Is it just for the sake of outlining the code for easy reading? Appreciate
> if anyone can explain. Thanks!

Hi,

As you can see above the macro does also error checking.I don't necessarily
think that the code is easier to read, but it can save you some typing.

thanks,
Daniel.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Linux-wireless: why use macro to call functions
@ 2013-12-23 10:23 Martin Brugnara
  2013-12-23 12:03 ` Bernd Petrovitsch
  0 siblings, 1 reply; 8+ messages in thread
From: Martin Brugnara @ 2013-12-23 10:23 UTC (permalink / raw)
  To: kernelnewbies

Hi,

Why here is used a macro instead of an inline func.

Thanks,
Martin

-----Original Message-----
From: "Daniel Baluta" <daniel.baluta@gmail.com>
Sent: ?23/?12/?2013 08.27
To: "Fred Chou" <fred.chou.nd@gmail.com>
Cc: "kernelnewbies" <kernelnewbies@kernelnewbies.org>
Subject: Re: Linux-wireless: why use macro to call functions

On Mon, Dec 23, 2013 at 8:05 AM, Fred Chou <fred.chou.nd@gmail.com> wrote:
> Hi,
>
> I was studying the Linux wireless subsystem code and noticed this code (in
> ieee80211_rx_handlers):
>
> It first defines the macro:
>
> #define CALL_RXH(rxh) \
>  do {                            \
>              res = rxh(rx);          \
>              if (res != RX_CONTINUE) \
>                      goto rxh_next;  \
>      } while (0);
>
> Then the macro is used to call a series of functions:
>
> CALL_RXH(ieee80211_rx_h_check_more_data)
> CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll)
> CALL_RXH(ieee80211_rx_h_sta_process)
> CALL_RXH(ieee80211_rx_h_decrypt)
> CALL_RXH(ieee80211_rx_h_defragment)
> CALL_RXH(ieee80211_rx_h_michael_mic_verify)
>
>
> My question is, why not just call the functions directly like:
>
> ieee80211_rx_h_check_more_data(rx);
> ieee80211_rx_h_uapsd_and_pspoll(rx);
>
> ...
>
> Is it just for the sake of outlining the code for easy reading? Appreciate
> if anyone can explain. Thanks!

Hi,

As you can see above the macro does also error checking.I don't necessarily
think that the code is easier to read, but it can save you some typing.

thanks,
Daniel.

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20131223/49f72ac0/attachment.html 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Linux-wireless: why use macro to call functions
  2013-12-23 10:23 Linux-wireless: why use macro to call functions Martin Brugnara
@ 2013-12-23 12:03 ` Bernd Petrovitsch
  2013-12-24 12:19   ` Fred Chou
  0 siblings, 1 reply; 8+ messages in thread
From: Bernd Petrovitsch @ 2013-12-23 12:03 UTC (permalink / raw)
  To: kernelnewbies

Hi!

On Mon, 2013-12-23 at 11:23 +0100, Martin Brugnara wrote:
[...]
> Why here is used a macro instead of an inline func.

Maybe you should get a book to learn "C"?
The macro uses "goto" which won't work with inline functions in that
situation.
Since the macro uses the only argument exactly once, it actually makes
no functional/semantic difference if it is a macro or would be an inline
function (and performance-wise very probably too with any somewhat
recent compiler).
And the macro just avoids to copy-paste identical lines/logic.

And you surely should learn to quote correctly and thus avoid top
posting completely.

[... fullquote deleted ...]

	Bernd
-- 
Bernd Petrovitsch                  Email : bernd at petrovitsch.priv.at
                     LUGA : http://www.luga.at

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Linux-wireless: why use macro to call functions
  2013-12-23 12:03 ` Bernd Petrovitsch
@ 2013-12-24 12:19   ` Fred Chou
  2013-12-24 13:35     ` Greg Freemyer
  2013-12-24 13:48     ` Adrian Ratnapala
  0 siblings, 2 replies; 8+ messages in thread
From: Fred Chou @ 2013-12-24 12:19 UTC (permalink / raw)
  To: kernelnewbies

Hi,

On Mon, Dec 23, 2013 at 8:03 PM, Bernd Petrovitsch <
bernd@petrovitsch.priv.at> wrote:
[...]
> And the macro just avoids to copy-paste identical lines/logic.

Thank you all for the explanation. Originally I was wondering if there
could be some other reasons. So the main (or only?) purpose of the macro is
to save some typing, and in terms of functionality I could equivalently
call each handlers.

Fred
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20131224/5ab5b544/attachment.html 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Linux-wireless: why use macro to call functions
  2013-12-24 12:19   ` Fred Chou
@ 2013-12-24 13:35     ` Greg Freemyer
  2013-12-24 13:48     ` Adrian Ratnapala
  1 sibling, 0 replies; 8+ messages in thread
From: Greg Freemyer @ 2013-12-24 13:35 UTC (permalink / raw)
  To: kernelnewbies



Fred Chou <fred.chou.nd@gmail.com> wrote:

>Hi,
>
>On Mon, Dec 23, 2013 at 8:03 PM, Bernd Petrovitsch <
>bernd at petrovitsch.priv.at> wrote:
>[...]
>> And the macro just avoids to copy-paste identical lines/logic.
>
>Thank you all for the explanation. Originally I was wondering if there
>could be some other reasons. So the main (or only?) purpose of the
>macro is
>to save some typing, and in terms of functionality I could equivalently
>call each handlers.
>
>Fred

If you do decide to inline the code, you should probably get rid of the do while(0) part.

That exists just to get rid of side effects of using macros in various syntax situations.

http://c-faq.com/cpp/multistmt.html

Greg
-- 
Sent from my Android phone with K-9 Mail. Please excuse my brevity.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20131224/7eaff5f3/attachment.html 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Linux-wireless: why use macro to call functions
  2013-12-24 12:19   ` Fred Chou
  2013-12-24 13:35     ` Greg Freemyer
@ 2013-12-24 13:48     ` Adrian Ratnapala
  2013-12-24 16:47       ` Valdis.Kletnieks at vt.edu
  1 sibling, 1 reply; 8+ messages in thread
From: Adrian Ratnapala @ 2013-12-24 13:48 UTC (permalink / raw)
  To: kernelnewbies

> So the main (or only?) purpose of the macro is to save

Better to say it saves cutting and pasting, which is  a damn good
thing -- even if macros are an old fashioned way of doing it.

> in terms of functionality I could equivalently call each handlers.

But then you would have to call them repeatedly inside a loop and
check for errors in the same way as is done in the macro.  Not just
call the function.


On 24 December 2013 13:19, Fred Chou <fred.chou.nd@gmail.com> wrote:
> Hi,
>
> On Mon, Dec 23, 2013 at 8:03 PM, Bernd Petrovitsch
> <bernd@petrovitsch.priv.at> wrote:
> [...]
>> And the macro just avoids to copy-paste identical lines/logic.
>
> Thank you all for the explanation. Originally I was wondering if there could
> be some other reasons. So the main (or only?) purpose of the macro is to
> save some typing, and in terms of functionality I could equivalently call
> each handlers.
>
> Fred
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>



-- 
Adrian Ratnapala
mobile: +49 1515 277 0000

NGBitS GmbH
Rainer Strasse 7
83104 Osterm?nchen

Web: www.ngbits.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Linux-wireless: why use macro to call functions
  2013-12-24 13:48     ` Adrian Ratnapala
@ 2013-12-24 16:47       ` Valdis.Kletnieks at vt.edu
  0 siblings, 0 replies; 8+ messages in thread
From: Valdis.Kletnieks at vt.edu @ 2013-12-24 16:47 UTC (permalink / raw)
  To: kernelnewbies

On Tue, 24 Dec 2013 14:48:33 +0100, Adrian Ratnapala said:
> > So the main (or only?) purpose of the macro is to save
> 
> Better to say it saves cutting and pasting, which is  a damn good
> thing -- even if macros are an old fashioned way of doing it.

The biggest benefit of macros isn't saving the typing.

It's so that if a change is required, you just have to make it once,
not 35 times for the 37 times you cut-n-pasted it.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20131224/98fa3a01/attachment.bin 

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-12-24 16:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-23 10:23 Linux-wireless: why use macro to call functions Martin Brugnara
2013-12-23 12:03 ` Bernd Petrovitsch
2013-12-24 12:19   ` Fred Chou
2013-12-24 13:35     ` Greg Freemyer
2013-12-24 13:48     ` Adrian Ratnapala
2013-12-24 16:47       ` Valdis.Kletnieks at vt.edu
  -- strict thread matches above, loose matches on Subject: below --
2013-12-23  6:05 Fred Chou
2013-12-23  7:25 ` Daniel Baluta

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).