In the Linux kernel, there can be found a line of code that looks redundant to me:
#define __arch_swahb32 __arch_swahb32
What is the purpose of an idiom like this?
Consider the following code:
#ifdef foo
foo();
#endif
If you want a snippet like the above to call function foo, you need to define foo. However, if you just
#define foo
then the function foo name will be replaced with an empty token, and the first snippet is preprocessed to just ();. If, however, you
#define foo foo
then the first snippet will preprocess to foo(); as it should.
Trick to ensure #if defined(__arch_swahb32) passes but doesn't replace. (Often used to implement macro type functions)
Related
I see in some source code this kind of definition
#define somemacro(a,b,c) (0)
And I see in the same source code:
#define anothermacro(a,b,c) (1)
Can any body explain this kind of macro definition? what is the purpose of such definition?
Usually non-used parameters of a function like macro occurs in the following situation:
#ifdef A_IS_GREAT_ALWAYS
#define anothermacro(a,b,c) (1)
#else
#define anothermacro(a,b,c) ((a)>(b)+(c))
#endif
Without parameters it would break the build in the following kind of places, when A_IS_GREAT_ALWAYS is not defined.
...
if (anothermacro(foo, bar, baz))
...
The precompiler translates the example code to:
if ((1))
or
if (((foo)>(bar)+(baz)))
depending is the A_IS_GREAT_ALWAYS defined or not.
I am going through a C code and I found something like this:
#define __UNUSED__
char buf[MAX_BUF_LENGHT];
int errors=0;
What does this mean?
I am not aware that __UNUSED__ is a predefined preprocessor symbol. So it must be a user defined symbol.
I myself have sometimes (test) code or obsolete code in a c-file that I mark-out with #ifdef BLIEP (and BLIEP is normally not defined), but can put it back into compilation by placing a #define BLIEP. Probably the original author of this code did something similar with __UNUSED__.
I found some online resources about this topic but still can not understand how it works.
Lets assume that I have a global variable with following specification
in file: /sys/sys/sysctl.h
#define USER_TZNAME_MAX 20 /*test var*/
and in file /usr/src/sys/kern/kern_mib.c a
SYSCTL_INT(_user, USER_TZNAME_MAX, tzname_max, CTLFLAG_RW, 0, 0, "something");
can anyone show practically how to change the variable value and set another value in a c source file?
Thank you
#define USER_TZNAME_MAX is not defining a global variable, it is a preprocessor macro.
Before the compiler compiles the code the preprocessor is run to expand macros and include/exclude code as defined by definitions.
In an example such as this, the preprocessor will replace all instances of the string "USER_TZNAME_MAX" in the source with the string "20":
// this
int i = USER_TZNAME_MAX;
// will be expanded to this:
int i = 20;
Therefore you can't change this variable at run time because a) it isn't a variable, and b) it's a constant.
If you're talking about changing the value used in your own code you can do this:
#ifdef USER_TZNAME_MAX
#undef USER_TZNAME_MAX
#endif
#define USER_TZNAME_MAX (32)
In programs, you should use sysctl(3) to get or set system information.
I need to temporarily overwrite a macro and then restore it. Like:
#define FOO X
#save FOO
#define FOO Y
...
#restore FOO
Is it possible in standard C preprocessor? In GCC?
ADDED. About real world example. I use a global macro for error exception. It acts like assert, but for persistent usage, not only for debug versions; so, for example, I usually call functions (with side-effect) inside the macro. It's defined once, but the definition isn't persistent; therefore I don't know it a-priori. For some piece of code I need its own, modified version of the macro, but I want to save general style of code. It's looks ugly when one part of code uses the one macro, other part uses other macro -- both macros have the same purpose, but slightly different implementation.
So, it's good for me to save original macro temporarily, use different version for a part of code, after that restore original macro.
This is possible with #pragma push_macro and #pragma pop_macro. These are not standard C—they're originally an MSVC extension—but clang supports them, and so does GCC.
Example usage:
int main() {
#define SOME_MACRO 1
printf("SOME_MACRO = %d\n", SOME_MACRO);
#pragma push_macro("SOME_MACRO")
#define SOME_MACRO 2
printf("SOME_MACRO = %d\n", SOME_MACRO);
#pragma pop_macro("SOME_MACRO")
printf("SOME_MACRO = %d\n", SOME_MACRO);
return 0;
}
prints:
SOME_MACRO = 1
SOME_MACRO = 2
SOME_MACRO = 1
You can also #undef a macro inside a push_macro / pop_macro pair, and the pop_macro call will redefine it.
As already said, it is not really possible. Depending on the situation, this might be a workaround:
#include "generalmacrodefs.h" // put them in here or include them indirectly
#undef macro1
#define macro1 "specialized temporary value"
#undef macro1
#include "generalmacrodefs.h" // restores
This requires that generalmacrodefs.h uses a pattern like this at least for the definitions you might temporarily overwrite:
#ifndef macro1
#define macro1 "original value"
#endif
The closest you can come in C is the #undef directive, which simply undefines the macro, allowing it to be replaced:
#define FOO X
...
#undef FOO
#define FOO Y
...
#undef FOO
#define FOO X
The problem is that you cannot know the 'old' value of FOO once you redefine it - so your values must be hard-coded in one place.
You cannot create a macro to save the values for you either, as it isn't possible to have a macro that creates other preprocessor directives in standard C.
I am reading source code of hoard memory allocator, and in the file of gnuwrapper.cpp, there is the following code
#define CUSTOM_MALLOC(x) CUSTOM_PREFIX(malloc)(x)
What's the meaning of CUSTOM_PREFIX(malloc)(x)? is CUSTOM_PREFIX a function? But as a function it didn't defined anywhere. If it's variable, then how can we use variable like var(malloc)(x)?
More code:
#ifndef __GNUC__
#error "This file requires the GNU compiler."
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#ifndef CUSTOM_PREFIX ==> here looks like it's a variable, so if it doesn't define, then define here.
#define CUSTOM_PREFIX
#endif
#define CUSTOM_MALLOC(x) CUSTOM_PREFIX(malloc)(x) ===> what's the meaning of this?
#define CUSTOM_FREE(x) CUSTOM_PREFIX(free)(x)
#define CUSTOM_REALLOC(x,y) CUSTOM_PREFIX(realloc)(x,y)
#define CUSTOM_MEMALIGN(x,y) CUSTOM_PREFIX(memalign)(x,y)
In your code, since CUSTOM_PREFIX is defined to be nothing, the string CUSTOM_PREFIX(malloc)(x) will expand to
(malloc)(x)
which is equivalent to the usual
malloc(x)
However, the CUSTOM_PREFIX allows the developer to choose a different memory management function. For example, if we define
#define CUSTOM_PREFIX(f) my_##f
then CUSTOM_PREFIX(malloc)(x) will be expanded to
my_malloc(x)
CUSTOM_PREFIX is defined as nothing, so it will just disappear, leaving behind (malloc)(x), which is the same as malloc(x). Why? I don't know. Perhaps other places in the code set CUSTOM_PREFIX to something else.
At a guess, its a macro which changes calls to malloc(x) etc. into something like:
DEBUG_malloc( x );
You can choose to supply the macro yourself, to provide a customised prefix for the functions, or not in which case the names won't be changed.