From 922323833c93fc23c294d6abd4d8175cf1826cde Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 18 Apr 2024 23:32:54 -0500 Subject: [PATCH] [ox] Cleanup --- deps/ox/src/ox/claw/read.cpp | 2 +- .../src/ox/fs/filestore/filestoretemplate.hpp | 8 +-- deps/ox/src/ox/fs/filesystem/filelocation.cpp | 2 +- deps/ox/src/ox/fs/filesystem/filelocation.hpp | 7 +- deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp | 12 ++-- deps/ox/src/ox/logconn/circularbuff.hpp | 10 +-- deps/ox/src/ox/mc/intops.hpp | 10 +-- deps/ox/src/ox/model/modelvalue.hpp | 12 ++-- deps/ox/src/ox/std/bit.hpp | 2 +- deps/ox/src/ox/std/bstring.hpp | 4 +- deps/ox/src/ox/std/buffer.hpp | 2 +- deps/ox/src/ox/std/memops.cpp | 53 +++++++++++++-- deps/ox/src/ox/std/memops.hpp | 68 +++++-------------- deps/ox/src/ox/std/string.hpp | 2 +- deps/ox/src/ox/std/test/tests.cpp | 8 +-- 15 files changed, 103 insertions(+), 99 deletions(-) diff --git a/deps/ox/src/ox/claw/read.cpp b/deps/ox/src/ox/claw/read.cpp index 9506cead..c4e43c1d 100644 --- a/deps/ox/src/ox/claw/read.cpp +++ b/deps/ox/src/ox/claw/read.cpp @@ -64,7 +64,7 @@ Result readClawHeader(const ox::Buffer &buff) noexcept { Result stripClawHeader(const char *buff, std::size_t buffLen) noexcept { oxRequire(header, readClawHeader(buff, buffLen)); Buffer out(header.dataSize); - ox_memcpy(out.data(), header.data, out.size()); + ox::listcpy(out.data(), header.data, out.size()); return out; } diff --git a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp index 6c00005c..ef4706c1 100644 --- a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp +++ b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp @@ -279,7 +279,7 @@ Error FileStoreTemplate::write(uint64_t id64, const void *data, FsSize_t oxAssert(destData.size() == dataSize, "Allocation size does not match data."); // write data if any was provided if (data != nullptr) { - ox_memcpy(destData, data, dest->size()); + ox::memcpy(destData, data, dest->size()); oxTrace("ox.fs.FileStoreTemplate.write", "Data written"); } auto fsData = fileStoreData(); @@ -336,7 +336,7 @@ Error FileStoreTemplate::read(uint64_t id, void *out, FsSize_t outSize, return OxError(1); } - ox_memcpy(out, srcData, srcData.size()); + ox::memcpy(out, srcData, srcData.size()); if (size) { *size = src.size(); } @@ -367,7 +367,7 @@ Error FileStoreTemplate::read(uint64_t id, FsSize_t readStart, FsSize_t return OxError(1); } - ox_memcpy(out, srcData.get() + readStart, readSize); + ox::memcpy(out, srcData.get() + readStart, readSize); if (size) { *size = src.size(); } @@ -400,7 +400,7 @@ Error FileStoreTemplate::read(uint64_t id, FsSize_t readStart, return OxError(1); } - ox_memcpy(out, srcData.get() + readStart, readSize); + ox::memcpy(out, srcData.get() + readStart, readSize); if (size) { *size = src.size(); } diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.cpp b/deps/ox/src/ox/fs/filesystem/filelocation.cpp index 72238e01..15cd529f 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.cpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.cpp @@ -48,7 +48,7 @@ FileAddress &FileAddress::operator=(const FileAddress &other) noexcept { if (other.m_data.path) { auto strSize = ox::strlen(other.m_data.path) + 1; m_data.path = new char[strSize]; - ox_memcpy(m_data.path, other.m_data.path, strSize); + ox::memcpy(m_data.path, other.m_data.path, strSize); } else { m_data.constPath = ""; m_type = FileAddressType::ConstPath; diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.hpp b/deps/ox/src/ox/fs/filesystem/filelocation.hpp index ded301e9..436d6cf1 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.hpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.hpp @@ -44,13 +44,10 @@ class FileAddress { protected: FileAddressType m_type = FileAddressType::None; - Data m_data; + Data m_data{}; public: - constexpr FileAddress() noexcept { - m_data.inode = 0; - m_type = FileAddressType::None; - } + constexpr FileAddress() noexcept = default; FileAddress(const FileAddress &other) noexcept; diff --git a/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp b/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp index c9bce570..e5ba4dd6 100644 --- a/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp +++ b/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp @@ -173,15 +173,15 @@ class OX_PACKED NodeBuffer { template NodeBuffer::NodeBuffer(std::size_t size) noexcept { m_header.size = static_cast(size); - ox_memset(this + 1, 0, size - sizeof(*this)); + ox::memset(this + 1, 0, size - sizeof(*this)); oxTracef("ox.NodeBuffer.constructor", "{}", m_header.firstItem.get()); } template NodeBuffer::NodeBuffer(const NodeBuffer &other, std::size_t size) noexcept { oxTracef("ox.ptrarith.NodeBuffer.copy", "other.m_header.firstItem: {}", other.m_header.firstItem.get()); - ox_memset(this + 1, 0, size - sizeof(*this)); - ox_memcpy(this, &other, size); + ox::memset(this + 1, 0, size - sizeof(*this)); + ox::memcpy(this, &other, size); } template @@ -291,7 +291,7 @@ Result::ItemPtr> NodeBuffer::mal oxTrace("ox.ptrarith.NodeBuffer.malloc.fail", "Unknown"); return OxError(1, "NodeBuffer::malloc: unknown failure"); } - ox_memset(out, 0, fullSize); + ox::memset(out, 0, fullSize); new (out) Item; out->setSize(sz); @@ -367,7 +367,7 @@ Error NodeBuffer::setSize(std::size_t size) noexcept { } else { m_header.size = static_cast(size); auto data = reinterpret_cast(this) + end; - ox_memset(data, 0, size - end); + ox::memset(data, 0, size - end); return OxError(0); } } @@ -405,7 +405,7 @@ Error NodeBuffer::compact(F cb) noexcept { return OxError(2); } // move node - ox_memcpy(dest, src, src->fullSize()); + ox::memcpy(dest, src, src->fullSize()); oxReturnError(cb(src, dest)); // update surrounding nodes auto prev = ptr(dest->prev); diff --git a/deps/ox/src/ox/logconn/circularbuff.hpp b/deps/ox/src/ox/logconn/circularbuff.hpp index 69a2c32f..32c0a3ee 100644 --- a/deps/ox/src/ox/logconn/circularbuff.hpp +++ b/deps/ox/src/ox/logconn/circularbuff.hpp @@ -47,13 +47,13 @@ class CirculerBuffer { } // write seg 1 const auto seg1Sz = ox::min(sz, m_buff.size() - m_writePt); - ox_memcpy(&m_buff[m_writePt], &buff[0], seg1Sz); + ox::listcpy(&m_buff[m_writePt], &buff[0], seg1Sz); m_writePt += sz; if (seg1Sz != sz) { m_writePt -= m_buff.size(); // write seg 2 const auto seg2Sz = sz - seg1Sz; - ox_memcpy(&m_buff[0], &buff[seg1Sz], seg2Sz); + ox::listcpy(&m_buff[0], &buff[seg1Sz], seg2Sz); oxAssert(m_buff[0] == buff[seg1Sz], "break"); } return {}; @@ -70,7 +70,7 @@ class CirculerBuffer { return {}; } - constexpr ox::Error seekp(int, ios_base::seekdir) { + constexpr ox::Error seekp(int, ios_base::seekdir) noexcept { return OxError(1, "Unimplemented"); } @@ -84,13 +84,13 @@ class CirculerBuffer { const auto bytesRead = ox::min(outSize, m_buff.size() - avail()); // read seg 1 const auto seg1Sz = ox::min(bytesRead, m_buff.size() - m_readPt); - ox_memcpy(&out[0], &m_buff[m_readPt], seg1Sz); + ox::listcpy(&out[0], &m_buff[m_readPt], seg1Sz); m_readPt += bytesRead; if (seg1Sz != bytesRead) { m_readPt -= m_buff.size(); // read seg 2 const auto seg2Sz = bytesRead - seg1Sz; - ox_memcpy(&out[seg1Sz], &m_buff[0], seg2Sz); + ox::listcpy(&out[seg1Sz], &m_buff[0], seg2Sz); } return bytesRead; } diff --git a/deps/ox/src/ox/mc/intops.hpp b/deps/ox/src/ox/mc/intops.hpp index b9c6bd92..7f369ef6 100644 --- a/deps/ox/src/ox/mc/intops.hpp +++ b/deps/ox/src/ox/mc/intops.hpp @@ -71,7 +71,7 @@ constexpr McInt encodeInteger(I pInput) noexcept { // move input to uint64_t to allow consistent bit manipulation, and to avoid // overflow concerns uint64_t val = 0; - ox_memcpy(&val, &input, sizeof(input)); + ox::memcpy(&val, &input, sizeof(input)); if (val) { // bits needed to represent number factoring in space possibly // needed for signed bit @@ -94,7 +94,7 @@ constexpr McInt encodeInteger(I pInput) noexcept { } if (bytes == 9) { out.data[0] = bytesIndicator; - ox_memcpy(&out.data[1], &leVal, 8); + ox::memcpy(&out.data[1], &leVal, 8); if (inputNegative) { out.data[1] |= 0b1000'0000; } @@ -104,7 +104,7 @@ constexpr McInt encodeInteger(I pInput) noexcept { auto intermediate = static_cast(leVal.raw() | (negBit << (valBits - 1))) << bytes | static_cast(bytesIndicator); - ox_memcpy(out.data, &intermediate, sizeof(intermediate)); + ox::memcpy(out.data, &intermediate, sizeof(intermediate)); } out.length = bytes; } @@ -160,7 +160,7 @@ constexpr Result decodeInteger(Reader_c auto&rdr, std::size_t *bytesRead) noe ox::Array d = {}; //d[0] = decoded & 0xffff'ffff; //d[1] = decoded >> 32; - ox_memcpy(d.data(), &decoded, sizeof(decoded)); + ox::memcpy(d.data(), &decoded, sizeof(decoded)); auto bit = negBit; for (; bit < ox::min(Bits, 32); ++bit) { d[0] |= 1 << bit; @@ -175,7 +175,7 @@ constexpr Result decodeInteger(Reader_c auto&rdr, std::size_t *bytesRead) noe d[0] = d[1]; d[1] = d0Tmp; } - ox_memcpy(&out, d.data(), sizeof(out)); + ox::memcpy(&out, d.data(), sizeof(out)); return out; } } diff --git a/deps/ox/src/ox/model/modelvalue.hpp b/deps/ox/src/ox/model/modelvalue.hpp index 53130335..416c894f 100644 --- a/deps/ox/src/ox/model/modelvalue.hpp +++ b/deps/ox/src/ox/model/modelvalue.hpp @@ -995,7 +995,7 @@ constexpr ModelValue::ModelValue(const ModelValue &other) noexcept { case Type::SignedInteger16: case Type::SignedInteger32: case Type::SignedInteger64: - ox_memcpy(&m_data, &other.m_data, sizeof(m_data)); + ox::memcpy(&m_data, &other.m_data, sizeof(m_data)); break; case Type::String: m_data.str = new String(other.get()); @@ -1028,8 +1028,8 @@ constexpr ModelValue::ModelValue(ModelValue &&other) noexcept { case Type::SignedInteger16: case Type::SignedInteger32: case Type::SignedInteger64: - ox_memcpy(&m_data, &other.m_data, sizeof(m_data)); - ox_memset(&other.m_data, 0, sizeof(m_data)); + ox::memcpy(&m_data, &other.m_data, sizeof(m_data)); + ox::memset(&other.m_data, 0, sizeof(m_data)); break; case Type::String: m_data.str = other.m_data.str; @@ -1220,7 +1220,7 @@ constexpr ModelValue &ModelValue::operator=(const ModelValue &other) noexcept { case Type::SignedInteger16: case Type::SignedInteger32: case Type::SignedInteger64: - ox_memcpy(&m_data, &other.m_data, sizeof(m_data)); + ox::memcpy(&m_data, &other.m_data, sizeof(m_data)); break; case Type::String: m_data.str = new String(other.get()); @@ -1258,8 +1258,8 @@ constexpr ModelValue &ModelValue::operator=(ModelValue &&other) noexcept { case Type::SignedInteger16: case Type::SignedInteger32: case Type::SignedInteger64: - ox_memcpy(&m_data, &other.m_data, sizeof(m_data)); - ox_memset(&other.m_data, 0, sizeof(m_data)); + ox::memcpy(&m_data, &other.m_data, sizeof(m_data)); + ox::memset(&other.m_data, 0, sizeof(m_data)); break; case Type::String: m_data.str = other.m_data.str; diff --git a/deps/ox/src/ox/std/bit.hpp b/deps/ox/src/ox/std/bit.hpp index d8be957f..f4b8d55d 100644 --- a/deps/ox/src/ox/std/bit.hpp +++ b/deps/ox/src/ox/std/bit.hpp @@ -34,7 +34,7 @@ namespace ox { template constexpr typename enable_if::type cbit_cast(From src) noexcept { To dst = {}; - ox_memcpy(&dst, &src, sizeof(src)); + ox::memcpy(&dst, &src, sizeof(src)); return dst; } diff --git a/deps/ox/src/ox/std/bstring.hpp b/deps/ox/src/ox/std/bstring.hpp index 7a450ede..d73585e7 100644 --- a/deps/ox/src/ox/std/bstring.hpp +++ b/deps/ox/src/ox/std/bstring.hpp @@ -119,7 +119,7 @@ constexpr BString &BString::operator=(ox::CRStringView str) noexcept if (cap() < strLen) { strLen = cap(); } - ox_memcpy(m_buff, str.data(), strLen); + ox::memcpy(m_buff, str.data(), strLen); // make sure last element is a null terminator m_buff[strLen] = 0; return *this; @@ -131,7 +131,7 @@ constexpr BString &BString::operator=(const char *str) noexcept { if (cap() < strLen) { strLen = cap(); } - ox_memcpy(m_buff, str, strLen); + ox::memcpy(m_buff, str, strLen); // make sure last element is a null terminator m_buff[cap()] = 0; return *this; diff --git a/deps/ox/src/ox/std/buffer.hpp b/deps/ox/src/ox/std/buffer.hpp index 2753c821..674a4a20 100644 --- a/deps/ox/src/ox/std/buffer.hpp +++ b/deps/ox/src/ox/std/buffer.hpp @@ -192,7 +192,7 @@ class BufferReader { if (m_it + sz > m_size) [[unlikely]] { return OxError(1, "Read failed: Buffer overrun"); } - ox_memcpy(v, &m_buff[m_it], sz); + ox::memcpy(v, &m_buff[m_it], sz); m_it += sz; return sz; } diff --git a/deps/ox/src/ox/std/memops.cpp b/deps/ox/src/ox/std/memops.cpp index 16c71ca1..c76dbf11 100644 --- a/deps/ox/src/ox/std/memops.cpp +++ b/deps/ox/src/ox/std/memops.cpp @@ -6,6 +6,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "defines.hpp" #include "types.hpp" #include "memops.hpp" @@ -16,15 +17,15 @@ extern "C" { void *ox_inhibit_loop_to_libcall memcpy(void *dest, const void *src, std::size_t size) { - return ox_memcpy(dest, src, size); + return ox::memcpy(dest, src, size); } void *ox_inhibit_loop_to_libcall memmove(void *dest, const void *src, std::size_t size) { - return ox_memcpy(dest, src, size); + return ox::memcpy(dest, src, size); } void *ox_inhibit_loop_to_libcall memset(void *ptr, int val, std::size_t size) { - return ox_memset(ptr, val, size); + return ox::memset(ptr, val, size); } @@ -50,6 +51,48 @@ int ox_inhibit_loop_to_libcall memcmp(const void *ptr1, const void *ptr2, std::s #endif -int ox_memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept { - return memcmp(ptr1, ptr2, size); +namespace ox { + +void *memmove(void *dest, const void *src, std::size_t size) noexcept { + if constexpr(!ox::defines::UseStdLib) { + auto srcBuf = static_cast(src); + auto dstBuf = static_cast(dest); + for (std::size_t i = 0; i < size; ++i) { + dstBuf[i] = static_cast(srcBuf[i]); + } + return dest; + } else { + return ::memmove(dest, src, size); + } +} + +void *memset(void *ptr, int val, std::size_t size) noexcept { + if constexpr(!ox::defines::UseStdLib) { + auto buf = static_cast(ptr); + for (std::size_t i = 0; i < size; ++i) { + buf[i] = static_cast(val); + } + return ptr; + } else { + return ::memset(ptr, val, size); + } +} + +void *memcpy(void *dest, const void *src, std::size_t size) noexcept { + if constexpr(!ox::defines::UseStdLib) { + auto srcBuf = static_cast(src); + auto dstBuf = static_cast(dest); + for (std::size_t i = 0; i < size; ++i) { + dstBuf[i] = static_cast(srcBuf[i]); + } + return dest; + } else { + return ::memcpy(dest, src, size); + } +} + +int memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept { + return ::memcmp(ptr1, ptr2, size); +} + } diff --git a/deps/ox/src/ox/std/memops.hpp b/deps/ox/src/ox/std/memops.hpp index 56bd7d11..470eebe5 100644 --- a/deps/ox/src/ox/std/memops.hpp +++ b/deps/ox/src/ox/std/memops.hpp @@ -8,7 +8,6 @@ #pragma once -#include "defines.hpp" #include "types.hpp" #include "typetraits.hpp" @@ -28,63 +27,28 @@ int memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept; } #endif -int ox_memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept; - -constexpr void *ox_memcpy(void *dest, const void *src, std::size_t size) noexcept { - if (std::is_constant_evaluated() || !ox::defines::UseStdLib) { - auto srcBuf = static_cast(src); - auto dstBuf = static_cast(dest); - for (std::size_t i = 0; i < size; ++i) { - dstBuf[i] = static_cast(srcBuf[i]); - } - return dest; - } else { - return memcpy(dest, src, size); - } -} - -constexpr void *ox_memmove(void *dest, const void *src, std::size_t size) noexcept { - if (std::is_constant_evaluated() || !ox::defines::UseStdLib) { - auto srcBuf = static_cast(src); - auto dstBuf = static_cast(dest); - for (std::size_t i = 0; i < size; ++i) { - dstBuf[i] = static_cast(srcBuf[i]); - } - return dest; - } else { - return memmove(dest, src, size); - } -} - -constexpr void *ox_memset(void *ptr, int val, std::size_t size) noexcept { - if (std::is_constant_evaluated() || !ox::defines::UseStdLib) { - auto buf = static_cast(ptr); - for (std::size_t i = 0; i < size; ++i) { - buf[i] = static_cast(val); - } - return ptr; - } else { - return memset(ptr, val, size); - } -} - namespace ox { -constexpr void *memmove(void *dest, const void *src, std::size_t size) { - return ox_memmove(dest, src, size); +template +constexpr T1 *listcpy(T1 *dest, T2 *src, std::size_t maxLen) noexcept { + using T1Type = typename ox::remove_reference::type; + std::size_t i = 0; + while (i < maxLen && src[i]) { + dest[i] = static_cast(src[i]); + ++i; + } + // set null terminator + dest[i] = 0; + return dest; } -constexpr void *memset(void *ptr, int val, std::size_t size) noexcept { - return ox_memset(ptr, val, size); -} +void *memmove(void *dest, const void *src, std::size_t size) noexcept; -constexpr void *memcpy(void *dest, const void *src, std::size_t size) noexcept { - return ox_memcpy(dest, src, size); -} +void *memset(void *ptr, int val, std::size_t size) noexcept; -inline int memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept { - return ox_memcmp(ptr1, ptr2, size); -} +void *memcpy(void *dest, const void *src, std::size_t size) noexcept; + +int memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept; template void *memsetElements(T *ptr, T val, std::size_t elements) noexcept { diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index e2604310..8b930920 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -167,7 +167,7 @@ class BasicString { constexpr Error append(const char *str, std::size_t strLen) noexcept { auto currentLen = len(); m_buff.resize(m_buff.size() + strLen); - ox_memcpy(&m_buff[currentLen], str, strLen); + ox::memcpy(&m_buff[currentLen], str, strLen); // make sure last element is a null terminator m_buff[currentLen + strLen] = 0; // this can't fail, but it returns an Error to match BString::append diff --git a/deps/ox/src/ox/std/test/tests.cpp b/deps/ox/src/ox/std/test/tests.cpp index 3aeb015a..14f8bdcb 100644 --- a/deps/ox/src/ox/std/test/tests.cpp +++ b/deps/ox/src/ox/std/test/tests.cpp @@ -44,25 +44,25 @@ static std::map tests = { { "ABCDEFG != HIJKLMN", []() { - return OxError(ox_memcmp("ABCDEFG", "HIJKLMN", 7) >= 0); + return OxError(ox::memcmp("ABCDEFG", "HIJKLMN", 7) >= 0); } }, { "HIJKLMN != ABCDEFG", []() { - return OxError(ox_memcmp("HIJKLMN", "ABCDEFG", 7) <= 0); + return OxError(ox::memcmp("HIJKLMN", "ABCDEFG", 7) <= 0); } }, { "ABCDEFG == ABCDEFG", []() { - return OxError(ox_memcmp("ABCDEFG", "ABCDEFG", 7) != 0); + return OxError(ox::memcmp("ABCDEFG", "ABCDEFG", 7) != 0); } }, { "ABCDEFGHI == ABCDEFG", []() { - return OxError(ox_memcmp("ABCDEFGHI", "ABCDEFG", 7) != 0); + return OxError(ox::memcmp("ABCDEFGHI", "ABCDEFG", 7) != 0); } }, {