From bd6293307d38ae0dd99a5acaa7d4adf20d10f046 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 13 Jul 2013 16:39:40 -0700 Subject: [PATCH] 3142 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. --- 010cell.test.cc | 13 +------------ 012compiledfn.cc | 4 ++-- 020lookup.test.cc | 34 +++++++++++++++++----------------- 021eval.cc | 2 +- 022primitives.cc | 2 +- 026table.cc | 5 ++--- 6 files changed, 24 insertions(+), 36 deletions(-) diff --git a/010cell.test.cc b/010cell.test.cc index f4370dd9..8b10ba3d 100644 --- a/010cell.test.cc +++ b/010cell.test.cc @@ -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); diff --git a/012compiledfn.cc b/012compiledfn.cc index c7f5843a..e22283ea 100644 --- a/012compiledfn.cc +++ b/012compiledfn.cc @@ -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); diff --git a/020lookup.test.cc b/020lookup.test.cc index 12b62ef9..2f4aef8a 100644 --- a/020lookup.test.cc +++ b/020lookup.test.cc @@ -4,16 +4,16 @@ 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); @@ -21,9 +21,9 @@ void test_lookup_returns_lexical_binding() { } 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); @@ -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); @@ -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); @@ -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); @@ -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 diff --git a/021eval.cc b/021eval.cc index a3f26b7c..ec551aff 100644 --- a/021eval.cc +++ b/021eval.cc @@ -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)) diff --git a/022primitives.cc b/022primitives.cc index 0017a25b..aa2205d4 100644 --- a/022primitives.cc +++ b/022primitives.cc @@ -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)); diff --git a/026table.cc b/026table.cc index 0cfffd27..68e8b456 100644 --- a/026table.cc +++ b/026table.cc @@ -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)",