2020-07-21 22:09:15 +00:00
|
|
|
/*
|
|
|
|
* BIRD Internet Routing Daemon -- Raw allocation
|
|
|
|
*
|
|
|
|
* (c) 2020 Maria Matejka <mq@ucw.cz>
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "lib/resource.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
|
2021-08-31 22:46:46 +00:00
|
|
|
long page_size = 0;
|
2021-09-08 09:29:49 +00:00
|
|
|
_Bool alloc_multipage = 0;
|
2021-08-31 22:46:46 +00:00
|
|
|
|
2020-07-21 22:09:15 +00:00
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
static _Bool use_fake = 0;
|
|
|
|
#else
|
2021-08-31 22:46:46 +00:00
|
|
|
static _Bool use_fake = 1;
|
2020-07-21 22:09:15 +00:00
|
|
|
#endif
|
|
|
|
|
2021-08-31 22:46:46 +00:00
|
|
|
void resource_sys_init(void)
|
2020-07-21 22:09:15 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_MMAP
|
2021-08-31 22:46:46 +00:00
|
|
|
if (!(page_size = sysconf(_SC_PAGESIZE)))
|
|
|
|
die("System page size must be non-zero");
|
2020-07-21 22:09:15 +00:00
|
|
|
|
2021-08-31 22:46:46 +00:00
|
|
|
if ((u64_popcount(page_size) > 1) || (page_size > 16384))
|
|
|
|
{
|
2020-07-21 22:09:15 +00:00
|
|
|
#endif
|
2021-08-31 22:46:46 +00:00
|
|
|
/* Too big or strange page, use the aligned allocator instead */
|
|
|
|
page_size = 4096;
|
|
|
|
use_fake = 1;
|
|
|
|
}
|
2020-07-21 22:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2021-08-31 22:46:46 +00:00
|
|
|
alloc_sys_page(void)
|
2020-07-21 22:09:15 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
if (!use_fake)
|
|
|
|
{
|
2021-09-08 09:29:49 +00:00
|
|
|
if (alloc_multipage)
|
|
|
|
{
|
|
|
|
void *big = mmap(NULL, page_size * 2, PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
if (big == MAP_FAILED)
|
|
|
|
bug("mmap(%lu) failed: %m", page_size);
|
|
|
|
|
|
|
|
uintptr_t offset = ((uintptr_t) big) % page_size;
|
|
|
|
if (offset)
|
|
|
|
{
|
|
|
|
void *ret = big + page_size - offset;
|
|
|
|
munmap(big, page_size - offset);
|
|
|
|
munmap(ret + page_size, offset);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
munmap(big + page_size, page_size);
|
|
|
|
return big;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 22:46:46 +00:00
|
|
|
void *ret = mmap(NULL, page_size, PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2020-07-21 22:09:15 +00:00
|
|
|
if (ret == MAP_FAILED)
|
|
|
|
bug("mmap(%lu) failed: %m", page_size);
|
2021-09-08 09:29:49 +00:00
|
|
|
|
2020-07-21 22:09:15 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
void *ret = aligned_alloc(page_size, page_size);
|
|
|
|
if (!ret)
|
|
|
|
bug("aligned_alloc(%lu) failed", page_size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-08-31 22:46:46 +00:00
|
|
|
free_sys_page(void *ptr)
|
2020-07-21 22:09:15 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
if (!use_fake)
|
|
|
|
{
|
2021-08-31 22:46:46 +00:00
|
|
|
if (munmap(ptr, page_size) < 0)
|
2020-07-21 22:09:15 +00:00
|
|
|
bug("munmap(%p) failed: %m", ptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
free(ptr);
|
|
|
|
}
|