Compare commits

..

6 Commits

27 changed files with 695 additions and 211 deletions

View File

@ -5,6 +5,7 @@ add_subdirectory(clargs)
add_subdirectory(claw)
add_subdirectory(event)
add_subdirectory(fs)
add_subdirectory(logconn)
add_subdirectory(mc)
add_subdirectory(model)
add_subdirectory(preloader)

View File

@ -324,7 +324,7 @@ Error FileStoreTemplate<size_t>::read(InodeId_t id, void *out, FsSize_t outSize,
auto srcData = m_buffer->template dataOf<uint8_t>(src);
oxTracef("ox::fs::FileStoreTemplate::read::found", "{} found at {} with data section at {}",
id, src.offset(), srcData.offset());
id, src.offset(), srcData.offset());
oxTracef("ox::fs::FileStoreTemplate::read::outSize", "{} {} {}", srcData.offset(), srcData.size(), outSize);
// error check
@ -344,40 +344,67 @@ Error FileStoreTemplate<size_t>::read(InodeId_t id, void *out, FsSize_t outSize,
}
template<typename size_t>
Error FileStoreTemplate<size_t>::read(InodeId_t id, FsSize_t readStart, FsSize_t readSize, void *data, FsSize_t *size) const {
oxRequireM(src, find(id).validate());
oxRequireM(srcData, src->data().validate());
oxRequire(sub, srcData.template subPtr<uint8_t>(readStart, readSize).validate());
memcpy(data, sub, sub.size());
if (size) {
*size = sub.size();
Error FileStoreTemplate<size_t>::read(InodeId_t id, FsSize_t readStart, FsSize_t readSize, void *out, FsSize_t *size) const {
oxTracef("ox::fs::FileStoreTemplate::read", "Attempting to read from inode {}", id);
auto src = find(id);
// error check
if (!src.valid()) {
oxTracef("ox::fs::FileStoreTemplate::read::fail", "Could not find requested item: {}", id);
return OxError(1);
}
return OxError(0);
auto srcData = m_buffer->template dataOf<uint8_t>(src);
oxTracef("ox::fs::FileStoreTemplate::read::found", "{} found at {} with data section at {}",
id, src.offset(), srcData.offset());
oxTracef("ox::fs::FileStoreTemplate::read::readSize", "{} {} {}", srcData.offset(), srcData.size(), readSize);
// error check
if (!(srcData.valid() && srcData.size() - readStart <= readSize)) {
oxTracef("ox::fs::FileStoreTemplate::read::fail", "Could not read data section of item: {}", id);
oxTracef("ox::fs::FileStoreTemplate::read::fail",
"Item data section size: {}, Expected size: {}", srcData.size(), readSize);
return OxError(1);
}
ox_memcpy(out, srcData.get() + readStart, readSize);
if (size) {
*size = src.size();
}
return {};
}
template<typename size_t>
template<typename T>
Error FileStoreTemplate<size_t>::read(InodeId_t id, FsSize_t readStart,
FsSize_t readSize, T *data, FsSize_t *size) const {
oxRequireM(src, find(id).validate());
oxRequireM(srcData, src->data().validate());
auto sub = srcData.template subPtr<uint8_t>(readStart, readSize);
if (sub.valid() && sub.size() % sizeof(T)) {
for (FsSize_t i = 0; i < sub.size() / sizeof(T); i++) {
// do byte-by-byte copy to ensure alignment is right when
// copying to final destination
T tmp;
for (size_t ii = 0; ii < sizeof(T); ii++) {
reinterpret_cast<uint8_t*>(&tmp)[ii] = *(sub.get() + ii);
}
*(data + i) = tmp;
}
if (size) {
*size = sub.size();
}
return OxError(0);
FsSize_t readSize, T *out, FsSize_t *size) const {
oxTracef("ox::fs::FileStoreTemplate::read", "Attempting to read from inode {}", id);
auto src = find(id);
// error check
if (!src.valid()) {
oxTracef("ox::fs::FileStoreTemplate::read::fail", "Could not find requested item: {}", id);
return OxError(1);
}
return OxError(1);
auto srcData = m_buffer->template dataOf<uint8_t>(src);
oxTracef("ox::fs::FileStoreTemplate::read::found", "{} found at {} with data section at {}",
id, src.offset(), srcData.offset());
oxTracef("ox::fs::FileStoreTemplate::read::readSize", "{} {} {}", srcData.offset(), srcData.size(), readSize);
// error check
if (!(srcData.valid() && srcData.size() - readStart <= readSize)) {
oxTracef("ox::fs::FileStoreTemplate::read::fail", "Could not read data section of item: {}", id);
oxTracef("ox::fs::FileStoreTemplate::read::fail",
"Item data section size: {}, Expected size: {}", srcData.size(), readSize);
return OxError(1);
}
ox_memcpy(out, srcData.get() + readStart, readSize);
if (size) {
*size = src.size();
}
return {};
}
template<typename size_t>
@ -478,7 +505,7 @@ Error FileStoreTemplate<size_t>::compact() {
if (!item.valid()) {
return OxError(1);
}
oxTracef("ox::FileStoreTemplate::compact::moveItem", "Moving Item: {} from {} to {}", item->id.get(), oldAddr, item.offset());
oxTracef("ox::fs::FileStoreTemplate::compact::moveItem", "Moving Item: {} from {} to {}", item->id.get(), oldAddr, item.offset());
// update rootInode if this is it
auto fsData = fileStoreData();
if (fsData && oldAddr == fsData->rootNode) {

View File

@ -13,7 +13,7 @@
namespace ox {
Result<const char*> FileSystem::directAccess(const FileAddress &addr) noexcept {
Result<const char*> MemFS::directAccess(const FileAddress &addr) noexcept {
switch (addr.type()) {
case FileAddressType::Inode:
return directAccess(addr.getInode().value);
@ -28,10 +28,10 @@ Result<const char*> FileSystem::directAccess(const FileAddress &addr) noexcept {
Error FileSystem::read(const FileAddress &addr, void *buffer, std::size_t size) noexcept {
switch (addr.type()) {
case FileAddressType::Inode:
return read(addr.getInode().value, buffer, size);
return readFileInode(addr.getInode().value, buffer, size);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return read(StringView(addr.getPath().value), buffer, size);
return readFilePath(StringView(addr.getPath().value), buffer, size);
default:
return OxError(1);
}
@ -45,9 +45,9 @@ Result<Buffer> FileSystem::read(const FileAddress &addr) noexcept {
}
Result<Buffer> FileSystem::read(CRStringView path) noexcept {
oxRequire(s, stat(path));
oxRequire(s, statPath(path));
Buffer buff(s.size);
oxReturnError(read(path, buff.data(), buff.size()));
oxReturnError(readFilePath(path, buff.data(), buff.size()));
return buff;
}
@ -78,10 +78,10 @@ Error FileSystem::remove(const FileAddress &addr, bool recursive) noexcept {
Error FileSystem::write(const FileAddress &addr, const void *buffer, uint64_t size, FileType fileType) noexcept {
switch (addr.type()) {
case FileAddressType::Inode:
return write(addr.getInode().value, buffer, size, fileType);
return writeFileInode(addr.getInode().value, buffer, size, fileType);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return write(StringView(addr.getPath().value), buffer, size, fileType);
return writeFilePath(StringView(addr.getPath().value), buffer, size, fileType);
default:
return OxError(1);
}
@ -90,10 +90,10 @@ Error FileSystem::write(const FileAddress &addr, const void *buffer, uint64_t si
Result<FileStat> FileSystem::stat(const FileAddress &addr) const noexcept {
switch (addr.type()) {
case FileAddressType::Inode:
return stat(addr.getInode().value);
return statInode(addr.getInode().value);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return stat(StringView(addr.getPath().value));
return statPath(StringView(addr.getPath().value));
default:
return OxError(1);
}

View File

@ -38,26 +38,21 @@ class FileSystem {
*/
virtual Error move(CRStringView src, CRStringView dest) noexcept = 0;
virtual Error read(CRStringView path, void *buffer, std::size_t buffSize) noexcept = 0;
virtual Result<const char*> directAccess(CRStringView path) noexcept = 0;
virtual Error read(uint64_t inode, void *buffer, std::size_t size) noexcept = 0;
virtual Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept = 0;
virtual Result<const char*> directAccess(uint64_t inode) noexcept = 0;
Error read(const FileAddress &addr, void *buffer, std::size_t size) noexcept;
Result<Buffer> read(const FileAddress &addr) noexcept;
Result<Buffer> read(CRStringView path) noexcept;
Error read(const FileAddress &addr, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept;
inline Error read(CRStringView path, void *buffer, std::size_t buffSize) noexcept {
return readFilePath(path, buffer, buffSize);
}
[[maybe_unused]]
Result<const char*> directAccess(const FileAddress &addr) noexcept;
inline Error read(uint64_t inode, void *buffer, std::size_t buffSize) noexcept {
return readFileInode(inode, buffer, buffSize);
}
Error read(const FileAddress &addr, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept;
virtual Result<Vector<String>> ls(CRStringView dir) const noexcept = 0;
@ -67,12 +62,8 @@ class FileSystem {
virtual Error resize(uint64_t size, void *buffer) noexcept = 0;
virtual Error write(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept = 0;
virtual Error write(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept = 0;
Error write(CRStringView path, const void *buffer, uint64_t size) noexcept {
return write(path, buffer, size, FileType::NormalFile);
return writeFilePath(path, buffer, size, FileType::NormalFile);
}
Error write(uint64_t inode, const void *buffer, uint64_t size) noexcept {
@ -81,9 +72,21 @@ class FileSystem {
Error write(const FileAddress &addr, const void *buffer, uint64_t size, FileType fileType = FileType::NormalFile) noexcept;
virtual Result<FileStat> stat(uint64_t inode) const noexcept = 0;
inline Error write(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept {
return writeFilePath(path, buffer, size, fileType);
}
virtual Result<FileStat> stat(CRStringView path) const noexcept = 0;
inline Error write(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept {
return writeFileInode(inode, buffer, size, fileType);
}
inline Result<FileStat> stat(uint64_t inode) const noexcept {
return statInode(inode);
}
inline Result<FileStat> stat(CRStringView path) const noexcept {
return statPath(path);
}
Result<FileStat> stat(const FileAddress &addr) const noexcept;
@ -104,6 +107,39 @@ class FileSystem {
[[nodiscard]]
virtual bool valid() const noexcept = 0;
protected:
virtual Result<FileStat> statInode(uint64_t inode) const noexcept = 0;
virtual Result<FileStat> statPath(CRStringView path) const noexcept = 0;
virtual Error readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept = 0;
virtual Error readFileInode(uint64_t inode, void *buffer, std::size_t size) noexcept = 0;
virtual Error readFileInodeRange(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept = 0;
virtual Error writeFilePath(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept = 0;
virtual Error writeFileInode(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept = 0;
};
class MemFS: public FileSystem {
public:
Result<const char*> directAccess(const FileAddress &addr) noexcept;
inline Result<const char*> directAccess(CRStringView path) noexcept {
return directAccessPath(path);
}
inline Result<const char*> directAccess(uint64_t inode) noexcept {
return directAccessInode(inode);
}
protected:
virtual Result<const char*> directAccessPath(CRStringView path) noexcept = 0;
virtual Result<const char*> directAccessInode(uint64_t inode) noexcept = 0;
};
/**
@ -113,7 +149,7 @@ class FileSystem {
* Note: Directory parameter must have a default constructor.
*/
template<typename FileStore, typename Directory>
class FileSystemTemplate: public FileSystem {
class FileSystemTemplate: public MemFS {
private:
static constexpr auto InodeFsData = 2;
@ -139,15 +175,15 @@ class FileSystemTemplate: public FileSystem {
Error move(CRStringView src, CRStringView dest) noexcept override;
Error read(CRStringView path, void *buffer, std::size_t buffSize) noexcept override;
Error readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept override;
Result<const char*> directAccess(CRStringView) noexcept override;
Result<const char*> directAccessPath(CRStringView) noexcept override;
Error read(uint64_t inode, void *buffer, std::size_t size) noexcept override;
Error readFileInode(uint64_t inode, void *buffer, std::size_t size) noexcept override;
Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept override;
Error readFileInodeRange(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept override;
Result<const char*> directAccess(uint64_t) noexcept override;
Result<const char*> directAccessInode(uint64_t) noexcept override;
Result<Vector<String>> ls(CRStringView dir) const noexcept override;
@ -163,14 +199,15 @@ class FileSystemTemplate: public FileSystem {
Error resize(uint64_t size, void *buffer) noexcept override;
Error write(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept override;
Error writeFilePath(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept override;
Error write(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept override;
Error writeFileInode(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept override;
Result<FileStat> stat(uint64_t inode) const noexcept override;
Result<FileStat> statInode(uint64_t inode) const noexcept override;
Result<FileStat> stat(CRStringView path) const noexcept override;
Result<FileStat> statPath(CRStringView path) const noexcept override;
[[nodiscard]]
uint64_t spaceNeeded(uint64_t size) const noexcept override;
Result<uint64_t> available() const noexcept override;
@ -254,33 +291,40 @@ Error FileSystemTemplate<FileStore, Directory>::move(CRStringView src, CRStringV
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::read(CRStringView path, void *buffer, std::size_t buffSize) noexcept {
Error FileSystemTemplate<FileStore, Directory>::readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept {
oxRequire(fd, fileSystemData());
Directory rootDir(m_fs, fd.rootDirInode);
oxRequire(s, stat(path));
if (s.size > buffSize) {
return OxError(1, "Buffer to small to load file");
}
return readFileInodeRange(s.inode, 0, s.size, buffer, &buffSize);
}
template<typename FileStore, typename Directory>
Result<const char*> FileSystemTemplate<FileStore, Directory>::directAccessPath(CRStringView path) noexcept {
oxRequire(fd, fileSystemData());
Directory rootDir(m_fs, fd.rootDirInode);
oxRequire(inode, rootDir.find(path));
return read(inode, buffer, buffSize);
return directAccessInode(inode);
}
template<typename FileStore, typename Directory>
Result<const char*> FileSystemTemplate<FileStore, Directory>::directAccess(CRStringView path) noexcept {
oxRequire(fd, fileSystemData());
Directory rootDir(m_fs, fd.rootDirInode);
oxRequire(inode, rootDir.find(path));
return directAccess(inode);
Error FileSystemTemplate<FileStore, Directory>::readFileInode(uint64_t inode, void *buffer, std::size_t buffSize) noexcept {
oxRequire(s, stat(inode));
if (s.size > buffSize) {
return OxError(1, "Buffer to small to load file");
}
return readFileInodeRange(inode, 0, s.size, buffer, &buffSize);
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::read(uint64_t inode, void *buffer, std::size_t buffSize) noexcept {
return m_fs.read(inode, buffer, buffSize);
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept {
Error FileSystemTemplate<FileStore, Directory>::readFileInodeRange(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept {
return m_fs.read(inode, readStart, readSize, reinterpret_cast<uint8_t*>(buffer), size);
}
template<typename FileStore, typename Directory>
Result<const char*> FileSystemTemplate<FileStore, Directory>::directAccess(uint64_t inode) noexcept {
Result<const char*> FileSystemTemplate<FileStore, Directory>::directAccessInode(uint64_t inode) noexcept {
auto data = m_fs.read(inode);
if (!data.valid()) {
return OxError(1, "Data not valid");
@ -312,7 +356,7 @@ Error FileSystemTemplate<FileStore, Directory>::remove(CRStringView path, bool r
oxRequire(fd, fileSystemData());
Directory rootDir(m_fs, fd.rootDirInode);
oxRequire(inode, rootDir.find(path));
oxRequire(st, stat(inode));
oxRequire(st, statInode(inode));
if (st.fileType == FileType::NormalFile || recursive) {
if (auto err = rootDir.remove(path)) {
// removal failed, try putting the index back
@ -338,7 +382,7 @@ Error FileSystemTemplate<FileStore, Directory>::resize(uint64_t size, void *buff
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::write(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept {
Error FileSystemTemplate<FileStore, Directory>::writeFilePath(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept {
auto [inode, err] = find(path);
if (err) {
oxRequire(generatedId, m_fs.generateInodeId());
@ -346,17 +390,17 @@ Error FileSystemTemplate<FileStore, Directory>::write(CRStringView path, const v
oxRequireM(rootDir, this->rootDir());
oxReturnError(rootDir.write(path, inode));
}
oxReturnError(write(inode, buffer, size, fileType));
oxReturnError(writeFileInode(inode, buffer, size, fileType));
return OxError(0);
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::write(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept {
Error FileSystemTemplate<FileStore, Directory>::writeFileInode(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept {
return m_fs.write(inode, buffer, size, static_cast<uint8_t>(fileType));
}
template<typename FileStore, typename Directory>
Result<FileStat> FileSystemTemplate<FileStore, Directory>::stat(uint64_t inode) const noexcept {
Result<FileStat> FileSystemTemplate<FileStore, Directory>::statInode(uint64_t inode) const noexcept {
oxRequire(s, m_fs.stat(inode));
FileStat out;
out.inode = s.inode;
@ -367,7 +411,7 @@ Result<FileStat> FileSystemTemplate<FileStore, Directory>::stat(uint64_t inode)
}
template<typename FileStore, typename Directory>
Result<FileStat> FileSystemTemplate<FileStore, Directory>::stat(CRStringView path) const noexcept {
Result<FileStat> FileSystemTemplate<FileStore, Directory>::statPath(CRStringView path) const noexcept {
oxRequire(inode, find(path));
return stat(inode);
}

View File

@ -63,41 +63,6 @@ Error PassThroughFS::move(CRStringView src, CRStringView dest) noexcept {
return OxError(0);
}
Error PassThroughFS::read(CRStringView path, void *buffer, std::size_t buffSize) noexcept {
try {
std::ifstream file((m_path / stripSlash(path)), std::ios::binary | std::ios::ate);
const std::size_t size = file.tellg();
file.seekg(0, std::ios::beg);
if (size > buffSize) {
oxTracef("ox::fs::PassThroughFS::read::error", "Read failed: Buffer too small: {}", path);
return OxError(1);
}
file.read(static_cast<char*>(buffer), static_cast<std::streamsize>(buffSize));
} catch (const std::fstream::failure &f) {
oxTracef("ox::fs::PassThroughFS::read::error", "Read of {} failed: {}", path, f.what());
return OxError(2);
}
return OxError(0);
}
Result<const char*> PassThroughFS::directAccess(CRStringView) noexcept {
return OxError(1, "PassThroughFS::directAccess not supported");
}
Error PassThroughFS::read(uint64_t, void*, std::size_t) noexcept {
// unsupported
return OxError(1, "read(uint64_t, void*, std::size_t) is not supported by PassThroughFS");
}
Error PassThroughFS::read(uint64_t, std::size_t, std::size_t, void*, std::size_t*) noexcept {
// unsupported
return OxError(1, "read(uint64_t, std::size_t, std::size_t, void*, std::size_t*) is not supported by PassThroughFS");
}
Result<const char*> PassThroughFS::directAccess(uint64_t) noexcept {
return OxError(1, "PassThroughFS::directAccess not supported");
}
Result<Vector<String>> PassThroughFS::ls(CRStringView dir) const noexcept {
Vector<String> out;
std::error_code ec;
@ -123,37 +88,20 @@ Error PassThroughFS::resize(uint64_t, void*) noexcept {
return OxError(1, "resize is not supported by PassThroughFS");
}
Error PassThroughFS::write(CRStringView path, const void *buffer, uint64_t size, FileType) noexcept {
const auto p = (m_path / stripSlash(path));
try {
std::ofstream f(p, std::ios::binary);
f.write(static_cast<const char*>(buffer), size);
} catch (const std::fstream::failure &f) {
oxTracef("ox::fs::PassThroughFS::read::error", "Write of {} failed: {}", path, f.what());
return OxError(1);
}
return OxError(0);
}
Error PassThroughFS::write(uint64_t, const void*, uint64_t, FileType) noexcept {
Result<FileStat> PassThroughFS::statInode(uint64_t) const noexcept {
// unsupported
return OxError(1, "write(uint64_t, void*, uint64_t, uint8_t) is not supported by PassThroughFS");
return OxError(1, "statInode(uint64_t) is not supported by PassThroughFS");
}
Result<FileStat> PassThroughFS::stat(uint64_t) const noexcept {
// unsupported
return OxError(1, "stat(uint64_t) is not supported by PassThroughFS");
}
Result<FileStat> PassThroughFS::stat(CRStringView path) const noexcept {
Result<FileStat> PassThroughFS::statPath(CRStringView path) const noexcept {
std::error_code ec;
const auto p = m_path / stripSlash(path);
const FileType type = std::filesystem::is_directory(p, ec) ?
FileType::Directory : FileType::NormalFile;
oxTracef("ox::fs::PassThroughFS::stat", "{} {}", ec.message(), path);
oxTracef("ox::fs::PassThroughFS::statInode", "{} {}", ec.message(), path);
const uint64_t size = type == FileType::Directory ? 0 : std::filesystem::file_size(p, ec);
oxTracef("ox::fs::PassThroughFS::stat", "{} {}", ec.message(), path);
oxTracef("ox::fs::PassThroughFS::stat::size", "{} {}", path, size);
oxTracef("ox::fs::PassThroughFS::statInode", "{} {}", ec.message(), path);
oxTracef("ox::fs::PassThroughFS::statInode::size", "{} {}", path, size);
oxReturnError(OxError(ec.value()));
return FileStat{0, 0, size, type};
}
@ -193,12 +141,56 @@ bool PassThroughFS::valid() const noexcept {
return false;
}
Error PassThroughFS::readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept {
try {
std::ifstream file((m_path / stripSlash(path)), std::ios::binary | std::ios::ate);
const std::size_t size = file.tellg();
file.seekg(0, std::ios::beg);
if (size > buffSize) {
oxTracef("ox::fs::PassThroughFS::read::error", "Read failed: Buffer too small: {}", path);
return OxError(1);
}
file.read(static_cast<char*>(buffer), static_cast<std::streamsize>(buffSize));
} catch (const std::fstream::failure &f) {
oxTracef("ox::fs::PassThroughFS::read::error", "Read of {} failed: {}", path, f.what());
return OxError(2);
}
return OxError(0);
}
Error PassThroughFS::readFileInode(uint64_t, void*, std::size_t) noexcept {
// unsupported
return OxError(1, "readFileInode(uint64_t, void*, std::size_t) is not supported by PassThroughFS");
}
Error PassThroughFS::readFileInodeRange(uint64_t, std::size_t, std::size_t, void*, std::size_t*) noexcept {
// unsupported
return OxError(1, "read(uint64_t, std::size_t, std::size_t, void*, std::size_t*) is not supported by PassThroughFS");
}
Error PassThroughFS::writeFilePath(CRStringView path, const void *buffer, uint64_t size, FileType) noexcept {
const auto p = (m_path / stripSlash(path));
try {
std::ofstream f(p, std::ios::binary);
f.write(static_cast<const char*>(buffer), static_cast<std::streamsize>(size));
} catch (const std::fstream::failure &f) {
oxTracef("ox::fs::PassThroughFS::read::error", "Write of {} failed: {}", path, f.what());
return OxError(1);
}
return OxError(0);
}
Error PassThroughFS::writeFileInode(uint64_t, const void*, uint64_t, FileType) noexcept {
// unsupported
return OxError(1, "writeFileInode(uint64_t, void*, uint64_t, uint8_t) is not supported by PassThroughFS");
}
std::string_view PassThroughFS::stripSlash(StringView path) noexcept {
const auto pathLen = ox_strlen(path);
for (auto i = 0u; i < pathLen && path[0] == '/'; i++) {
path = path.substr(1);
}
return std::string_view(path.data(), path.bytes());
return {path.data(), path.bytes()};
}
}

View File

@ -11,7 +11,6 @@
#if __has_include(<filesystem>) && defined(OX_USE_STDLIB)
#include <filesystem>
#include <string>
#include <ox/std/bit.hpp>
#include "filesystem.hpp"
@ -37,36 +36,22 @@ class PassThroughFS: public FileSystem {
[[nodiscard]]
String basePath() const noexcept;
Error mkdir(CRStringView path, bool recursive = false) noexcept override;
Error mkdir(CRStringView path, bool recursive) noexcept override;
Error move(CRStringView src, CRStringView dest) noexcept override;
Error read(CRStringView path, void *buffer, std::size_t buffSize) noexcept override;
Result<const char*> directAccess(CRStringView) noexcept override;
Error read(uint64_t inode, void *buffer, std::size_t size) noexcept override;
Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept override;
Result<const char*> directAccess(uint64_t) noexcept override;
Result<Vector<String>> ls(CRStringView dir) const noexcept override;
template<typename F>
Error ls(CRStringView dir, F cb) const noexcept;
Error remove(CRStringView path, bool recursive = false) noexcept override;
Error remove(CRStringView path, bool recursive) noexcept override;
Error resize(uint64_t size, void *buffer = nullptr) noexcept override;
Error resize(uint64_t size, void *buffer) noexcept override;
Error write(CRStringView path, const void *buffer, uint64_t size, FileType fileType = FileType::NormalFile) noexcept override;
Result<FileStat> statInode(uint64_t inode) const noexcept override;
Error write(uint64_t inode, const void *buffer, uint64_t size, FileType fileType = FileType::NormalFile) noexcept override;
Result<FileStat> stat(uint64_t inode) const noexcept override;
Result<FileStat> stat(CRStringView path) const noexcept override;
Result<FileStat> statPath(CRStringView path) const noexcept override;
[[nodiscard]]
uint64_t spaceNeeded(uint64_t size) const noexcept override;
@ -83,6 +68,17 @@ class PassThroughFS: public FileSystem {
[[nodiscard]]
bool valid() const noexcept override;
protected:
Error readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept override;
Error readFileInode(uint64_t inode, void *buffer, std::size_t size) noexcept override;
Error readFileInodeRange(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept override;
Error writeFilePath(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept override;
Error writeFileInode(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept override;
private:
/**
* Strips the leading slashes from a string.

View File

@ -223,7 +223,6 @@ template<typename SubT>
constexpr Ptr<SubT, size_t, sizeof(T)> Ptr<T, size_t, minOffset>::subPtr(size_t offset) noexcept {
oxTracef("ox::fs::Ptr::subPtr", "{} {} {} {} {}", m_itemOffset, this->size(), offset, m_itemSize, (m_itemSize - offset));
return subPtr<SubT>(offset, m_itemSize - offset);
return subPtr<SubT>(offset, m_itemSize - offset);
}
template<typename T, typename size_t, size_t minOffset>

34
deps/ox/src/ox/logconn/CMakeLists.txt vendored Normal file
View File

@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.10)
add_library(
OxLogConn
logconn.cpp
)
set_property(
TARGET
OxLogConn
PROPERTY
POSITION_INDEPENDENT_CODE ON
)
target_link_libraries(
OxLogConn PUBLIC
OxStd
OxMetalClaw
)
install(
FILES
circularbuff.hpp
logconn.hpp
DESTINATION
include/ox/logconn
)
install(
TARGETS
OxLogConn
LIBRARY DESTINATION lib/ox
ARCHIVE DESTINATION lib/ox
)

99
deps/ox/src/ox/logconn/circularbuff.hpp vendored Normal file
View File

@ -0,0 +1,99 @@
/*
* Copyright 2015 - 2022 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <ox/std/assert.hpp>
#include <ox/std/buffer.hpp>
#include <ox/std/units.hpp>
namespace ox::detail {
class CirculerBuffer {
private:
std::size_t m_readPt = 0;
std::size_t m_writePt = 0;
ox::Buffer m_buff = ox::Buffer(ox::units::MB);
private:
[[nodiscard]]
constexpr auto avail() const noexcept {
if (m_writePt >= m_readPt) {
return m_buff.size() - (m_writePt - m_readPt);
} else {
return (m_buff.size() - m_writePt) - (m_buff.size() - m_readPt);
}
}
public:
constexpr ox::Error put(char v) noexcept {
return write(&v, 1);
if (1 > avail()) {
return OxError(1, "Insufficient space in buffer");
}
m_buff[m_writePt] = v;
++m_writePt;
return {};
}
constexpr ox::Error write(const char *buff, std::size_t sz) noexcept {
if (sz > avail()) {
return OxError(1, "Insufficient space in buffer");
}
// write seg 1
const auto seg1Sz = ox::min(sz, m_buff.size() - m_writePt);
ox_memcpy(&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);
oxAssert(m_buff[0] == buff[seg1Sz], "break");
}
return {};
}
constexpr ox::Error seekp(std::size_t bytesFwd) noexcept {
if (bytesFwd > avail()) {
return OxError(1, "Insufficient space in buffer to seek that far ahead");
}
m_writePt += bytesFwd;
if (m_writePt > m_buff.size()) {
m_writePt -= m_buff.size();
}
return {};
}
constexpr ox::Error seekp(int, ios_base::seekdir) {
return OxError(1, "Unimplemented");
}
[[nodiscard]]
constexpr std::size_t tellp() const noexcept {
return m_buff.size() - avail();
}
[[nodiscard]]
constexpr std::size_t read(char *out, std::size_t outSize) noexcept {
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);
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);
}
return bytesRead;
}
};
}

90
deps/ox/src/ox/logconn/logconn.cpp vendored Normal file
View File

@ -0,0 +1,90 @@
/*
* Copyright 2015 - 2022 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#ifdef OX_USE_STDLIB
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstdio>
#include <ox/std/bit.hpp>
#include "logconn.hpp"
namespace ox {
using namespace trace;
LoggerConn::LoggerConn() noexcept: m_netThread([this]{this->msgSend();}) {
}
LoggerConn::~LoggerConn() noexcept {
m_running = false;
m_waitCond.notify_one();
m_netThread.join();
if (m_socket) {
close(m_socket);
}
}
ox::Error LoggerConn::initConn(const char *appName) noexcept {
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = htons(5590);
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
oxReturnError(OxError(connect(m_socket, reinterpret_cast<sockaddr*>(&addr), sizeof(addr))));
return sendInit({.appName = appName});
}
ox::Error LoggerConn::send(const char *buff, std::size_t len) const noexcept {
std::size_t totalSent = 0;
while (totalSent < len) {
//std::fprintf(stdout, "Sending %lu/%lu bytes on socket %d\n", len, totalSent, m_socket);
const auto sent = ::send(m_socket, buff, len, 0);
if (sent < 0) {
std::fprintf(stderr, "Could not send msg\n");
return OxError(1, "Could not send msg");
}
totalSent += static_cast<std::size_t>(sent);
}
return {};
}
ox::Error LoggerConn::send(const TraceMsg &msg) noexcept {
return send(MsgId::TraceEvent, msg);
}
ox::Error LoggerConn::sendInit(const InitTraceMsg &msg) noexcept {
return send(MsgId::Init, msg);
}
void LoggerConn::msgSend() noexcept {
while (true) {
std::unique_lock lk(m_waitMut);
m_waitCond.wait(lk);
if (!m_running) {
break;
}
std::lock_guard buffLk(m_buffMut);
while (true) {
ox::Array<char, ox::units::KB> tmp;
const auto read = m_buff.read(tmp.data(), tmp.size());
if (!read) {
break;
}
//std::printf("LoggerConn: sending %lu bytes\n", read);
oxIgnoreError(send(tmp.data(), read));
}
}
}
}
#endif

78
deps/ox/src/ox/logconn/logconn.hpp vendored Normal file
View File

@ -0,0 +1,78 @@
/*
* Copyright 2015 - 2022 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#ifdef OX_USE_STDLIB
#include <condition_variable>
#include <mutex>
#include <thread>
#endif
#include <ox/mc/write.hpp>
#include <ox/std/trace.hpp>
#include "circularbuff.hpp"
namespace ox {
#ifdef OX_USE_STDLIB
class LoggerConn: public trace::Logger {
private:
int m_socket = 0;
detail::CirculerBuffer m_buff;
std::thread m_netThread;
std::condition_variable m_waitCond;
std::mutex m_waitMut;
std::mutex m_buffMut;
bool m_running = true;
public:
LoggerConn() noexcept;
LoggerConn(const LoggerConn&) noexcept = delete;
~LoggerConn() noexcept override;
LoggerConn &operator=(const LoggerConn&) noexcept = delete;
ox::Error send(const trace::TraceMsg&) noexcept final;
ox::Error sendInit(const trace::InitTraceMsg&) noexcept final;
ox::Error initConn(const char *appName = "") noexcept;
ox::Error send(const char *buff, std::size_t len) const noexcept;
private:
void msgSend() noexcept;
ox::Error send(trace::MsgId msgId, const auto &msg) noexcept {
ox::Array<char, 10 * ox::units::KB> buff;
std::size_t sz = 0;
oxReturnError(ox::writeMC(&buff[0], buff.size(), &msg, &sz));
//std::printf("sz: %lu\n", sz);
oxRequire(szBuff, serialize(static_cast<uint32_t>(sz)));
std::unique_lock buffLk(m_buffMut);
oxReturnError(m_buff.put(static_cast<char>(msgId)));
oxReturnError(m_buff.write(szBuff.data(), szBuff.size()));
oxReturnError(m_buff.write(buff.data(), sz));
buffLk.unlock();
m_waitCond.notify_one();
return {};
}
};
#else
class LoggerConn: public trace::Logger {
private:
public:
constexpr LoggerConn() noexcept = default;
LoggerConn(const LoggerConn&) noexcept = delete;
constexpr ~LoggerConn() noexcept override = default;
LoggerConn &operator=(const LoggerConn&) noexcept = delete;
ox::Error send(const trace::TraceMsg&) noexcept final { return {}; }
static ox::Error initConn() noexcept { return {}; }
static ox::Error send(const char*, std::size_t) noexcept { return {}; }
};
#endif
}

View File

@ -41,7 +41,7 @@ class MetalClawWriter {
public:
constexpr MetalClawWriter(uint8_t *buff, std::size_t buffLen, int unionIdx = -1) noexcept;
constexpr ~MetalClawWriter() noexcept;
constexpr ~MetalClawWriter() noexcept = default;
constexpr Error field(const char*, CommonPtrWith<int8_t> auto *val) noexcept;
constexpr Error field(const char*, CommonPtrWith<int16_t> auto *val) noexcept;
@ -159,12 +159,6 @@ constexpr MetalClawWriter::MetalClawWriter(uint8_t *buff, std::size_t buffLen, i
m_buff(buff) {
}
constexpr MetalClawWriter::~MetalClawWriter() noexcept {
if (m_field != m_fields) {
oxTrace("ox::mc::MetalClawWriter::error") << "MetalClawReader: incorrect fields number given";
}
}
constexpr Error MetalClawWriter::field(const char*, CommonPtrWith<int8_t> auto *val) noexcept {
return appendInteger(*val);
}

View File

@ -46,4 +46,4 @@ install(TARGETS OxModel
if(OX_RUN_TESTS)
add_subdirectory(test)
endif()
endif()

View File

@ -10,7 +10,7 @@
#include <ox/std/concepts.hpp>
#define oxModelBegin(modelName) constexpr ox::Error model(auto *io, ox::CommonPtrWith<modelName> auto *o) noexcept { io->template setTypeInfo<modelName>();
#define oxModelBegin(modelName) constexpr ox::Error model(auto *io, [[maybe_unused]] ox::CommonPtrWith<modelName> auto *o) noexcept { io->template setTypeInfo<modelName>();
#define oxModelEnd() return OxError(0); }
#define oxModelField(fieldName) oxReturnError(io->field(#fieldName, &o->fieldName));
#define oxModelFieldRename(serFieldName, objFieldName) oxReturnError(io->field(#serFieldName, &o->objFieldName));

View File

@ -47,6 +47,7 @@ class FieldCounter {
template<typename ...Args>
constexpr Error fieldCString(Args&&...) noexcept {
++fields;
return OxError(0);
}

View File

@ -12,6 +12,7 @@
#include <ox/std/serialize.hpp>
#include <ox/std/string.hpp>
#include <ox/std/trace.hpp>
#include <ox/std/typetraits.hpp>
#include <ox/std/types.hpp>
#include <ox/std/utility.hpp>
#include <ox/std/vector.hpp>

View File

@ -133,7 +133,7 @@ class Array {
};
private:
T m_items[ArraySize];
T m_items[ArraySize]{};
public:
constexpr Array() noexcept = default;

View File

@ -62,7 +62,7 @@ StringView toStringView(const std::string &s) noexcept {
#if __has_include(<QString>)
template<bool force = false>
inline StringView toStringView(const QString &s) noexcept {
return s.toUtf8();
return s.toUtf8().data();
}
#endif

View File

@ -304,8 +304,8 @@ constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operat
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(char c) noexcept {
char str[] = {c, 0};
set(str);
ox::Array<char, 2> str{c, 0};
set(str.data());
return *this;
}
@ -317,23 +317,25 @@ constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operat
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(int64_t i) noexcept {
char str[65] = {};
ox_itoa(i, str);
set(str);
ox::Array<char, 65> str{};
ox_itoa(i, str.data());
set(str.data());
return *this;
}
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(uint64_t i) noexcept {
char str[65] = {};
ox_itoa(i, str);
set(str);
ox::Array<char, 65> str{};
ox_itoa(i, str.data());
set(str.data());
return *this;
}
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(const BasicString &src) noexcept {
set(src);
if (this != &src) {
set(src);
}
return *this;
}

View File

@ -43,6 +43,7 @@ class StringView {
m_max = max;
}
[[nodiscard]]
constexpr auto offset() const noexcept {
return m_offset;
}
@ -220,6 +221,7 @@ class StringView {
return m_str[m_len - 1];
}
[[nodiscard]]
constexpr auto substr(std::size_t pos) const noexcept {
return StringView(m_str + pos, m_len - pos);
}
@ -228,11 +230,7 @@ class StringView {
return m_str[i];
}
constexpr auto operator=(const StringView &other) noexcept {
m_str = other.m_str;
m_len = other.m_len;
return *this;
}
constexpr ox::StringView &operator=(const StringView &other) noexcept = default;
constexpr auto operator==(const StringView &other) const noexcept {
if (other.len() != len()) {

View File

@ -14,4 +14,30 @@ void init() {
oxTraceInitHook();
}
void init(Logger *logger) {
oxTraceInitHook();
setLogger(logger);
}
class NullLogger: public Logger {
public:
ox::Error send(const TraceMsg&) noexcept final {
return {};
}
ox::Error sendInit(const InitTraceMsg&) noexcept final {
return {};
}
};
static NullLogger defaultLogger;
static Logger *logger = &defaultLogger;
void setLogger(Logger *logger) noexcept {
trace::logger = logger;
}
void send(const TraceMsg &msg) noexcept {
oxIgnoreError(logger->send(msg));
}
}

View File

@ -30,9 +30,35 @@ void oxTraceHook(const char *file, int line, const char *ch, const char *msg);
namespace ox::trace {
enum class MsgId: char {
Init = 2,
TraceEvent = 1,
Json = '{',
};
struct TraceMsgRcv {
static constexpr auto TypeName = "net.drinkingtea.ox.trace.TraceMsg";
static constexpr auto TypeVersion = 1;
BasicString<50> file = "";
int line = 0;
uint64_t time = 0;
BasicString<50> ch = "";
BasicString<100> msg;
};
template<typename T>
constexpr Error model(T *io, ox::CommonPtrWith<TraceMsgRcv> auto *obj) noexcept {
io->template setTypeInfo<TraceMsgRcv>();
oxReturnError(io->field("file", &obj->file));
oxReturnError(io->field("line", &obj->line));
oxReturnError(io->field("time", &obj->time));
oxReturnError(io->field("ch", &obj->ch));
oxReturnError(io->field("msg", &obj->msg));
return {};
}
struct TraceMsg {
static constexpr auto TypeName = "net.drinkingtea.ox.trace.TraceMsg";
static constexpr auto Fields = 5;
static constexpr auto TypeVersion = 1;
const char *file = "";
int line = 0;
@ -42,16 +68,58 @@ struct TraceMsg {
};
template<typename T>
constexpr Error model(T *io, TraceMsg *obj) {
constexpr Error model(T *io, ox::CommonPtrWith<TraceMsg> auto *obj) noexcept {
io->template setTypeInfo<TraceMsg>();
oxReturnError(io->field("ch", &obj->ch));
oxReturnError(io->field("file", &obj->file));
oxReturnError(io->fieldCString("file", &obj->file));
oxReturnError(io->field("line", &obj->line));
oxReturnError(io->field("time", &obj->time));
oxReturnError(io->fieldCString("ch", &obj->ch));
oxReturnError(io->field("msg", &obj->msg));
return OxError(0);
return {};
}
struct InitTraceMsgRcv {
static constexpr auto TypeName = "net.drinkingtea.ox.trace.InitTraceMsg";
static constexpr auto TypeVersion = 1;
ox::String appName;
};
template<typename T>
constexpr Error model(T *io, ox::CommonPtrWith<InitTraceMsgRcv> auto *obj) noexcept {
io->template setTypeInfo<InitTraceMsgRcv>();
oxReturnError(io->field("appName", &obj->appName));
return {};
}
struct InitTraceMsg {
static constexpr auto TypeName = "net.drinkingtea.ox.trace.InitTraceMsg";
static constexpr auto TypeVersion = 1;
const char *appName = "";
};
template<typename T>
constexpr Error model(T *io, ox::CommonPtrWith<InitTraceMsg> auto *obj) noexcept {
io->template setTypeInfo<InitTraceMsg>();
oxReturnError(io->fieldCString("appName", &obj->appName));
return {};
}
class Logger {
public:
constexpr virtual ~Logger() noexcept = default;
virtual ox::Error send(const TraceMsg&) noexcept = 0;
virtual ox::Error sendInit(const InitTraceMsg&) noexcept = 0;
};
/**
* @param logger pointer to the new logger, does NOT take ownership
*/
void setLogger(Logger *logger) noexcept;
void send(const TraceMsg &msg) noexcept;
class OutStream {
protected:
@ -109,6 +177,7 @@ class OutStream {
inline ~OutStream() noexcept {
oxTraceHook(m_msg.file, m_msg.line, m_msg.ch, m_msg.msg.c_str());
send(m_msg);
}
constexpr OutStream &operator<<(Integer_c auto v) noexcept;
@ -238,5 +307,6 @@ inline void logError(const char *file, int line, const Error &err) noexcept {
}
void init();
void init(Logger *logger);
}

View File

@ -72,13 +72,17 @@ void oxTraceHook([[maybe_unused]] const char *file, [[maybe_unused]] int line,
std::cout << " " << file << ':' << line << "\n";
} else if (ox_strcmp(ch, "debug") == 0 || ox_strcmp(ch, "info") == 0) {
printf("%s\n", msg);
fflush(stdout);
} else if (ox_strcmp(ch, "stdout") == 0) {
printf("%s", msg);
fflush(stdout);
} else if (ox_strcmp(ch, "stderr") == 0) {
printf("%s", msg);
fflush(stdout);
} else if (ox_strcmp(ch, "error") == 0) {
//std::cerr << "\033[31;1;1mERROR:\033[0m (" << file << ':' << line << "): " << msg << '\n';
fprintf(stderr, "\033[31;1;1mERROR:\033[0m (%s:%d): %s\n", file, line, msg);
fflush(stderr);
}
#else
if (ox_strcmp(ch, "info") == 0) {

View File

@ -15,6 +15,8 @@ using size_t = decltype(alignof(int));
}
using size_t = decltype(alignof(int));
#if __has_include(<cstdint>)
#include <cstdint>
@ -160,4 +162,4 @@ static_assert(sizeof(uint8_t) == 1, "uint8_t is wrong size");
static_assert(sizeof(uint16_t) == 2, "uint16_t is wrong size");
static_assert(sizeof(uint32_t) == 4, "uint32_t is wrong size");
static_assert(sizeof(uint64_t) == 8, "uint64_t is wrong size");
static_assert(sizeof(uintptr_t) == sizeof(void*), "uintptr_t is wrong size");
static_assert(sizeof(uintptr_t) == sizeof(void*), "uintptr_t is wrong size");

View File

@ -342,9 +342,12 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
[[nodiscard]]
constexpr bool contains(const T&) const;
constexpr void insert(std::size_t pos, std::size_t cnt, const T &val);
constexpr iterator<> insert(std::size_t pos, std::size_t cnt, const T &val);
constexpr void insert(std::size_t pos, const T &val);
constexpr iterator<> insert(std::size_t pos, const T &val);
template<typename... Args>
constexpr iterator<> emplace(std::size_t pos, Args&&... args);
template<typename... Args>
constexpr T &emplace_back(Args&&... args);
@ -571,7 +574,8 @@ constexpr bool Vector<T, SmallVectorSize, Allocator>::contains(const T &v) const
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr void Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, std::size_t cnt, const T &val) {
constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<>
Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, std::size_t cnt, const T &val) {
// TODO: insert should ideally have its own reserve
if (m_size + cnt > m_cap) {
reserve(m_cap ? m_size + cnt : initialSize);
@ -587,10 +591,12 @@ constexpr void Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, st
}
}
++m_size;
return begin() + pos;
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr void Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, const T &val) {
constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<>
Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, const T &val) {
// TODO: insert should ideally have its own reserve
if (m_size == m_cap) {
reserve(m_cap ? m_cap * 2 : initialSize);
@ -604,6 +610,26 @@ constexpr void Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, co
std::construct_at(&m_items[pos], val);
}
++m_size;
return begin() + pos;
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
template<typename... Args>
constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<>
Vector<T, SmallVectorSize, Allocator>::emplace(std::size_t pos, Args&&... args) {
// TODO: insert should ideally have its own reserve
if (m_size == m_cap) {
reserve(m_cap ? m_cap * 2 : initialSize);
}
if (pos < m_size) {
for (auto i = m_size; i > pos; --i) {
std::construct_at(&m_items[i], std::move(m_items[i - 1]));
}
m_items[pos].~T();
}
std::construct_at(&m_items[pos], ox::forward<Args>(args)...);
++m_size;
return begin() + pos;
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>

View File

@ -151,7 +151,7 @@ ox::Error loadBgTileSheet(Context *ctx,
const ox::FileAddress &tilesheetAddr,
const ox::FileAddress &paletteAddr) noexcept {
oxRequire(tsStat, ctx->rom->stat(tilesheetAddr));
oxRequire(ts, ctx->rom->directAccess(tilesheetAddr));
oxRequire(ts, static_cast<ox::MemFS*>(ctx->rom.get())->directAccess(tilesheetAddr));
GbaTileMapTarget target;
target.pal.palette = MEM_BG_PALETTE;
target.cbbData = &g_cbbData[cbb];
@ -160,7 +160,7 @@ ox::Error loadBgTileSheet(Context *ctx,
// load external palette if available
if (paletteAddr) {
oxRequire(palStat, ctx->rom->stat(paletteAddr));
oxRequire(pal, ctx->rom->directAccess(paletteAddr));
oxRequire(pal, static_cast<ox::MemFS*>(ctx->rom.get())->directAccess(paletteAddr));
oxReturnError(ox::readMC(pal, palStat.size, &target.pal));
}
// update bpp of all bgs with the updated cbb
@ -177,7 +177,7 @@ ox::Error loadSpriteTileSheet(Context *ctx,
const ox::FileAddress &tilesheetAddr,
const ox::FileAddress &paletteAddr) noexcept {
oxRequire(tsStat, ctx->rom->stat(tilesheetAddr));
oxRequire(ts, ctx->rom->directAccess(tilesheetAddr));
oxRequire(ts, static_cast<ox::MemFS*>(ctx->rom.get())->directAccess(tilesheetAddr));
GbaTileMapTarget target;
target.pal.palette = MEM_SPRITE_PALETTE;
target.tileMap = MEM_SPRITE_TILES;
@ -185,7 +185,7 @@ ox::Error loadSpriteTileSheet(Context *ctx,
// load external palette if available
if (paletteAddr) {
oxRequire(palStat, ctx->rom->stat(paletteAddr));
oxRequire(pal, ctx->rom->directAccess(paletteAddr));
oxRequire(pal, static_cast<ox::MemFS*>(ctx->rom.get())->directAccess(paletteAddr));
oxReturnError(ox::readMC(pal, palStat.size, &target.pal));
}
return {};
@ -195,7 +195,7 @@ ox::Error loadBgPalette(Context *ctx, unsigned, const ox::FileAddress &paletteAd
GbaPaletteTarget target;
target.palette = MEM_BG_PALETTE;
oxRequire(palStat, ctx->rom->stat(paletteAddr));
oxRequire(pal, ctx->rom->directAccess(paletteAddr));
oxRequire(pal, static_cast<ox::MemFS*>(ctx->rom.get())->directAccess(paletteAddr));
oxReturnError(ox::readMC(pal, palStat.size, &target));
return {};
}
@ -204,7 +204,7 @@ ox::Error loadSpritePalette(Context *ctx, unsigned cbb, const ox::FileAddress &p
GbaPaletteTarget target;
target.palette = &MEM_SPRITE_PALETTE[cbb];
oxRequire(palStat, ctx->rom->stat(paletteAddr));
oxRequire(pal, ctx->rom->directAccess(paletteAddr));
oxRequire(pal, static_cast<ox::MemFS*>(ctx->rom.get())->directAccess(paletteAddr));
oxReturnError(ox::readMC(pal, palStat.size, &target));
return {};
}

View File

@ -33,7 +33,7 @@ void unloadRom(char*) noexcept {
ox::Result<std::size_t> getPreloadAddr(Context *ctx, ox::CRStringView path) noexcept {
oxRequire(stat, ctx->rom->stat(path));
oxRequire(buff, ctx->rom->directAccess(path));
oxRequire(buff, static_cast<ox::MemFS*>(ctx->rom.get())->directAccess(path));
PreloadPtr p;
oxReturnError(ox::readMC(buff, stat.size, &p));
return p.preloadAddr + ctx->preloadSectionOffset;
@ -41,7 +41,7 @@ ox::Result<std::size_t> getPreloadAddr(Context *ctx, ox::CRStringView path) noex
ox::Result<std::size_t> getPreloadAddr(Context *ctx, const ox::FileAddress &file) noexcept {
oxRequire(stat, ctx->rom->stat(file));
oxRequire(buff, ctx->rom->directAccess(file));
oxRequire(buff, static_cast<ox::MemFS*>(ctx->rom.get())->directAccess(file));
PreloadPtr p;
oxReturnError(ox::readMC(buff, stat.size, &p));
return p.preloadAddr + ctx->preloadSectionOffset;