From 0c65d294edb9e7c54df1f045e7107ae3991ca5f0 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sat, 4 Jun 2022 12:24:53 +0900 Subject: [PATCH] [sys] Add a function to lock a region of memory Locked memory is needed for real-time threads (which the sound mixer is when using JACK). --- include/QF/sys.h | 1 + libs/util/sys.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/include/QF/sys.h b/include/QF/sys.h index efe621f5d..45d818660 100644 --- a/include/QF/sys.h +++ b/include/QF/sys.h @@ -130,6 +130,7 @@ void Sys_PageIn (void *ptr, size_t size); size_t Sys_PageSize (void); void *Sys_Alloc (size_t size); void Sys_Free (void *mem, size_t size); +int Sys_LockMemory (void *mem, size_t size); int Sys_ProcessorCount (void); diff --git a/libs/util/sys.c b/libs/util/sys.c index e08557c81..937d832b2 100644 --- a/libs/util/sys.c +++ b/libs/util/sys.c @@ -758,6 +758,19 @@ Sys_Free (void *mem, size_t size) #endif } +VISIBLE int +Sys_LockMemory (void *mem, size_t size) +{ + size_t page_size = Sys_PageSize (); + size_t page_mask = page_size - 1; + size = (size + page_mask) & ~page_mask; +#ifdef _WIN32 + return VirtualLock (mem, size) != 0; +#else + return mlock (mem, size) == 0; +#endif +} + VISIBLE int Sys_ProcessorCount (void) {