├── CacheLineSize.c ├── CacheLineSize.h ├── LICENSE └── README.md /CacheLineSize.c: -------------------------------------------------------------------------------- 1 | #include "CacheLineSize.h" 2 | 3 | #if defined(__APPLE__) 4 | 5 | #include 6 | size_t CacheLineSize() { 7 | size_t lineSize = 0; 8 | size_t sizeOfLineSize = sizeof(lineSize); 9 | sysctlbyname("hw.cachelinesize", &lineSize, &sizeOfLineSize, 0, 0); 10 | return lineSize; 11 | } 12 | 13 | #elif defined(_WIN32) 14 | 15 | #include 16 | #include 17 | size_t CacheLineSize() { 18 | size_t lineSize = 0; 19 | DWORD bufferSize = 0; 20 | DWORD i = 0; 21 | SYSTEM_LOGICAL_PROCESSOR_INFORMATION * buffer = 0; 22 | 23 | GetLogicalProcessorInformation(0, &bufferSize); 24 | buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION *) malloc(bufferSize); 25 | GetLogicalProcessorInformation(&buffer[0], &bufferSize); 26 | 27 | for (i = 0; i != bufferSize / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); ++i) { 28 | if (buffer[i].Relationship == RelationCache && buffer[i].Cache.Level == 1) { 29 | lineSize = buffer[i].Cache.LineSize; 30 | break; 31 | } 32 | } 33 | 34 | free(buffer); 35 | return lineSize; 36 | } 37 | 38 | #elif defined(__linux__) 39 | 40 | #include 41 | size_t CacheLineSize() { 42 | FILE * p = 0; 43 | p = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size", "r"); 44 | unsigned int lineSize = 0; 45 | if (p) { 46 | fscanf(p, "%d", &lineSize); 47 | fclose(p); 48 | } 49 | return lineSize; 50 | } 51 | 52 | #else 53 | #error Unrecognized platform 54 | #endif -------------------------------------------------------------------------------- /CacheLineSize.h: -------------------------------------------------------------------------------- 1 | #ifndef CACHELINESIZE_H_INCLUDED 2 | #define CACHELINESIZE_H_INCLUDED 3 | 4 | // Author: Nick Strupat 5 | // Date: October 29, 2010 6 | // Returns the cache line size (in bytes) of the processor, or 0 on failure 7 | 8 | #include 9 | size_t CacheLineSize(); 10 | 11 | #endif -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Nick Strupat 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CacheLineSize 2 | ================ 3 | 4 | A cross-platform C function to get the cache line size (in bytes) of the processor, or 0 on failure 5 | 6 | ## See also 7 | 8 | - [https://github.com/ulipollo/CacheLineSizeMex](https://github.com/ulipollo/CacheLineSizeMex) for the MATLAB function 9 | - [https://github.com/NickStrupat/CacheLineSize.NET](https://github.com/NickStrupat/CacheLineSize.NET) for the equivalent .NET library 10 | --------------------------------------------------------------------------------