Skip to content

Commit 00f6ff0

Browse files
committed
fix bug in Memory::Cons
1 parent c31cb39 commit 00f6ff0

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

example/sqrt.ss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
(iter 1.0))
1414

1515
(display (sqrt 2))
16+
(newline)

src/memory.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,11 @@ Data Memory::NewPair(const Data& x, const Data& y)
132132
tmp_y = y;
133133
GarbageCollect();
134134
// GarbageCollect should ERROR if out of memory
135-
if (free_ < size_)
135+
if (free_ >= size_)
136136
ERROR("Garbage collect fail: out of memory");
137137
car_[free_] = tmp_x;
138138
cdr_[free_] = tmp_y;
139+
tmp_x = tmp_y = Data::null;
139140
} else {
140141
car_[free_] = x;
141142
cdr_[free_] = y;

src/test_garbage_collect.cc

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
using namespace sscheme;
1616

17-
const int kMemSize = 5*1024;
17+
const int kMemSize = 500;
1818

1919
// return the occupied size in memory
2020
int OccupiedSize()
@@ -25,7 +25,7 @@ int OccupiedSize()
2525
class EvalTestGarbageCollect : public ::testing::Test {
2626
protected:
2727
virtual void SetUp() {
28-
sscheme::Initialize(5*1024);
28+
sscheme::Initialize(kMemSize);
2929
}
3030
// virtual void TearDown() {}
3131
};
@@ -84,5 +84,35 @@ TEST_F(EvalTestGarbageCollect, test_garbage_collect) {
8484
g_machine.arg1 = g_machine.val = Data::null;
8585
Memory::GarbageCollect();
8686
EXPECT_EQ(0,OccupiedSize());
87-
8887
}
88+
89+
90+
TEST_F(EvalTestGarbageCollect, test_traffic)
91+
{
92+
Data& exp = g_machine.exp;
93+
srand(clock());
94+
for(int i = 0; i < 20000; i++) {
95+
// generate a list under kMemSize/2
96+
// verify it is right
97+
exp = Data::null;
98+
int n = rand() % int(kMemSize/2);
99+
for(int j = 0; j < n; j++)
100+
exp = CONS(INT(j),exp);
101+
while(--n >= 0) {
102+
EXPECT_EQ(n,FIRST(exp).Int());
103+
exp = REST(exp);
104+
}
105+
if (i % 5000 == 0) {
106+
printf(".");
107+
fflush(stdout);
108+
}
109+
}
110+
printf("\n");
111+
exp = Data::null;
112+
Memory::GarbageCollect();
113+
EXPECT_EQ(0,OccupiedSize());
114+
}
115+
116+
117+
118+

0 commit comments

Comments
 (0)