Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.

Commit

Permalink
3142
Browse files Browse the repository at this point in the history
Done auditing all declarations of the form 'cell* ... = new_...'
We should basically never declare a cell* unless it's in the low-level
new_.. helpers, or it's not a new cell, or it's going to be returned.
  • Loading branch information
akkartik committed Jul 13, 2013
1 parent 1f142bc commit bd62933
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 36 deletions.
13 changes: 1 addition & 12 deletions 010cell.test.cc
Expand Up @@ -9,20 +9,9 @@ void test_new_cell_has_nil_car_and_cdr() {
CHECK_EQ(x->cdr, nil);
}

void test_new_cell_doesnt_mkref() {
CLEAR_TRACE;
unused cell* dummy = new_cell();
CHECK_EQ(trace_count("gc", "alloc"), 1);
CHECK_TRACE_DOESNT_CONTAIN("gc", "mkref");
rmref(dummy);
}

void test_rmref_frees_space() {
CLEAR_TRACE;
cell* c = new_cell();
rmref(c);
CHECK_EQ(trace_count("gc", "free"), 1);
c = mkref(new_cell());
cell* c = mkref(new_cell());
CLEAR_TRACE;
rmref(c);
CHECK_EQ(trace_count("gc", "free"), 1);
Expand Down
4 changes: 2 additions & 2 deletions 012compiledfn.cc
Expand Up @@ -18,12 +18,12 @@ const compiledfn_metadata Compiledfns[] = {
void setup_compiledfns() {
new_dynamic_scope(sym_compiled, new_table());
for (unsigned long i=0; i < sizeof(Compiledfns)/sizeof(Compiledfns[0]); ++i) {
cell* f = new_table();
TEMP(f, mkref(new_table()));
set(f, sym_name, new_sym(Compiledfns[i].name));
indent_sensitive_stream ss(Compiledfns[i].params);
set(f, sym_sig, next_cell(ss));
set(f, sym_body, new_compiledfn(Compiledfns[i].impl));
cell* obj = new_object("function", f);
TEMP(obj, mkref(new_object("function", f)));
new_dynamic_scope(Compiledfns[i].name, obj);
// save to a second, immutable place
set(lookup(sym_compiled), Compiledfns[i].name, obj);
Expand Down
34 changes: 17 additions & 17 deletions 020lookup.test.cc
Expand Up @@ -4,26 +4,26 @@ void test_Curr_lexical_scope_has_nil_cdr_on_startup() {
}

void test_lookup_returns_dynamic_binding() {
cell* var = new_sym("a");
cell* val = new_num(34);
TEMP(var, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
new_dynamic_scope(var, val);
CHECK_EQ(lookup(var), val);
end_dynamic_scope(var);
}

void test_lookup_returns_lexical_binding() {
cell* var = new_sym("a");
cell* val = new_num(34);
TEMP(var, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
new_lexical_scope();
add_lexical_binding(var, val);
CHECK_EQ(lookup(var), val);
end_lexical_scope();
}

void test_lexical_binding_always_overrides_dynamic() {
cell* var = new_sym("a");
cell* val = new_num(34);
cell* dyn_val = new_num(35);
TEMP(var, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
TEMP(dyn_val, mkref(new_num(35)));
new_dynamic_scope(var, dyn_val);
new_lexical_scope();
add_lexical_binding(var, val);
Expand All @@ -35,8 +35,8 @@ void test_lexical_binding_always_overrides_dynamic() {
}

void test_nil_lexical_binding_works() {
cell* var = new_sym("a");
cell* dyn_val = new_num(35);
TEMP(var, mkref(new_sym("a")));
TEMP(dyn_val, mkref(new_num(35)));
new_dynamic_scope(var, dyn_val);
new_lexical_scope();
add_lexical_binding(var, nil);
Expand All @@ -46,10 +46,10 @@ void test_nil_lexical_binding_works() {
}

void test_lexical_scopes_nest_correctly() {
cell* var = new_sym("a");
cell* val = new_num(34);
cell* val2 = new_num(35);
cell* dyn_val = new_num(36);
TEMP(var, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
TEMP(val2, mkref(new_num(35)));
TEMP(dyn_val, mkref(new_num(36)));
new_dynamic_scope(var, dyn_val);
new_lexical_scope();
CHECK(Curr_lexical_scope != nil);
Expand All @@ -65,8 +65,8 @@ void test_lexical_scopes_nest_correctly() {
}

void test_lower_lexical_scopes_are_available() {
cell* var = new_sym("a");
cell* val = new_num(34);
TEMP(var, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
new_lexical_scope();
add_lexical_binding(var, val);
CHECK_EQ(lookup(var), val);
Expand All @@ -77,8 +77,8 @@ void test_lower_lexical_scopes_are_available() {
}

void test_new_dynamic_scope_increments_refcounts() {
cell* var = new_sym("a");
cell* val = new_num(34);
TEMP(var, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
CLEAR_TRACE;
new_dynamic_scope(var, val);
CHECK_EQ(excess_mkrefs(), 2); // one for var, one for val
Expand Down
2 changes: 1 addition & 1 deletion 021eval.cc
Expand Up @@ -441,7 +441,7 @@ cell* args_in_param_order(cell* params, cell* non_keyword_args, cell* keyword_ar
cell* keyword_param(cell* arg, cell* params, bool& is_rest) {
is_rest = false;
if (!is_keyword_sym(arg)) return NULL;
cell* candidate = new_sym(to_string(arg).substr(1));
TEMP(candidate, mkref(new_sym(to_string(arg).substr(1))));
for (params=strip_quote(params); params != nil; params=strip_quote(cdr(params))) {
cell* param = (is_cons(params) && !is_alias(params))
? strip_quote(car(params))
Expand Down
2 changes: 1 addition & 1 deletion 022primitives.cc
Expand Up @@ -8,7 +8,7 @@
// always increment the nrefs of a single cell along all codepaths

COMPILE_FN(fn, compiledfn_fn, "'($params ... $body)",
cell* f = new_table();
TEMP(f, mkref(new_table()));
set(f, sym_sig, lookup("$params"));
set(f, sym_body, lookup("$body"));
set(f, sym_env, cdr(Curr_lexical_scope));
Expand Down
5 changes: 2 additions & 3 deletions 026table.cc
Expand Up @@ -23,14 +23,13 @@ COMPILE_FN(table_get, compiledfn_table_get, "($table $key)",

COMPILE_FN(table_to_list, compiledfn_table_to_list, "($table)",
cell_map table = to_table(lookup("$table"))->value;
cell* result = new_cell();
cell* curr = result;
cell* p_result = new_cell(); cell* curr = p_result;
for (cell_map::iterator p = table.begin(); p != table.end(); ++p) {
if (!p->second) continue;
add_cons(curr, new_cons(p->first, new_cons(p->second)));
curr=cdr(curr);
}
return drop_ptr(result);
return drop_ptr(p_result);
)

COMPILE_FN(table_length, compiledfn_table_length, "($table)",
Expand Down

0 comments on commit bd62933

Please sign in to comment.