-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);
}
aikhs
  • 949
  • 2
  • 8
  • 20

1 Answers1

1

Why the posix_memalign is written as ::posix_memalign

:: without the namespace on the left refers to the global namespace

Why

Probably you are inside a namespace and you need a function in the global one. I can`t tell from the snippet

What is memory here?

A raw pointer allocated in ::posix_memalign and released in ::free(memory);

And why are we making it as a pointer to void?

Because it's just raw memory with no type, so it suits a raw pointer. Plain old C style.