0

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 :

  1. 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
  2. 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.
  3. 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.

  • Is creating an accessor object an option? `auto accessorWithCallback = MakeAccessor(someValue, callback, context); auto accessorWithoutCallback = MakeAccessor(someValue);` This would allow you to return objects of different types depending on the overload of the function used. – fabian Nov 03 '22 at 12:53
  • Check this post: https://stackoverflow.com/questions/257288/templated-check-for-the-existence-of-a-class-member-function – Patricio Loncomilla Nov 03 '22 at 12:55
  • Hmm, i'm not sure. Returning an empty function seems to be the euqivalent of #1, therefore a runtime test will happen. No? – Pier-Yves Lessard Nov 03 '22 at 12:56

0 Answers0