@@ -153,7 +153,8 @@ void OsRetrieveCacheSize(std::array<int64_t, N>* cache_sizes) {
153153 while (offset + sizeof (SYSTEM_LOGICAL_PROCESSOR_INFORMATION ) <= buffer_size) {
154154 if (RelationCache == buffer_position->Relationship ) {
155155 PCACHE_DESCRIPTOR cache = &buffer_position->Cache ;
156- if (cache->Level >= 1 && cache->Level <= N) {
156+ using level_t = decltype (cache->Level );
157+ if (cache->Level >= 1 && cache->Level <= static_cast <level_t >(N)) {
157158 const int64_t current = (*cache_sizes)[cache->Level - 1 ];
158159 (*cache_sizes)[cache->Level - 1 ] = std::max<int64_t >(current, cache->Size );
159160 }
@@ -206,26 +207,28 @@ void OsRetrieveCacheSize(std::array<int64_t, N>* cache_sizes) {
206207int64_t LinuxGetCacheSize (int level) {
207208 // get cache size by sysconf()
208209# ifdef _SC_LEVEL1_DCACHE_SIZE
209- const int kCacheSizeConf [] = {
210+ constexpr auto kCacheSizeConf = std::array< int , 3 > {
210211 _SC_LEVEL1_DCACHE_SIZE,
211212 _SC_LEVEL2_CACHE_SIZE,
212213 _SC_LEVEL3_CACHE_SIZE,
213214 };
214215
215216 errno = 0 ;
217+ DCHECK (0 <= level && static_cast <std::size_t >(level) < kCacheSizeConf .size ());
216218 const int64_t cache_size = sysconf (kCacheSizeConf [level]);
217219 if (errno == 0 && cache_size > 0 ) {
218220 return cache_size;
219221 }
220222# endif
221223
222224 // get cache size from sysfs if sysconf() fails or not supported
223- const char * kCacheSizeSysfs [] = {
225+ constexpr auto kCacheSizeSysfs = std::array< const char *, 3 > {
224226 " /sys/devices/system/cpu/cpu0/cache/index0/size" , // l1d (index1 is l1i)
225227 " /sys/devices/system/cpu/cpu0/cache/index2/size" , // l2
226228 " /sys/devices/system/cpu/cpu0/cache/index3/size" , // l3
227229 };
228230
231+ DCHECK (0 <= level && static_cast <std::size_t >(level) < kCacheSizeSysfs .size ());
229232 std::ifstream cacheinfo (kCacheSizeSysfs [level], std::ios::in);
230233 if (!cacheinfo) {
231234 return 0 ;
@@ -248,7 +251,7 @@ int64_t LinuxGetCacheSize(int level) {
248251
249252template <std::size_t N>
250253void OsRetrieveCacheSize (std::array<int64_t , N>* cache_sizes) {
251- static_assert (N > = 3 );
254+ static_assert (N < = 3 );
252255 for (int i = 0 ; i < static_cast <int >(N); ++i) {
253256 const int64_t cache_size = LinuxGetCacheSize (i);
254257 if (cache_size > 0 ) {
0 commit comments