Skip to content

Commit 2ef60e0

Browse files
committed
refactor: move log out of record state access
1 parent aa2076d commit 2ef60e0

File tree

3 files changed

+19
-39
lines changed

3 files changed

+19
-39
lines changed

src/machine.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,7 +2376,8 @@ access_log machine::log_reset_uarch(const access_log::type &log_type) {
23762376
hash_type root_hash_before;
23772377
get_root_hash(root_hash_before);
23782378
// Call uarch_reset_state with a uarch_record_state_access object
2379-
uarch_record_state_access a(*this, log_type);
2379+
access_log log(log_type);
2380+
uarch_record_state_access a(*this, log);
23802381
{
23812382
[[maybe_unused]] auto note = a.make_scoped_note("reset_uarch_state");
23822383
uarch_reset_state(a);
@@ -2385,8 +2386,8 @@ access_log machine::log_reset_uarch(const access_log::type &log_type) {
23852386
hash_type root_hash_after;
23862387
update_merkle_tree();
23872388
get_root_hash(root_hash_after);
2388-
verify_reset_uarch(root_hash_before, *a.get_log(), root_hash_after);
2389-
return std::move(*a.get_log());
2389+
verify_reset_uarch(root_hash_before, log, root_hash_after);
2390+
return log;
23902391
}
23912392

23922393
void machine::verify_reset_uarch(const hash_type &root_hash_before, const access_log &log,
@@ -2412,17 +2413,18 @@ access_log machine::log_step_uarch(const access_log::type &log_type) {
24122413
}
24132414
hash_type root_hash_before;
24142415
get_root_hash(root_hash_before);
2416+
access_log log(log_type);
24152417
// Call interpret with a logged state access object
2416-
uarch_record_state_access a(*this, log_type);
2418+
uarch_record_state_access a(*this, log);
24172419
{
24182420
[[maybe_unused]] auto note = a.make_scoped_note("step");
24192421
uarch_step(a);
24202422
}
24212423
// Verify access log before returning
24222424
hash_type root_hash_after;
24232425
get_root_hash(root_hash_after);
2424-
verify_step_uarch(root_hash_before, *a.get_log(), root_hash_after);
2425-
return std::move(*a.get_log());
2426+
verify_step_uarch(root_hash_before, log, root_hash_after);
2427+
return log;
24262428
}
24272429

24282430
// Declaration of explicit instantiation in module uarch-step.cpp

src/scoped-note.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ namespace cartesi {
2323
template <typename STATE_ACCESS>
2424
class scoped_note {
2525

26-
STATE_ACCESS &m_a;
26+
STATE_ACCESS m_a;
2727
const char *const m_text; ///< String with the text for the annotation
2828

2929
public:
3030
/// \brief Constructor adds the "begin" bracketing note
3131
/// \param a State access receiving annotations
3232
/// \param text Pointer to annotation text (must be valid until destruction)
3333
/// \details A note is added at the moment of construction and destruction
34-
scoped_note(STATE_ACCESS &a, const char *text) : m_a{a}, m_text(text) {
34+
scoped_note(STATE_ACCESS a, const char *text) : m_a{a}, m_text(text) {
3535
m_a.push_begin_bracket(m_text);
3636
}
3737

src/uarch-record-state-access.h

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <cassert>
2323
#include <cstdint>
2424
#include <cstring>
25-
#include <memory>
2625
#include <stdexcept>
2726
#include <string>
2827
#include <utility>
@@ -61,8 +60,8 @@ class uarch_record_state_access :
6160

6261
// NOLINTBEGIN(cppcoreguidelines-avoid-const-or-ref-data-members)
6362
machine &m_m; ///< Macro machine
63+
access_log &m_log; ///< Access log
6464
// NOLINTEND(cppcoreguidelines-avoid-const-or-ref-data-members)
65-
std::shared_ptr<access_log> m_log; ///< Pointer to access log
6665

6766
static auto get_hash(hasher_type &hasher, const access_data &data) {
6867
hash_type hash{};
@@ -72,35 +71,14 @@ class uarch_record_state_access :
7271

7372
public:
7473
/// \brief Constructor from machine and uarch states.
75-
/// \param um Reference to uarch state.
7674
/// \param m Reference to machine state.
77-
explicit uarch_record_state_access(machine &m, access_log::type log_type) :
75+
/// \param log Reference to log.
76+
explicit uarch_record_state_access(machine &m, access_log &log) :
7877
m_m(m),
79-
m_log(std::make_shared<access_log>(log_type)) {
78+
m_log(log) {
8079
;
8180
}
8281

83-
/// \brief No copy constructor
84-
uarch_record_state_access(const uarch_record_state_access &) = delete;
85-
/// \brief No copy assignment
86-
uarch_record_state_access &operator=(const uarch_record_state_access &) = delete;
87-
/// \brief No move constructor
88-
uarch_record_state_access(uarch_record_state_access &&) = delete;
89-
/// \brief No move assignment
90-
uarch_record_state_access &operator=(uarch_record_state_access &&) = delete;
91-
/// \brief Default destructor
92-
~uarch_record_state_access() = default;
93-
94-
/// \brief Returns const pointer to access log.
95-
std::shared_ptr<const access_log> get_log() const {
96-
return m_log;
97-
}
98-
99-
/// \brief Returns pointer to access log.
100-
std::shared_ptr<access_log> get_log() {
101-
return m_log;
102-
}
103-
10482
private:
10583
static std::pair<uint64_t, int> adjust_access(uint64_t paddr, int log2_size) {
10684
static_assert(cartesi::log2_size_v<uint64_t> <= machine_merkle_tree::get_log2_word_size(),
@@ -115,7 +93,7 @@ class uarch_record_state_access :
11593
}
11694

11795
void log_access(access &&a, const char *text) const {
118-
m_log->push_access(std::move(a), text);
96+
m_log.push_access(std::move(a), text);
11997
}
12098

12199
static void log_access_type(access &a, access_type type) {
@@ -150,7 +128,7 @@ class uarch_record_state_access :
150128
}
151129

152130
void log_read_data_if_requested(access &a, uint64_t paddr, int log2_size) const {
153-
if (m_log->get_log_type().has_large_data()) {
131+
if (m_log.get_log_type().has_large_data()) {
154132
std::ignore = log_read_data(a, paddr, log2_size);
155133
}
156134
}
@@ -165,7 +143,7 @@ class uarch_record_state_access :
165143
}
166144

167145
void log_written_data_if_requested(access &a, uint64_t paddr, int log2_size) const {
168-
if (m_log->get_log_type().has_large_data()) {
146+
if (m_log.get_log_type().has_large_data()) {
169147
log_written_data(a, paddr, log2_size);
170148
}
171149
}
@@ -320,11 +298,11 @@ class uarch_record_state_access :
320298
friend i_accept_scoped_note<uarch_record_state_access>;
321299

322300
void do_push_begin_bracket(const char *text) {
323-
m_log->push_begin_bracket(text);
301+
m_log.push_begin_bracket(text);
324302
}
325303

326304
void do_push_end_bracket(const char *text) {
327-
m_log->push_end_bracket(text);
305+
m_log.push_end_bracket(text);
328306
}
329307

330308
auto do_make_scoped_note(const char *text) {

0 commit comments

Comments
 (0)