1

kfifo.h is made up almost entirely of #define. Why is that? Why have they not declared the functions in the h file like its "normally" done.

[edit]

It seems like my question is easily interpreted as a questioning of the implementation rather than what I ment to ask, why is this implementation better, from a learning standpoint. Anyway, since I didn't know I was looking at "Function-like macros" I am voting to close since it's obviosly a duplicate.

evading
  • 3,032
  • 6
  • 37
  • 57
  • 5
    Why not? Many of the macros there are not expressible as C functions.... – Basile Starynkevitch Apr 20 '13 at 06:12
  • "Why is that?" - Perhaps because Mr Torvalds has put them in? –  Apr 20 '13 at 11:31
  • 1
    @H2CO3 How is that helpful? – evading Apr 21 '13 at 06:27
  • Well, it is not really a duplicate. As [Basile](http://stackoverflow.com/users/841108/basile-starynkevitch) points out, both functions and data are used in this header. `macros` have some downfalls, but here they enforce some data sizes which make the structure *lock free* under some circumstances. It is quite different than the cited duplicate. – artless noise Apr 21 '13 at 20:19
  • In fact, this is a *ring buffer* with some interesting algorithms details. Such algorithms are known to be *lock free* for some time and are documented in **The art of computing** by *Donald Knuth*. Unfortunately, the question is closed, so addition answers can not be given as to why it is particularly useful here. Also, the cited duplicate refers to *function* like macros. Ie, they look like a *C* function. – artless noise Apr 21 '13 at 20:28
  • I guess as it stands, my question is more about kfifo.h than about function-like macros. My intention was to ask about function like macros. Now I don't know what to do. Should I edit to reflect my intent better? – evading Apr 22 '13 at 09:48

1 Answers1

2

It is permitted to have a lot of macros. Why does that trouble you?

A macro like

 #define STRUCT_KFIFO_PTR(type) \
   struct __STRUCT_KFIFO_PTR(type, 0, type)

is not a function-like macro.

A macro like

 #define kfifo_initialized(fifo) ((fifo)->kfifo.mask)

could be also used on the left side of some assignment (even if you probably should not do that). And it is shorter to write than the corresponding inline function

 static inline unsigned kfifo_initialezed_f(struct __kfifo *fifo) {
    return fifo->kfifo.mask;
 }

and more importantly the macro kfifo_initialized would work with several different declarations of its fifo actual argument (it is "generic" in some limited sense).

A macro like

 #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \
    { \
   __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
        type    buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \
    }

would expand to a declaration equivalent to an array buf[-1] if given a size of 3 and that would make the compiler yell. (The recent C++2011 standard also has static_assert for such purposes).

So I don't understand why you are surprised. The C preprocessor is useful (IMHO it is even not powerful enough). Why avoid using it?

This is free software; if you don't like that source file, you could work to improve it (and that would take time) to propose a better solution. I do like the current one.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Who said anything about being hurt? I'm wondering for the sake of learning. I really don't get your harsh tone. Surley someone with 38k rep must have read the faq on Etiquette at some point. – evading Apr 20 '13 at 11:11
  • Well, I am not a native English speaker (and most French people are bad at English, much worse than most Swedish). I was not thinking that "been hurt" is agressive (usually it is not, especially for "feelings" which are by definition subjective). I tried to improve slightly my wordings. – Basile Starynkevitch Apr 20 '13 at 11:12
  • No worries. I guess my question is easy to missinterpret as well. My intention was to ask why that way of implementing it is better than the "normal" way, not questioning the implementation itself. – evading Apr 20 '13 at 12:03
  • 2
    Yes, `STRUCT_KFIFO_PTR` *is* a "function-like macro". The C standard defines two kinds of macros "object-like" macros (which don't take arguments) and "function-like" macros (which do). The name "function-like doesn't imply that they behave like functions apart from the fact that they take arguments in a syntax similar to a function call. – Keith Thompson Apr 20 '13 at 17:26
  • @KeithThompson I believe people are mixing up notation of *function-like*. The cited duplicate is using macros as a *function replacement*. A standard may use other language. Not every person live and breaths standards documents; it would be useful if natural languages were unambiguous and maybe [LALR](http://en.wikipedia.org/wiki/LALR_parser) ;-) – artless noise Apr 21 '13 at 20:34