莎拉杜蒙个人资料:Cross-platform function to get your cache line size | Nick Strupat
来源:百度文库 编辑:九乡新闻网 时间:2024/05/03 21:16:05
Cross-platform function to get your cache line size
- October 29th, 2010
- Posted in Uncategorized
- Write comment
I wrote this function for a cache line aligned memoryallocator which is for eventual use in my thread pool. It returns theline size (in bytes) of the host machine’s CPU cache, or 0 on failure.You can download it here. The source is below. Feel free to do whatever you want with it.
#ifndef GET_CACHE_LINE_SIZE_H_INCLUDED
#define GET_CACHE_LINE_SIZE_H_INCLUDED
// Author: Nick Strupat
// Date: October 29, 2010
// Returns the cache line size (in bytes) of the processor, or 0 on failure
#include
size_t cache_line_size();
#if defined(__APPLE__)
#include
size_t cache_line_size() {
size_t line_size = 0;
size_t sizeof_line_size = sizeof(line_size);
sysctlbyname("hw.cachelinesize", &line_size, &sizeof_line_size, 0, 0);
return line_size;
}
#elif defined(_WIN32)
#include
#include
size_t cache_line_size() {
size_t line_size = 0;
DWORD buffer_size = 0;
DWORD i = 0;
SYSTEM_LOGICAL_PROCESSOR_INFORMATION * buffer = 0;
GetLogicalProcessorInformation(0, &buffer_size);
buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION *)malloc(buffer_size);
GetLogicalProcessorInformation(&buffer[0], &buffer_size);
for (i = 0; i != buffer_size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); ++i) {
if (buffer[i].Relationship == RelationCache && buffer[i].Cache.Level == 1) {
line_size = buffer[i].Cache.LineSize;
break;
}
}
free(buffer);
return line_size;
}
#elif defined(linux)
#include
size_t cache_line_size() {
FILE * p = 0;
p = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size", "r");
unsigned int i = 0;
if (p) {
fscanf(p, "%d", &i);
fclose(p);
}
return i;
}
#else
#error Unrecognized platform
#endif
#endif
Cross-platform function to get your cache line size | Nick Strupat
Get On Your Boots
Cross-line iphone 4 mc-1
E-commerce platform operators urged to establ...
一定要学的公众礼仪 cross your heart
How to cross-compile BASH for Android
Nick
cache
cache
size
take me to your heart
take me to your heart
How to Organize Your Thesis
Take me to your heart
Take me to your heart``]
How To Build Traffic To Your Blog
[解剖Disruptor] Dissecting the Disruptor: Why it's so fast (part two) - Magic cache line padding
Don’t Get Your Hopes Up About Chinese Political Reform
DirectFB porting to uCLinux/EM8620L platform(转)
How do you get to school?
Learn to get to know people of high status
China to continue expanding cross-border yuan settlement
How to make your visit successful films
Points on how to improve your life