spurious gc allocation

Benjamin Thaut code at benjamin-thaut.de
Thu Nov 7 22:12:54 PST 2013


Am 08.11.2013 06:19, schrieb Ellery Newcomer:
> hello all.
>
> I have a class member function that essentially looks like this:
>
> ThisNode* _InsertAllBut(int value) {
>      ThisNode* node = MallocAllocator.allocate!(ThisNode)(1);
>      node.value = value;
>      node_count++;
>      return node;
> }
>
>
> I compile it on x86_64 and the compiler inserts a gc allocation.
>
> I decompiled it, and it looks like it does this:
>
>    ThisNode* _InsertAllBut(int value) {
>        struct Thing {
>            typeof(this) thing1;
>            ThisNode* thing2;
>            int thing3;
>        }
>        Thing* rbp28 = _d_allocmemory(0x14);
>        rbp28.thing1 = this;
>        rbp28.thing3 = value;
>        if (this == 0) {
>            // not wasting my time figuring out _d_assert_msg's calling
> conventions
>            r8d = 0x137c;
>            rcx = something pointing to "src/multi_index.d";
>            rdi = {length associated with rsi};
>            rsi = something pointing to "null this";
>            rdx = {length associated with rcx};
>            _d_assert_msg();
>        }
>        invariant._d_invariant(this);
>        rbp28.thing2 = MallocAllocator.allocate(1);
>        rbp28.thing2.value = rbp28.thing3;
>        this.nodecount ++;
>        return rbp28.thing2;
>    }
>
>
> So. Why the heck is it using heap space for stack space? How the heck am
> I supposed to call this from within a destructor?


The problem is that you define the struct Thing as a inner struct. Inner 
structs have references to the outer class instance they have been 
created from. If you want a plain old struct you have to write
static struct Thing { ... }

Kind Regards
Benjamin Thaut


More information about the Digitalmars-d-learn mailing list