Skip to content

feat(vfs): implement VFS context, disk, mount support, and tests#550

Open
jnyfah wants to merge 2 commits into
JeanPhilippeKernel:developfrom
jnyfah:user/jnyfah/vfs-context
Open

feat(vfs): implement VFS context, disk, mount support, and tests#550
jnyfah wants to merge 2 commits into
JeanPhilippeKernel:developfrom
jnyfah:user/jnyfah/vfs-context

Conversation

@jnyfah

@jnyfah jnyfah commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

@jnyfah jnyfah requested a review from JeanPhilippeKernel July 5, 2026 13:56
@jnyfah jnyfah marked this pull request as ready for review July 5, 2026 13:56
@JeanPhilippeKernel JeanPhilippeKernel added area-linux Work on Linux system area-window Work on Window system area-macOS Work on macOS system Feature request New feature labels Jul 6, 2026
@JeanPhilippeKernel JeanPhilippeKernel added this to the Born baby (0.3.0) milestone Jul 6, 2026
@JeanPhilippeKernel JeanPhilippeKernel linked an issue Jul 6, 2026 that may be closed by this pull request
Comment thread ZEngine/ZEngine/Core/VFS/IVFSBackend.h Outdated
Comment thread ZEngine/ZEngine/Core/Containers/Array.h
Comment thread ZEngine/ZEngine/Core/Containers/Array.h
@JeanPhilippeKernel JeanPhilippeKernel self-requested a review July 9, 2026 10:19
return VFSResult<IVFSFile*>::Fail(VFSError::PermissionDenied);
}

VFSDiskFile* file = new VFSDiskFile();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this present itself as good candidate of PoolAllocator
eg of usage:

// IVFSContext.h
struct IVFSContext
{
protected:
    explicit IVFSContext(size_t file_chunk_size, size_t max_open_files, size_t file_alignment)
    {
        const size_t pool_size = max_open_files * file_chunk_size;
        m_arena.Initialize(pool_size, platform_page_size());
        m_file_pool.Initialize(&m_arena, pool_size, file_chunk_size, file_alignment);
    }

    Core::Memory::PoolAllocator  m_file_pool;  // must be before m_arena
    Core::Memory::ArenaAllocator m_arena;

public:
    virtual ~IVFSContext() = default;

    virtual VFSResult<IVFSFile*>                            Open(const VFSPath& path, VFSOpenFlags flags) = 0;
    virtual void                                            Close(IVFSFile* file)                         = 0;
    // ... rest unchanged
};

struct VFSDiskContext final : public IVFSContext
{
    explicit VFSDiskContext(cstring native_root)
        : IVFSContext(sizeof(VFSDiskFile), 20000, alignof(VFSDiskFile))
    { /* root setup only */ }
};

struct ZipFileContext final : public IVFSContext
{
    explicit ZipFileContext(cstring zip_path)
        : IVFSContext(sizeof(ZipFile), 4000, alignof(ZipFile))
    { /* zip setup only */ }
};

struct MemoryFileContext final : public IVFSContext
{
    explicit MemoryFileContext(/* ... */)
        : IVFSContext(sizeof(MemoryFile), 1000, alignof(MemoryFile))
    { }
};

Each context passes its own sizeof/alignof/capacity — the pool wiring lives in exactly one place. The Close() implementation could even be provided once in the base since the pattern is always the same (~IVFSFile() then m_file_pool.Free()):

// base provides a default Close() — concrete types only override if they need extra teardown
void IVFSContext::Close(IVFSFile* file)
{
    if (!file) return;
    file->~IVFSFile();
    m_file_pool.Free(file);
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here since VFSDiskFile has a non-trivial destructor (fclose). The pool only manages raw memory, so you'd need the placement-new + explicit destructor pattern:

VFSResult<IVFSFile*> VFSDiskContext::Open(const VFSPath& path, VFSOpenFlags flags)
{
    // ... existing fopen logic ...

    void* mem = m_file_pool.Allocate(__FILE__, __LINE__);
    if (!mem)
    {
        std::fclose(handle);
        return VFSResult<IVFSFile*>::Fail(VFSError::OutOfMemory);
    }
    return VFSResult<IVFSFile*>::Ok(new (mem) VFSDiskFile(handle, path));
}

void VFSDiskContext::Close(IVFSFile* file)
{
    if (!file) return;
    file->~IVFSFile();           // runs fclose via ~VFSDiskFile
    m_file_pool.Free(file);      // returns chunk to free list — O(1)
}

This purely idea. - you might want to refine on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-linux Work on Linux system area-macOS Work on macOS system area-window Work on Window system Feature request New feature

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Virtual File System

2 participants