26 #ifndef CORE_UTILS_MEMORY_UTILS_H_ 27 #define CORE_UTILS_MEMORY_UTILS_H_ 39 const static long PAGE_SIZE = sysconf(_SC_PAGESIZE);
41 inline constexpr
size_t operator"" _kB(
unsigned long long int bytes) {
45 inline constexpr
size_t operator"" _MB(
unsigned long long int bytes) {
46 return 1024 * 1024 * bytes;
49 inline constexpr
size_t operator"" _GB(
unsigned long long int bytes) {
50 return 1024 * 1024 * 1024 * bytes;
53 inline constexpr
double operator"" _kB(
long double bytes) {
57 inline constexpr
double operator"" _MB(
long double bytes) {
58 return 1024. * 1024. * bytes;
61 inline constexpr
double operator"" _GB(
long double bytes) {
62 return 1024. * 1024. * 1024. * bytes;
67 return aligned_alloc(PAGE_SIZE, bytes);
72 size_t bytes = elems *
sizeof(T);
74 madvise(p, bytes, MADV_HUGEPAGE);
82 inline constexpr T
kB(T bytes) {
83 return bytes / (T)1024;
87 inline constexpr T
MB(T bytes) {
88 return bytes / ((T)1024 * T(1024));
92 inline constexpr T
GB(T bytes) {
93 return bytes / ((T)1024 * (T)1024 * (T)1024);
100 for (uint32_t alignPower = 0; alignPower < 32; alignPower++) {
101 const uint32_t alignment = 1
u << alignPower;
102 if ((ptr & alignment) != 0) {
117 inline T
align(T number, uint32_t alignment) {
118 T off = number % alignment;
122 return number + alignment - off;
126 template<
typename T,
typename... Args>
129 return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
void * page_aligned_alloc(size_t bytes)
T align(T number, uint32_t alignment)
std::unique_ptr< T > make_unique(Args &&... args)
uint32_t alignmentOf(uintptr_t ptr)