[ODE] Re: ALLOCA

Jon Watte hplus-ode at mindcontrol.org
Thu Mar 4 08:19:42 MST 2004


You could easily make the smart pointer revert to individual malloc()-s when the 
initial big chunk runs out.

Something like:

class SmartPtr {

public:

  SmartPtr( size_t size ) {
    if( bigChunkTop+size > bigChunk+bigChunkSize ) {
      ptr_ = malloc( size );
    }
    else {
      ptr_ = bigChunkTop;
      bigChunkTop += size;
    }
  ~SmartPtr() {
    if( ptr_ >= bigChunk && ptr_ < bigChunkTop ) {
      bigChunkTop_ = ptr_;
    }
    else if( ptr_ < bigChunk || ptr_ > bigChunk+bigChunkSize ) {
      free( ptr_ );
    }
  }

  static void initialize( size_t size ) {
    assert( !bigChunk );
    bigChunk = malloc( size );
    bigChunkTop = bigChunk;
    bigChunkSize = size;
  }

private:

  char * ptr_;

  static char * bigCunnk;
  static char * bigChunkTop;
  static size_t bigChunkSize;
};


-----Original Message-----
From: ode-bounces at q12.org [mailto:ode-bounces at q12.org]On Behalf Of
Olivier Michel
Sent: Thursday, March 04, 2004 7:25 AM
To: ode
Subject: Re: [ODE] Re: ALLOCA


After compiling under Windows I realized that it is not possible to make 
a realloc (under Linux, I was lucky since realloc didn't changed the 
pointer, but it may change it and it does change it under Windows). 
Realloc simply makes the previously returned pointers crazy and the app 
crashes. So, no realloc :(. Instead a really big initial memory chunk is 
allocated (I found 1MB to be a good compromise in my case).

I updated the code and documentation in the following patch:

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

Hopefully this is the last one for today.

-Olivier

Olivier Michel wrote:

> 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));
> [...]
>
_______________________________________________
ODE mailing list
ODE at q12.org
http://q12.org/mailman/listinfo/ode




More information about the ODE mailing list