[+] Getting ready to strip void *s from the api

This commit is contained in:
Reece Wilson 2021-09-14 20:10:57 +01:00
parent 03368d262f
commit e0ac8344d0
5 changed files with 547 additions and 3 deletions

View File

@ -11,6 +11,7 @@
#include "MemRef.hpp"
#include "Heap.hpp"
#include "MemoryView.hpp"
namespace Aurora::Memory
{

View File

@ -0,0 +1,72 @@
#pragma once
namespace Aurora::Memory
{
template<bool Readonly_b>
struct MemoryView
{
using Void_t = std::conditional_t<Readonly_b, const void *, void *>;
template<typename T>
constexpr MemoryView(const AuList<T> &list)
{
this->ptr = list.data();
this->length = list.size() * sizeof(T);
}
template<typename T>
constexpr MemoryView(Void_t start, Void_t end)
{
this->ptr = start;
if constexpr (std::is_same_v<T, Void_t>)
{
this->length = reinterpret_cast<const AuUInt8*>(end) - reinterpret_cast<const AuUInt8*>(start);
}
else
{
this->length = (end - start) * sizeof(T);
}
}
template<typename T>
constexpr MemoryView(Void_t start, AuUInt length)
{
this->ptr = start;
this->length = length;
}
AuUInt ToPointer()
{
return reinterpret_cast<AuUInt>(ptr);
}
AuUInt ToLength()
{
return length;
}
template<typename T>
AuUInt ToCount()
{
return length / sizeof(T);
}
template<typename T>
T* Begin()
{
return reinterpret_cast<T *>(ptr);
}
template<typename T>
T* End()
{
return Begin<T>() + ToCount<T>();
}
Void_t const ptr;
AuUInt const length;
};
using MemoryViewRead = MemoryView<true>;
using MemoryViewWrite = MemoryView<false>;
}

View File

@ -19,8 +19,10 @@
#include "../AuroraMacros.hpp"
#include <memory>
#include <optional>
#include <functional>
#include <intrin.h>
#include "../AuroraTypedefs.hpp"

View File

@ -498,4 +498,474 @@ template<typename T>
static inline void AuClearBit(T &value, AuUInt8 idx)
{
value &= ~(T(1) << T(idx));
}
}
#if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86) || defined(AURORA_ARCH_ARM)
#define AURORA_PERMIT_ARBITRARY_REF
#endif
template<typename T>
static inline T AuReadGenericLE(const void *ptr, int offset)
{
#if defined(AURORA_PERMIT_ARBITRARY_REF) && defined(AU_CPU_ENDIAN_LITTLE)
return *reinterpret_cast<const T *>(reinterpret_cast<const AuUInt8 *>(ptr) + offset);
#else
T temp;
std::memcpy(&temp, reinterpret_cast<const AuUInt8 *>(ptr) + offset, sizeof(temp));
#if !defined(AU_CPU_ENDIAN_LITTLE)
#if defined(AURORA_COMPILER_MSVC)
if constexpr (std::is_same_v<T, AuUInt32> || std::is_same_v<T, AuInt32>)
{
temp = _byteswap_ulong(temp);
}
else if constexpr (std::is_same_v<T, AuUInt64> || std::is_same_v<T, AuInt64>)
{
temp = _byteswap_uint64(temp);
}
else if constexpr (std::is_same_v<T, AuUInt16> || std::is_same_v<T, AuInt16>)
{
temp = _byteswap_ushort(temp);
}
#else
if constexpr (std::is_same_v<T, AuUInt32> || std::is_same_v<T, AuInt32>)
{
temp = __builtin_bswap32(temp);
}
else if constexpr (std::is_same_v<T, AuUInt64> || std::is_same_v<T, AuInt64>)
{
temp = __builtin_bswap64(temp);
}
else if constexpr (std::is_same_v<T, AuUInt16> || std::is_same_v<T, AuInt16>)
{
temp = (temp << 8) | ((temp >> 8) & 0xFF);
}
#endif
#endif
return temp;
#endif
}
template<typename T>
static inline T AuReadGenericBE(const void *ptr, int offset)
{
#if defined(AURORA_PERMIT_ARBITRARY_REF) && defined(AU_CPU_ENDIAN_BIG)
return *reinterpret_cast<const T *>(reinterpret_cast<const AuUInt8 *>(ptr) + offset);
#else
T temp;
std::memcpy(&temp, reinterpret_cast<const AuUInt8 *>(ptr) + offset, sizeof(temp));
#if defined(AU_CPU_ENDIAN_LITTLE)
#if defined(AURORA_COMPILER_MSVC)
if constexpr (std::is_same_v<T, AuUInt32> || std::is_same_v<T, AuInt32>)
{
temp = _byteswap_ulong(temp);
}
else if constexpr (std::is_same_v<T, AuUInt64> || std::is_same_v<T, AuInt64>)
{
temp = _byteswap_uint64(temp);
}
else if constexpr (std::is_same_v<T, AuUInt16> || std::is_same_v<T, AuInt16>)
{
temp = _byteswap_ushort(temp);
}
#else
if constexpr (std::is_same_v<T, AuUInt32> || std::is_same_v<T, AuInt32>)
{
temp = __builtin_bswap32(temp);
}
else if constexpr (std::is_same_v<T, AuUInt64> || std::is_same_v<T, AuInt64>)
{
temp = __builtin_bswap64(temp);
}
else if constexpr (std::is_same_v<T, AuUInt16> || std::is_same_v<T, AuInt16>)
{
temp = (temp << 8) | ((temp >> 8) & 0xFF);
}
#endif
#endif
return temp;
#endif
}
static inline AuUInt64 AuReadU64LE(const void *ptr, int offset)
{
return AuReadGenericLE<AuUInt64>(ptr, offset);
}
static inline AuUInt32 AuReadU32LE(const void *ptr, int offset)
{
return AuReadGenericLE<AuUInt32>(ptr, offset);
}
static inline AuUInt16 AuReadU16LE(const void *ptr, int offset)
{
return AuReadGenericLE<AuUInt16>(ptr, offset);
}
static inline AuUInt8 AuReadU8LE(const void *ptr, int offset)
{
return AuReadGenericLE<AuUInt8>(ptr, offset);
}
static inline AuInt64 AuReadS64LE(const void *ptr, int offset)
{
return AuReadGenericLE<AuInt64>(ptr, offset);
}
static inline AuInt32 AuReadS32LE(const void *ptr, int offset)
{
return AuReadGenericLE<AuInt32>(ptr, offset);
}
static inline AuInt16 AuReadS16LE(const void *ptr, int offset)
{
return AuReadGenericLE<AuInt16>(ptr, offset);
}
static inline AuInt8 AuReadS8LE(const void *ptr, int offset)
{
return AuReadGenericLE<AuInt8>(ptr, offset);
}
static inline AuUInt64 AuReadU64BE(const void *ptr, int offset)
{
return AuReadGenericBE<AuUInt64>(ptr, offset);
}
static inline AuUInt32 AuReadU32BE(const void *ptr, int offset)
{
return AuReadGenericBE<AuUInt32>(ptr, offset);
}
static inline AuUInt16 AuReadU16BE(const void *ptr, int offset)
{
return AuReadGenericBE<AuUInt16>(ptr, offset);
}
static inline AuUInt8 AuReadU8BE(const void *ptr, int offset)
{
return AuReadGenericBE<AuUInt8>(ptr, offset);
}
static inline AuInt64 AuReadS64BE(const void *ptr, int offset)
{
return AuReadGenericBE<AuInt64>(ptr, offset);
}
static inline AuInt32 AuReadS32BE(const void *ptr, int offset)
{
return AuReadGenericBE<AuInt32>(ptr, offset);
}
static inline AuInt16 AuReadS16BE(const void *ptr, int offset)
{
return AuReadGenericBE<AuInt16>(ptr, offset);
}
static inline AuInt8 AuReadS8BE(const void *ptr, int offset)
{
return AuReadGenericBE<AuInt8>(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<typename T>
static inline void AuWriteGenericLE(void *ptr, int offset, T value)
{
#if defined(AURORA_PERMIT_ARBITRARY_REF) && defined(AU_CPU_ENDIAN_LITTLE)
*reinterpret_cast<T *>(reinterpret_cast<AuUInt8 *>(ptr) + offset) = value;
#else
T temp = value;
#if !defined(AU_CPU_ENDIAN_LITTLE)
#if defined(AURORA_COMPILER_MSVC)
if constexpr (std::is_same_v<T, AuUInt32> || std::is_same_v<T, AuInt32>)
{
temp = _byteswap_ulong(temp);
}
else if constexpr (std::is_same_v<T, AuUInt64> || std::is_same_v<T, AuInt64>)
{
temp = _byteswap_uint64(temp);
}
else if constexpr (std::is_same_v<T, AuUInt16> || std::is_same_v<T, AuInt16>)
{
temp = _byteswap_ushort(temp);
}
#else
if constexpr (std::is_same_v<T, AuUInt32> || std::is_same_v<T, AuInt32>)
{
temp = __builtin_bswap32(temp);
}
else if constexpr (std::is_same_v<T, AuUInt64> || std::is_same_v<T, AuInt64>)
{
temp = __builtin_bswap64(temp);
}
else if constexpr (std::is_same_v<T, AuUInt16> || std::is_same_v<T, AuInt16>)
{
temp = (temp << 8) | ((temp >> 8) & 0xFF);
}
#endif
#endif
std::memcpy(reinterpret_cast<AuUInt8 *>(ptr) + offset, &temp, sizeof(temp));
#endif
}
template<typename T>
static inline void AuWriteGenericBE(void *ptr, T value, int offset)
{
#if defined(AURORA_PERMIT_ARBITRARY_REF) && defined(AU_CPU_ENDIAN_BIG)
*reinterpret_cast<T *>(reinterpret_cast<AuUInt8 *>(ptr) + offset) = value;
#else
T temp = value;
#if defined(AU_CPU_ENDIAN_LITTLE)
#if defined(AURORA_COMPILER_MSVC)
if constexpr (std::is_same_v<T, AuUInt32> || std::is_same_v<T, AuInt32>)
{
temp = _byteswap_ulong(temp);
}
else if constexpr (std::is_same_v<T, AuUInt64> || std::is_same_v<T, AuInt64>)
{
temp = _byteswap_uint64(temp);
}
else if constexpr (std::is_same_v<T, AuUInt16> || std::is_same_v<T, AuInt16>)
{
temp = _byteswap_ushort(temp);
}
#else
if constexpr (std::is_same_v<T, AuUInt32> || std::is_same_v<T, AuInt32>)
{
temp = __builtin_bswap32(temp);
}
else if constexpr (std::is_same_v<T, AuUInt64> || std::is_same_v<T, AuInt64>)
{
temp = __builtin_bswap64(temp);
}
else if constexpr (std::is_same_v<T, AuUInt16> || std::is_same_v<T, AuInt16>)
{
temp = (temp << 8) | ((temp >> 8) & 0xFF);
}
#endif
#endif
std::memcpy(reinterpret_cast<AuUInt8 *>(ptr) + offset, &temp, sizeof(temp));
#endif
}
static inline void AuWriteU64LE(void *ptr, int offset, AuUInt64 value)
{
AuWriteGenericLE<AuUInt64>(ptr, offset, value);
}
static inline void AuWriteU32LE(void *ptr, int offset, AuUInt32 value)
{
AuWriteGenericLE<AuUInt32>(ptr, offset, value);
}
static inline void AuWriteU16LE(void *ptr, int offset, AuUInt16 value)
{
AuWriteGenericLE<AuUInt16>(ptr, offset, value);
}
static inline void AuWriteU8LE(void *ptr, int offset, AuUInt8 value)
{
AuWriteGenericLE<AuUInt8>(ptr, offset, value);
}
static inline void AuWriteS64LE(void *ptr, int offset, AuInt64 value)
{
AuWriteGenericLE<AuInt64>(ptr, offset, value);
}
static inline void AuWriteS32LE(void *ptr, int offset, AuInt32 value)
{
AuWriteGenericLE<AuInt32>(ptr, offset, value);
}
static inline void AuWriteS16LE(void *ptr, int offset, AuInt16 value)
{
AuWriteGenericLE<AuInt16>(ptr, offset, value);
}
static inline void AuWriteS8LE(void *ptr, int offset, AuInt8 value)
{
AuWriteGenericLE<AuInt8>(ptr, offset, value);
}
static inline void AuWriteU64BE(void *ptr, int offset, AuUInt64 value)
{
AuWriteGenericBE<AuUInt64>(ptr, offset, value);
}
static inline void AuWriteU32BE(void *ptr, int offset, AuUInt32 value)
{
AuWriteGenericBE<AuUInt32>(ptr, offset, value);
}
static inline void AuWriteU16BE(void *ptr, int offset, AuUInt16 value)
{
AuWriteGenericBE<AuUInt16>(ptr, offset, value);
}
static inline void AuWriteU8BE(void *ptr, int offset, AuUInt8 value)
{
AuWriteGenericBE<AuUInt8>(ptr, offset, value);
}
static inline void AuWriteS64BE(void *ptr, int offset, AuInt64 value)
{
AuWriteGenericBE<AuInt64>(ptr, offset, value);
}
static inline void AuWriteS32BE(void *ptr, int offset, AuInt32 value)
{
AuWriteGenericBE<AuInt32>(ptr, offset, value);
}
static inline void AuWriteS16BE(void *ptr, int offset, AuInt16 value)
{
AuWriteGenericBE<AuInt16>(ptr, offset, value);
}
static inline void AuWriteS8BE(void *ptr, int offset, AuInt8 value)
{
AuWriteGenericBE<AuInt8>(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
}

View File

@ -54,7 +54,6 @@ namespace Aurora::IO::FS
bool Wait(AuUInt32 timeout) override;
void DispatchCb();
HANDLE GetHandle();
AuSPtr<FileHandle> 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