The Pedigree Project 0.1
RamFile-allocation.cc
1/* Copyright (c) 2026, Pedigree Developers. */
2#include "pedigree/kernel/LockGuard.h"
3#include "pedigree/kernel/syscallError.h"
4
5#include "RamFs.h"
6
7bool RamFile::allocateFileRange(size_t offset, size_t length) {
8 if (!canWrite()) {
9 SYSCALL_ERROR(PermissionDenied);
10 return false;
11 }
12 LockGuard<Mutex> guard(m_FileBlocksLock);
13 const size_t blockSize = getBlockSize();
14 const size_t last = (offset + length - 1) / blockSize;
15 for (size_t index = offset / blockSize; index <= last; ++index) {
16 const size_t location = index * blockSize;
17 uintptr_t page = m_FileBlocks.lookup(location);
18 if (page) {
19 m_FileBlocks.release(location);
20 continue;
21 }
22 if (!m_BlockOffsets.tryReserve(m_BlockOffsets.count() + 1)) {
23 SYSCALL_ERROR(OutOfMemory);
24 return false;
25 }
26 page = m_FileBlocks.insert(location);
27 if (!page) {
28 SYSCALL_ERROR(OutOfMemory);
29 return false;
30 }
31 ByteSet(reinterpret_cast<void*>(page), 0, blockSize);
32 m_BlockOffsets.pushBack(location);
33 m_FileBlocks.markNoLongerEditing(location);
34 // Its base reference owns actual memory until truncation or file destruction.
35 }
36 return true;
37}
An in-RAM filesystem.
void release(uintptr_t key)
Definition Cache.cc:1488
uintptr_t insert(uintptr_t key, bool *alreadyExisted=nullptr)
Definition Cache.cc:857
void markNoLongerEditing(uintptr_t key, size_t length=0)
Definition Cache.cc:2484
uintptr_t lookup(uintptr_t key)
Definition Cache.cc:709
virtual size_t getBlockSize() const
Definition File.cc:978
virtual bool allocateFileRange(size_t offset, size_t length) override
void pushBack(const T &value)
Definition Vector.h:275
size_t count() const
Definition Vector.h:270