Ace
Ace

Reputation: 1601

Structure of #define Macro

I found this arcane construct inside include/linux/wait.h

 #define DEFINE_WAIT_FUNC(name, function)                                \
        wait_queue_t name = {                                            \
                 .private        = current,                              \
                 .func           = function,                             \
                 .task_list      = LIST_HEAD_INIT((name).task_list),     \
        }

I know good amount on macros and preproc directives in general, but I am absolutely lost on this one. Can someone please explain the above code structure in detail including the '\' at the end of the line. Thanks.

Note: I dont need to know what it does in linux, only the syntactic meaning behind it.

Upvotes: 0

Views: 314

Answers (3)

artless-noise-bye-due2AI
artless-noise-bye-due2AI

Reputation: 22395

As per others (and many references on-line), the \ character continues any line via the c-preprocessor. As for the rest,

#define DEFINE_WAIT_FUNC(name, function) \

Definition of the macro.

   wait_queue_t name = {                                            \

Declares a wait_queue_t with the macro substitution name.

            .private        = current,                              \

Initialize the private wait_queue_t structure member with the current task pointer. This is also a macro (perhaps inline assembler) defined by each architecture in the Linux tree.

            .func           = function,                             \

Set the func member to the function parameter.

            .task_list      = LIST_HEAD_INIT((name).task_list),     \

Initializes the list as empty. task_list points to itself.

The . notation is used through-out the kernel source and is a gcc feature (and later C99), called designated intializers. Instead of having to set all members of a structure, only the named ones are initialized with the others set to zero. This allows people to extend the structure without modifying all the declarations. It is not a c-preprocessor feature, but a 'C' language (extension).

Upvotes: 1

marc
marc

Reputation: 54

The macro (presumably) is asociating an structure with a function pointer and doing common initialization. Lest say you want to add those structures to a list and then (during an execution step) call different functions. A better question would at least include wait_queue_t definition.

Upvotes: 1

JaredPar
JaredPar

Reputation: 754565

The \ character in a macros is a line continuation character. It simply allows the macro to span multiple lines.

Upvotes: 2

Related Questions