假设有一个库函数(不能修改),它接受一个回调(函数指针)作为它的参数,它将在将来的某个时候被调用。我的问题:有没有办法将额外的数据与函数指针一起存储,以便在调用回调时,可以检索额外的数据。该程序在c中。
例如:
// callback's type, no argument
typedef void (*callback_t)();
// the library function
void regist_callback(callback_t cb);
// store data with the function pointer
callback_t store_data(callback_t cb, int data);
// retrieve data within the callback
int retrieve_data();
void my_callback() {
int a;
a = retrieve_data();
// do something with a ...
}
int my_func(...) {
// some variables that i want to pass to my_callback
int a;
// ... regist_callback may be called multiple times
regist_callback(store_data(my_callback, a));
// ...
}
问题是因为callback_t不接受任何参数。我的想法是每次生成一小段asm代码填充到regist_callback中,当它被调用时,它可以找到真正的回调和它的数据并将其存储在堆栈中(或一些未使用的寄存器),然后跳转到真正的回调,并且在回调内部可以找到数据。
伪代码:
typedef struct {
// some asm code knows the following is the real callback
char trampoline_code[X];
callback_t real_callback;
int data;
} func_ptr_t;
callback_t store_data(callback_t cb, int data) {
// ... malloc a func_ptr_t
func_ptr_t * fpt = malloc(...);
// fill the trampoline_code, different machine and
// different calling conversion are different
// ...
fpt->real_callback = cb;
fpt->data = data;
return (callback_t)fpt;
}
int retrieve_data() {
// ... some asm code to retrive data on stack (or some register)
// and return
}
合理吗?有没有针对此类问题做过任何工作?