How to restore the initial state of the module?

未关闭
#1,163 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
文档
描述清晰度
需要澄清
活跃度
停滞
技术栈
c
领域
devtools

调研方向

首先阅读 mi_manage_os_memory_ex()、mi_heap_delete()、mi_heap_destroy() 和 mi_heap_new_in_arena() 的 API 文档。将它们的所有权和生命周期语义与提供的 mmap 示例进行比较,包括对 mi_option_show_errors 的更改。当四个问题都有明确且有文档依据的答案时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

I want to new a heap for a module. To avoid memory fragmentation, when the module is idle, I want to restore the initial state of the heap. How should I do this? Thank you.

#include <sys/mman.h>
#include <mimalloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

int main() {
const size_t size = 1UL << 30; // 1GB
int protection = PROT_READ | PROT_WRITE;
int flags = MAP_ANONYMOUS | MAP_SHARED;
void* addr = mmap(NULL, size, protection, flags, -1, 0);

if (addr == MAP_FAILED) {
    fprintf(stderr, "Error: mmap failed\n");
    return 1;
}

memset(addr, 0, size);
printf("addr: %p, end:%p\n", addr, (char*)addr + size);

// Tell mimalloc we (the user) own the region (own=false).
// => We are responsible for calling munmap.
mi_arena_id_t arena_id;
bool ok = mi_manage_os_memory_ex(addr, size,
                                 /*commit?*/ true,
                                 /*allow_large?*/ false,
                                 /*is_pinned?*/ false,
                                 /*alignment*/ -1,
                                 /*own?*/ false, 
                                 &arena_id);
if (!ok) {
    fprintf(stderr, "Error: mi_manage_os_memory_ex failed\n");
    munmap(addr, size);
    return 1;
}

// Create a custom heap in that arena
mi_heap_t* my_heap = mi_heap_new_in_arena(arena_id);
if (!my_heap) {
    fprintf(stderr, "Error: could not create heap\n");
    munmap(addr, size);
    return 1;
}

// Allocate memory from our custom heap
void* p1 = mi_heap_malloc(my_heap, 1 * 1024 * 1024); // 1MB
void* p2 = mi_heap_malloc(my_heap, 1 * 1024 * 1024); // 1MB
printf("p1=%p, p2=%p\n", p1, p2);

// Free the allocated blocks
mi_free(p1);
mi_free(p2);

// Delete the heap
mi_heap_delete(my_heap);

// Now we can safely unmap, since mimalloc doesn't "own" the region
munmap(addr, size);

return 0;

}

(1)When the "own" parameter of mi_manage_os_memory_ex() is set to true, Do I still need to munmap?

(2)Should I use mi_heap_delete or mi_heap_destroy to restore the initial state of the heap?

(3)After restoring to the initial state, should I call mi_heap_new_in_arena(arena_id) to re-allocate a heap?

(4)I found that after calling mi_heap_destroy and mi_heap_new_in_arena(arena_id), mi_option_is_enabled(mi_option_show_errors) changed from 0 to 1.

主要语言
C
星标
13.6k
派生
1.2k
平均合并
18 小时 12 分钟
30 天内合并 PR
20

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/mimalloc 的其他 Issue

查看 microsoft/mimalloc 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。