<p dir="ltr">My bad, it would be captured as well during thunk allocation in a ThunkIdToThis[] array, and last line should be:<br>
    - RedirectMethodFuncWithIntParameter would call ThunkIdToFuncPtr[X](ThunkIdToThis[X], arg1)</p>
<div class="gmail_quote">On Nov 30, 2014 4:32 PM, "Sanjoy Das" <<a href="mailto:sanjoy@playingwithpointers.com">sanjoy@playingwithpointers.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Virgile,<br>
<br>
> One (rather complicated) option I was thinking is:<br>
> - Define a maximum number of those callback alive (let's say 4096) -- it's<br>
> not per function signature, but global<br>
> - Define i8* thunkTargets[4096]<br>
> - Define i8* ThunkIdToFuncPtr[4096]<br>
> - Define 4096 funcs (not even sure how to do that with LLVM, might need to<br>
> emit assembly?)<br>
>     Thunk0: jmp thunkTargets[0];<br>
>     Thunk1: jmp thunkTargets[1];<br>
>     ...<br>
>     Thunk4095: jmp thunkTargets[4095];<br>
><br>
> - When I call a C function from C# with a callback, what happens is:<br>
>   - Find an unused slot in this thunk table (X)<br>
>   - Register C# member to function pointer in ThunkIdToFuncPtr[X]<br>
>   - Replace thunkTargets[X] with pointer address to<br>
> "RedirectMethodFuncWithIntParameter" (one such function per callback<br>
> signature)<br>
>     - This redirect method would receive arguments unmodified from C<br>
> functions (since previous call was a simple jmp)<br>
>     - It would check in the call stack the current slot X being called (up<br>
> in the callstack, if call instruction is "call Thunk3" from address Thunk3<br>
> we know X is 3 -- it will need assembly, might be difficult to compute and<br>
> won't be portable...)<br>
>     - ThunkIdToFuncPtr[X] would give us the actual method to forward to<br>
>     - RedirectMethodFuncWithIntParameter would call<br>
> ThunkIdToFuncPtr[X](arg1)<br>
<br>
I couldn't quite understand this approach -- at what point do you<br>
figure out what the value of 'this' is?<br>
<br>
-- Sanjoy<br>
</blockquote></div>