Skip to content

Commit

Permalink
HACK: Use sbrk instead of mmap to allocate wasm pages, to skip memset…
Browse files Browse the repository at this point in the history
…. Disable vfree.
  • Loading branch information
kg committed Mar 21, 2024
1 parent 6b9d458 commit 7aff160
Showing 1 changed file with 5 additions and 28 deletions.
33 changes: 5 additions & 28 deletions src/mono/mono/utils/mono-mmap-wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ mono_setmmapjit (int flag)
/* Ignored on HOST_WASM */
}

void *sbrk (intptr_t increment);

static void*
valloc_impl (void *addr, size_t size, int flags, MonoMemAccountType type)
{
Expand All @@ -104,14 +106,8 @@ valloc_impl (void *addr, size_t size, int flags, MonoMemAccountType type)
/* emscripten throws an exception on 0 length */
return NULL;

mflags |= MAP_ANONYMOUS;
mflags |= MAP_PRIVATE;

BEGIN_CRITICAL_SECTION;
ptr = mmap (addr, size, prot, mflags, -1, 0);
END_CRITICAL_SECTION;

if (ptr == MAP_FAILED)
ptr = sbrk (size);
if (ptr == 0)
return NULL;

mono_account_mem (type, (ssize_t)size);
Expand Down Expand Up @@ -167,26 +163,7 @@ mono_valloc_aligned (size_t size, size_t alignment, int flags, MonoMemAccountTyp
int
mono_vfree (void *addr, size_t length, MonoMemAccountType type)
{
VallocInfo *info = (VallocInfo*)(valloc_hash ? g_hash_table_lookup (valloc_hash, addr) : NULL);

if (info) {
/*
* We are passed the aligned address in the middle of the mapping allocated by
* mono_valloc_align (), free the original mapping.
*/
BEGIN_CRITICAL_SECTION;
munmap (info->addr, info->size);
END_CRITICAL_SECTION;
g_free (info);
g_hash_table_remove (valloc_hash, addr);
} else {
BEGIN_CRITICAL_SECTION;
munmap (addr, length);
END_CRITICAL_SECTION;
}

mono_account_mem (type, -(ssize_t)length);

g_warning ("mono_vfree not implemented on webassembly; silently failing to free\n");
return 0;
}

Expand Down

0 comments on commit 7aff160

Please sign in to comment.