Skip to content

Commit

Permalink
Merge pull request #11726 from dra27/munmap
Browse files Browse the repository at this point in the history
Don't call `mmap`/`munmap` with `len = 0`
  • Loading branch information
dra27 committed Nov 21, 2022
2 parents 6bd5cdd + ed1cab0 commit ee75054
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions runtime/platform.c
Expand Up @@ -152,6 +152,14 @@ uintnat caml_mem_round_up_pages(uintnat size)
static struct lf_skiplist mmap_blocks = {NULL};
#endif

#ifndef _WIN32
Caml_inline void safe_munmap(uintnat addr, uintnat size)
{
if (size > 0)
munmap((void*)addr, size);
}
#endif

void* caml_mem_map(uintnat size, uintnat alignment, int reserve_only)
{
uintnat alloc_sz = caml_mem_round_up_pages(size + alignment);
Expand Down Expand Up @@ -212,8 +220,8 @@ void* caml_mem_map(uintnat size, uintnat alignment, int reserve_only)
}
CAMLassert(mem == (void*)aligned_start);
#else
munmap((void*)base, aligned_start - base);
munmap((void*)aligned_end, (base + alloc_sz) - aligned_end);
safe_munmap(base, aligned_start - base);
safe_munmap(aligned_end, (base + alloc_sz) - aligned_end);
#endif
#ifdef DEBUG
caml_lf_skiplist_insert(&mmap_blocks,
Expand Down Expand Up @@ -255,13 +263,13 @@ void* caml_mem_commit(void* mem, uintnat size)

void caml_mem_decommit(void* mem, uintnat size)
{
if (size) {
#ifdef _WIN32
/* VirtualFree can't decommit zero bytes */
if (size)
VirtualFree(mem, size, MEM_DECOMMIT);
#else
map_fixed(mem, size, PROT_NONE);
map_fixed(mem, size, PROT_NONE);
#endif
}
}

void caml_mem_unmap(void* mem, uintnat size)
Expand Down

0 comments on commit ee75054

Please sign in to comment.