forked from NickStrupat/CacheLineSize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_line_size.c
More file actions
48 lines (38 loc) · 1.09 KB
/
cache_line_size.c
File metadata and controls
48 lines (38 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "cache_line_size.h"
#if defined(__APPLE__)
#include <sys/sysctl.h>
size_t cache_line_size() {
size_t lineSize = 0;
size_t sizeOfLineSize = sizeof(lineSize);
sysctlbyname("hw.cachelinesize", &lineSize, &sizeOfLineSize, 0, 0);
return lineSize;
}
#elif defined(_WIN32)
#include <stdlib.h>
#include <windows.h>
size_t cache_line_size() {
size_t lineSize = 0;
DWORD bufferSize = 0;
DWORD i = 0;
SYSTEM_LOGICAL_PROCESSOR_INFORMATION * buffer = 0;
GetLogicalProcessorInformation(0, &bufferSize);
buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION *) malloc(bufferSize);
GetLogicalProcessorInformation(&buffer[0], &bufferSize);
for (i = 0; i != bufferSize / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); ++i) {
if (buffer[i].Relationship == RelationCache && buffer[i].Cache.Level == 1) {
lineSize = buffer[i].Cache.LineSize;
break;
}
}
free(buffer);
return lineSize;
}
#elif defined(__linux__)
#include <unistd.h>
size_t cache_line_size() {
long lineSize = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
return lineSize < 0 ? 0 : (size_t)lineSize;
}
#else
#error Unrecognized platform
#endif