[ODE] Segfaulting ODE with too many objects -- ALLOC vs MALLOC

Jon Watte (ODE) hplus-ode at mindcontrol.org
Sat Sep 30 10:16:38 MST 2006



Kyle Hubert wrote:
> One more thing, if you look in release 0.7 on line 409 in
> ode/src/quickstep.cpp, you'll see an unprotected alloca. In fact, I
> count two unprotected allocas in collision_space.cpp and six
> unprotected allocas in quickstep.cpp. That's only my quick poking. I
> smell a code review.
>   

As I said before:

With the proper implementation of alloca on top of malloc(), you don't 
need to protect the alloca calls with free calls.

Insert a call to something like "init_alloca" at the beginning of the 
solver, and have this call clear out all the previous frames alloca()-ed 
memory.
Then start over.

Implement alloca() by allocating a large chunk of memory (1 MB?), and 
moving an allocation pointer through this chunk. If you overrun the end, 
allocate a second chunk, and link it to the first, and start allocating 
from there instead. You might not want to ever free these chunks; just 
re-use them for future frames. This way, the malloc() contention cost is 
only hit the first frame, and it's otherwise almost as fast as alloca() 
(one additional cache line needed). The draw-back is that it's not 
thread safe, unless you use TLS (which costs another cache line).

The currently proposed patch is not as optimal as what I describe above, 
if I remember correctly.

Cheers,

          / h+



More information about the ODE mailing list