2 ms·
I once worked on a project where we were building a browser plugin in IE (back when plugins existed) and found ourselves needing to use Open Dynamics Engine wit
by shadowgovt 5y ago
I once worked on a project where we were building a browser plugin in IE (back when plugins existed) and found ourselves needing to use Open Dynamics Engine within the plugin. Under the hood, ODE uses `alloca` a lot... Once per frame to set aside enough RAM to compute a Jacobian that solves for all active collisions. But on Windows, you can configure "redzone protection" to fault the program if the stack takes a flying leap of a (relatively) paltry few kB off the current top of the stack, relative to the default configuration of redzone protection on most single-threaded Windows apps. From ODE's point of view this was fine; they didn't really care about the "compiling on Windows" use case at all and considered our problem to be ours. But this proved disastrous in the context of running on Windows in a thread, and since we were running in IE's process context, we couldn't change the thread allocation rules because they're baked into the process itself.
The nice thing about C++ is that the preprocessor is a monster. We #define'd all references to alloca in the ODE codebase away into calls to our own function that returned, instead of a bare pointer, an object with the right pointer-dereference semantics to act like a bare pointer but that was really a reference to a slice of a colossal chunk of heap memory. When the context of the alloca call closed, the returned object was destroyed, and its destructor moved our imaginary "stack" pointer in the big chunk of heap memory back into place.
Ugliest thing I ever wrote. ;)