[ox] General cleanup and updating to newer coding standards

This commit is contained in:
Gary Talent 2021-04-20 01:55:40 -05:00
parent c587bf0997
commit 161780cb91
14 changed files with 95 additions and 86 deletions

View File

@ -203,9 +203,7 @@ Error Directory<FileStore, InodeId_t>::write(PathIterator path, InodeId_t inode,
oxTrace("ox::fs::Directory::write") << "Attempting to write to next sub-Directory: "
<< name->c_str() << " of " << path.fullPath();
auto [nextChild, err] = findEntry(*name);
oxReturnError(err);
oxRequire(nextChild, findEntry(*name));
oxTrace("ox::fs::Directory::write") << name->c_str() << ": " << nextChild;
if (nextChild) {
@ -227,9 +225,8 @@ Error Directory<FileStore, InodeId_t>::write(PathIterator path, InodeId_t inode,
// find existing version of directory
oxTrace("ox::fs::Directory::write") << "Searching for directory inode" << m_inodeId;
auto oldStat = m_fs.stat(m_inodeId);
oxReturnError(oldStat);
oxTrace("ox::fs::Directory::write") << "Found existing directory of size" << oldStat.value.size;
oxRequire(oldStat, m_fs.stat(m_inodeId));
oxTrace("ox::fs::Directory::write") << "Found existing directory of size" << oldStat.size;
auto old = m_fs.read(m_inodeId).template to<Buffer>();
if (!old.valid()) {
oxTrace("ox::fs::Directory::write::fail") << "Could not read existing version of Directory";
@ -238,8 +235,8 @@ Error Directory<FileStore, InodeId_t>::write(PathIterator path, InodeId_t inode,
const auto pathSize = name->len() + 1;
const auto entryDataSize = DirectoryEntry<InodeId_t>::DirectoryEntryData::spaceNeeded(pathSize);
const auto newSize = oldStat.value.size + Buffer::spaceNeeded(entryDataSize);
auto cpy = ox_malloca(newSize, Buffer, *old, oldStat.value.size);
const auto newSize = oldStat.size + Buffer::spaceNeeded(entryDataSize);
auto cpy = ox_malloca(newSize, Buffer, *old, oldStat.size);
if (cpy == nullptr) {
oxTrace("ox::fs::Directory::write::fail") << "Could not allocate memory for copy of Directory";
return OxError(1);
@ -349,11 +346,10 @@ Result<typename FileStore::InodeId_t> Directory<FileStore, InodeId_t>::find(Path
auto name = nameBuff;
oxReturnError(path.get(name));
auto v = findEntry(name->c_str());
oxReturnError(v);
oxRequire(v, findEntry(name->c_str()));
// recurse if not at end of path
if (auto p = path.next(); p.valid()) {
Directory dir(m_fs, v.value);
Directory dir(m_fs, v);
name = nullptr;
return dir.find(p, nameBuff);
}

View File

@ -255,8 +255,7 @@ Error MetalClawReader::field(const char* name, ox::Vector<T> *val) {
if (m_unionIdx == -1 || m_unionIdx == m_field) {
// set size of val if the field is present, don't worry about it if not
if (m_fieldPresence.get(m_field)) {
const auto [len, err] = arrayLength(name, false);
oxReturnError(err);
oxRequire(len, arrayLength(name, false));
val->resize(len);
}
return field(name, val->data(), val->size());

View File

@ -18,7 +18,7 @@
namespace ox {
template<>
void assertFunc<bool>([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]bool pass, [[maybe_unused]]const char *msg) {
void assertFunc<bool>([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]bool pass, [[maybe_unused]]const char *msg) noexcept {
if (!pass) {
#if defined(OX_USE_STDLIB)
std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m (" << file << ':' << line << "): " << msg << std::endl;
@ -32,7 +32,7 @@ void assertFunc<bool>([[maybe_unused]]const char *file, [[maybe_unused]]int line
}
template<>
void assertFunc<Error>(const char *file, int line, Error err, const char *assertMsg) {
void assertFunc<Error>(const char *file, int line, Error err, const char *assertMsg) noexcept {
if (err) {
#if defined(OX_USE_STDLIB)
std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m (" << file << ':' << line << "): " << assertMsg << '\n';
@ -53,7 +53,7 @@ void assertFunc<Error>(const char *file, int line, Error err, const char *assert
}
#if defined(OX_USE_STDLIB)
void panic(const char *file, int line, const char *panicMsg, Error err) {
void panic(const char *file, int line, const char *panicMsg, Error err) noexcept {
std::cerr << "\033[31;1;1mPANIC:\033[0m (" << file << ':' << line << "): " << panicMsg << '\n';
if (err.msg) {
std::cerr << "\tError Message:\t" << err.msg << '\n';

View File

@ -14,16 +14,16 @@
namespace ox {
template<typename T>
void assertFunc(const char*, int, T, const char*) {
void assertFunc(const char*, int, T, const char*) noexcept {
}
template<>
void assertFunc<bool>(const char *file, int line, bool pass, const char *msg);
void assertFunc<bool>(const char *file, int line, bool pass, const char *msg) noexcept;
template<>
void assertFunc<Error>(const char *file, int line, Error err, const char*);
void assertFunc<Error>(const char *file, int line, Error err, const char*) noexcept;
void panic([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]const char *msg, [[maybe_unused]]Error err = OxError(0));
void panic([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]const char *msg, [[maybe_unused]]Error err = OxError(0)) noexcept;
}

View File

@ -11,12 +11,12 @@
namespace ox {
template<typename T>
static constexpr bool testLittleEndian(T i) {
static constexpr bool testLittleEndian(T i) noexcept {
return LittleEndian<T>(i) == i;
}
template<typename T>
static constexpr bool testBigEndian(T i) {
static constexpr bool testBigEndian(T i) noexcept {
return BigEndian<T>(i) == i;
}

View File

@ -16,17 +16,20 @@
namespace ox {
template<typename T>
[[nodiscard]] constexpr inline T byteSwap(typename enable_if<sizeof(T) == 1, T>::type i) noexcept {
[[nodiscard]]
constexpr T byteSwap(typename enable_if<sizeof(T) == 1, T>::type i) noexcept {
return i;
}
template<typename T>
[[nodiscard]] constexpr inline T byteSwap(typename enable_if<sizeof(T) == 2, T>::type i) noexcept {
[[nodiscard]]
constexpr T byteSwap(typename enable_if<sizeof(T) == 2, T>::type i) noexcept {
return (i << 8) | (i >> 8);
}
template<typename T>
[[nodiscard]] constexpr inline T byteSwap(typename enable_if<sizeof(T) == 4, T>::type i) noexcept {
[[nodiscard]]
constexpr T byteSwap(typename enable_if<sizeof(T) == 4, T>::type i) noexcept {
return ((i >> 24) & 0x000000ff) |
((i >> 8) & 0x0000ff00) |
((i << 8) & 0x00ff0000) |
@ -34,7 +37,8 @@ template<typename T>
}
template<typename T>
[[nodiscard]] constexpr inline T byteSwap(typename enable_if<sizeof(T) == 8, T>::type i) noexcept {
[[nodiscard]]
constexpr T byteSwap(typename enable_if<sizeof(T) == 8, T>::type i) noexcept {
return ((i >> 56) & 0x00000000000000ff) |
((i >> 40) & 0x000000000000ff00) |
((i >> 24) & 0x0000000000ff0000) |
@ -50,7 +54,8 @@ template<typename T>
* Takes an int and byte swaps if the platform is the given condition is true.
*/
template<typename T, bool byteSwap>
[[nodiscard]] constexpr inline T conditionalByteSwap(T i) noexcept {
[[nodiscard]]
constexpr T conditionalByteSwap(T i) noexcept {
if constexpr(byteSwap) {
return ox::byteSwap<T>(i);
} else {
@ -64,118 +69,119 @@ class OX_PACKED ByteSwapInteger {
T m_value;
public:
constexpr inline ByteSwapInteger() noexcept = default;
constexpr ByteSwapInteger() noexcept = default;
constexpr inline ByteSwapInteger(const ByteSwapInteger &other) noexcept {
constexpr ByteSwapInteger(const ByteSwapInteger &other) noexcept {
m_value = other.m_value;
}
constexpr inline ByteSwapInteger(T value) noexcept: m_value(ox::conditionalByteSwap<T, byteSwap>(value)) {
constexpr ByteSwapInteger(T value) noexcept: m_value(ox::conditionalByteSwap<T, byteSwap>(value)) {
}
constexpr inline const ByteSwapInteger &operator=(const ByteSwapInteger &other) noexcept {
constexpr const ByteSwapInteger &operator=(const ByteSwapInteger &other) noexcept {
m_value = other.m_value;
return *this;
}
template<typename I>
constexpr inline T operator=(I value) noexcept {
constexpr T operator=(I value) noexcept {
m_value = ox::conditionalByteSwap<T, byteSwap>(value);
return value;
}
constexpr inline operator T() const noexcept {
constexpr operator T() const noexcept {
return ox::conditionalByteSwap<T, byteSwap>(m_value);
}
template<typename I>
constexpr inline T operator+=(I other) noexcept {
constexpr T operator+=(I other) noexcept {
auto newVal = *this + other;
m_value = ox::conditionalByteSwap<T, byteSwap>(newVal);
return newVal;
}
template<typename I>
constexpr inline T operator-=(I other) noexcept {
constexpr T operator-=(I other) noexcept {
auto newVal = *this - other;
m_value = ox::conditionalByteSwap<T, byteSwap>(newVal);
return newVal;
}
template<typename I>
constexpr inline T operator*=(I other) noexcept {
constexpr T operator*=(I other) noexcept {
auto newVal = *this * other;
m_value = ox::conditionalByteSwap<T, byteSwap>(newVal);
return newVal;
}
template<typename I>
constexpr inline T operator/=(I other) noexcept {
constexpr T operator/=(I other) noexcept {
auto newVal = *this / other;
m_value = ox::conditionalByteSwap<T, byteSwap>(newVal);
return newVal;
}
// Prefix increment
constexpr inline T operator++() noexcept {
constexpr T operator++() noexcept {
return operator+=(1);
}
// Postfix increment
constexpr inline T operator++(int) noexcept {
constexpr T operator++(int) noexcept {
auto old = *this;
++*this;
return old;
}
// Prefix decrement
constexpr inline T operator--() noexcept {
constexpr T operator--() noexcept {
return operator-=(1);
}
// Postfix decrement
constexpr inline T operator--(int) noexcept {
constexpr T operator--(int) noexcept {
auto old = *this;
--*this;
return old;
}
template<typename I>
constexpr inline T operator&=(I other) noexcept {
constexpr T operator&=(I other) noexcept {
auto newVal = *this & other;
m_value = ox::conditionalByteSwap<T, byteSwap>(newVal);
return newVal;
}
template<typename I>
constexpr inline T operator|=(I other) noexcept {
constexpr T operator|=(I other) noexcept {
auto newVal = *this | other;
m_value = ox::conditionalByteSwap<T, byteSwap>(newVal);
return newVal;
}
template<typename I>
constexpr inline T operator^=(I other) noexcept {
constexpr T operator^=(I other) noexcept {
auto newVal = *this ^ other;
m_value = ox::conditionalByteSwap<T, byteSwap>(newVal);
return newVal;
}
template<typename I>
constexpr inline T operator>>=(I other) noexcept {
constexpr T operator>>=(I other) noexcept {
auto newVal = *this >> other;
m_value = ox::conditionalByteSwap<T, byteSwap>(newVal);
return newVal;
}
template<typename I>
constexpr inline T operator<<=(I other) noexcept {
constexpr T operator<<=(I other) noexcept {
auto newVal = *this << other;
m_value = ox::conditionalByteSwap<T, byteSwap>(newVal);
return newVal;
}
[[nodiscard]] constexpr auto get() const noexcept -> T {
[[nodiscard]]
constexpr auto get() const noexcept -> T {
return static_cast<T>(*this);
}
@ -184,7 +190,8 @@ class OX_PACKED ByteSwapInteger {
* a little endian integer is returned regardless of the endianness of
* the system.
*/
[[nodiscard]] constexpr auto raw() const noexcept -> T {
[[nodiscard]]
constexpr auto raw() const noexcept -> T {
return m_value;
}

View File

@ -16,34 +16,34 @@ static struct HeapSegment *volatile g_heapBegin = nullptr;
static struct HeapSegment *volatile g_heapEnd = nullptr;
static struct HeapSegment *volatile heapIdx = nullptr;
static constexpr std::size_t alignedSize(std::size_t sz) {
static constexpr std::size_t alignedSize(std::size_t sz) noexcept {
return sz + (sz & 7);
}
template<typename T>
static constexpr std::size_t alignedSize(T = {}) {
static constexpr std::size_t alignedSize(T = {}) noexcept {
return alignedSize(sizeof(T));
}
void HeapSegment::init(std::size_t maxSize = ox::bit_cast<std::size_t>(g_heapEnd)) {
void HeapSegment::init(std::size_t maxSize = ox::bit_cast<std::size_t>(g_heapEnd)) noexcept {
this->size = maxSize - ox::bit_cast<std::size_t>(this);
this->inUse = false;
}
template<typename T>
T *HeapSegment::data() {
T *HeapSegment::data() noexcept {
return ox::bit_cast<T*>(ox::bit_cast<uint8_t*>(this) + alignedSize(this));
}
template<typename T>
T *HeapSegment::end() {
T *HeapSegment::end() noexcept {
const auto size = alignedSize(this) + alignedSize(this->size);
auto e = ox::bit_cast<uintptr_t>(ox::bit_cast<uint8_t*>(this) + size);
return ox::bit_cast<T*>(e);
}
void initHeap(char *heapBegin, char *heapEnd) {
void initHeap(char *heapBegin, char *heapEnd) noexcept {
g_heapBegin = ox::bit_cast<HeapSegment*>(heapBegin);
g_heapEnd = ox::bit_cast<HeapSegment*>(heapEnd);
heapIdx = g_heapBegin;
@ -56,7 +56,7 @@ struct SegmentPair {
HeapSegment *segment = nullptr;
};
static SegmentPair findSegmentOf(void *ptr) {
static SegmentPair findSegmentOf(void *ptr) noexcept {
HeapSegment *prev = nullptr;
for (auto seg = g_heapBegin; seg < g_heapEnd;) {
if (seg->data<void>() == ptr) {
@ -68,7 +68,7 @@ static SegmentPair findSegmentOf(void *ptr) {
return {};
}
static HeapSegment *findSegmentFor(std::size_t sz) {
static HeapSegment *findSegmentFor(std::size_t sz) noexcept {
for (auto s = g_heapBegin; s <= g_heapEnd; s = s->end<HeapSegment>()) {
if (s->size >= sz && !s->inUse) {
return s;
@ -78,7 +78,8 @@ static HeapSegment *findSegmentFor(std::size_t sz) {
return nullptr;
}
[[nodiscard]] void *malloc(std::size_t allocSize) {
[[nodiscard]]
void *malloc(std::size_t allocSize) noexcept {
const auto targetSize = alignedSize(sizeof(HeapSegment)) + alignedSize(allocSize);
auto seg = findSegmentFor(targetSize);
if (seg == nullptr) {
@ -91,7 +92,7 @@ static HeapSegment *findSegmentFor(std::size_t sz) {
return seg->data<void>();
}
void free(void *ptr) {
void free(void *ptr) noexcept {
auto p = findSegmentOf(ptr);
if (p.anteSegment) {
p.anteSegment->size += p.segment->size;
@ -108,35 +109,35 @@ void free(void *ptr) {
using namespace ox;
void *operator new(std::size_t allocSize) {
void *operator new(std::size_t allocSize) noexcept {
return heapmgr::malloc(allocSize);
}
void *operator new[](std::size_t allocSize) {
void *operator new[](std::size_t allocSize) noexcept {
return heapmgr::malloc(allocSize);
}
void operator delete(void *ptr) {
void operator delete(void *ptr) noexcept {
heapmgr::free(ptr);
}
void operator delete[](void *ptr) {
void operator delete[](void *ptr) noexcept {
heapmgr::free(ptr);
}
void operator delete(void *ptr, unsigned) {
void operator delete(void *ptr, unsigned) noexcept {
heapmgr::free(ptr);
}
void operator delete[](void *ptr, unsigned) {
void operator delete[](void *ptr, unsigned) noexcept {
heapmgr::free(ptr);
}
void operator delete(void *ptr, unsigned long int) {
void operator delete(void *ptr, unsigned long int) noexcept {
heapmgr::free(ptr);
}
void operator delete[](void *ptr, unsigned long int) {
void operator delete[](void *ptr, unsigned long int) noexcept {
heapmgr::free(ptr);
}

View File

@ -14,20 +14,23 @@ struct HeapSegment {
std::size_t size;
uint8_t inUse;
void init(std::size_t maxSize);
void init(std::size_t maxSize) noexcept;
template<typename T>
T *data();
[[nodiscard]]
T *data() noexcept;
template<typename T = uint8_t>
T *end();
[[nodiscard]]
T *end() noexcept;
};
void initHeap(char *heapBegin, char *heapEnd);
void initHeap(char *heapBegin, char *heapEnd) noexcept;
[[nodiscard]] void *malloc(std::size_t allocSize);
[[nodiscard]]
void *malloc(std::size_t allocSize) noexcept;
void free(void *ptr);
void free(void *ptr) noexcept;
}

View File

@ -13,17 +13,20 @@
namespace ox {
template<typename T>
inline constexpr const T &min(const T &a, const T &b) {
[[nodiscard]]
constexpr const T &min(const T &a, const T &b) noexcept {
return a < b ? a : b;
}
template<typename T>
inline constexpr const T &max(const T &a, const T &b) {
[[nodiscard]]
constexpr const T &max(const T &a, const T &b) noexcept {
return a > b ? a : b;
}
template<typename I>
inline constexpr I pow(I v, int e) {
[[nodiscard]]
constexpr I pow(I v, int e) noexcept {
I out = 1;
for (I i = 0; i < e; i++) {
out *= v;

View File

@ -11,17 +11,17 @@
namespace ox {
Random::Random() {
Random::Random() noexcept {
m_seed[0] = 540932923848;
m_seed[1] = 540932540932;
}
Random::Random(RandomSeed seed) {
Random::Random(RandomSeed seed) noexcept {
m_seed[0] = seed[0];
m_seed[1] = seed[1];
}
uint64_t Random::gen() {
uint64_t Random::gen() noexcept {
// An implementation of the Xoroshiro128+ algorithm
auto s0 = m_seed[0];
auto s1 = m_seed[1];

View File

@ -20,11 +20,11 @@ class OX_PACKED Random {
RandomSeed m_seed;
public:
Random();
Random() noexcept;
explicit Random(RandomSeed seed);
explicit Random(RandomSeed seed) noexcept;
uint64_t gen();
uint64_t gen() noexcept;
};
}

View File

@ -17,7 +17,7 @@
namespace ox {
void printStackTrace([[maybe_unused]]int shave) {
void printStackTrace([[maybe_unused]]int shave) noexcept {
#if defined(OX_USE_STDLIB) && __has_include(<unistd.h>)
std::array<void*, 100> frames;
auto size = static_cast<int>(backtrace(frames.data(), frames.size()));

View File

@ -15,6 +15,6 @@ namespace ox {
*
* @param shave number of call levels to shave off the top
*/
void printStackTrace(int shave = 1);
void printStackTrace(int shave = 1) noexcept;
}

View File

@ -14,12 +14,12 @@ namespace ox {
struct BaseInteger {
constexpr BaseInteger() = default;
constexpr BaseInteger() noexcept = default;
constexpr BaseInteger(const BaseInteger&) {
constexpr BaseInteger(const BaseInteger&) noexcept {
}
constexpr BaseInteger operator=(const BaseInteger&) {
constexpr BaseInteger operator=(const BaseInteger&) noexcept {
return *this;
}