Skip to content

Commit f351e55

Browse files
committed
fix(snapshot): cleare temporary data used for reconstruction
reconstructing globals from a snapshot leaves some state behind, when reconstruction again globals from the same snapshot this data need to be cleared first. refactor: fixes clang compiler warnings
1 parent 315662a commit f351e55

39 files changed

+1212
-25687
lines changed

.clang-format-ignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
inkcpp_compiler/json.hpp

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
repos:
22
- repo: https://github.com/pre-commit/mirrors-clang-format
3-
rev: v18.1.0
3+
rev: v18.1.8
44
hooks:
55
- id: clang-format
66
types_or: [c++, c, cuda]
7+
exclude: inkcpp_compiler/json.hpp
78

89
- repo: https://github.com/pre-commit/pre-commit-hooks
910
rev: v6.0.0

inkcpp/array.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,17 @@ class managed_array : public snapshot_interface
5757
}
5858
}
5959

60-
const T& operator[](size_t i) const { return data()[i]; }
60+
const T& operator[](size_t i) const
61+
{
62+
inkAssert(i < _size, "Access array out of bounds, index %u in array of size %u", i, _size);
63+
return data()[i];
64+
}
6165

62-
T& operator[](size_t i) { return data()[i]; }
66+
T& operator[](size_t i)
67+
{
68+
inkAssert(i < _size, "Access array out of bounds, index %u in array of size %u", i, _size);
69+
return data()[i];
70+
}
6371

6472
const T* data() const
6573
{

inkcpp/avl_array.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class avl_array
7070
ink::runtime::internal::if_t<dynamic, std::int8_t*, std::int8_t[Size]> balance_;
7171
ink::runtime::internal::if_t<dynamic, child_type*, child_type[Size]> child_;
7272
size_type size_; // actual size
73-
size_type _capacity;
73+
size_type _capacity;
7474
size_type root_; // root node
7575
ink::runtime::internal::if_t<dynamic, size_type*, size_type[Fast ? Size : 1]> parent_;
7676

@@ -112,6 +112,12 @@ class avl_array
112112
return *this;
113113
}
114114

115+
tag_avl_array_iterator(const tag_avl_array_iterator& other)
116+
: instance_{other.instance_}
117+
, idx_{other.idx_}
118+
{
119+
}
120+
115121
inline bool operator==(const tag_avl_array_iterator& rhs) const { return idx_ == rhs.idx_; }
116122

117123
inline bool operator!=(const tag_avl_array_iterator& rhs) const { return ! (*this == rhs); }

inkcpp/choice.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ namespace ink
1616
namespace runtime
1717
{
1818

19-
size_t choice::num_tags() const { return static_cast<size_t>(std::distance(_tags_start, _tags_end)); }
19+
size_t choice::num_tags() const
20+
{
21+
return static_cast<size_t>(std::distance(_tags_start, _tags_end));
22+
}
2023

2124
const char* choice::get_tag(size_t index) const
2225
{

inkcpp/collections/restorable.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ unsigned char*
1616
ptr = snapshot_interface::snap_write(ptr, jump, write);
1717
ptr = snapshot_interface::snap_write(ptr, save, write);
1818
max = pos;
19-
if (jump != ~0 && jump > max) {
19+
if (jump != ~0U && jump > max) {
2020
max = jump;
2121
}
22-
if (save != ~0 && save > max) {
22+
if (save != ~0U && save > max) {
2323
max = save;
2424
}
2525
return ptr;
@@ -32,10 +32,10 @@ const unsigned char*
3232
ptr = snapshot_interface::snap_read(ptr, jump);
3333
ptr = snapshot_interface::snap_read(ptr, save);
3434
max = pos;
35-
if (jump != ~0 && jump > max) {
35+
if (jump != ~0U && jump > max) {
3636
max = jump;
3737
}
38-
if (save != ~0 && save > max) {
38+
if (save != ~0U && save > max) {
3939
max = save;
4040
}
4141
return ptr;

inkcpp/collections/restorable.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
#pragma once
88

9-
#include "../snapshot_impl.h"
9+
#include "../snapshot_interface.h"
1010

1111
#include <system.h>
1212
#include <traits.h>
@@ -102,15 +102,15 @@ class restorable : public snapshot_interface
102102
}
103103

104104
// Checks if we have a save state
105-
bool is_saved() const { return _save != ~0; }
105+
bool is_saved() const { return _save != ~0U; }
106106

107107
// Creates a save point which can later be restored to or forgotten
108108
void save()
109109
{
110110
inkAssert(
111-
_save == ~0, "Collection is already saved. You should never call save twice. Ignoring."
111+
_save == ~0U, "Collection is already saved. You should never call save twice. Ignoring."
112112
);
113-
if (_save != ~0) {
113+
if (_save != ~0U) {
114114
return;
115115
}
116116

@@ -121,8 +121,8 @@ class restorable : public snapshot_interface
121121
// Restore to the last save point
122122
void restore()
123123
{
124-
inkAssert(_save != ~0, "Collection can't be restored because it's not saved. Ignoring.");
125-
if (_save == ~0) {
124+
inkAssert(_save != ~0U, "Collection can't be restored because it's not saved. Ignoring.");
125+
if (_save == ~0U) {
126126
return;
127127
}
128128

@@ -137,8 +137,8 @@ class restorable : public snapshot_interface
137137
template<typename NullifyMethod>
138138
void forget(NullifyMethod nullify)
139139
{
140-
inkAssert(_save != ~0, "Can't forget save point because there is none. Ignoring.");
141-
if (_save == ~0) {
140+
inkAssert(_save != ~0U, "Can't forget save point because there is none. Ignoring.");
141+
if (_save == ~0U) {
142142
return;
143143
}
144144

@@ -170,7 +170,7 @@ class restorable : public snapshot_interface
170170
ElementType& push(const ElementType& elem)
171171
{
172172
// Don't destroy saved data. Jump over it
173-
if (_pos < _save && _save != ~0) {
173+
if (_pos < _save && _save != ~0U) {
174174
_jump = _pos;
175175
_pos = _save;
176176
}
@@ -279,7 +279,7 @@ class restorable : public snapshot_interface
279279
void for_each_all(CallbackMethod callback) const
280280
{
281281
// no matter if we're saved or not, we iterate everything
282-
int len = (_save == ~0 || _pos > _save) ? _pos : _save;
282+
int len = (_save == ~0U || _pos > _save) ? _pos : _save;
283283

284284
// Iterate
285285
for (int i = 0; i < len; i++)
@@ -358,10 +358,7 @@ class restorable : public snapshot_interface
358358

359359
protected:
360360
// Called when we run out of space in buffer.
361-
virtual void overflow(ElementType*&, size_t&)
362-
{
363-
inkFail("Restorable run out of memory!");
364-
}
361+
virtual void overflow(ElementType*&, size_t&) { inkFail("Restorable run out of memory!"); }
365362

366363
private:
367364
template<typename Predicate>

inkcpp/container_operations.cpp

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,35 @@
1313

1414
#include <iostream>
1515

16-
namespace ink::runtime::internal {
16+
namespace ink::runtime::internal
17+
{
1718

18-
void operation<Command::READ_COUNT_VAR, value_type::divert, void>::operator()(
19-
basic_eval_stack& stack, value* vals)
20-
{
21-
container_t id;
22-
bool success = _story.get_container_id(
23-
_story.instructions() + vals[0].get<value_type::divert>(),
24-
id);
25-
inkAssert(success, "failed to find container to read visit count!");
26-
stack.push(value{}.set<value_type::int32>(
27-
static_cast<int32_t>(_visit_counts.visits( id )
28-
)));
29-
}
30-
31-
void operation<Command::TURNS, value_type::divert, void>::operator()(
32-
basic_eval_stack& stack, value* vals)
33-
{
34-
container_t id;
35-
bool success = _story.get_container_id(
36-
_story.instructions() + vals[0].get<value_type::divert>(),
37-
id);
38-
inkAssert(success, "failed to find container to read turn count!");
39-
stack.push(value{}.set<value_type::int32>(
40-
static_cast<int32_t>(_visit_counts.turns(id)
41-
)));
42-
}
19+
void operation<Command::READ_COUNT_VAR, value_type::divert, void>::operator()(
20+
basic_eval_stack& stack, value* vals
21+
)
22+
{
23+
container_t id;
24+
bool success
25+
= _story.get_container_id(_story.instructions() + vals[0].get<value_type::divert>(), id);
26+
inkAssert(success, "failed to find container to read visit count!");
27+
stack.push(value{}.set<value_type::int32>(static_cast<int32_t>(_visit_counts.visits(id))));
28+
}
4329

44-
void operation<Command::CHOICE_COUNT, value_type::none, void>::operator()
45-
(basic_eval_stack& stack, value*)
46-
{
47-
stack.push(value{}.set<value_type::int32>(static_cast<int32_t>(
48-
_runner.num_choices()
49-
)));
50-
}
30+
void operation<Command::TURNS, value_type::divert, void>::operator()(
31+
basic_eval_stack& stack, value* vals
32+
)
33+
{
34+
container_t id;
35+
bool success
36+
= _story.get_container_id(_story.instructions() + vals[0].get<value_type::divert>(), id);
37+
inkAssert(success, "failed to find container to read turn count!");
38+
stack.push(value{}.set<value_type::int32>(static_cast<int32_t>(_visit_counts.turns(id))));
39+
}
5140

41+
void operation<
42+
Command::CHOICE_COUNT, value_type::none, void>::operator()(basic_eval_stack& stack, value*)
43+
{
44+
stack.push(value{}.set<value_type::int32>(static_cast<int32_t>(_runner.num_choices())));
5245
}
46+
47+
} // namespace ink::runtime::internal

0 commit comments

Comments
 (0)