-1
  1. Why the posix_memalign is written as ::posix_memalign?
  2. What is memory here?

I am looking to benchmark the read and write speeds of my cache memories and RAM. For this purpose, I want to use google benchmark library and I saw an example code that utilizes it. More or less I get the idea of the code but what does memory stand here? And why are we making it as a pointer to void? Also, why the example writes posix_memalign with ::? Is it because we are referencing to the google benchmark class?

#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);
}
4

1 回答 1

1

为什么 posix_memalign 写成 ::posix_memalign

:: 左边没有命名空间是指全局命名空间

为什么

可能你在一个命名空间内,你需要一个全局函数。我无法从片段中看出

这里的记忆是什么?

在 ::posix_memalign 中分配并在 ::free(memory) 中释放的原始指针;

为什么我们将它作为指向 void 的指针?

因为它只是没有类型的原始内存,所以它适合原始指针。简单的旧 C 风格。

于 2019-03-30T10:42:32.403 回答