MCPcopy Create free account
hub / github.com/Simple-XX/SimpleKernel / ExpandFile

Method ExpandFile

src/filesystem/ramfs/ramfs.cpp:580–607  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

578}
579
580auto RamFs::ExpandFile(RamInode* inode, size_t new_size) -> Expected<void> {
581 if (inode == nullptr) {
582 return std::unexpected(Error(ErrorCode::kInvalidArgument));
583 }
584
585 if (new_size <= inode->capacity) {
586 return {};
587 }
588
589 // 计算新容量(按 256 字节对齐)
590 size_t new_capacity = ((new_size + 255) / 256) * 256;
591
592 uint8_t* new_data = static_cast<uint8_t*>(AllocateFileData(new_capacity));
593 if (new_data == nullptr) {
594 return std::unexpected(Error(ErrorCode::kOutOfMemory));
595 }
596
597 // 复制旧数据
598 if (inode->data != nullptr) {
599 memcpy(new_data, inode->data, inode->inode.size);
600 // Old allocation stays in pool (bump allocator — no per-block free)
601 }
602
603 inode->data = new_data;
604 inode->capacity = new_capacity;
605
606 return {};
607}
608
609auto RamFs::AllocateFileData(size_t size) -> void* {
610 // Align up to 16 bytes

Callers 1

WriteMethod · 0.80

Calls 2

ErrorClass · 0.85
memcpyFunction · 0.85

Tested by

no test coverage detected