New memory APIs

[+] AuMemory::Cache
[+] AuMemory::SwapLock
[+] AuMemory::Transition

AuROXTL
[+] AuPageRoundUp
[+] AuPageRound
This commit is contained in:
Reece Wilson 2022-03-21 05:20:19 +00:00
parent 8b4bdbd04b
commit 0fb514f856
16 changed files with 294 additions and 8 deletions

View File

@ -0,0 +1,14 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Cache.hpp
Date: 2022-3-21
Author: Reece
***/
#pragma once
namespace Aurora::Memory::Cache
{
AUKN_SYM void OptimizeAddressRangeOnCore(const AuList<AuPair<AuUInt, AuUInt>> &addressRange);
AUKN_SYM void ClearInstructionCache(const AuPair<AuUInt, AuUInt> &addressRange);
}

View File

@ -13,6 +13,10 @@
#include "Heap.hpp" #include "Heap.hpp"
#include "MemoryView.hpp" #include "MemoryView.hpp"
#include "Cache.hpp"
#include "SwapLock.hpp"
#include "Transition.hpp"
namespace Aurora::Memory namespace Aurora::Memory
{ {
AUKN_SYM AU_ALLOC void *_ZAlloc(Types::size_t length); AUKN_SYM AU_ALLOC void *_ZAlloc(Types::size_t length);
@ -25,7 +29,8 @@ namespace Aurora::Memory
AUKN_SYM AU_ALLOC void *_FRealloc(void *buffer, Types::size_t length); AUKN_SYM AU_ALLOC void *_FRealloc(void *buffer, Types::size_t length);
AUKN_SYM void _Free(void *buffer); AUKN_SYM void _Free(void *buffer);
AUKN_SYM Types::size_t GetChunkSize(const void *head); AUKN_SYM AuUInt GetChunkSize(const void *head);
AUKN_SYM AuUInt GetPageSize();
// These memory management APIs do not support class types, and will likely *never* support them // These memory management APIs do not support class types, and will likely *never* support them
// QST already handles dynamic allocation of structs in a given heap properly (afaik) // QST already handles dynamic allocation of structs in a given heap properly (afaik)

View File

@ -75,9 +75,9 @@ namespace Aurora::Memory
this->length = length; this->length = length;
} }
AuUInt8 *ToPointer() AuConditional_t<Readonly_b, const AuUInt8 *, AuUInt8 *> ToPointer()
{ {
return reinterpret_cast<AuUInt8 *>(ptr); return reinterpret_cast<AuConditional_t<Readonly_b, const AuUInt8 *, AuUInt8 *>>(ptr);
} }
AuUInt ToPointerValue() const AuUInt ToPointerValue() const

View File

@ -0,0 +1,14 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: SwapLock.hpp
Date: 2022-3-21
Author: Reece
***/
#pragma once
namespace Aurora::Memory::SwapLock
{
AUKN_SYM bool Lock(const AuList<AuPair<AuUInt, AuUInt>> &addressRange);
AUKN_SYM bool Unlock(const AuList<AuPair<AuUInt, AuUInt>> &addressRange);
}

View File

@ -0,0 +1,33 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Transition.hpp
Date: 2022-3-21
Author: Reece
***/
#pragma once
namespace Aurora::Memory::Transition
{
struct EXTransitionOperationBase
{
AuUInt RWAddress;
AuUInt EXAddress;
AuUInt length;
};
struct EXTransitionOperation
{
EXTransitionOperationBase base;
union
{
AuUInt8 reserved[64];
};
};
AUKN_SHARED_API(MemoryTransactor, EXTransitionOperation);
AUKN_SYM bool ReservePhysicalMemory(const AuSPtr<EXTransitionOperation> &operation, AuUInt length);
AUKN_SYM bool CommitToPhysical(const AuSPtr<EXTransitionOperation> &operation);
AUKN_SYM void Free(const AuSPtr<EXTransitionOperation> &operation);
}

View File

@ -23,4 +23,16 @@ template<class T>
constexpr const T AuConstPow(const T base, const AuUInt8 exponent) constexpr const T AuConstPow(const T base, const AuUInt8 exponent)
{ {
return exponent ? base * AuConstPow(base, exponent - 1) : 1; return exponent ? base * AuConstPow(base, exponent - 1) : 1;
}
template<class T>
constexpr const T AuPageRoundUp(const T value, const T pageSize)
{
return (value + (pageSize - 1)) & ~(pageSize - 1);
}
template<class T>
constexpr const T AuPageRound(const T value, const T pageSize)
{
return value & ~(pageSize - 1);
} }

View File

@ -9,6 +9,8 @@
namespace Aurora::HWInfo namespace Aurora::HWInfo
{ {
inline AuUInt32 gPageSize;
template<typename T> template<typename T>
AuOptional<T> QueryBsdHwStat(int id); AuOptional<T> QueryBsdHwStat(int id);
void Init(); void Init();

View File

@ -30,7 +30,6 @@
namespace Aurora::HWInfo namespace Aurora::HWInfo
{ {
static AuOptional<RamStat> gMemStartup; static AuOptional<RamStat> gMemStartup;
static AuUInt32 gPageSize;
AUKN_SYM AuOptional<RamStat> GetMemStatProcess() AUKN_SYM AuOptional<RamStat> GetMemStatProcess()
{ {

77
Source/Memory/Cache.cpp Normal file
View File

@ -0,0 +1,77 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Cache.cpp
Date: 2022-3-21
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Memory.hpp"
#include "Cache.hpp"
#include <Source/HWInfo/HWInfo.hpp>
#if defined(AURORA_IS_LINUX_DERIVED)
#include <sys/mman.h>
#endif
namespace Aurora::Memory::Cache
{
AUKN_SYM void OptimizeAddressRangeOnCore(const AuList<AuPair<AuUInt, AuUInt>> &addressRanges)
{
#if defined(AURORA_IS_MODERNNT_DERIVED)
AuList<WIN32_MEMORY_RANGE_ENTRY> arry;
if (!AuTryResize(arry, addressRanges.size()))
{
return;
}
AuUInt index {};
#endif
for (const auto &addressRange : addressRanges)
{
auto base = AuPageRound(addressRange.first, AuHwInfo::gPageSize);
auto length = AuPageRoundUp(addressRange.second + (addressRange.first - base), AuHwInfo::gPageSize);
#if defined(AURORA_IS_MODERNNT_DERIVED)
arry[index++] = {
AuReinterpretCast<LPVOID>(base),
length
};
#endif
#if defined(AURORA_IS_POSIX_DERIVED)
// The posix_madvise() interface conforms to IEEE Std 1003.1-2001 ("POSIX.1").
// The madvise() system call first appeared in 4.4BSD.
// https://www.freebsd.org/cgi/man.cgi?query=madvise&apropos=0&sektion=2&manpath=FreeBSD+9.0-RELEASE&arch=default&format=html
// https://man7.org/linux/man-pages/man2/madvise.2.html
if (madvise(AuReinterpretCast<const void *>(base), length, MADV_WILLNEED) != 0)
{
SysPushErrorHAL("Couldn't advice memory range 0x{:x} {}", base, length);
}
#endif
}
#if defined(AURORA_IS_MODERNNT_DERIVED)
if (!PrefetchVirtualMemory(GetCurrentProcess(), arry.size(), arry.data(), 0))
{
SysPushErrorHAL("Couldn't pin memory range into cache");
}
#endif
}
AUKN_SYM void ClearInstructionCache(const AuPair<AuUInt, AuUInt> &addressRange)
{
auto base = AuPageRound(addressRange.first, AuHwInfo::gPageSize);
auto length = AuPageRoundUp(addressRange.second + (addressRange.first - base), AuHwInfo::gPageSize);
#if defined(AURORA_IS_MODERNNT_DERIVED)
FlushInstructionCache(GetCurrentProcess(), AuReinterpretCast<LPCVOID>(base), length);
#elif defined(AURORA_COMPILER_GCC)
__builtin___clear_cache(AuReinterpretCast<const char *>(base), AuReinterpretCast<const char *>(base) + length);
#elif defined(AURORA_COMPILER_CLANG)
__clear_cache(AuReinterpretCast<const char *>(base), AuReinterpretCast<const char *>(base) + length);
#endif
}
}

View File

@ -1,13 +1,13 @@
/*** /***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved. Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: PlatformChannel.Win32.hpp File: Cache.hpp
Date: 2022-2-1 Date: 2022-3-21
Author: Reece Author: Reece
***/ ***/
#pragma once #pragma once
namespace Aurora::IO::Net namespace Aurora::Memory::Cache
{ {
} }

View File

@ -7,3 +7,7 @@
***/ ***/
#pragma once #pragma once
namespace Aurora::Memory
{
}

View File

@ -8,10 +8,15 @@
#include <Source/RuntimeInternal.hpp> #include <Source/RuntimeInternal.hpp>
#include "Memory.hpp" #include "Memory.hpp"
#include <mimalloc.h> #include <mimalloc.h>
#include <Source/HWInfo/HWInfo.hpp>
#if defined(AURORA_IS_LINUX_DERIVED)
#include <sys/mman.h>
#endif
namespace Aurora::Memory namespace Aurora::Memory
{ {
AUKN_SYM Types::size_t GetChunkSize(const void *head) AUKN_SYM AuUInt GetChunkSize(const void *head)
{ {
return mi_malloc_size(head); return mi_malloc_size(head);
} }
@ -60,4 +65,9 @@ namespace Aurora::Memory
{ {
mi_free(buffer); mi_free(buffer);
} }
AUKN_SYM AuUInt GetPageSize()
{
return AuHwInfo::GetPageSize();
}
} }

View File

@ -0,0 +1,70 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: SwapLock.cpp
Date: 2022-3-21
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Memory.hpp"
#include "SwapLock.hpp"
#include <Source/HWInfo/HWInfo.hpp>
#if defined(AURORA_IS_LINUX_DERIVED)
#include <sys/mman.h>
#endif
namespace Aurora::Memory::SwapLock
{
AUKN_SYM bool Lock(const AuList<AuPair<AuUInt, AuUInt>> &addressRanges)
{
for (const auto &addressRange : addressRanges)
{
auto base = AuPageRound(addressRange.first, AuHwInfo::gPageSize);
auto length = AuPageRoundUp(addressRange.second + (addressRange.first - base), AuHwInfo::gPageSize);
#if defined(AURORA_IS_MODERNNT_DERIVED)
if (!VirtualLock(AuReinterpretCast<LPVOID>(base), length))
{
SysPushErrorHAL("Couldn't lock memory 0x{:x} {}", base, length);
return false;
}
#endif
#if defined(AURORA_IS_POSIX_DERIVED)
if (mlock(AuReinterpretCast<const void *>(base), length) != 0)
{
SysPushErrorHAL("Couldn't lock memory 0x{:x} {}", base, length);
return false;
}
#endif
}
return true;
}
AUKN_SYM bool Unlock(const AuList<AuPair<AuUInt, AuUInt>> &addressRanges)
{
for (const auto &addressRange : addressRanges)
{
auto base = AuPageRound(addressRange.first, AuHwInfo::gPageSize);
auto length = AuPageRoundUp(addressRange.second + (addressRange.first - base), AuHwInfo::gPageSize);
#if defined(AURORA_IS_MODERNNT_DERIVED)
if (!VirtualUnlock(AuReinterpretCast<LPVOID>(base), length))
{
SysPushErrorHAL("Couldn't unlock memory 0x{:x} {}", base, length);
}
#endif
#if defined(AURORA_IS_POSIX_DERIVED)
if (munlock(AuReinterpretCast<const void *>(base), length) != 0)
{
SysPushErrorHAL("Couldn't unlock memory 0x{:x} {}", base, length);
}
#endif
}
return true;
}
}

View File

@ -0,0 +1,13 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: SwapLock.hpp
Date: 2022-3-21
Author: Reece
***/
#pragma once
namespace Aurora::Memory::SwapLock
{
}

View File

@ -0,0 +1,20 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: SwapLock.cpp
Date: 2022-3-21
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Memory.hpp"
#include "SwapLock.hpp"
#include <Source/HWInfo/HWInfo.hpp>
#if defined(AURORA_IS_LINUX_DERIVED)
#include <sys/mman.h>
#endif
namespace Aurora::Memory::Transition
{
}

View File

@ -0,0 +1,13 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Transition.hpp
Date: 2022-3-21
Author: Reece
***/
#pragma once
namespace Aurora::Memory::Transition
{
}