Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 94e58d3

Browse files
authored
Merge pull request #2465 from ibuclaw/fixsideeffect
rt: Remove side-effects from 'p[0..ci.initializer.length] = ci.initializer[]' merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents e89660d + ee9ba1d commit 94e58d3

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/rt/ehalloc.d

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ extern (C) Throwable _d_newThrowable(const TypeInfo_Class ci)
3636
assert(!(ci.m_flags & TypeInfo_Class.ClassFlags.isCPPclass));
3737

3838
import core.stdc.stdlib : malloc;
39-
void* p = malloc(ci.initializer.length);
39+
auto init = ci.initializer;
40+
void* p = malloc(init.length);
4041
if (!p)
4142
{
4243
import core.exception : onOutOfMemoryError;
@@ -46,14 +47,14 @@ extern (C) Throwable _d_newThrowable(const TypeInfo_Class ci)
4647
debug(PRINTF) printf(" p = %p\n", p);
4748

4849
// initialize it
49-
p[0 .. ci.initializer.length] = ci.initializer[];
50+
p[0 .. init.length] = init[];
5051

5152
if (!(ci.m_flags & TypeInfo_Class.ClassFlags.noPointers))
5253
{
5354
// Inform the GC about the pointers in the object instance
5455
import core.memory : GC;
5556

56-
GC.addRange(p, ci.initializer.length, ci);
57+
GC.addRange(p, init.length, ci);
5758
}
5859

5960
debug(PRINTF) printf("initialization done\n");

src/rt/lifetime.d

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ extern (C) Object _d_newclass(const ClassInfo ci)
6868
import core.stdc.stdlib;
6969
import core.exception : onOutOfMemoryError;
7070
void* p;
71+
auto init = ci.initializer;
7172

7273
debug(PRINTF) printf("_d_newclass(ci = %p, %s)\n", ci, cast(char *)ci.name);
7374
if (ci.m_flags & TypeInfo_Class.ClassFlags.isCOMclass)
@@ -76,7 +77,7 @@ extern (C) Object _d_newclass(const ClassInfo ci)
7677
* function called by Release() when Release()'s reference count goes
7778
* to zero.
7879
*/
79-
p = malloc(ci.initializer.length);
80+
p = malloc(init.length);
8081
if (!p)
8182
onOutOfMemoryError();
8283
}
@@ -90,26 +91,26 @@ extern (C) Object _d_newclass(const ClassInfo ci)
9091
attr |= BlkAttr.FINALIZE;
9192
if (ci.m_flags & TypeInfo_Class.ClassFlags.noPointers)
9293
attr |= BlkAttr.NO_SCAN;
93-
p = GC.malloc(ci.initializer.length, attr, ci);
94+
p = GC.malloc(init.length, attr, ci);
9495
debug(PRINTF) printf(" p = %p\n", p);
9596
}
9697

9798
debug(PRINTF)
9899
{
99100
printf("p = %p\n", p);
100-
printf("ci = %p, ci.init.ptr = %p, len = %llu\n", ci, ci.initializer.ptr, cast(ulong)ci.initializer.length);
101-
printf("vptr = %p\n", *cast(void**) ci.initializer);
102-
printf("vtbl[0] = %p\n", (*cast(void***) ci.initializer)[0]);
103-
printf("vtbl[1] = %p\n", (*cast(void***) ci.initializer)[1]);
104-
printf("init[0] = %x\n", (cast(uint*) ci.initializer)[0]);
105-
printf("init[1] = %x\n", (cast(uint*) ci.initializer)[1]);
106-
printf("init[2] = %x\n", (cast(uint*) ci.initializer)[2]);
107-
printf("init[3] = %x\n", (cast(uint*) ci.initializer)[3]);
108-
printf("init[4] = %x\n", (cast(uint*) ci.initializer)[4]);
101+
printf("ci = %p, ci.init.ptr = %p, len = %llu\n", ci, init.ptr, cast(ulong)init.length);
102+
printf("vptr = %p\n", *cast(void**) init);
103+
printf("vtbl[0] = %p\n", (*cast(void***) init)[0]);
104+
printf("vtbl[1] = %p\n", (*cast(void***) init)[1]);
105+
printf("init[0] = %x\n", (cast(uint*) init)[0]);
106+
printf("init[1] = %x\n", (cast(uint*) init)[1]);
107+
printf("init[2] = %x\n", (cast(uint*) init)[2]);
108+
printf("init[3] = %x\n", (cast(uint*) init)[3]);
109+
printf("init[4] = %x\n", (cast(uint*) init)[4]);
109110
}
110111

111112
// initialize it
112-
p[0 .. ci.initializer.length] = ci.initializer[];
113+
p[0 .. init.length] = init[];
113114

114115
debug(PRINTF) printf("initialization done\n");
115116
return cast(Object) p;

0 commit comments

Comments
 (0)