Re: RFC: glocal - automatically freeing memory when it goes out of scope

On 05/29/2012 07:23 PM, Ben Pfaff wrote:
Mikkel Kamstrup Erlandsen<mikkel kamstrup canonical com>  writes:

I have been looking at gcc's "cleanup" attribute[1] that allows one to
specify a callback that will be invoked when a variable goes out of
scope. This allows one to play with automatically freeing resources.

Is it possible to combine use of this GCC-specific attribute with
the concept in Doug Gwyn's "mostly-portable" alloca
implementation, so that you would end up with something that
works pretty much everywhere (but is more efficient with GCC)?

Here's one copy of the Doug Gwyn alloca that just happened to be
on the top of the results of my web search:

There is already g_alloca() and g_newa() for allocating on the stack.

I'va also been thinking quite a lot about how we could leverage stack allocation to get rid of even more memory management hassles.

One could conceivably introduce *a() variants of various key calls, like g_strdupa(), g_strdupa_printf(), maybe array types g_{ptr_,}array_newa, etc. Noting that GList and GSList doesn't strictly need special variants as you can already stack allocate them as they are fully defined structs. Although g_list_prependa() and friends would be more convenient.

It will bloat the API quite a lot though that's why I never seriously proposed it. Another solution would be to have a g_push/pop_allocator() kind of thing. Didn't think too deeply about it though.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]