Lumenarium/src_v2/platform/linux/lumenarium_linux_memory.h

74 lines
1.4 KiB
C
Raw Normal View History

2022-05-13 11:45:40 +00:00
#ifndef LUMENARIUM_LINUX_MEMORY_H
#define LUMENARIUM_LINUX_MEMORY_H 1
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
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);
linux_page_size_cache_ = (uint64_t)page_size;
2022-05-13 11:45:40 +00:00
}
return linux_page_size_cache_;
}
#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)
#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)
{
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
{
uint64_t grow = os_page_size() - rem;
2022-05-13 11:45:40 +00:00
size += grow;
}
return (size_t)size;
}
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;
}
uint8_t*
os_mem_commit(uint8_t* base, uint64_t size)
2022-05-13 11:45:40 +00:00
{
return base;
}
bool
os_mem_decommit(uint8_t* base, uint64_t size)
2022-05-13 11:45:40 +00:00
{
return 1; // true
}
bool
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