www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is there a GC'd malloc alternative?

reply 0ffh <spam frankhirsch.net> writes:
[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'.
Feb 19 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Feb 19 2007
parent Lutger <lutger.blijdestijn gmail.com> writes:
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);
Feb 20 2007
prev sibling parent Sean Kelly <sean f4.ca> writes:
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
Feb 19 2007