[Vala] evangelise talloc for vala memory management



I find the way vala deals with weak references and ownership slightly
confusing; because it requires to programmer to track ownership rather
than the program;

however as I'm also trying to put vala in Samba4, I thought I should
point out the talloc library used for memory management; used for samba
and other projects for many years.
A particular question will be whether or not vala can be made to use a
specific allocator for certain object classes or hierarchies, because
samba4 will require that I use talloc.
Talloc is designed to be able to plug in as a malloc replacement.

Details at: http://talloc.samba.org/
http://samba.org/ftp/unpacked/talloc/talloc_guide.txt

Some specific features of note:

* Only 10% overhead over normal malloc type management.
* Type safety - you can add a name to each allocation and then check later
* You can add destructors to allocations.
* Allocations can (and usually are) owned by previous allocations (or
contexts) so:

struct object {
  char* name;
}


struct object thing=talloc_zero(NULL, struct object);
thing->name=talloc_strdup(thing, "hello there");
talloc_free(thing);

will free the thing->name at the same time, so you don't have to
manually track and free every allocation.
You could just start a new talloc_context at the top of a function and
free it on exit, thus freeing everything else along with it (that hadn't
also been talloc_reference'd or talloc_steal'd)

* You can take counted references to allocations so they don't free
until everyone has finished with them
* You can transfer ownership
  request->buffer=talloc_steal(request, incoming->buffer);
  talloc_free(incoming);
* allocations can be speed efficient, often coming from the parent's
allocation by means of a sub-allocation pointer
* Good debugging built in, showing who talloc'd what and when.
* Type-less talloc's record the file and line number at which the
allocation was made.

The main complexity is that the instantiator code statement knows who
should "own" a newly instantiated object/struct/thingy, but maybe this
need not affect construction; if every construction uses a null (or
application) context, then the instantiator could talloc_steal in the
next line, although vala's ownership transferring could be used for this
IF it could be extended to support either mere reference taking
(talloc_add_reference) OR ownership (talloc_steal).

Sam






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