On Mon, 11 Nov 2013, Ondřej Bílka wrote:

On Sun, Nov 10, 2013 at 04:27:00PM +0100, Marc Glisse wrote:
Hello,

I am posting this patch to get some feedback on the approach. The
goal is to replace malloc+free with a stack allocation (a decl
actually) when the size is a small constant.

Why constraint yourself to small sizes.

I am trying to get something to actually work and be accepted in gcc. That may mean being conservative.

Below is a simple implementation which creates a separate stack for
that (for simplicity and because it does not need to find bounds on
thread stack.)

One difficulty with using a stack is that lifetimes cannot partially overlap, whereas with malloc+free they can. Using the main stack has the advantage that I don't have to think of deallocation, it happens automatically. And using a decl instead of alloca means that even if malloc+free was in a loop, not deallocating won't make me grow the stack use linearly in the number of iterations of the loop.

With that extension it would be possible to mark pointer so its free
would be a nop.

That would help indeed, but it does require a libc that I haven't seen yet, and it may cause trouble if people try to LD_PRELOAD a different malloc implementation.

--
Marc Glisse

Reply via email to