2022-05-13 11:45:40 +00:00
|
|
|
#ifndef LUMENARIUM_LINUX_MEMORY_H
|
|
|
|
#define LUMENARIUM_LINUX_MEMORY_H 1
|
|
|
|
|
2022-05-25 12:30:20 +00:00
|
|
|
static uint64_t linux_page_size_cache_ = 0;
|
2022-05-13 11:45:40 +00:00
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/3351940/detecting-the-memory-page-size
|
2022-05-25 12:30:20 +00:00
|
|
|
uint64_t
|
2022-05-13 11:45:40 +00:00
|
|
|
os_page_size()
|
|
|
|
{
|
|
|
|
if (linux_page_size_cache_ == 0)
|
|
|
|
{
|
|
|
|
long page_size = sysconf(_SC_PAGE_SIZE);
|
2022-05-25 12:30:20 +00:00
|
|
|
linux_page_size_cache_ = (uint64_t)page_size;
|
2022-05-13 11:45:40 +00:00
|
|
|
}
|
|
|
|
return linux_page_size_cache_;
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:30:20 +00:00
|
|
|
#define OS_MEM_PAGE_SIZE os_page_size()
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
# define linux_memory_assert_always (*((volatile int*)0) = 0xFFFF)
|
|
|
|
# define linux_memory_assert(c) do { \
|
|
|
|
if (!(c)) { \
|
|
|
|
linux_memory_assert_always;\
|
|
|
|
}} while(false)
|
2022-08-10 19:52:49 +00:00
|
|
|
#else
|
|
|
|
# define linux_memory_assert(c)
|
2022-05-25 12:30:20 +00:00
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
#define SIZE_T_MAX_U64 (uint64_t)((size_t)0 - 1)
|
|
|
|
|
2022-05-13 11:45:40 +00:00
|
|
|
size_t
|
|
|
|
linux_round_to_page_size(uint64_t size)
|
|
|
|
{
|
2022-05-25 12:30:20 +00:00
|
|
|
uint64_t m = SIZE_T_MAX_U64;
|
|
|
|
linux_memory_assert(size < SIZE_T_MAX_U64);
|
|
|
|
uint64_t rem = size % os_page_size();
|
|
|
|
if (rem != 0 || size < os_page_size())
|
2022-05-13 11:45:40 +00:00
|
|
|
{
|
2022-05-25 12:30:20 +00:00
|
|
|
uint64_t grow = os_page_size() - rem;
|
2022-05-13 11:45:40 +00:00
|
|
|
size += grow;
|
|
|
|
}
|
|
|
|
return (size_t)size;
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:30:20 +00:00
|
|
|
uint8_t*
|
|
|
|
os_mem_reserve(uint64_t size)
|
2022-05-13 11:45:40 +00:00
|
|
|
{
|
|
|
|
size_t size_cvt = linux_round_to_page_size(size);
|
|
|
|
uint8_t* result = (uint8_t*)malloc(size_cvt);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:30:20 +00:00
|
|
|
uint8_t*
|
|
|
|
os_mem_commit(uint8_t* base, uint64_t size)
|
2022-05-13 11:45:40 +00:00
|
|
|
{
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2022-05-25 12:30:20 +00:00
|
|
|
os_mem_decommit(uint8_t* base, uint64_t size)
|
2022-05-13 11:45:40 +00:00
|
|
|
{
|
|
|
|
return 1; // true
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2022-05-25 12:30:20 +00:00
|
|
|
os_mem_release(uint8_t* base, uint64_t size)
|
2022-05-13 11:45:40 +00:00
|
|
|
{
|
|
|
|
free(base);
|
|
|
|
return 1; // true
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif // LUMENARIUM_LINUX_MEMORY_H
|