Files
microReticulum/src/FileSystem.h
T
Chad Attermann 4d6f0b9de2 Added dual-class PSRAM/TLSF allocator system and refactored memory management
Introduces a flexible two-class allocator architecture (RNS_DEFAULT_ALLOCATOR and RNS_CONTAINER_ALLOCATOR) supporting heap, PSRAM, and TLSF pool variants for fine-grained control over embedded memory layout.

- New Memory.h/cpp with ContainerAllocator<T>, heap/PSRAM/pool allocator types, and heap stats reporting
- IdentityTable and PathTable now use ContainerAllocator
- Replaced RNS_USE_TLSF/RNS_USE_ALLOCATOR/RNS_USE_PSRAM flags with per-class allocator flags (RNS_DEFAULT_ALLOCATOR, RNS_CONTAINER_ALLOCATOR)
- Added ESP32 PSRAM allocator support (RNS_PSRAM_ALLOCATOR, RNS_PSRAM_POOL_ALLOCATOR)
- Added Transport::get_path()/remove_path() helpers
- Added loop callback (set_loop_callback) to enable more responsive app during long operations (like clean_cache)
- Aded guard against re-entrancy to clean_cache()
- Added callback option to list_directory() to avoid heap allocation
- Added OOM auto-restart (ESP.restart/NVIC_SystemReset) in Interface send/receive and Reticulum loop.
- Renamed setLogCallback to set_log_callback
- Made Link/Identity static members private
- Added native14 env to CI and platformio.ini
- Bumped version to 0.2.10
2026-03-06 16:43:04 -07:00

139 lines
5.4 KiB
C++

#pragma once
#include "FileStream.h"
#include "Log.h"
#include "Bytes.h"
#include "Type.h"
#include <list>
#include <memory>
#include <cassert>
#include <stdint.h>
namespace RNS {
class FileSystemImpl {
public:
struct Callbacks {
//using DirectoryListing = void(*)(const char* file_name);
using DirectoryListing = std::function<void(const char*)>;
};
protected:
FileSystemImpl() { MEMF("FileSystem::FileSystemImpl object created, this: 0x%X", this); }
public:
virtual ~FileSystemImpl() { MEMF("FileSystem::FileSystemImpl object destroyed, this: 0x%X", this); }
protected:
virtual bool init() { return true; }
virtual void loop() {}
virtual bool file_exists(const char* file_path) = 0;
virtual size_t read_file(const char* file_path, Bytes& data) = 0;
virtual size_t write_file(const char* file_path, const Bytes& data) = 0;
virtual FileStream open_file(const char* file_path, FileStream::MODE file_mode) = 0;
virtual bool remove_file(const char* file_path) = 0;
virtual bool rename_file(const char* from_file_path, const char* to_file_path) = 0;
virtual bool directory_exists(const char* directory_path) = 0;
virtual bool create_directory(const char* directory_path) = 0;
virtual bool remove_directory(const char* directory_path) = 0;
virtual std::list<std::string> list_directory(const char* directory_path, Callbacks::DirectoryListing callback = nullptr) = 0;
virtual size_t storage_size() = 0;
virtual size_t storage_available() = 0;
friend class FileSystem;
};
class FileSystem {
public:
using Callbacks = FileSystemImpl::Callbacks;
public:
FileSystem(Type::NoneConstructor none) {
MEMF("FileSystem NONE object created, this: 0x%X, impl: 0x%X", this, _impl.get());
}
FileSystem(const FileSystem& obj) : _impl(obj._impl) {
MEMF("FileSystem object copy created, this: 0x%X, impl: 0x%X", this, _impl.get());
}
FileSystem(FileSystemImpl* impl) : _impl(impl) {
MEMF("FileSystem object impl created, this: 0x%X, impl: 0x%X", this, _impl.get());
}
virtual ~FileSystem() {
MEMF("FileSystem object destroyed, this: 0x%X, impl: 0x%X", this, _impl.get());
}
inline FileSystem& operator = (const FileSystem& obj) {
_impl = obj._impl;
MEMF("FileSystem object copy created by assignment, this: 0x%X, impl: 0x%X", this, _impl.get());
return *this;
}
inline FileSystem& operator = (FileSystemImpl* impl) {
_impl.reset(impl);
MEMF("FileSystem object copy created by impl assignment, this: 0x%X, impl: 0x%X", this, _impl.get());
return *this;
}
inline operator bool() const {
MEMF("FileSystem object bool, this: 0x%X, impl: 0x%X", this, _impl.get());
return _impl.get() != nullptr;
}
inline bool operator < (const FileSystem& obj) const {
MEMF("FileSystem object <, this: 0x%X, impl: 0x%X", this, _impl.get());
return _impl.get() < obj._impl.get();
}
inline bool operator > (const FileSystem& obj) const {
MEMF("FileSystem object <, this: 0x%X, impl: 0x%X", this, _impl.get());
return _impl.get() > obj._impl.get();
}
inline bool operator == (const FileSystem& obj) const {
MEMF("FileSystem object ==, this: 0x%X, impl: 0x%X", this, _impl.get());
return _impl.get() == obj._impl.get();
}
inline bool operator != (const FileSystem& obj) const {
MEMF("FileSystem object !=, this: 0x%X, impl: 0x%X", this, _impl.get());
return _impl.get() != obj._impl.get();
}
inline FileSystemImpl* get() {
return _impl.get();
}
inline void clear() {
_impl.reset();
}
public:
inline bool init() { assert(_impl); return _impl->init(); }
inline void loop() { assert(_impl); return _impl->loop(); }
inline bool file_exists(const char* file_path) { assert(_impl); return _impl->file_exists(file_path); }
inline size_t read_file(const char* file_path, Bytes& data) { assert(_impl); return _impl->read_file(file_path, data); }
inline size_t write_file(const char* file_path, const Bytes& data) { assert(_impl); return _impl->write_file(file_path, data); }
inline FileStream open_file(const char* file_path, FileStream::MODE file_mode) { return _impl->open_file(file_path, file_mode); }
inline bool remove_file(const char* file_path) { assert(_impl); return _impl->remove_file(file_path); }
inline bool rename_file(const char* from_file_path, const char* to_file_path) { assert(_impl); return _impl->rename_file(from_file_path, to_file_path); }
inline bool directory_exists(const char* directory_path) { assert(_impl); return _impl->directory_exists(directory_path); }
inline bool create_directory(const char* directory_path) { assert(_impl); return _impl->create_directory(directory_path); }
inline bool remove_directory(const char* directory_path) { assert(_impl); return _impl->remove_directory(directory_path); }
inline std::list<std::string> list_directory(const char* directory_path, Callbacks::DirectoryListing callback = nullptr) { assert(_impl); return _impl->list_directory(directory_path, callback); }
inline size_t storage_size() { assert(_impl); return _impl->storage_size(); }
inline size_t storage_available() { assert(_impl); return _impl->storage_available(); }
private:
std::list<std::string> _empty;
// getters/setters
protected:
public:
#ifndef NDEBUG
inline std::string debugString() const {
std::string dump;
dump = "FileSystem object, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_impl.get());
return dump;
}
#endif
protected:
std::shared_ptr<FileSystemImpl> _impl;
};
}