digitalmars.D - Knowledge of managed memory pointers
- Manu via Digitalmars-d (21/21) Apr 16 2014 It occurs to me that a central issue regarding the memory management
- Kagamin (2/2) Apr 17 2014 You can do anything, what fits your task, see RefCounted and
- Manu via Digitalmars-d (10/12) Apr 17 2014 ... what?
- Kagamin (3/6) Apr 17 2014 Then what?
- Manu via Digitalmars-d (7/13) Apr 17 2014 Whatever. inc/dec ref, or not. core.memory.addRoot/Range, or not. That s...
- Tove (4/24) Apr 18 2014 Both NaCl and go-lang uses a trick to reserve a huge amount of
- Manu via Digitalmars-d (3/28) Apr 18 2014 Yeah, that's what I imagined too... but does that work in 32bit apps?
- Orvid King via Digitalmars-d (4/18) Apr 17 2014 I think the biggest advantage to this distinction would really be the
- Manu via Digitalmars-d (10/13) Apr 17 2014 But in a lightning fast way? Let's imagine ARC refcounts were stored at
- Timon Gehr (14/26) Apr 17 2014 It does not matter if changes to the type system are 'convoluted'. (They...
- Steven Schveighoffer (3/13) Apr 17 2014 I don't think this is a viable mechanism to check pointers. It's too slo...
- Kagamin (4/6) Apr 17 2014 I suggested to write a smart pointer. It could provide
- Jacob Carlborg (4/5) Apr 19 2014 Couldn't a single bit be used to indicate if it's a GC pointer or not?
- Yota (6/11) Apr 19 2014 I think he's hoping more for the static type system to
- Walter Bright (5/9) Apr 18 2014 No it isn't, in fact, the gc has to be able to tell the difference or it...
It occurs to me that a central issue regarding the memory management debate, and a major limiting factor with respect to options, is the fact that, currently, it's impossible to tell a raw pointer apart from a gc pointer. Is this is a problem worth solving? And would it be as big an enabler to address some tricky problems as it seems to be at face value? What are some options? Without turning to fat pointers or convoluted changes in the type system, are there any clever mechanisms that could be applied to distinguish managed from unmanaged pointers. If an API could be provided in druntime, it may be used by GC's, ARC, allocators, or systems that operate at the barrier between languages. Obviously it needs to be super trivial to gather this information from the pointer... On embedded systems with fixed/limited memory it's easy, just make the gc allocate pages in course physically aligned blocks and check a bit indexed by a couple of bits in pointers whether that page is owned by the GC or not. On large scale OS's with unknown quantity (perhaps lots) of memory, it's not so simple, but they have other advantages, like virtual memory managers. Can virtual pages be attributed with a bit of data somehow that's easy to look up? What about 'hacks' like an unlikely sentinel value at ptr[-1]?
Apr 16 2014
You can do anything, what fits your task, see RefCounted and Unique for an example on how to write smart pointers.
Apr 17 2014
On 17 April 2014 18:20, Kagamin via Digitalmars-d < digitalmars-d puremagic.com> wrote:You can do anything, what fits your task, see RefCounted and Unique for an example on how to write smart pointers.... what? I don't think you understood my post. void f(void* ptr) { // was ptr allocated with malloc, or new? } If we knew this, we may be able to address some problems with designing better GC's, or cross-language API's.
Apr 17 2014
On Thursday, 17 April 2014 at 12:39:59 UTC, Manu via Digitalmars-d wrote:void f(void* ptr) { // was ptr allocated with malloc, or new?Then what?
Apr 17 2014
On 18 April 2014 04:10, Kagamin via Digitalmars-d < digitalmars-d puremagic.com> wrote:On Thursday, 17 April 2014 at 12:39:59 UTC, Manu via Digitalmars-d wrote:Whatever. inc/dec ref, or not. core.memory.addRoot/Range, or not. That sort of thing. There's a persistent problem when dealing with memory systems in D that it must remain backward compatible with C and raw pointers, and that creates complications.void f(void* ptr) { // was ptr allocated with malloc, or new?Then what?
Apr 17 2014
On Friday, 18 April 2014 at 00:01:25 UTC, Manu via Digitalmars-d wrote:On 18 April 2014 04:10, Kagamin via Digitalmars-d < digitalmars-d puremagic.com> wrote:Both NaCl and go-lang uses a trick to reserve a huge amount of continuos virtual memory...On Thursday, 17 April 2014 at 12:39:59 UTC, Manu via Digitalmars-d wrote:Whatever. inc/dec ref, or not. core.memory.addRoot/Range, or not. That sort of thing. There's a persistent problem when dealing with memory systems in D that it must remain backward compatible with C and raw pointers, and that creates complications.void f(void* ptr) { // was ptr allocated with malloc, or new?Then what?
Apr 18 2014
On 18 April 2014 20:10, Tove via Digitalmars-d <digitalmars-d puremagic.com>wrote:On Friday, 18 April 2014 at 00:01:25 UTC, Manu via Digitalmars-d wrote:Yeah, that's what I imagined too... but does that work in 32bit apps?On 18 April 2014 04:10, Kagamin via Digitalmars-d < digitalmars-d puremagic.com> wrote: On Thursday, 17 April 2014 at 12:39:59 UTC, Manu via Digitalmars-d wrote:Both NaCl and go-lang uses a trick to reserve a huge amount of continuos virtual memory...void f(void* ptr)Whatever. inc/dec ref, or not. core.memory.addRoot/Range, or not. That sort of thing. There's a persistent problem when dealing with memory systems in D that it must remain backward compatible with C and raw pointers, and that creates complications.{ // was ptr allocated with malloc, or new?Then what?
Apr 18 2014
I think the biggest advantage to this distinction would really be the cross-language API's, the GC can determine which pointers it owns, although I don't believe it currently exposes this capability. On 4/17/14, Manu via Digitalmars-d <digitalmars-d puremagic.com> wrote:On 17 April 2014 18:20, Kagamin via Digitalmars-d < digitalmars-d puremagic.com> wrote:You can do anything, what fits your task, see RefCounted and Unique for an example on how to write smart pointers.... what? I don't think you understood my post. void f(void* ptr) { // was ptr allocated with malloc, or new? } If we knew this, we may be able to address some problems with designing better GC's, or cross-language API's.
Apr 17 2014
On 17 April 2014 23:14, Orvid King via Digitalmars-d < digitalmars-d puremagic.com> wrote:I think the biggest advantage to this distinction would really be the cross-language API's, the GC can determine which pointers it owns, although I don't believe it currently exposes this capability.But in a lightning fast way? Let's imagine ARC refcounts were stored at ptr[-1], how can we know if this is a managed pointer or not? I think the major hurdle in an ARC implementation is distinguishing a managed pointer from a malloc pointer without making (breaking?) changes to the type system. I would also find this useful in language boundary interaction though. I have had numerous issues identifying proper handling of cross-language memory.
Apr 17 2014
On 04/17/2014 08:55 AM, Manu via Digitalmars-d wrote:It occurs to me that a central issue regarding the memory management debate, and a major limiting factor with respect to options, is the fact that, currently, it's impossible to tell a raw pointer apart from a gc pointer. Is this is a problem worth solving? And would it be as big an enabler to address some tricky problems as it seems to be at face value? What are some options? Without turning to fat pointers or convoluted changes in the type system, are there any clever mechanisms that could be applied to distinguish managed from unmanaged pointers.It does not matter if changes to the type system are 'convoluted'. (They don't need to be.)If an API could be provided in druntime, it may be used by GC's, ARC, allocators, or systems that operate at the barrier between languages.There already is. bool isGCPointer(void* ptr){ import core.memory; return !!GC.addrOf(ptr); } void main(){ import std.c.stdlib; auto x=cast(int*)malloc(int.sizeof); auto y=new int; assert(!x.isGCPointer() && y.isGCPointer()); }
Apr 17 2014
On Thu, 17 Apr 2014 10:52:19 -0400, Timon Gehr <timon.gehr gmx.ch> wrote:On 04/17/2014 08:55 AM, Manu via Digitalmars-d wrote:I don't think this is a viable mechanism to check pointers. It's too slow. -SteveIf an API could be provided in druntime, it may be used by GC's, ARC, allocators, or systems that operate at the barrier between languages.There already is. bool isGCPointer(void* ptr){ import core.memory; return !!GC.addrOf(ptr); }
Apr 17 2014
On Thursday, 17 April 2014 at 14:59:14 UTC, Steven Schveighoffer wrote:I don't think this is a viable mechanism to check pointers. It's too slow.I suggested to write a smart pointer. It could provide compile-time checks and whatever developer feels like.
Apr 17 2014
On 2014-04-17 16:59, Steven Schveighoffer wrote:I don't think this is a viable mechanism to check pointers. It's too slow.Couldn't a single bit be used to indicate if it's a GC pointer or not? -- /Jacob Carlborg
Apr 19 2014
On Saturday, 19 April 2014 at 10:30:42 UTC, Jacob Carlborg wrote:On 2014-04-17 16:59, Steven Schveighoffer wrote:I think he's hoping more for the static type system to provide the information. I think it would be nifty if the fancy layered allocators that were being designed would return pointers with types describing the allocator itself. Could this sort of info be useful in shared libs?I don't think this is a viable mechanism to check pointers. It's too slow.Couldn't a single bit be used to indicate if it's a GC pointer or not?
Apr 19 2014
On 4/16/2014 11:55 PM, Manu via Digitalmars-d wrote:It occurs to me that a central issue regarding the memory management debate, and a major limiting factor with respect to options, is the fact that, currently, it's impossible to tell a raw pointer apart from a gc pointer.No it isn't, in fact, the gc has to be able to tell the difference or it cannot do mark/sweep. It tells the difference with "does the pointer point into the gc memory pool."What about 'hacks' like an unlikely sentinel value at ptr[-1]?That doesn't work as soon as one increments a pointer.
Apr 18 2014