2 * Contains the garbage collector configuration.
4 * Copyright: Copyright Digital Mars 2016
5 * License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
10 import core
.stdc
.stdlib
;
11 import core
.stdc
.stdio
;
12 import core
.stdc
.ctype
;
13 import core
.stdc
.string
;
17 extern extern(C
) string
[] rt_args();
19 extern extern(C
) __gshared
bool rt_envvars_enabled
;
20 extern extern(C
) __gshared
bool rt_cmdline_enabled
;
21 extern extern(C
) __gshared string
[] rt_options
;
23 __gshared Config config
;
27 bool disable
; // start disabled
28 ubyte profile
; // enable profiling with summary when terminating program
29 string gc
= "conservative"; // select gc implementation conservative|manual
31 size_t initReserve
; // initial reserve (MB)
32 size_t minPoolSize
= 1; // initial and minimum pool size (MB)
33 size_t maxPoolSize
= 64; // maximum pool size (MB)
34 size_t incPoolSize
= 3; // pool size increment (MB)
35 float heapSizeFactor
= 2.0; // heap size to used memory ratio
41 import core
.internal
.traits
: externDFunc
;
43 alias rt_configCallBack
= string
delegate(string
) @nogc nothrow;
44 alias fn_configOption
= string
function(string opt
, scope rt_configCallBack dg
, bool reverse
) @nogc nothrow;
46 alias rt_configOption
= externDFunc
!("rt.config.rt_configOption", fn_configOption
);
48 string
parse(string opt
) @nogc nothrow
50 if (!parseOptions(opt
))
52 return null; // continue processing
54 string s
= rt_configOption("gcopt", &parse
, true);
60 version (unittest) if (inUnittest
) return;
62 string s
= "GC options are specified as white space separated assignments:
63 disable:0|1 - start disabled (%d)
64 profile:0|1|2 - enable profiling with summary when terminating program (%d)
65 gc:conservative|manual - select gc implementation (default = conservative)
67 initReserve:N - initial memory to reserve in MB (%lld)
68 minPoolSize:N - initial and minimum pool size in MB (%lld)
69 maxPoolSize:N - maximum pool size in MB (%lld)
70 incPoolSize:N - pool size increment MB (%lld)
71 heapSizeFactor:N - targeted heap size to used memory ratio (%g)
73 printf(s
.ptr
, disable
, profile
, cast(long)initReserve
, cast(long)minPoolSize
,
74 cast(long)maxPoolSize
, cast(long)incPoolSize
, heapSizeFactor
);
77 bool parseOptions(string opt
)
79 opt
= skip
!isspace(opt
);
82 auto tail
= find
!(c
=> c
== ':' || c
== '=' || c
== ' ')(opt
);
83 auto name
= opt
[0 .. $ - tail
.length
];
87 opt
= skip
!isspace(tail
);
90 if (tail
.length
<= 1 || tail
[0] == ' ')
91 return optError("Missing argument for", name
);
96 foreach (field
; __traits(allMembers
, Config
))
98 static if (!is(typeof(__traits(getMember
, this, field
)) == function))
101 if (!parse(name
, tail
, __traits(getMember
, this, field
)))
109 return optError("Unknown", name
);
111 opt
= skip
!isspace(tail
);
119 bool optError(in char[] msg
, in char[] name
)
121 version (unittest) if (inUnittest
) return false;
123 fprintf(stderr
, "%.*s GC option '%.*s'.\n",
124 cast(int)msg
.length
, msg
.ptr
,
125 cast(int)name
.length
, name
.ptr
);
129 inout(char)[] skip(alias pred
)(inout(char)[] str)
131 return find
!(c
=> !pred(c
))(str);
134 inout(char)[] find(alias pred
)(inout(char)[] str)
136 foreach (i
; 0 .. str.length
)
137 if (pred(str[i
])) return str[i
.. $];
141 bool parse(T
:size_t
)(const(char)[] optname
, ref inout(char)[] str, ref T res
)
142 in { assert(str.length
); }
146 for (; i
< str.length
&& isdigit(str[i
]); ++i
)
147 v
= 10 * v
+ str[i
] - '0';
150 return parseError("a number", optname
, str);
152 return parseError("a number " ~ T
.max
.stringof
~ " or below", optname
, str[0 .. i
]);
158 bool parse(const(char)[] optname
, ref inout(char)[] str, ref bool res
)
159 in { assert(str.length
); }
162 if (str[0] == '1' ||
str[0] == 'y' ||
str[0] == 'Y')
164 else if (str[0] == '0' ||
str[0] == 'n' ||
str[0] == 'N')
167 return parseError("'0/n/N' or '1/y/Y'", optname
, str);
172 bool parse(const(char)[] optname
, ref inout(char)[] str, ref float res
)
173 in { assert(str.length
); }
177 char[1 + 10 + 1 + 2 + 1] fmt
=void;
179 immutable n
= snprintf(fmt
.ptr
, fmt
.length
, "%%%uf%%n", cast(uint)str.length
);
180 assert(n
> 4 && n
< fmt
.length
);
183 version (CRuntime_DigitalMars
)
185 /* Older sscanf's in snn.lib can write to its first argument, causing a crash
186 * if the string is in readonly memory. Recent updates to DMD
187 * https://github.com/dlang/dmd/pull/6546
188 * put string literals in readonly memory.
189 * Although sscanf has been fixed,
190 * http://ftp.digitalmars.com/snn.lib
191 * this workaround is here so it still works with the older snn.lib.
193 // Create mutable copy of str
194 const length
= str.length
;
195 char* mptr
= cast(char*)malloc(length
+ 1);
197 memcpy(mptr
, str.ptr
, length
);
199 const result
= sscanf(mptr
, fmt
.ptr
, &res
, &nscanned
);
202 return parseError("a float", optname
, str);
206 if (sscanf(str.ptr
, fmt
.ptr
, &res
, &nscanned
) < 1)
207 return parseError("a float", optname
, str);
209 str = str[nscanned
.. $];
213 bool parse(const(char)[] optname
, ref inout(char)[] str, ref inout(char)[] res
)
214 in { assert(str.length
); }
217 auto tail
= str.find
!(c
=> c
== ':' || c
== '=' || c
== ' ');
218 res
= str[0 .. $ - tail
.length
];
220 return parseError("an identifier", optname
, str);
225 bool parseError(in char[] exp
, in char[] opt
, in char[] got
)
227 version (unittest) if (inUnittest
) return false;
229 fprintf(stderr
, "Expecting %.*s as argument for GC option '%.*s', got '%.*s' instead.\n",
230 cast(int)exp
.length
, exp
.ptr
,
231 cast(int)opt
.length
, opt
.ptr
,
232 cast(int)got
.length
, got
.ptr
);
236 size_t
min(size_t a
, size_t b
) { return a
<= b ? a
: b
; }
238 version (unittest) __gshared
bool inUnittest
;
243 scope (exit
) inUnittest
= false;
246 assert(!conf
.parseOptions("disable"));
247 assert(!conf
.parseOptions("disable:"));
248 assert(!conf
.parseOptions("disable:5"));
249 assert(conf
.parseOptions("disable:y") && conf
.disable
);
250 assert(conf
.parseOptions("disable:n") && !conf
.disable
);
251 assert(conf
.parseOptions("disable:Y") && conf
.disable
);
252 assert(conf
.parseOptions("disable:N") && !conf
.disable
);
253 assert(conf
.parseOptions("disable:1") && conf
.disable
);
254 assert(conf
.parseOptions("disable:0") && !conf
.disable
);
256 assert(conf
.parseOptions("disable=y") && conf
.disable
);
257 assert(conf
.parseOptions("disable=n") && !conf
.disable
);
259 assert(conf
.parseOptions("profile=0") && conf
.profile
== 0);
260 assert(conf
.parseOptions("profile=1") && conf
.profile
== 1);
261 assert(conf
.parseOptions("profile=2") && conf
.profile
== 2);
262 assert(!conf
.parseOptions("profile=256"));
264 assert(conf
.parseOptions("disable:1 minPoolSize:16"));
265 assert(conf
.disable
);
266 assert(conf
.minPoolSize
== 16);
268 assert(conf
.parseOptions("heapSizeFactor:3.1"));
269 assert(conf
.heapSizeFactor
== 3.1f);
270 assert(conf
.parseOptions("heapSizeFactor:3.1234567890 disable:0"));
271 assert(conf
.heapSizeFactor
> 3.123f);
272 assert(!conf
.disable
);
273 assert(!conf
.parseOptions("heapSizeFactor:3.0.2.5"));
274 assert(conf
.parseOptions("heapSizeFactor:2"));
275 assert(conf
.heapSizeFactor
== 2.0f);
277 assert(!conf
.parseOptions("initReserve:foo"));
278 assert(!conf
.parseOptions("initReserve:y"));
279 assert(!conf
.parseOptions("initReserve:20.5"));
281 assert(conf
.parseOptions("help"));
282 assert(conf
.parseOptions("help profile:1"));
283 assert(conf
.parseOptions("help profile:1 help"));
285 assert(conf
.parseOptions("gc:manual") && conf
.gc
== "manual");
286 assert(conf
.parseOptions("gc:my-gc~modified") && conf
.gc
== "my-gc~modified");
287 assert(conf
.parseOptions("gc:conservative help profile:1") && conf
.gc
== "conservative" && conf
.profile
== 1);
289 // the config parse doesn't know all available GC names, so should accept unknown ones
290 assert(conf
.parseOptions("gc:whatever"));