A lot of what sl_malloc() is used for was really intended for a function that behaves like alloca(). What problems are we signing up for by allowing use of alloca()?
On Fri, Nov 05, 2010 at 04:17:57AM -0700, Howard Chu wrote:
A lot of what sl_malloc() is used for was really intended for a function that behaves like alloca(). What problems are we signing up for by allowing use of alloca()?
In Samba, we have a replacement called talloc_stackframe() and talloc_tos(). It depends on talloc and thus malloc down below. For performance we have added a very fast chunk allocator for the request/response style smb server.
Apart from that, in Samba it's urban legend that alloca is VERBOTEN... Not sure about the validity of that these days.
Volker
Howard Chu writes:
What problems are we signing up for by allowing use of alloca()?
This is from memory, and for all I know outdated:
It's somewhat unportable, and alloca from GNU something (gcc? glibc?) is or was a malloc wrapper on systems where it cannot provide a proper alloca(). So I think we'd need #if <has a good alloca, and not making things worse by using malloc> # define slap_sl_alloca(size, ctx) alloca(size) # define slap_sl_freea(ptr, ctx) ((void) (ptr)) #else # define slap_sl_alloca slap_sl_malloc # define slap_sl_freea slap_sl_free #endif
Unlike variable-size arrays, alloca() memory lives to the end of the function call. So avoid alloca() in loops. Be careful moving code using alloca() into other functions. gcc seems unwilling to inline functions using alloca, hopefully other compilers have similar sense.
I think it can be incompatible with variable-size arrays - in the same stack frame, I presume. Which is all right when we stay C90-compatible, but we may be moving on someday. That said, I think some var-array implementations use/used alloca, so...
In any case, a more future-friendly way might be to invent a syntax to declare "variable-sized arrays" that actually use sl_malloc or alloca if the compiler doesn't support variable-sized arrays.
man alloca on my Linux system says:
NOTES ON THE GNU VERSION (...) there is no NULL error return. BUGS The alloca() function is machine and compiler dependent. On many systems its implementation is buggy. Its use is discouraged.
On many systems alloca() cannot be used inside the list of arguments of a function call, because the stack space reserved by alloca() would appear on the stack in the middle of the space for the function arguments.
OTOH, programs like gcc and emacs have been using alloca() a lot, though I think at least gcc has mostly gotten rid of it.