auto expLong()

in src/CodeSetSize/CodeSetSize.cpp [168:203]


auto expLong(F trans, const size_t startSize, const size_t stepSize, const size_t maxSize, const int retries, bool nop=false)
{
	auto buf = (char*)VirtualAlloc(nullptr, maxSize, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
	if (!buf)
	{
		cerr << "Argh 0\n";
		exit(-1);
	}

	map<size_t, ErrorHistogram> me;
	if (nop)
	{
		fillBuffer<uint8_t>(buf, maxSize, 0x90);
	}
	else
	{
		constexpr uint32_t instr = 0xd221c921; // and ecx, ecx; and edx, edx
		fillBuffer<uint32_t>(buf, maxSize, instr);
	}

	for (auto currentSize = maxSize; currentSize >= startSize; currentSize -= stepSize)
	{
		auto& e = me[currentSize];
		// write "jmp rax" to the end of the buffer
		buf[currentSize - 2] = 255;
		buf[currentSize - 1] = 224;

		_mm_mfence();
		for (int i = 0; i < retries; i++)
		{
			auto status = trans(buf);
			e[status]++;
		}
	}
	return me;
}