[En-Nut-Discussion] Use of alloca in Nut/OS

Alain M. alainm at pobox.com
Mon Aug 11 17:34:08 CEST 2008


Yes I have some thoughts:

In most cases alloca() can simply be raplaces by simple local variable. 
In fact, alloca is only usefull if variable data will be stored, which 
is never a good thing for embedded.

Alain

Ole Reinhardt escreveu:
> Hi Henrik,
> 
>> This incident brought my attention to alloca and I am wondering if the main
>> code of Nut/OS should use alloca at all? 
>>
>> Alloca is used in two modules ssi.c and cgi.c.
> 
> We just differentiate between GNU and IMAGECRAFT in the usage of alloca.
> Imagefcraft does not support alloca as well.
> 
>> On systems with larger amounts of memory alloca would be a good option for
>> dynamic memory. However on the AVR stack space is very limited and often
>> only a few hundred bytes per thread. Allocating larger strings on the stack
>> can easily cause stack overflow. 
> 
> That's right. My intention was to speedup the code by using alloca for
> dynamic allocation of just small amounts of stack for temporaly holding
> a short string. Using NutHeapAlloc is much slower and will potentially
> fragment the heap if some other heap is allocated in the meanwhile.
> 
>> The other issue which arises is that Imagecraft does not support it either.
>>
>> I suggest to use it only for the ARM/gcc combination and guard it's use with
>> conditional compilation of the form "#if defined(__GNUC__) &&
>> defined(__arm__) "
> 
> Would be an idea. I even would vote to use it in more places, but that's
> only possible if every compiler would support alloca. Perhaps we could
> add our own alloca implementation? (By just moving the stack pointer by
> x bytes). Shure, it does not help on systems with low memory...
> 
>> Any thoughts on this?
> 
> Any more thoughts on it?
> 
> Ole Reinhardt
> 



More information about the En-Nut-Discussion mailing list