[英]Can I use a single call to munmap() to unmap two memory mappings located in a contiguous range of virtual memory?
例如,如果我這樣做:
char *pMap1; /* First mapping */
char *pReq; /* Address we would like the second mapping at */
char *pMap2; /* Second mapping */
/* Map the first 1 MB of the file. */
pMap1 = (char *)mmap(0, 1024*1024, PROT_READ, MAP_SHARED, fd, 0);
assert( pMap1!=MAP_FAILED );
/* Now map the second MB of the file. Request that the OS positions the
** second mapping immediately after the first in virtual memory. */
pReq = pMap1 + 1024*1024;
pMap2 = (char *)mmap(pReq, 1024*1024, PROT_READ, MAP_SHARED, fd, 1024*1024);
assert( pMap2!=MAP_FAILED );
/* Unmap the mappings created above. */
if( pMap2==pReq ){
munmap(pMap1, 2 * 1024*1024);
}else{
munmap(pMap1, 1 * 1024*1024);
munmap(pMap2, 1 * 1024*1024);
}
OS確實將我的第二個映射放在請求的位置(這樣(pMap2 == pReq)條件為真),對munmap()的單個調用是否會釋放所有分配的資源?
Linux手冊頁說“munmap()系統調用刪除指定地址范圍的映射......”,這表明這將起作用,但我仍然有點緊張。 即使它在Linux上運行,是否有人知道這可能是多么便攜?
首先十分感謝。
glibc手冊說沒關系:
munmap從(addr)中刪除任何內存映射到(addr + length)。 length應該是映射的長度。
在一個命令中取消映射多個映射是安全的,或者在該范圍內包含未映射的空間。 也可以僅取消映射現有映射的一部分。 但是,只能刪除整個頁面。
POSIX規范說 :
int munmap(void *addr, size_t len);
munmap()
函數應刪除包含從addr
開始並繼續len
字節的進程的地址空間的任何部分的整個頁面的任何映射。
對我來說,措辭清楚地表明好像用單個munmap()
刪除多個映射是好的,並且任何兼容的實現都應該支持。
我認為它應該有效。 POSIX規范說它會刪除
包含從
addr
開始並繼續len
字節的進程的地址空間的任何部分的整個頁面的任何映射
它描述的唯一未指定的行為是:
如果未通過調用
mmap()
建立映射,則不指定此函數的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.