c++ - Passing function pointers as an API interface to a compiled library -


dearest stack exchange,

i'm programming mri scanner. won't go background, i'm constrained in how code i've got access to, , way things have been set is...suboptimal. have situation follows:

  • there big library, written in c++. "transcoding" (in worst possible way), writing out fpga assembly doesthings. provides set of functions "userland" translated (through mix of preprocessor macros , black magic) long strings of 16 bit , 32 bit words. way done prone buffer overflows, , falling over.*
  • the fpga assembly strung out on glorified serial link relevant electronics, executes (doing scan), , returning data again processing.
  • programmers expected use functions provided library thing, in c (not c++) functions linked against standard library. unfortunately, in case, need extend library.
  • there's complicated chain of preprocessor substitution , tokenization, calling, , (in general) stuff happening between writing dosomething() in code, , relevant library function executing it. think i've got figured out extent, means i've got no real idea scope of anything...

in short, problem is:

  • in middle of method, in deep dark corner of many thousands of lines of code in big blob have little control over, god-knows-what variable scoping going on, need to:
    • extend method take function pointer (to userland function) argument, but
    • let userland function, written after library has been compiled, have access variables local both scope of method appears, variables in (c) function called.

this seems absolute mire of memory management, , thought i'd ask here "best practice" in these situations, it's there lots of subtle issues might run -- , others might have lots of relevant wisdom impart. debugging system nightmare, , i've not got support scanner's manufacturer on this.

a brief sketch of how plan proceed follows:

in .cpp library:

/* in something::something() /* /* declare pointer function */     void (*fp)(int*, int, int, ...); /* default, pointer points placeholder @ compile time*/     fp = &donothing(...);  ...  /* @ appropriate time, point pointer userland function, address supplied argument something(): /*     fp= userfuncptr; /* declare memory user function plonk data */      i_arr_coefficients = (int) malloc(something_sensible); /* create pointer array userland function */      i_ptr_array=&i_arr_coefficients[0]; /* define struct of pointers local variables userland function use*/  ptrstrct=createptrstruct();   /* call user's function: */ fp(i_ptr_array,ptrstrct, ...);  carryonwithsomethingelse(); 

the point of placeholder function keep things ticking on if user function isn't linked in. replaced #define, compiler's cleverness or stupidity might result in odd (to ignorant mind, @ least) behaviour.

in userland function, we'd have like:

void dousefulthings(i_ptr_array, ptrstrct, localvariableaddresses, ...) {      double a=*ptrstrct.a;     double b=*ptrstrct.b;     double c=*localvariableaddresses.c;     double d=domaths(a, b, c);     /* i.e. maths using of these numbers we've got different sources */      storedata(i_ptr_array, d);     /* , put results of maths c++ method can see */ }  ...  something(&dousefulthings(i_ptr_array, ptrstrct, localvariableaddresses, ...), ...);   ... 

if clear mud please tell me! thank help. and, way, sincerely wish make open hardware/source mri system.

*as aside, primary justification manufacturer uses discourage modifying big library in first place!

you have full access c code. have limited access c++ library code. c code defining "dousefullthings" function. c code calling "something" function ( c++ class/function) function pointer "dousefullthings" argument. control goes c++ library. here various arguments allocated memory , initialized. the "dousefullthings" called arguments. here control transfers c code. in short, main program(c) calls library(c++) , library calls c function.

one of requirements "userland function should have access local variable c code called". when call "something" giving address of "dousefullthings". there no parameter/argument of "something" captures address of local variables. "dousefullthings" not have access variables.

malloc statement returns pointer. has not been handled properly.( trying give overview ). must taking care free somewhere.

since mixture of c , c++ code, difficult use raii (taking care of allocated memory), perfect forwarding ( avoid copying variables), lambda functions ( access local varibales) etc. under circumstances, approach seems way go.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -