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

Commit

Permalink
- pass optional delegate to rt_configOption to allow processing each …
Browse files Browse the repository at this point in the history
…found option

- use rt_configOption from gc.config
  • Loading branch information
rainers committed Nov 22, 2014
1 parent f7d5f30 commit 6e1d47c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 29 deletions.
40 changes: 16 additions & 24 deletions src/gc/config.d
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,26 @@ struct Config
size_t maxPoolSize = 64; // maximum pool size (MB)
size_t incPoolSize = 3; // pool size increment (MB)

bool initialize(...) // avoid inlining
bool initialize() @nogc
{
foreach (a; rt_options)
{
if(a.length >= 6 && a[0..6] == "gcopt=")
if (!parseOptions(a[6 .. $]))
return false;
}
if(rt_envvars_enabled)
{
auto p = getenv("DRT_GCOPT");
if (p)
if (!parseOptions(p[0 .. strlen(p)]))
return false;
}
if(rt_cmdline_enabled)
import core.internal.traits : externDFunc;

alias rt_configCallBack = string delegate(string) @nogc nothrow;
alias fn_configOption = string function(string opt, scope rt_configCallBack dg, bool reverse) @nogc nothrow;

alias rt_configOption = externDFunc!("rt.config.rt_configOption", fn_configOption);

string parse(string opt) @nogc nothrow
{
foreach (a; rt_args)
{
if(a.length >= 12 && a[0..12] == "--DRT-gcopt=")
if (!parseOptions(a[12 .. $]))
return false;
}
if (!parseOptions(opt))
return "err";
return null; // continue processing
}
return true;
string s = rt_configOption("gcopt", &parse, true);
return s is null;
}

void help() @nogc
void help() @nogc nothrow
{
string s = "GC options are specified as white space separated assignments:
disable:0|1 - start disabled (%d)
Expand All @@ -75,7 +67,7 @@ struct Config
cast(long)minPoolSize, cast(long)maxPoolSize, cast(long)incPoolSize);
}

bool parseOptions(const(char)[] opt) @nogc
bool parseOptions(const(char)[] opt) @nogc nothrow
{
size_t p = 0;
while(p < opt.length)
Expand Down
43 changes: 38 additions & 5 deletions src/rt/config.d
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,54 @@ import core.stdc.string : strlen;

extern extern(C) string[] rt_args() @nogc nothrow;

alias rt_configCallBack = string delegate(string) @nogc nothrow;

/**
* get a druntime config option using standard configuration options
* opt name of the option to retreive
* dg if non-null, passes the option through this
* delegate and only returns its return value if non-null
* reverse reverse the default processing order cmdline/envvar/rt_options
* to allow overwriting settings in the delegate with values
* from higher priority
*
* returns the options' value if
* - set on the command line as "--DRT-<opt>=value" (rt_cmdline_enabled enabled)
* - the environment variable "DRT_<OPT>" is set (rt_envvars_enabled enabled)
* - rt_options[] contains an entry "<opt>=value"
* - null otherwise
*/
string rt_configOption(string opt) @nogc nothrow
string rt_configOption(string opt, scope rt_configCallBack dg = null, bool reverse = false) @nogc nothrow
{
if (!dg)
dg = (string s) => s;

if (string s = (reverse ? rt_linkOption(opt, dg) : rt_cmdlineOption(opt, dg)))
return s;
if (string s = rt_envvarsOption(opt, dg))
return s;
if (string s = (reverse ? rt_cmdlineOption(opt, dg) : rt_linkOption(opt, dg)))
return s;
return null;
}

string rt_cmdlineOption(string opt, rt_configCallBack dg) @nogc nothrow
{
if(rt_cmdline_enabled!())
{
foreach (a; rt_args)
{
if (a.length >= opt.length + 7 && a[0..6] == "--DRT-" &&
a[6 .. 6 + opt.length] == opt && a[6 + opt.length] == '=')
return a[7 + opt.length .. $];
if (string s = dg(a[7 + opt.length .. $]))
return s;
}
}
return null;
}

string rt_envvarsOption(string opt, rt_configCallBack dg) @nogc nothrow
{
if(rt_envvars_enabled!())
{
if (opt.length >= 32)
Expand All @@ -92,13 +119,19 @@ string rt_configOption(string opt) @nogc nothrow

auto p = getenv(var.ptr);
if (p)
return cast(string) p[0 .. strlen(p)];
if (string s = dg(cast(string) p[0 .. strlen(p)]))
return s;
}
return null;
}

string rt_linkOption(string opt, rt_configCallBack dg) @nogc nothrow
{
foreach (a; rt_options!())
{
if(a.length > opt.length && a[0..opt.length] == opt && a[opt.length] == '=')
return a[opt.length + 1 .. $];
if (string s = dg(a[opt.length + 1 .. $]))
return s;
}
return null;
}

0 comments on commit 6e1d47c

Please sign in to comment.