From e0ac8344d016a790f984f6659d984034cee9c9cc Mon Sep 17 00:00:00 2001 From: Reece Date: Tue, 14 Sep 2021 20:10:57 +0100 Subject: [PATCH] [+] Getting ready to strip void *s from the api --- Include/Aurora/Memory/Memory.hpp | 1 + Include/Aurora/Memory/MemoryView.hpp | 72 ++++ Include/Aurora/Runtime.hpp | 2 + Include/AuroraUtils.hpp | 472 ++++++++++++++++++++++++++- Source/IO/FS/Async.NT.cpp | 3 +- 5 files changed, 547 insertions(+), 3 deletions(-) create mode 100644 Include/Aurora/Memory/MemoryView.hpp diff --git a/Include/Aurora/Memory/Memory.hpp b/Include/Aurora/Memory/Memory.hpp index b7972483..ac62a18a 100644 --- a/Include/Aurora/Memory/Memory.hpp +++ b/Include/Aurora/Memory/Memory.hpp @@ -11,6 +11,7 @@ #include "MemRef.hpp" #include "Heap.hpp" +#include "MemoryView.hpp" namespace Aurora::Memory { diff --git a/Include/Aurora/Memory/MemoryView.hpp b/Include/Aurora/Memory/MemoryView.hpp new file mode 100644 index 00000000..202c01b8 --- /dev/null +++ b/Include/Aurora/Memory/MemoryView.hpp @@ -0,0 +1,72 @@ +#pragma once + +namespace Aurora::Memory +{ + template + struct MemoryView + { + using Void_t = std::conditional_t; + + template + constexpr MemoryView(const AuList &list) + { + this->ptr = list.data(); + this->length = list.size() * sizeof(T); + } + + template + constexpr MemoryView(Void_t start, Void_t end) + { + this->ptr = start; + if constexpr (std::is_same_v) + { + this->length = reinterpret_cast(end) - reinterpret_cast(start); + } + else + { + this->length = (end - start) * sizeof(T); + } + } + + template + constexpr MemoryView(Void_t start, AuUInt length) + { + this->ptr = start; + this->length = length; + } + + AuUInt ToPointer() + { + return reinterpret_cast(ptr); + } + + AuUInt ToLength() + { + return length; + } + + template + AuUInt ToCount() + { + return length / sizeof(T); + } + + template + T* Begin() + { + return reinterpret_cast(ptr); + } + + template + T* End() + { + return Begin() + ToCount(); + } + + Void_t const ptr; + AuUInt const length; + }; + + using MemoryViewRead = MemoryView; + using MemoryViewWrite = MemoryView; +} \ No newline at end of file diff --git a/Include/Aurora/Runtime.hpp b/Include/Aurora/Runtime.hpp index 74bb4846..e7cfd116 100644 --- a/Include/Aurora/Runtime.hpp +++ b/Include/Aurora/Runtime.hpp @@ -19,8 +19,10 @@ #include "../AuroraMacros.hpp" +#include #include #include +#include #include "../AuroraTypedefs.hpp" diff --git a/Include/AuroraUtils.hpp b/Include/AuroraUtils.hpp index 28199fff..ef60033b 100644 --- a/Include/AuroraUtils.hpp +++ b/Include/AuroraUtils.hpp @@ -498,4 +498,474 @@ template static inline void AuClearBit(T &value, AuUInt8 idx) { value &= ~(T(1) << T(idx)); -} \ No newline at end of file +} + +#if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86) || defined(AURORA_ARCH_ARM) +#define AURORA_PERMIT_ARBITRARY_REF +#endif + +template +static inline T AuReadGenericLE(const void *ptr, int offset) +{ +#if defined(AURORA_PERMIT_ARBITRARY_REF) && defined(AU_CPU_ENDIAN_LITTLE) + return *reinterpret_cast(reinterpret_cast(ptr) + offset); +#else + T temp; + std::memcpy(&temp, reinterpret_cast(ptr) + offset, sizeof(temp)); +#if !defined(AU_CPU_ENDIAN_LITTLE) +#if defined(AURORA_COMPILER_MSVC) + if constexpr (std::is_same_v || std::is_same_v) + { + temp = _byteswap_ulong(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = _byteswap_uint64(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = _byteswap_ushort(temp); + } +#else + if constexpr (std::is_same_v || std::is_same_v) + { + temp = __builtin_bswap32(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = __builtin_bswap64(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = (temp << 8) | ((temp >> 8) & 0xFF); + } +#endif +#endif + return temp; +#endif +} + +template +static inline T AuReadGenericBE(const void *ptr, int offset) +{ +#if defined(AURORA_PERMIT_ARBITRARY_REF) && defined(AU_CPU_ENDIAN_BIG) + return *reinterpret_cast(reinterpret_cast(ptr) + offset); +#else + T temp; + std::memcpy(&temp, reinterpret_cast(ptr) + offset, sizeof(temp)); +#if defined(AU_CPU_ENDIAN_LITTLE) +#if defined(AURORA_COMPILER_MSVC) + if constexpr (std::is_same_v || std::is_same_v) + { + temp = _byteswap_ulong(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = _byteswap_uint64(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = _byteswap_ushort(temp); + } +#else + if constexpr (std::is_same_v || std::is_same_v) + { + temp = __builtin_bswap32(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = __builtin_bswap64(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = (temp << 8) | ((temp >> 8) & 0xFF); + } +#endif +#endif + return temp; +#endif +} + +static inline AuUInt64 AuReadU64LE(const void *ptr, int offset) +{ + return AuReadGenericLE(ptr, offset); +} + +static inline AuUInt32 AuReadU32LE(const void *ptr, int offset) +{ + return AuReadGenericLE(ptr, offset); +} + +static inline AuUInt16 AuReadU16LE(const void *ptr, int offset) +{ + return AuReadGenericLE(ptr, offset); +} + +static inline AuUInt8 AuReadU8LE(const void *ptr, int offset) +{ + return AuReadGenericLE(ptr, offset); +} + +static inline AuInt64 AuReadS64LE(const void *ptr, int offset) +{ + return AuReadGenericLE(ptr, offset); +} + +static inline AuInt32 AuReadS32LE(const void *ptr, int offset) +{ + return AuReadGenericLE(ptr, offset); +} + +static inline AuInt16 AuReadS16LE(const void *ptr, int offset) +{ + return AuReadGenericLE(ptr, offset); +} + +static inline AuInt8 AuReadS8LE(const void *ptr, int offset) +{ + return AuReadGenericLE(ptr, offset); +} + +static inline AuUInt64 AuReadU64BE(const void *ptr, int offset) +{ + return AuReadGenericBE(ptr, offset); +} + +static inline AuUInt32 AuReadU32BE(const void *ptr, int offset) +{ + return AuReadGenericBE(ptr, offset); +} + +static inline AuUInt16 AuReadU16BE(const void *ptr, int offset) +{ + return AuReadGenericBE(ptr, offset); +} + +static inline AuUInt8 AuReadU8BE(const void *ptr, int offset) +{ + return AuReadGenericBE(ptr, offset); +} + +static inline AuInt64 AuReadS64BE(const void *ptr, int offset) +{ + return AuReadGenericBE(ptr, offset); +} + +static inline AuInt32 AuReadS32BE(const void *ptr, int offset) +{ + return AuReadGenericBE(ptr, offset); +} + +static inline AuInt16 AuReadS16BE(const void *ptr, int offset) +{ + return AuReadGenericBE(ptr, offset); +} + +static inline AuInt8 AuReadS8BE(const void *ptr, int offset) +{ + return AuReadGenericBE(ptr, offset); +} + +static inline AuUInt64 AuReadU64(const void *ptr, int offset) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + return AuReadU64LE(ptr, offset); +#else + return AuReadU64BE(ptr, offset); +#endif +} + +static inline AuUInt32 AuReadU32(const void *ptr, int offset) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + return AuReadU32LE(ptr, offset); +#else + return AuReadU32BE(ptr, offset); +#endif +} + +static inline AuUInt16 AuReadU16(const void *ptr, int offset) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + return AuReadU16LE(ptr, offset); +#else + return AuReadU16BE(ptr, offset); +#endif +} + +static inline AuUInt8 AuReadU8(const void *ptr, int offset) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + return AuReadU8LE(ptr, offset); +#else + return AuReadU8BE(ptr, offset); +#endif +} + +static inline AuInt64 AuReadS64(const void *ptr, int offset) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + return AuReadS64LE(ptr, offset); +#else + return AuReadS64BE(ptr, offset); +#endif +} + +static inline AuInt32 AuReadS32(const void *ptr, int offset) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + return AuReadS32LE(ptr, offset); +#else + return AuReadS32BE(ptr, offset); +#endif +} + +static inline AuInt16 AuReadS16(const void *ptr, int offset) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + return AuReadS16LE(ptr, offset); +#else + return AuReadS16BE(ptr, offset); +#endif +} + +static inline AuInt8 AuReadS8(const void *ptr, int offset) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + return AuReadS8LE(ptr, offset); +#else + return AuReadS8BE(ptr, offset); +#endif +} + +template +static inline void AuWriteGenericLE(void *ptr, int offset, T value) +{ +#if defined(AURORA_PERMIT_ARBITRARY_REF) && defined(AU_CPU_ENDIAN_LITTLE) + *reinterpret_cast(reinterpret_cast(ptr) + offset) = value; +#else + T temp = value; +#if !defined(AU_CPU_ENDIAN_LITTLE) +#if defined(AURORA_COMPILER_MSVC) + if constexpr (std::is_same_v || std::is_same_v) + { + temp = _byteswap_ulong(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = _byteswap_uint64(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = _byteswap_ushort(temp); + } +#else + if constexpr (std::is_same_v || std::is_same_v) + { + temp = __builtin_bswap32(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = __builtin_bswap64(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = (temp << 8) | ((temp >> 8) & 0xFF); + } +#endif +#endif + std::memcpy(reinterpret_cast(ptr) + offset, &temp, sizeof(temp)); +#endif +} + +template +static inline void AuWriteGenericBE(void *ptr, T value, int offset) +{ +#if defined(AURORA_PERMIT_ARBITRARY_REF) && defined(AU_CPU_ENDIAN_BIG) + *reinterpret_cast(reinterpret_cast(ptr) + offset) = value; +#else + T temp = value; +#if defined(AU_CPU_ENDIAN_LITTLE) +#if defined(AURORA_COMPILER_MSVC) + if constexpr (std::is_same_v || std::is_same_v) + { + temp = _byteswap_ulong(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = _byteswap_uint64(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = _byteswap_ushort(temp); + } +#else + if constexpr (std::is_same_v || std::is_same_v) + { + temp = __builtin_bswap32(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = __builtin_bswap64(temp); + } + else if constexpr (std::is_same_v || std::is_same_v) + { + temp = (temp << 8) | ((temp >> 8) & 0xFF); + } +#endif +#endif + std::memcpy(reinterpret_cast(ptr) + offset, &temp, sizeof(temp)); +#endif +} + +static inline void AuWriteU64LE(void *ptr, int offset, AuUInt64 value) +{ + AuWriteGenericLE(ptr, offset, value); +} + +static inline void AuWriteU32LE(void *ptr, int offset, AuUInt32 value) +{ + AuWriteGenericLE(ptr, offset, value); +} + +static inline void AuWriteU16LE(void *ptr, int offset, AuUInt16 value) +{ + AuWriteGenericLE(ptr, offset, value); +} + +static inline void AuWriteU8LE(void *ptr, int offset, AuUInt8 value) +{ + AuWriteGenericLE(ptr, offset, value); +} + +static inline void AuWriteS64LE(void *ptr, int offset, AuInt64 value) +{ + AuWriteGenericLE(ptr, offset, value); +} + +static inline void AuWriteS32LE(void *ptr, int offset, AuInt32 value) +{ + AuWriteGenericLE(ptr, offset, value); +} + +static inline void AuWriteS16LE(void *ptr, int offset, AuInt16 value) +{ + AuWriteGenericLE(ptr, offset, value); +} + +static inline void AuWriteS8LE(void *ptr, int offset, AuInt8 value) +{ + AuWriteGenericLE(ptr, offset, value); +} + +static inline void AuWriteU64BE(void *ptr, int offset, AuUInt64 value) +{ + AuWriteGenericBE(ptr, offset, value); +} + +static inline void AuWriteU32BE(void *ptr, int offset, AuUInt32 value) +{ + AuWriteGenericBE(ptr, offset, value); +} + +static inline void AuWriteU16BE(void *ptr, int offset, AuUInt16 value) +{ + AuWriteGenericBE(ptr, offset, value); +} + +static inline void AuWriteU8BE(void *ptr, int offset, AuUInt8 value) +{ + AuWriteGenericBE(ptr, offset, value); +} + +static inline void AuWriteS64BE(void *ptr, int offset, AuInt64 value) +{ + AuWriteGenericBE(ptr, offset, value); +} + +static inline void AuWriteS32BE(void *ptr, int offset, AuInt32 value) +{ + AuWriteGenericBE(ptr, offset, value); +} + +static inline void AuWriteS16BE(void *ptr, int offset, AuInt16 value) +{ + AuWriteGenericBE(ptr, offset, value); +} + +static inline void AuWriteS8BE(void *ptr, int offset, AuInt8 value) +{ + AuWriteGenericBE(ptr, offset, value); +} + +static inline void AuWriteU64(void *ptr, int offset, AuUInt64 value) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + AuWriteU64LE(ptr, offset, value); +#else + AuWriteU64BE(ptr, offset, value); +#endif +} + +static inline void AuWriteU32(void *ptr, int offset, AuUInt32 value) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + AuWriteU32LE(ptr, offset, value); +#else + AuWriteU32BE(ptr, offset, value); +#endif +} + +static inline void AuWriteU16(void *ptr, int offset, AuUInt16 value) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + AuWriteU16LE(ptr, offset, value); +#else + AuWriteU16BE(ptr, offset, value); +#endif +} + +static inline void AuWriteU8(void *ptr, int offset, AuUInt8 value) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + AuWriteU8LE(ptr, offset, value); +#else + AuWriteU8BE(ptr, offset, value); +#endif +} + +static inline void AuWriteS64(void *ptr, int offset, AuInt64 value) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + AuWriteS64LE(ptr, offset, value); +#else + AuWriteS64BE(ptr, offset, value); +#endif +} + +static inline void AuWriteS32(void *ptr, int offset, AuInt32 value) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + AuWriteS32LE(ptr, offset, value); +#else + AuWriteS32BE(ptr, offset, value); +#endif +} + +static inline void AuWriteS16(void *ptr, int offset, AuInt16 value) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + AuWriteS16LE(ptr, offset, value); +#else + AuWriteS16BE(ptr, offset, value); +#endif +} + +static inline void AuWriteS8(void *ptr, int offset, AuInt8 value) +{ +#if defined(AU_CPU_ENDIAN_LITTLE) + AuWriteS8LE(ptr, offset, value); +#else + AuWriteS8BE(ptr, offset, value); +#endif +} diff --git a/Source/IO/FS/Async.NT.cpp b/Source/IO/FS/Async.NT.cpp index c7e02453..4b56c038 100644 --- a/Source/IO/FS/Async.NT.cpp +++ b/Source/IO/FS/Async.NT.cpp @@ -54,7 +54,6 @@ namespace Aurora::IO::FS bool Wait(AuUInt32 timeout) override; void DispatchCb(); - HANDLE GetHandle(); AuSPtr GetFileHandle(); @@ -173,7 +172,7 @@ namespace Aurora::IO::FS } } - VOID WINAPI GenericCompletionRoutine( + static VOID WINAPI GenericCompletionRoutine( _In_ DWORD dwErrorCode, _In_ DWORD dwNumberOfBytesTransfered, _Inout_ LPOVERLAPPED lpOverlapped