Skip to content

Commit

Permalink
🐎 RLE for memory in snapshots (#264)
Browse files Browse the repository at this point in the history
* Use run length encoding when taking a memory snapshot

* Fix issue with run length encoding when there are 0 memory pages

* Restore memory using run length encoding

* Remove old commented out code

* Clang-format

* Remove unnecessary print

* Add check to validate if run length encoding restored memory correctly

* Fix conflicting variable name `end`

---------

Co-authored-by: Tom Lauwaerts <[email protected]>
  • Loading branch information
MaartenS11 and tolauwae authored Dec 14, 2024
1 parent 14ac89b commit c73a20b
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/Debug/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -873,9 +873,24 @@ void Debugger::inspect(Module *m, const uint16_t sizeStateArray,
addComma ? "," : "", m->memory.pages, m->memory.maximum,
m->memory.initial);
addComma = true;
for (uint32_t j = 0; j < total_elems; j++) {
this->channel->write("%" PRIu8 "%s", m->memory.bytes[j],
(j + 1) == total_elems ? "" : ",");
if (total_elems != 0) {
uint8_t data = m->memory.bytes[0];
uint32_t count = 1;
bool arrayComma = false;
for (uint32_t j = 1; j < total_elems; j++) {
if (m->memory.bytes[j] == data) {
count++;
} else {
this->channel->write("%s%" PRIu8 ",%d",
arrayComma ? "," : "", data,
count);
arrayComma = true;
data = m->memory.bytes[j];
count = 1;
}
}
this->channel->write("%s%" PRIu8 ",%d",
arrayComma ? "," : "", data, count);
}
this->channel->write("]}"); // closing memory
break;
Expand Down Expand Up @@ -1237,12 +1252,24 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) {
static_cast<void *>(m->bytes + start + total_bytes),
static_cast<void *>(mem_end));
}
memcpy(m->memory.bytes + start, program_state, total_bytes);

uint32_t byte_count = read_B32(&program_state);
uint8_t *end_pos = program_state + byte_count;
uint32_t current_pos = start;
while (program_state < end_pos) {
uint32_t count = read_LEB_32(&program_state);
uint8_t byte = *program_state++;
memset(m->memory.bytes + current_pos, byte, count);
current_pos += count;
}
if (current_pos != limit + 1) {
FATAL("RLE did not restore the expected amount of bytes\n");
}

for (auto i = start; i < (start + total_bytes); i++) {
debug("GOT byte idx %" PRIu32 " =%" PRIu8 "\n", i,
m->memory.bytes[i]);
}
program_state += total_bytes;
break;
}
case branchingTableState: {
Expand Down

0 comments on commit c73a20b

Please sign in to comment.