[ODE] Re: ALLOCA

Olivier Michel Olivier.Michel at cyberbotics.com
Mon Jun 21 15:53:55 MST 2004


Jon,

Your improvement seems to be great, as long as we can ensure that the
"top" variable will reach 0 regularly in the execution of the
simulation, which should be the case at the end of each dWorldStep(),
correct ?

I'd propose that someone with cvs write access review and commit my
original patch, then we could review and apply Jon's patch over mine ?

-Olivier

jon klein wrote:

>
> On Jun 21, 2004, at 8:12 AM, Olivier Michel wrote:
>
>> Hi,
>>
>> I finally patched the whole ODE against the stack overflow common 
>> problem using the ALLOCA replacement discussed earlier on this list. 
>> I had to change the invocation of ALLOCA in many files to make the 
>> patch simple and convenient to use. I wrote this patch very 
>> carefully. By default, it is off (and no changes occur comparing the 
>> normal version of ODE, i.e., stack allocation with risk of stack 
>> overflow). You have to edit the config/user-settings to turn it on 
>> and all the files will use the ALLOCA replacement after a 
>> recompilation. You can specify the size of the memory chunk used in 
>> config/user-settings as documented in this file (by default I set 
>> 4MB, but you can change it).
>
>
> I'd like to propose a small change to this scheme.
>
> Even with the new scheme, you still have to specify a compile-time 
> memory limit.  If you go over this limit, ODE will crash.
>
> I've made some changes so that the dynamic memory block can grow when 
> needed:
>
> 1) allocate a small dynamic memory chunk
> 2) start by allocating memory from this chunk
> 3) if the memory usage goes over the chunk size, mark the chunk for 
> expansion and return a malloc'd pointer instead
> 4) when all of the ALLOCA'd memory has been released (when dWorldStep 
> returns), expand the dynamic memory chunk.
>
> class StackMemory {
> public:
>  StackMemory(int s) {
>    if(top == 0 && expand == 1) {
>      // the stack is empty, and we got an expand request.
>      free(block);
>      block_size += STACK_MEMORY_CHUNK_SIZE;
>      block = (char*)malloc(block_size);
>      expand = 0;
>    }
>
>    size = (s + 15) & -16;
>
>    if (top + size > block_size) {
>     // if we're beyond our allocated stack, use malloc instead,
>     // and tell the stack to expand next time it can.
>     expand = 1;
>     ptr_ = (char*)malloc(s);
>     return;
>    } else {
>     top += size;
>     ptr_ = block+top-size;
>    }
>  }
>  ~StackMemory() {
>    if (block+top == ptr_+size) top-=size;
>    else if(ptr_ < block || ptr_ > block+top) free(ptr_);
>  }
>  char * ptr_;
> private:
>  size_t       size;
>  static char expand;
>  static char *block;
>  static size_t top;
>  static size_t block_size;
> };
>
> - jon
>
>



More information about the ODE mailing list