Manual memory management in D2

Leandro Lucarella luca at llucax.com.ar
Tue Jul 13 15:50:50 PDT 2010


Andrei Alexandrescu, el 13 de julio a las 16:58 me escribiste:
> On 07/13/2010 04:42 PM, Vladimir Panteleev wrote:
> >On Wed, 14 Jul 2010 00:33:20 +0300, bearophile
> ><bearophileHUGS at lycos.com> wrote:
> >
> >>Vladimir Panteleev:
> >>>ARGHARGHARGH
> >>
> >>There is emplace() now. I don't know how much good it currently is.
> >
> >I know that I can do this manually in other ways, the problem is that
> >this creates verbose and unintuitive code.
> 
> I agree. I think we should, again, define two primitives:
> 
> T* malloc(T, A...)(A args) if (!is(T == class) && !isDynamicArray!T);
> T malloc(T, A...)(A args) if (is(T == class) || isDynamicArray!T);
> free(T)(ref T obj);
> 
> The first two call C's malloc, constructor (if any), and returns the
> allocated object. The last calls the destructor (if any) and then
> free().
> 
> worksforyou?

Please, don't name them malloc() and free(), their names are highly
associated with just asking for memory. If you ditch new and delete,
just name them new() and delete() :). But maybe it isn't a good idea
anyway because new/delete are highly associated with the GC (OTOH you
can overload new/delete to use other allocators). Maybe create() and
destroy()?

They could accept an optional function pointer/delegate with the same
signature as malloc/free to do the actual allocation/freeing and default
to C's malloc/free.

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
Vivimos en una época muy contemporánea, Don Inodoro...
	-- Mendieta


More information about the Digitalmars-d mailing list