I'm making a library that is meant to run on an embedded device. This library does some specific memory accesses and I want to let the user register a pre-access callback to prepare the memory (such as flushing a cache, for instance). I want this callback to be optional. Ideally, if no callback is provided, I'd like the library to optimize that and go straight to its task without calling an empty func or even testing for the existence of the callback. How can I achieve that?
Here's what I considered :
- Defining the callback at runtime and passing a function pointer : I do not think I can optimize that as I will need to test the function pointer for null
- Defining a default empty callback as a weak symbol : that works, but I end up with a call to an empty func that doesn't get optimized properly. I've been able to strip that with the gcc
-s
option, but that does more than just speed optimization. - Preprocessor macro - That could work, but the user will need to recompile the library depending if he want to add a callback or not; not so convenient.
Ideally, option #2 would be the way to go. What disturbs me is the definition of the -s option :
-s : Remove all symbol table and relocation information from the executable.
That seem to do way more than I am asking for. I'm not exactly sure what are the consequence of removing all that. My goal is simply to get a correct optimization for something that is obviously optimizable when disassembling the code. By that, I mean a call ret
sequence.