[ODE] Re: ALLOCA

Olivier Michel Olivier.Michel at cyberbotics.com
Thu Mar 4 14:46:45 MST 2004


Hi,

I completed my dynamic memory allocation patch for ODE and I am pretty 
satisfied with it (no FREEA necessary, efficient, automatically grow the 
memory chunk if necessary). ODE won't crash any more ;). You can get it 
from here, test it and merge it into the CVS if it's ok:

http://cyberboticspc1.epfl.ch/ftp/ode.tar.gz

Here are some explanations from the modified user-config.example file. I 
appreciate any feedback.

-Olivier

# (7) ODE uses the stack for most memory allocation. This is a very fast way
#     to perform memory allocation. However, the stack size is limited 
and ODE
#     simply crashes with SIGSEGV when it runs out of stack space. Hence, if
#     your simulation uses complex models and turns out to run out of stack
#     space, you should compile ODE with dynamic memory allocation 
rather than
#     stack allocation. This will be probably slower but won't crash any 
more
#     with complex simulations. Please note that this first version of 
dynamic
#     memory allocation only affect the LCP solver (lcp.cpp file) which 
turns
#     out to be the most critical source of stack overflow. If you still
#     experience stack overflows, you should also modify other files 
that use
#     ALLOCA to make stack allocation and introduce the system used in 
lcp.cpp
#     These files include step.cpp, stepfast.cpp, timer.cpp, space.cpp,
#     ode.cpp, matrix.cpp and collision_space.cpp.
#     The dynamic memory allocation system doesn't rely on simple vanilla
#     malloc/free. In order to be more efficient, it allocates a big 
chunk of
#     memory at the beginning (64KB) and handle simple memory allocation and
#     deallocation inside this chunk. If more memory is needed, it a bigger
#     memory chunk is reallocated.

#DYNAMIC_MEMORY_ALLOCATION=1



// memory allocation system
#ifdef dDYNAMIC_MEMORY_ALLOCATION
#define STACK_MEMORY_CHUNK_SIZE 65536
class StackMemory {
public:
  StackMemory(int s) {
    size = (s + 15) & -16;
    top += size;
    if (top > block_size) {
      block_size+=STACK_MEMORY_CHUNK_SIZE;
      // printf("increased block size to %d\n",block_size);
      block = (char *)realloc(block,block_size);
    }
    ptr_ = block+top-size;
  }
  ~StackMemory() {
    if (block+top == ptr_+size) top-=size;
  }
  char * ptr_;
private:
  size_t       size;
  static char *block;
  static size_t top;
  static size_t block_size;
};

char * StackMemory::block      = (char *)malloc(STACK_MEMORY_CHUNK_SIZE);
size_t StackMemory::top        = 0;
size_t StackMemory::block_size = STACK_MEMORY_CHUNK_SIZE;

#define ALLOCA(t,v,s) \
  StackMemory alloca_ ## v(s); t* v = (t*)alloca_ ## v .ptr_

#else
#define ALLOCA(t,v,s) t* v=(t*)dALLOCA16(s)
#endif
[...]
    ALLOCA (dReal,tmprow,n * sizeof(dReal));
[...]



More information about the ODE mailing list