How to restore the initial state of the module?
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
首先阅读 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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/mimalloc 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
Building mimalloc with `-Wundef` compiler flag logs many warnings on undefined macros可能已有人在做 @vstinner 于 1 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 64/100
microsoft/mimalloc#1506 · 5 条评论 ·
维护者通常 1 天内回复
-
stale
难度 2/5 1-3 小时 新手友好度 74/100
microsoft/mimalloc#1113 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
microsoft/mimalloc#886 · 4 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 64/100
microsoft/mimalloc#368 · 1 条评论 ·
维护者通常 1 天内回复
查看 microsoft/mimalloc 的全部 Issue
相似的 Issue
-
needs triage
难度 2/5 1-3 小时 新手友好度 70/100
389ds/389-ds-base#8157 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 67/100
coolsnowwolf/lede#14305 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
Needs-Triage
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 2 天内回复