// modified pointer chase from: // http://www.mathematik.uni-ulm.de/numerik/teaching/ss11/ScientificComputing/teil1/pointer-chasing.cpp #include #include #include typedef std::chrono::system_clock::time_point tp_t; typedef std::chrono::milliseconds ms_t; volatile void* global; // so not to have optimizations void chase_pointers(ms_t allowed_time); int main(int argc, char* argv[]) { chase_pointers(ms_t(atoi(argv[1]))); return 0; } void chase_pointers(ms_t allowed_time) { tp_t exit_time(std::chrono::system_clock::now() + allowed_time); static unsigned int size = 1024; static unsigned int len = size/sizeof(void*) + 1; void** memory = new void*[len]; static unsigned int count = size*256; void** p; // shuffle indices int* indices = new int[len]; for (unsigned int i = 0; i < len; i++) { if (exit_time < std::chrono::system_clock::now()) { goto do_exit; } indices[i] = i; } for (unsigned int i = 0; i < (len - 1); i++) { if (exit_time < std::chrono::system_clock::now()) { goto do_exit; } unsigned int j = i + lrand48()%(len - i); if (i != j) { int tmp = indices[i]; indices[i] = indices[j]; indices[j] = tmp; } } // fill memory with pointer references for (unsigned int i = 1; i < len; ++i) { if (exit_time < std::chrono::system_clock::now()) { goto do_exit; } memory[indices[i - 1]] = (void*)&memory[indices[i]]; } memory[indices[len - 1]] = (void*)&memory[indices[0]]; delete[] indices; // chase the pointers p = (void**)memory[0]; while (count-- > 0) { if (exit_time < std::chrono::system_clock::now()) { goto do_exit; } p = (void**)*p; } global = *p; do_exit: delete[] memory; }