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

Commit 47ed2e1

Browse files
committed
Added UDA for GC config memory values
1 parent d0df206 commit 47ed2e1

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

src/core/gc/config.d

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ import core.internal.parseoptions;
1212

1313
__gshared Config config;
1414

15+
/// UDA for field treated as memory value
16+
struct MemVal {}
17+
1518
struct Config
1619
{
1720
bool disable; // start disabled
1821
ubyte profile; // enable profiling with summary when terminating program
1922
string gc = "conservative"; // select gc implementation conservative|precise|manual
2023

21-
size_t initReserve; // initial reserve (bytes)
22-
size_t minPoolSize = 1 << 20; // initial and minimum pool size (bytes)
23-
size_t maxPoolSize = 64 << 20; // maximum pool size (bytes)
24-
size_t incPoolSize = 3 << 20; // pool size increment (bytes)
24+
@MemVal size_t initReserve; // initial reserve (bytes)
25+
@MemVal size_t minPoolSize = 1 << 20; // initial and minimum pool size (bytes)
26+
@MemVal size_t maxPoolSize = 64 << 20; // maximum pool size (bytes)
27+
@MemVal size_t incPoolSize = 3 << 20; // pool size increment (bytes)
2528
uint parallel = 99; // number of additional threads for marking (limited by cpuid.threadsPerCPU-1)
2629
float heapSizeFactor = 2.0; // heap size to used memory ratio
2730
string cleanup = "collect"; // select gc cleanup method none|collect|finalize

src/core/internal/parseoptions.d

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import core.stdc.stdio;
1414
import core.stdc.ctype;
1515
import core.stdc.string;
1616
import core.vararg;
17-
import core.internal.traits : externDFunc;
17+
import core.internal.traits : externDFunc, hasUDA;
18+
import core.gc.config : MemVal;
1819

1920

2021
@nogc nothrow:
@@ -87,36 +88,27 @@ bool parseOptions(CFG)(ref CFG cfg, string opt)
8788
return optError("Missing argument for", name, errName);
8889
tail = tail[1 .. $];
8990

91+
NAMES_SWITCH:
9092
switch (name)
9193
{
92-
foreach (field; __traits(allMembers, CFG))
94+
static foreach (field; __traits(allMembers, CFG))
9395
{
9496
static if (!is(typeof(__traits(getMember, cfg, field)) == function))
9597
{
9698
case field:
97-
alias fieldT = typeof(__traits(getMember, cfg, field));
98-
99-
static if (is(fieldT == size_t))
100-
{
101-
// names of field what can contain B/K/M suffixes
102-
bool isSuffValue = (
103-
name == "initReserve" ||
104-
name == "minPoolSize" ||
105-
name == "maxPoolSize" ||
106-
name == "incPoolSize"
107-
);
108-
109-
bool r = parseSuff(name, tail, __traits(getMember, cfg, field), errName, true);
110-
}
99+
bool r;
100+
101+
static if (hasUDA!(__traits(getMember, cfg, field), MemVal))
102+
r = parseSuff(name, tail, __traits(getMember, cfg, field), errName, true);
111103
else
112-
bool r = parse(name, tail, __traits(getMember, cfg, field), errName);
104+
r = parse(name, tail, __traits(getMember, cfg, field), errName);
113105

114106
if (!r)
115107
return false;
116-
break;
108+
109+
break NAMES_SWITCH;
117110
}
118111
}
119-
break;
120112

121113
default:
122114
return optError("Unknown", name, errName);
@@ -336,8 +328,8 @@ unittest
336328
ubyte profile; // enable profiling with summary when terminating program
337329
string gc = "conservative"; // select gc implementation conservative|manual
338330

339-
size_t initReserve; // initial reserve (bytes)
340-
size_t minPoolSize = 1 << 20; // initial and minimum pool size (bytes)
331+
@MemVal size_t initReserve; // initial reserve (bytes)
332+
@MemVal size_t minPoolSize = 1 << 20; // initial and minimum pool size (bytes)
341333
float heapSizeFactor = 2.0; // heap size to used memory ratio
342334

343335
@nogc nothrow:

src/core/internal/traits.d

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,3 +743,33 @@ unittest
743743
static assert(!isTrue!(T, "g"));
744744
static assert(!isTrue!(T, "h"));
745745
}
746+
747+
template hasUDA(alias symbol, alias attribute)
748+
{
749+
alias attrs = __traits(getAttributes, symbol);
750+
751+
static foreach (a; attrs)
752+
{
753+
static if (is(a == attribute))
754+
{
755+
enum hasUDA = true;
756+
}
757+
}
758+
759+
static if (!__traits(compiles, (hasUDA == true)))
760+
enum hasUDA = false;
761+
}
762+
763+
unittest
764+
{
765+
struct SomeUDA{}
766+
767+
struct Test
768+
{
769+
int woUDA;
770+
@SomeUDA int withUDA;
771+
}
772+
773+
static assert(hasUDA!(Test.withUDA, SomeUDA));
774+
static assert(!hasUDA!(Test.woUDA, SomeUDA));
775+
}

0 commit comments

Comments
 (0)