posix_memalign
写成::posix_memalign
?memory
在这里?我希望对我的缓存和RAM的读写速度进行基准测试。为此,我想使用谷歌基准测试库,我看到了一个利用它的示例代码。或多或少我得到了代码的想法,但memory
站在这里?为什么我们将它作为指向void的指针?另外,为什么这个例子用posix_memalign
写::
?是因为我们引用了谷歌基准测试类吗?
#include <cstddef>
#include <cstdlib>
#include <string.h>
#include <emmintrin.h>
#include <immintrin.h>
#include "benchmark/benchmark.h"
#define ARGS \
->RangeMultiplier(2)->Range(1024, 2*1024*1024) \
->UseRealTime()
template <class Word>
void BM_write_seq(benchmark::State& state) {
void* memory;
if (::posix_memalign(&memory, 64, state.range_x()) != 0) return;
void* const end = static_cast<char*>(memory) + state.range_x();
Word* const p0 = static_cast<Word*>(memory);
Word* const p1 = static_cast<Word*>(end);
Word fill; ::memset(&fill, 0xab, sizeof(fill));
while (state.KeepRunning()) {
for (Word* p = p0; p < p1; ++p) {
benchmark::DoNotOptimize(*p = fill);
}
}
::free(memory);
}
为什么posix_memalign被写为:: posix_memalign
::左边没有命名空间是指全局命名空间
为什么
可能你在命名空间内,你需要一个全局功能。我不能从片段中说出来
什么是记忆?
在:: posix_memalign中分配的原始指针,在:: free(内存)中释放;
为什么我们将它作为指向void的指针?
因为它只是没有类型的原始内存,所以它适合原始指针。简单的旧C风格。