How do I pass an argument to a function of a pointer function using a wrapper function?

I have this function:

void boot(uint ncores, uint nterm, Task boot_task, int argl, void* args) { for(int i=0; i<MAX_PROC;i++) { PT[i].ppid = NOPROC; } nextproc = 0; curproc = NOPROC; Exec(boot_task, argl, args); } 

and I want to use pthread instead of Exec() , so I need to call cpu_boot:

 void cpu_boot(uint cores, interrupt_handler bootfunc, uint serialno) { //I cannot change this function } 

These are argument types.

 typedef void interrupt_handler(); typedef int (* Task)(int, void*); 

I tried:

 void boot(uint ncores, uint nterm, Task boot_task, int argl, void* args) { void my_wrapper() { int y; y= boot_task(argl, args); } cpu_boot(ncores, my_wrapper , nterm); } 

But it's not right. How to implement this?

+5
source share
2 answers

You need something like this:

 void some_interrupt_handler(){ /* code here */ return; } interrupt_handler* my_wrapper(Task boot_task, int argl, void* args) { /* this is where you run boot task */ boot_task(argl, args); /* then pick an interrupt_handler to return... */ void (*function_ptr)() = some_interrupt_handler; return function_ptr; } 

Then you can use your wrapper as follows:

 void boot(uint ncores, uint nterm, Task boot_task, int argl, void* args) { cpu_boot(ncores, my_wrapper(boot_task, argl, args) , nterm); } 
+2
source

A more general and much more flexible way ...

 void some_interrupt_handler(){ /* code here */ return; } typdef struct boot_data_struct { int argc; void * argv; Task boot_task; interrupt_handler * handler; } boot_data; interrupt_handler* my_wrapper(void * data) { boot_data * bootData = data; /* this is where you run boot task */ data->boot_task(bootData->argc, bootData->argv); return bootData->handler; } 

Then you can use your wrapper as follows:

 void boot(uint ncores, uint nterm, Task boot_task, int argl, void* args) { boot_data * data = malloc(sizeof *data); //remember to memset if you arent going to fill out all variables! data->argc = argl; data->argv = args; data->boot_task = boot_task; data->handler = some_interrupt_handler; cpu_boot(ncores, my_wrapper(data) , nterm); free(data); } 
+1
source

Source: https://habr.com/ru/post/1209040/


All Articles