Thread overview
Is there a GC'd malloc alternative?
Feb 19, 2007
0ffh
Feb 19, 2007
Bill Baxter
Feb 20, 2007
Lutger
Feb 19, 2007
Sean Kelly
February 19, 2007
[Now, is this the right newsgroup? I am confused...]

Hi, I'm new to D but got into it pretty quickly...
it's heaven for a lazy old C coder! }:->>>

I just wonder if there is no library function that
works like 'malloc' but in garbage collected memory?

Happy hacking, 0ffh

p.s.
I am probably not looking for the 'new' operator,
as 'new' seems to take only certain types as
"argument", not the size of the requested memory
chunk, as does 'malloc'.
February 19, 2007
0ffh wrote:
> 
> [Now, is this the right newsgroup? I am confused...]

Yep this is right.

> Hi, I'm new to D but got into it pretty quickly...
> it's heaven for a lazy old C coder! }:->>>
> 
> I just wonder if there is no library function that
> works like 'malloc' but in garbage collected memory?
> 
> Happy hacking, 0ffh
> 
> p.s.
> I am probably not looking for the 'new' operator,
> as 'new' seems to take only certain types as
> "argument", not the size of the requested memory
> chunk, as does 'malloc'.

You need to be careful with that kind of thing because the garbage collector needs to know whether each chunk of allocated memory could contain pointers or not.

If you allocate as void (as Johan recommended) the gc will assume that pointers *are* possible in that memory.  That's great if you plan on putting things with pointers in there, but if you're just going to use it for some kind of raw data then you'll be wasting time having the gc scan that memory.

If you allocate as ubyte, then the gc will assume there are no pointers there and won't scan that memory for outstanding references.

(note this behavior is only for DMD 1.001 and higher, DMD 1.0 was not type-aware and just scanned everything no matter what.)

--bb
February 19, 2007
0ffh wrote:
> 
> [Now, is this the right newsgroup? I am confused...]
> 
> Hi, I'm new to D but got into it pretty quickly...
> it's heaven for a lazy old C coder! }:->>>
> 
> I just wonder if there is no library function that
> works like 'malloc' but in garbage collected memory?
> 
> Happy hacking, 0ffh
> 
> p.s.
> I am probably not looking for the 'new' operator,
> as 'new' seems to take only certain types as
> "argument", not the size of the requested memory
> chunk, as does 'malloc'.

In Tango, you'd do:

    import tango.core.Memory;

    void* rawbuf = gc.malloc( 512 );
    void* zerobuf = gc.calloc( 512 );


Sean
February 20, 2007
Bill Baxter wrote:
> You need to be careful with that kind of thing because the garbage collector needs to know whether each chunk of allocated memory could contain pointers or not.
> 
> If you allocate as void (as Johan recommended) the gc will assume that pointers *are* possible in that memory.  That's great if you plan on putting things with pointers in there, but if you're just going to use it for some kind of raw data then you'll be wasting time having the gc scan that memory.

This should also work right? std.gc.hasNoPointers(rawData.ptr);