b151511e96b30dd4d28079d90708d95cdc3eda1e
[platform/upstream/coreclr.git] / src / gc / gcconfig.cpp
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 #include "common.h"
6 #include "gcenv.h"
7 #include "gc.h"
8
9 #define BOOL_CONFIG(name, key, default, unused_doc)            \
10   bool GCConfig::Get##name() { return s_##name; }              \
11   bool GCConfig::s_##name = default;
12
13 #define INT_CONFIG(name, key, default, unused_doc)             \
14   int64_t GCConfig::Get##name() { return s_##name; }           \
15   int64_t GCConfig::s_##name = default;
16
17 // String configs are not cached because 1) they are rare and
18 // not on hot paths and 2) they involve transfers of ownership
19 // of EE-allocated strings, which is potentially complicated.
20 #define STRING_CONFIG(name, key, unused_doc)                   \
21   GCConfigStringHolder GCConfig::Get##name()                   \
22   {                                                            \
23       const char* resultStr = nullptr;                         \
24       GCToEEInterface::GetStringConfigValue(key, &resultStr);  \
25       return GCConfigStringHolder(resultStr);                  \
26   }
27
28 GC_CONFIGURATION_KEYS
29
30 #undef BOOL_CONFIG
31 #undef INT_CONFIG
32 #undef STRING_CONFIG
33
34 void GCConfig::Initialize()
35 {
36 #define BOOL_CONFIG(name, key, default, unused_doc)          \
37     GCToEEInterface::GetBooleanConfigValue(key, &s_##name);
38
39 #define INT_CONFIG(name, key, default, unused_doc)           \
40     GCToEEInterface::GetIntConfigValue(key, &s_##name);
41
42 #define STRING_CONFIG(unused_name, unused_key, unused_doc)
43
44 GC_CONFIGURATION_KEYS
45
46 #undef BOOL_CONFIG
47 #undef INT_CONFIG
48 }
49
50 bool ParseGCHeapAffinitizeRanges(AffinitySet* config_affinity_set)
51 {
52     bool success = true;
53
54     GCConfigStringHolder cpu_index_ranges_holder(GCConfig::GetGCHeapAffinitizeRanges());
55     const char* cpu_index_ranges = cpu_index_ranges_holder.Get();
56
57     // The cpu index ranges is a comma separated list of indices or ranges of indices (e.g. 1-5).
58     // Example 1,3,5,7-9,12
59
60     if (cpu_index_ranges != NULL)
61     {
62         char* number_end;
63
64         do
65         {
66             size_t start_index = strtoul(cpu_index_ranges, &number_end, 10);
67
68             if (number_end == cpu_index_ranges)
69             {
70                 // No number found, invalid format
71                 break;
72             }
73
74             size_t end_index = start_index;
75
76             if (*number_end == '-')
77             {
78                 char* range_end_start = number_end + 1;
79                 end_index = strtoul(range_end_start, &number_end, 10);
80                 if (number_end == range_end_start)
81                 {
82                     // No number found, invalid format
83                     break;
84                 }
85             }
86
87             if ((start_index < MAX_SUPPORTED_CPUS) && end_index < (MAX_SUPPORTED_CPUS))
88             {
89                 for (size_t i = start_index; i <= end_index; i++)
90                 {
91                     config_affinity_set->Add(i);
92                 }
93             }
94
95             cpu_index_ranges = number_end + 1;
96         }
97         while (*number_end == ',');
98
99         success = (*number_end == '\0');
100     }
101
102     return success;
103 }