| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- /* mmap backing for the ring-3 allocator reused by the host browser.
- *
- * libc/malloc.c reserves a 64 MB arena (its HEAP_MAX_BYTES) and grows it in
- * place; reserve the whole range PROT_NONE up front so the grow is a
- * mprotect and the arena can never move. */
- #include <stddef.h>
- #include <sys/mman.h>
- #define HOST_ARENA_BYTES (64u * 1024u * 1024u)
- static unsigned char *base;
- void *__heap_map(unsigned int bytes)
- {
- if (base || bytes == 0 || bytes > HOST_ARENA_BYTES)
- return 0;
- base = (unsigned char *)mmap(0, HOST_ARENA_BYTES, PROT_NONE,
- MAP_PRIVATE | MAP_ANONYMOUS |
- MAP_NORESERVE, -1, 0);
- if (base == MAP_FAILED) {
- base = 0;
- return 0;
- }
- if (mprotect(base, bytes, PROT_READ | PROT_WRITE) != 0) {
- munmap(base, HOST_ARENA_BYTES);
- base = 0;
- return 0;
- }
- return base;
- }
- int __heap_grow(unsigned int total_bytes)
- {
- if (!base || total_bytes > HOST_ARENA_BYTES)
- return -1;
- return mprotect(base, total_bytes, PROT_READ | PROT_WRITE) == 0 ?
- 0 : -1;
- }
- void __heap_unmap(void)
- {
- if (base) {
- munmap(base, HOST_ARENA_BYTES);
- base = 0;
- }
- }
|