返回资讯中心
外部精选
软件工程
#开发者实时流

A neat trick I found for reusing physical memory

to it (this is what will be dup'd later):

Hacker News 最新ibobev2026/10/23 · 22 天前1 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

to it (this is what will be dup'd later):

memset(template, 67, SIZE);

now, to allocate a chunk of memory with this template, use mmap with MAP_PRIVATE:

char *copy = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);

this is cool because regardless of the size of the initial allocation, the copy isn't taking up any physical memory. and say you mutate the copy now:

copy[i] = 69;

this will only allocate a single page of memory (4 KiB); the rest of the pages are still shared with the original template!

i did a test here where i allocated 32 MiB, and then

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →