diff options
author | Damien George <damien.p.george@gmail.com> | 2014-09-23 15:31:56 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-09-23 15:31:56 +0000 |
commit | 52b5d76a6b0c5e4c779211fdf7bca56d87a2f83e (patch) | |
tree | ea4bae0dd4ba80e80ec8e540882c97df1988ce67 /py | |
parent | d6230f62c785c6c1ef1797500617c33e24607980 (diff) | |
download | micropython-52b5d76a6b0c5e4c779211fdf7bca56d87a2f83e.tar.gz micropython-52b5d76a6b0c5e4c779211fdf7bca56d87a2f83e.zip |
py: Free non-interned strings in the parser when not needed.
mp_parse_node_free now frees the memory associated with non-interned
strings. And the parser calls mp_parse_node_free when discarding a
non-used node (such as a doc string).
Also, the compiler now frees the parse tree explicitly just before it
exits (as opposed to relying on the caller to do this).
Addresses issue #708 as best we can.
Diffstat (limited to 'py')
-rw-r--r-- | py/builtinevex.c | 1 | ||||
-rw-r--r-- | py/builtinimport.c | 1 | ||||
-rw-r--r-- | py/compile.c | 3 | ||||
-rw-r--r-- | py/compile.h | 1 | ||||
-rw-r--r-- | py/parse.c | 5 |
5 files changed, 7 insertions, 4 deletions
diff --git a/py/builtinevex.c b/py/builtinevex.c index 6faeb6a071..63c1547fbe 100644 --- a/py/builtinevex.c +++ b/py/builtinevex.c @@ -63,7 +63,6 @@ STATIC mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse // compile the string mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false); - mp_parse_node_free(pn); if (module_fun == mp_const_none) { // TODO handle compile error correctly diff --git a/py/builtinimport.c b/py/builtinimport.c index 9bcc07487d..07b7c199ae 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -141,7 +141,6 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { // compile the imported script mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false); - mp_parse_node_free(pn); if (module_fun == mp_const_none) { // TODO handle compile error correctly diff --git a/py/compile.c b/py/compile.c index c584f0cf54..a9d19d2a94 100644 --- a/py/compile.c +++ b/py/compile.c @@ -3710,6 +3710,9 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is #endif #endif // !MICROPY_EMIT_CPYTHON + // free the parse tree + mp_parse_node_free(pn); + // free the scopes mp_raw_code_t *outer_raw_code = module_scope->raw_code; for (scope_t *s = module_scope; s;) { diff --git a/py/compile.h b/py/compile.h index 52c42aff4f..983a443e95 100644 --- a/py/compile.h +++ b/py/compile.h @@ -33,4 +33,5 @@ enum { MP_EMIT_OPT_ASM_THUMB, }; +// the compiler will free the parse tree (pn) before it returns mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl); diff --git a/py/parse.c b/py/parse.c index 26713dfe71..9508b82a02 100644 --- a/py/parse.c +++ b/py/parse.c @@ -179,6 +179,7 @@ void mp_parse_node_free(mp_parse_node_t pn) { mp_uint_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); mp_uint_t rule_id = MP_PARSE_NODE_STRUCT_KIND(pns); if (rule_id == RULE_string) { + m_del(char, (char*)pns->nodes[0], (mp_uint_t)pns->nodes[1]); return; } bool adjust = ADD_BLANK_NODE(rule_id); @@ -562,8 +563,8 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p if (input_kind != MP_PARSE_SINGLE_INPUT && rule->rule_id == RULE_expr_stmt && peek_result(&parser, 0) == MP_PARSE_NODE_NULL) { mp_parse_node_t p = peek_result(&parser, 1); if ((MP_PARSE_NODE_IS_LEAF(p) && !MP_PARSE_NODE_IS_ID(p)) || MP_PARSE_NODE_IS_STRUCT_KIND(p, RULE_string)) { - pop_result(&parser); - pop_result(&parser); + pop_result(&parser); // MP_PARSE_NODE_NULL + mp_parse_node_free(pop_result(&parser)); // RULE_string push_result_rule(&parser, rule_src_line, rules[RULE_pass_stmt], 0); break; } |