2

I have this code:

#ifdef something32                           <----- might not be defined
    the_real_value = thing[something32];
    thing[something32] = my_value;
#else
    the_real_value = thing[something];   <------- guarantied to be defined (something)
    thing[something] = my_value;
#endif

#ifdef THE_OTHER_THING                       <--------- might not be defined
#ifdef something32
    thing32[something32] = my_value;
#else
    thing32[something] = my_value;
#endif
#endif

and I'll be using that a lot of times so I'd like to replace it with a macro. I know #ifdef's can't live insde a macro so I'm wondering how else I could replace all that code. Any ideas?

EDIT:

I'm sorry, I forgot to mention that something32 is just one of a pretty long list of variables.

The idea is to have something like

SHORTEN_MY_CODE(something, something32);
SHORTEN_MY_CODE(another_something, another_something32);
etc...
alexandernst
  • 14,352
  • 22
  • 97
  • 197

2 Answers2

2

Turn your logic around and change what the macro is defined as based on condition - rather than defining a macro that contains a condition:

// define

#ifdef something32
    #define SOMETHING_VAR something32
#else
    #define SOMETHING_VAR something

#define SHORTEN_MY_CODE the_real_value = thing[SOMETHING_VAR]; \
    thing[SOMETHING_VAR] = my_value;

#ifdef THE_OTHER_THING
     #define SHORT_OTHER() thing32[SOMETHING_VAR] = my_value
#else
     #define SHORT_OTHER() 
#endif

// usage
SHORTEN_MY_CODE()
SHORT_OTHER()

ORIGINAL ANSWER

#ifdef something32
    #define MY_MACRO the_real_value = thing[something32]; \
    thing[something32] = my_value; 
#else
    #define MY_MACRO the_real_value = thing[something]; \
    thing[something] = my_value;
#endif
Justin Meiners
  • 10,754
  • 6
  • 50
  • 92
  • @alexandernst does it have to use a #ifdef or could you use ternary. A compiler will optimize out branches that never happen in this case – Justin Meiners Apr 18 '13 at 21:55
  • No, I can't as ```something32``` might not be defined. Anyways, ```something``` is guarantied to be defined. – alexandernst Apr 18 '13 at 21:57
  • @alexandernst best I can think of right now - it is still reduced – Justin Meiners Apr 18 '13 at 22:00
  • Thank you for the update, anyways, this is still missing the ```THE_OTHER_THING``` case which will always happen, just after the ifdef/else. – alexandernst Apr 18 '13 at 22:04
  • The last - 1 edit was ok, the last one has a bug/no-no, that is: something32 is just one variable from a list of at least 20, so I can't #ifdef it. A part from that, the last - 1 edit is what I'll probably use :) – alexandernst Apr 18 '13 at 22:12
  • @alexandernst if its a variable then what is #ifdef checking - is a #define constantly changing? – Justin Meiners Apr 18 '13 at 22:15
  • The thing is that something is defined with around another 20 variables, and I need to write that ```SHORTEN_CODE``` for each one. So I can't #ifdef every single variable that is defined as this defeats the whole purpose of my question :p – alexandernst Apr 18 '13 at 22:17
0

If you are doing C++, I would consider just writing a function and declare it inline. If you have a full function, then you can put #ifdef inside the function.

If you post more of your code, I think we can offer more help. Misuse of the c-preprocessor is rampant. You probably don't want to use it the way you propose.

I don't understand how something,something32 and OTHER_THING get defined. Are they defined at the project level, or inside some header file?


Update: suggestion. However, you may want to read this first : access to the sys_call_table in kernel 2.6+

void* Hook(int f, void* hooked) {
#ifdef CONFIG_IA32_EMULATION
    void *old = ia32_sys_call_table[f];
    ia32_sys_call_table[f] = hooked;
#else
    void *old = sys_call_table[f];
    sys_call_table[f] = hooked;
#endif
    return old;
}


...

Hook(__NR_read, hooked_sys_read);
Hook(__NR_write, hooked_sys_write);
Community
  • 1
  • 1
Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
  • I'm using C/C++, so that would be an option. ```OTHER_THING``` is ```CONFIG_IA32_EMULATION``` (Linux Kernel), while ```something``` and ```something32``` are ```__NR_read``` and ```__NR_read32``` (sys_calls). ```__NR_read``` will always exists, no matter what's the architecture, but ```__NR_read32``` will exist only on x64 AND IA32 emulation activated. Also, please keep in mind that this is not the only sys_call (there are a lot more). – alexandernst Apr 20 '13 at 12:07
  • Still need more sample code... You're obviously playing with fire if you are using library symbols such as `__NR_read` as offsets into some array. If you are writing assembly code in C, you are using the wrong tool.... – Mark Lakata Apr 23 '13 at 06:13
  • Ok, here's the actual code: https://github.com/alexandernst/procmon/blob/master/syshijack.h and https://github.com/alexandernst/procmon/blob/master/hookfns.c – alexandernst Apr 23 '13 at 18:38