c++ - 如何在 Windows 中使用 sys 标头(或找到它们的 MSVC 对应标头)?
问题描述
我正在学习如何构建 JIT 编译器并偶然发现了一段代码(附在下面):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
// Allocates RWX memory of given size and returns a pointer to it. On failure,
// prints out the error and returns NULL.
void* alloc_executable_memory(size_t size) {
void* ptr = mmap(0, size,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ptr == (void*)-1) {
perror("mmap");
return NULL;
}
return ptr;
}
void emit_code_into_memory(unsigned char* m) {
unsigned char code[] = {
0x48, 0x89, 0xf8, // mov %rdi, %rax
0x48, 0x83, 0xc0, 0x04, // add $4, %rax
0xc3 // ret
};
memcpy(m, code, sizeof(code));
}
const size_t SIZE = 1024;
typedef long (*JittedFunc)(long);
// Allocates RWX memory directly.
void run_from_rwx() {
void* m = alloc_executable_memory(SIZE);
emit_code_into_memory(m);
JittedFunc func = m;
int result = func(2);
printf("result = %d\n", result);
}
现在,在我的终端上乱扔错误消息之前,我在 MSDN 上搜索了这些功能,令我惊讶的是,它们都没有出现。这些显然是在 Windows 中不可用的 POSIX 头文件。我的问题是这些标头的 MSVC 替代品是否存在?
我已经安装了 Cygwin,但我得到 header not found 错误。
解决方案
推荐阅读
- c# - 将日期时间传递给 sql server 存储过程添加 1 秒
- reactjs - 如何使用 webpack 将拆分的块文件注入 aspx 文件?
- jenkins - 发送在 jenkins ui 生成测试结果的邮件
- sql - 如何使用 4tables 中的 case 语句连接 postgres sql 中的行
- junit - 免费标记模板 - FTL 给出新行的测试错误
- php - 使用 codeigniter 进行表单验证时出错
- azure - 是否需要有 azure 备份计划?
- javascript - 当在字段中按下 Enter 时,以编程方式模拟对元素的单击
- php - XDebug 已安装,但未在 phpinfo() 中显示
- apache-flink - Apache Zeppelin Flink Interpretor 无法连接 Flink 1.5.2