Skip to content

Commit c73a20b

Browse files
MaartenS11tolauwae
andauthored
🐎 RLE for memory in snapshots (#264)
* 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 <tom.lauwaerts@ugent.be>
1 parent 14ac89b commit c73a20b

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

src/Debug/debugger.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -873,9 +873,24 @@ void Debugger::inspect(Module *m, const uint16_t sizeStateArray,
873873
addComma ? "," : "", m->memory.pages, m->memory.maximum,
874874
m->memory.initial);
875875
addComma = true;
876-
for (uint32_t j = 0; j < total_elems; j++) {
877-
this->channel->write("%" PRIu8 "%s", m->memory.bytes[j],
878-
(j + 1) == total_elems ? "" : ",");
876+
if (total_elems != 0) {
877+
uint8_t data = m->memory.bytes[0];
878+
uint32_t count = 1;
879+
bool arrayComma = false;
880+
for (uint32_t j = 1; j < total_elems; j++) {
881+
if (m->memory.bytes[j] == data) {
882+
count++;
883+
} else {
884+
this->channel->write("%s%" PRIu8 ",%d",
885+
arrayComma ? "," : "", data,
886+
count);
887+
arrayComma = true;
888+
data = m->memory.bytes[j];
889+
count = 1;
890+
}
891+
}
892+
this->channel->write("%s%" PRIu8 ",%d",
893+
arrayComma ? "," : "", data, count);
879894
}
880895
this->channel->write("]}"); // closing memory
881896
break;
@@ -1237,12 +1252,24 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) {
12371252
static_cast<void *>(m->bytes + start + total_bytes),
12381253
static_cast<void *>(mem_end));
12391254
}
1240-
memcpy(m->memory.bytes + start, program_state, total_bytes);
1255+
1256+
uint32_t byte_count = read_B32(&program_state);
1257+
uint8_t *end_pos = program_state + byte_count;
1258+
uint32_t current_pos = start;
1259+
while (program_state < end_pos) {
1260+
uint32_t count = read_LEB_32(&program_state);
1261+
uint8_t byte = *program_state++;
1262+
memset(m->memory.bytes + current_pos, byte, count);
1263+
current_pos += count;
1264+
}
1265+
if (current_pos != limit + 1) {
1266+
FATAL("RLE did not restore the expected amount of bytes\n");
1267+
}
1268+
12411269
for (auto i = start; i < (start + total_bytes); i++) {
12421270
debug("GOT byte idx %" PRIu32 " =%" PRIu8 "\n", i,
12431271
m->memory.bytes[i]);
12441272
}
1245-
program_state += total_bytes;
12461273
break;
12471274
}
12481275
case branchingTableState: {

0 commit comments

Comments
 (0)