Skip to content

Commit

Permalink
Take program assets into account when computing flash allocation size…
Browse files Browse the repository at this point in the history
…s. (#2379)
  • Loading branch information
kasperl authored Jun 3, 2024
1 parent 6d6bdd6 commit 41f3bc2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/flash_allocation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ FlashAllocation::Header::Header(const void* memory,
checksum_ = compute_checksum(memory);
}

word FlashAllocation::size() const {
word size = size_no_assets();
if (is_program()) size += program_assets_size(null, null);
return size;
}

bool FlashAllocation::Header::is_valid(bool embedded) const {
if (marker_ != FORMAT_MARKER || size_in_pages_ == 0) return false;
if (embedded) {
Expand Down Expand Up @@ -100,7 +106,7 @@ bool FlashAllocation::commit(const void* memory, word size, const Header* header
int FlashAllocation::program_assets_size(uint8** bytes, word* length) const {
if (!program_has_assets()) return 0;
uword allocation_address = reinterpret_cast<uword>(this);
uword assets_address = allocation_address + size();
uword assets_address = allocation_address + size_no_assets();
int assets_length = *reinterpret_cast<uint32*>(assets_address);
if (bytes) *bytes = reinterpret_cast<uint8*>(assets_address + sizeof(uint32));
if (length) *length = assets_length;
Expand Down
5 changes: 4 additions & 1 deletion src/flash_allocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ class FlashAllocation {
bool is_region() const { return type() == FLASH_ALLOCATION_TYPE_REGION; }

// Simple accessors.
word size() const { return header_.size(); }
word size_no_assets() const { return header_.size(); }
uint8 type() const { return header_.type(); }
const uint8* id() const { return header_.id(); }
const uint8* metadata() const { return header_.metadata(); }

// Get the full size of the allocation. For programs, this includes the assets.
word size() const;

// Check if the allocation is valid.
bool is_valid() const { return header_.is_valid(false); }
bool is_valid_embedded() const { return header_.is_valid(true); }
Expand Down
4 changes: 1 addition & 3 deletions src/primitive_flash_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ PRIMITIVE(get_size) {
ARGS(word, offset);
const FlashAllocation* allocation = FlashRegistry::allocation(offset);
if (allocation == null) FAIL(INVALID_ARGUMENT);
word size = allocation->size();
if (allocation->is_program()) size += allocation->program_assets_size(null, null);
return Smi::from(size);
return Smi::from(allocation->size());
}

PRIMITIVE(get_header_page) {
Expand Down
2 changes: 1 addition & 1 deletion src/process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Process::Process(Program* program, ProcessRunner* runner, ProcessGroup* group, S
, runner_(runner)
, group_(group)
, program_heap_address_(reinterpret_cast<uword>(program))
, program_heap_size_(program ? program->size() : 0)
, program_heap_size_(program ? program->size_no_assets() : 0)
, entry_(Method::invalid())
, spawn_method_(Method::invalid())
, object_heap_(
Expand Down

0 comments on commit 41f3bc2

Please sign in to comment.