www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4047] New: [CTFE] class/struct heap allocation

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4047

           Summary: [CTFE] class/struct heap allocation
           Product: D
           Version: future
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2010-04-02 14:31:22 PDT ---
struct Foo {}
class Bar {}
int spam() {
    Foo* f = new Foo();
    Bar b = new Bar();
    return 0;
}
enum int _ = spam();
void main() {}


dmd 2.042 gives:

test.d(4): Error: Cannot interpret new Foo at compile time
test.d(8): Error: cannot evaluate spam() at compile time
test.d(8): Error: cannot evaluate spam() at compile time

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 02 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4047


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au


--- Comment #1 from Don <clugdbug yahoo.com.au> 2010-04-03 11:29:04 PDT ---
This is because pointers are not supported in CTFE. (Nor are classes).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 03 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4047



--- Comment #2 from bearophile_hugs eml.cc 2010-04-04 07:56:58 PDT ---
This is a (possibly silly) idea for a low-priority enhancement, to allow the
allocation of class instances at compile-time. This can be useful because their
creation time can be moved from run-time to compile-time.

When a class is instantiated at compile-time, it can be used and it can even
get garbage collected. When the compile-time computation ends, the compile-time
GC must collect all unused class instances (otherwise you can't tell what class
instance the compiled program will contain. The compile-time GC can be a
reference counter too, that is more deterministic) and put the active ones in a
data segment of the binary.

If a class instance is mutable, but the data segment is read-only, then at the
start of run time the runtime has to copy it into the heap and to update the
internal pointers and references.

To reduce implementation complexity, the compile-time class instances can be
immutable only, so there's no need to copy them to the heap and change the
pointers. If the the class instance is immutable but its reference is mutable
then the class instance may need a mutable boolean somewhere to tell if it's
it's garbage-collected at runtime (because it can't be really deleted from the
ROM).


This whole idea is related to the section "Pointer Hydration" written by
Walter, the main difference is that it's done by the compiler (and the data is
stored inside the binary itself), so for the programmer it's safer and simpler
to use:

http://www.artima.com/cppsource/backyard.html

Such loading of already initialized class instances is commonly done in games
written in C++, see for example the "Fast data load trick" in the Game
Programming Gems 1 book.

Many things in D are designed taking the idea from a common C++ idiom, where it
is usually implemented in a more or less manual way, and change it into a
explicit D construct, that is semantically clean and possibly safer. So this
compile-time class instances can be another exaple of such kind of idiom
conversions.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 04 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4047


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


--- Comment #3 from Walter Bright <bugzilla digitalmars.com> 2011-11-02
13:06:23 PDT ---
https://github.com/D-Programming-Language/dmd/commit/3055cc6a8b36a2e00c270b0e48e1d1bd2c931f37

https://github.com/D-Programming-Language/dmd/commit/72e750209b370b66157f320d3517c5816a3951d7

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 02 2011