summaryrefslogtreecommitdiff
path: root/sysdep/unix/alloc.c
diff options
context:
space:
mode:
authorMaria Matejka <mq@ucw.cz>2021-09-01 00:46:46 +0200
committerMaria Matejka <mq@ucw.cz>2021-09-10 18:13:50 +0200
commit7f0e59820899c30a243c18556ce2e3fb72d6d221 (patch)
treec91b070b707c3c7788925619e625f465c2b290a2 /sysdep/unix/alloc.c
parent2c13759136951ef0e70a3e3c2b2d3c9a387f7ed9 (diff)
Bound allocated pages to resource pools with page caches to avoid unnecessary syscalls
Diffstat (limited to 'sysdep/unix/alloc.c')
-rw-r--r--sysdep/unix/alloc.c37
1 files changed, 16 insertions, 21 deletions
diff --git a/sysdep/unix/alloc.c b/sysdep/unix/alloc.c
index c525f713..f6296afe 100644
--- a/sysdep/unix/alloc.c
+++ b/sysdep/unix/alloc.c
@@ -16,41 +16,36 @@
#include <sys/mman.h>
#endif
+long page_size = 0;
+
#ifdef HAVE_MMAP
-static u64 page_size = 0;
static _Bool use_fake = 0;
#else
-static const u64 page_size = 4096; /* Fake page size */
+static _Bool use_fake = 1;
#endif
-u64 get_page_size(void)
+void resource_sys_init(void)
{
- if (page_size)
- return page_size;
-
#ifdef HAVE_MMAP
- if (page_size = sysconf(_SC_PAGESIZE))
- {
- if ((u64_popcount(page_size) > 1) || (page_size > 16384))
- {
- /* Too big or strange page, use the aligned allocator instead */
- page_size = 4096;
- use_fake = 1;
- }
- return page_size;
- }
+ if (!(page_size = sysconf(_SC_PAGESIZE)))
+ die("System page size must be non-zero");
- bug("Page size must be non-zero");
+ if ((u64_popcount(page_size) > 1) || (page_size > 16384))
+ {
#endif
+ /* Too big or strange page, use the aligned allocator instead */
+ page_size = 4096;
+ use_fake = 1;
+ }
}
void *
-alloc_page(void)
+alloc_sys_page(void)
{
#ifdef HAVE_MMAP
if (!use_fake)
{
- void *ret = mmap(NULL, get_page_size(), PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ void *ret = mmap(NULL, page_size, PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ret == MAP_FAILED)
bug("mmap(%lu) failed: %m", page_size);
return ret;
@@ -66,12 +61,12 @@ alloc_page(void)
}
void
-free_page(void *ptr)
+free_sys_page(void *ptr)
{
#ifdef HAVE_MMAP
if (!use_fake)
{
- if (munmap(ptr, get_page_size()) < 0)
+ if (munmap(ptr, page_size) < 0)
bug("munmap(%p) failed: %m", ptr);
}
else