summaryrefslogtreecommitdiff
path: root/src/os.c
diff options
context:
space:
mode:
authorGravatar Michael Smith <mikesmiffy128@gmail.com> 2026-05-10 14:51:46 +0100
committerGravatar Michael Smith <mikesmiffy128@gmail.com> 2026-10-10 04:23:05 +0100
commite66dbc42d523ec6d1a4e1b4fec21fb211eb660bb (patch)
tree4e6a4b88ff960b552fd77a43ba8902fe1295188b /src/os.c
parentc219ee378c374ceaf576ac477ccbe34a73e65d94 (diff)
downloadsst-e66dbc42d523ec6d1a4e1b4fec21fb211eb660bb.tar.gz
sst-e66dbc42d523ec6d1a4e1b4fec21fb211eb660bb.zip
Abstract page allocation and locking in os.c
Simplifies ac.c just a little bit.
Diffstat (limited to 'src/os.c')
-rw-r--r--src/os.c44
1 files changed, 40 insertions, 4 deletions
diff --git a/src/os.c b/src/os.c
index 2bc0e45..dce0d17 100644
--- a/src/os.c
+++ b/src/os.c
@@ -117,10 +117,20 @@ int os_dlfile(void *lib, ushort *buf, int sz) {
return n;
}
+void *os_pagealloc(int sz, int prot) {
+ return VirtualAlloc(0, sz, MEM_RESERVE | MEM_COMMIT, prot);
+}
+void os_pagefree(void *addr, int sz) {
+ VirtualFree(addr, sz, MEM_RELEASE);
+}
+
bool os_mprot(void *addr, int len, int mode) {
ulong old;
return !!VirtualProtect(addr, len, mode, &old);
}
+bool os_mlock(void *addr, int sz) {
+ return VirtualLock(addr, 4096);
+}
#else
@@ -155,13 +165,39 @@ void *os_dlsym(void *restrict lib, const char *restrict name) {
return dlsym(lib, name);
}
+void *os_pagealloc(int sz, int prot) {
+ sz = sz + 4095 & ~4095; // round up size
+ void *ret = mmap(0, sz, prot, MAP_SHARED | MAP_ANON, -1, 0);
+ if (ret == MAP_FAILED) return 0;
+ return ret;
+}
+void os_pagefree(void *addr, int sz) {
+ // round down address and round up size
+ addr = (void *)((ulong)addr & ~4095);
+ len = len + 4095 & ~4095;
+ munmap(addr, len);
+}
+
bool os_mprot(void *addr, int len, int mode) {
// round down address and round up size
- addr = (void *)((ulong)addr & ~(4095));
- len = len + 4095 & ~(4095);
+ addr = (void *)((ulong)addr & ~4095);
+ len = len + 4095 & ~4095;
return mprotect(addr, len, mode) != -1;
}
+bool os_mlock(void *addr, int sz) {
+#ifdef __linux__
+ // Linux-specific madvise stuff (there are some equivalents in OpenBSD and
+ // FreeBSD, if anyone's wondering, but we don't use need them for our
+ // purposes).
+ if (madvise(addr, len, MADV_DONTFORK) == -1 ||
+ madvise(addr, len, MADV_DONTDUMP) == - 1) {
+ return false;
+ }
+#endif
+ return mlock(addr, len) != -1;
+}
+
void os_randombytes(void *buf, int sz) { while (getentropy(buf, sz) == -1); }
#endif
@@ -200,8 +236,8 @@ void *os_dlhandle(const char *name) {
int namelen = strlen(lm->l_name);
int sublen = strlen(name);
if (sublen >= namelen) continue;
- if (lm->l_name[namelen - sublen - 1] == '/' && !memcmp(
- lm->l_name + namelen - sublen, name, sublen)) {
+ if (lm->l_name[namelen - sublen - 1] == '/' &&
+ !memcmp(lm->l_name + namelen - sublen, name, sublen)) {
return lm;
}
}