3 ms·
Here's my variation on bind() - generating executable code on the heap allows you to treat the bound function as a raw ordinary function pointer; https://gist.g
by mappu 14y ago
Here's my variation on bind() - generating executable code on the heap allows you to treat the bound function as a raw ordinary function pointer; https://gist.github.com/3154939 https://gist.github.com/3154939
- cioc 14y agoI really like this.
- RodgerTheGreat 14y agoI've used a very similar trick for adding partial function application to Forth: https://github.com/JohnEarnest/Mako/blob/master/lib/Algorithms/Curry.fs https://github.com/JohnEarnest/Mako/blob/master/lib/Algorith... If I understand correctly, though, your implementation leaks memory. In my case I use a GC to free the thunks when they are no longer accessible.
- mappu 14y agoIndeed, in typical C fashion all the created stubs must be freed manually. Adding a GC is a separate task and several exist for C. Though if you were using C++, you could put together some RAII sugar or smart pointers around it all, but in that case you have std::bind1st and functors available anyway (although this is still the only way to meet the raw-pointer use case).
- asveikau 14y agoThere's also libffi, which I'm pretty sure also works by generating stub functions in executable pages, but has the advantage of having been ported to a bunch of platforms already, and hides the ugliness in a library.