mprotect_test.cpp
1 #define XBYAK_NO_OP_NAMES 2 #include "xbyak/xbyak.h" 3 #include <string.h> 4 #include <vector> 5 6 struct Code : Xbyak::CodeGenerator { 7 Code(int x) 8 { 9 mov(eax, x); 10 ret(); 11 } 12 }; 13 14 int main() 15 try 16 { 17 #ifdef XBYAK_USE_MMAP_ALLOCATOR 18 puts("use Allocator with mmap"); 19 #else 20 puts("use Allocator with posix_memalign"); 21 #endif 22 const int N = 70000; 23 std::vector<Code*> v(N); 24 for (int i = 0; i < N; i++) { 25 v[i] = new Code(i); 26 } 27 long long sum = 0; 28 for (int i = 0; i < N; i++) { 29 sum += v[i]->getCode<int (*)()>()(); 30 } 31 for (int i = 0; i < N; i++) { 32 delete v[i]; 33 } 34 printf("sum=%lld\n", sum); 35 } catch (std::exception& e) { 36 printf("ERR %s\n", e.what()); 37 }