Use arena allocator (currently leaky!)

This commit is contained in:
Nikita Popov 2014-07-25 20:24:15 +02:00
parent 1644bf01da
commit d46f67a3a7
5 changed files with 383 additions and 378 deletions

View File

@ -26,7 +26,7 @@
ZEND_API zend_ast *zend_ast_create_znode(znode *node)
{
TSRMLS_FETCH();
zend_ast_znode *ast = emalloc(sizeof(zend_ast_znode));
zend_ast_znode *ast = zend_arena_alloc(&CG(ast_arena), sizeof(zend_ast_znode));
ast->kind = ZEND_AST_ZNODE;
ast->attr = 0;
ast->lineno = CG(zend_lineno);
@ -37,7 +37,7 @@ ZEND_API zend_ast *zend_ast_create_znode(znode *node)
ZEND_API zend_ast *zend_ast_create_zval_ex(zval *zv, zend_ast_attr attr)
{
TSRMLS_FETCH();
zend_ast_zval *ast = emalloc(sizeof(zend_ast_zval));
zend_ast_zval *ast = zend_arena_alloc(&CG(ast_arena), sizeof(zend_ast_zval));
ast->kind = ZEND_AST_ZVAL;
ast->attr = attr;
ast->lineno = CG(zend_lineno);
@ -50,7 +50,7 @@ ZEND_API zend_ast *zend_ast_create_decl(
unsigned char *lex_pos, zend_string *doc_comment, zend_string *name,
zend_ast *child0, zend_ast *child1, zend_ast *child2
) {
zend_ast_decl *ast = emalloc(sizeof(zend_ast_decl));
zend_ast_decl *ast = zend_arena_alloc(&CG(ast_arena), sizeof(zend_ast_decl));
ast->kind = kind;
ast->attr = 0;
@ -74,7 +74,7 @@ static zend_ast *zend_ast_create_from_va_list(
zend_uint i;
zend_ast *ast;
ast = emalloc(sizeof(zend_ast) + (children - 1) * sizeof(zend_ast *));
ast = zend_arena_alloc(&CG(ast_arena), sizeof(zend_ast) + (children - 1) * sizeof(zend_ast *));
ast->kind = kind;
ast->attr = attr;
ast->lineno = UINT_MAX;
@ -124,7 +124,7 @@ ZEND_API zend_ast *zend_ast_create_dynamic(zend_ast_kind kind)
{
/* use 4 children as default */
TSRMLS_FETCH();
zend_ast *ast = emalloc(sizeof(zend_ast) + sizeof(zend_ast *) * 3);
zend_ast *ast = zend_arena_alloc(&CG(ast_arena), sizeof(zend_ast) + sizeof(zend_ast *) * 3);
ast->kind = kind;
ast->attr = 0;
ast->lineno = CG(zend_lineno);
@ -139,7 +139,11 @@ static inline zend_bool is_power_of_two(unsigned short n) {
ZEND_API zend_ast *zend_ast_dynamic_add(zend_ast *ast, zend_ast *op)
{
if (ast->children >= 4 && is_power_of_two(ast->children)) {
ast = erealloc(ast, sizeof(zend_ast) + sizeof(zend_ast *) * (ast->children * 2 - 1));
size_t old_size = sizeof(zend_ast) + sizeof(zend_ast *) * (ast->children - 1);
zend_ast *new_ast = zend_arena_alloc(&CG(ast_arena),
sizeof(zend_ast) + sizeof(zend_ast *) * (ast->children * 2 - 1));
memcpy(new_ast, ast, old_size);
ast = new_ast;
}
ast->child[ast->children++] = op;
return ast;
@ -332,9 +336,12 @@ ZEND_API zend_ast *zend_ast_copy(zend_ast *ast)
if (ast == NULL) {
return NULL;
} else if (ast->kind == ZEND_AST_ZVAL) {
zend_ast *copy = zend_ast_create_zval_ex(zend_ast_get_zval(ast), ast->attr);
zval_copy_ctor(zend_ast_get_zval(copy));
return copy;
zend_ast_zval *copy = emalloc(sizeof(zend_ast_zval));
copy->kind = ZEND_AST_ZVAL;
copy->attr = ast->attr;
copy->lineno = ast->lineno;
ZVAL_DUP(&copy->val, zend_ast_get_zval(ast));
return (zend_ast *) copy;
} else {
zend_ast *new = emalloc(sizeof(zend_ast) + sizeof(zend_ast *) * (ast->children - 1));
int i;
@ -384,5 +391,5 @@ ZEND_API void zend_ast_destroy(zend_ast *ast)
}
}
efree(ast);
//efree(ast);
}

View File

@ -3422,16 +3422,17 @@ void zend_do_constant_expression(znode *result, zend_ast *ast TSRMLS_DC) /* {{{
// TODO.AST Sort out the whole constant folding issue
static void _tmp_compile_const_expr(zval *result, zend_ast *ast TSRMLS_DC) {
ast = zend_ast_copy(ast);
zend_ast *orig_ast = ast;
zend_eval_const_expr(&ast TSRMLS_CC);
zend_compile_const_expr(&ast TSRMLS_CC);
if (ast->kind == ZEND_AST_ZVAL) {
ZVAL_COPY_VALUE(result, zend_ast_get_zval(ast));
ZVAL_COPY(result, zend_ast_get_zval(ast));
if (Z_TYPE_P(result) == IS_ARRAY) {
zend_make_immutable_array_r(result TSRMLS_CC);
}
efree(ast);
orig_ast->kind = ZEND_AST_ZNODE;
} else {
ast = zend_ast_copy(ast);
ZVAL_NEW_AST(result, ast);
}
}
@ -4714,7 +4715,6 @@ static void zend_compile_static_var_common(
if (by_ref) {
zend_ast *fetch_ast = zend_ast_create_unary(ZEND_AST_VAR, var_ast);
zend_compile_assign_ref_common(NULL, fetch_ast, &result TSRMLS_CC);
efree(fetch_ast);
} else {
zend_ast *fetch_ast = zend_ast_create_unary(ZEND_AST_VAR, var_ast);
zend_ast *znode_ast = zend_ast_create_znode(&result);
@ -4722,9 +4722,6 @@ static void zend_compile_static_var_common(
znode dummy_node;
zend_compile_expr(&dummy_node, assign_ast TSRMLS_CC);
zend_do_free(&dummy_node TSRMLS_CC);
efree(znode_ast);
efree(assign_ast);
efree(fetch_ast);
}
}
@ -5071,8 +5068,6 @@ void zend_compile_foreach(zend_ast *ast TSRMLS_DC) {
zend_ast *assign_ast = zend_ast_create_binary(ZEND_AST_ASSIGN, value_ast, znode_ast);
zend_compile_expr(&dummy_node, assign_ast TSRMLS_CC);
zend_do_free(&dummy_node TSRMLS_CC);
efree(znode_ast);
efree(assign_ast);
}
if (key_ast) {
@ -5080,8 +5075,6 @@ void zend_compile_foreach(zend_ast *ast TSRMLS_DC) {
zend_ast *assign_ast = zend_ast_create_binary(ZEND_AST_ASSIGN, key_ast, znode_ast);
zend_compile_expr(&dummy_node, assign_ast TSRMLS_CC);
zend_do_free(&dummy_node TSRMLS_CC);
efree(znode_ast);
efree(assign_ast);
}
do_begin_loop(TSRMLS_C);
@ -6921,7 +6914,6 @@ void zend_compile_isset_or_empty(znode *result, zend_ast *ast TSRMLS_DC) {
/* empty(expr) can be transformed to !expr */
zend_ast *not_ast = zend_ast_create_unary(ZEND_BOOL_NOT, var_ast);
zend_compile_expr(result, not_ast TSRMLS_CC);
efree(not_ast);
return;
} else {
zend_error_noreturn(E_COMPILE_ERROR,
@ -6989,9 +6981,7 @@ void zend_compile_shell_exec(znode *result, zend_ast *ast TSRMLS_DC) {
zend_compile_expr(result, call_ast TSRMLS_CC);
efree(call_ast);
efree(args_ast);
zend_ast_destroy(name_ast);
zval_ptr_dtor(&fn_name);
}
void zend_compile_array(znode *result, zend_ast *ast TSRMLS_DC) {
@ -7144,8 +7134,7 @@ void zend_compile_resolve_class_name(znode *result, zend_ast *ast TSRMLS_DC) {
zend_compile_expr(result, class_const_ast TSRMLS_CC);
zend_ast_destroy(class_str_ast);
efree(class_const_ast);
zval_ptr_dtor(&class_str_zv);
}
break;
case ZEND_FETCH_CLASS_DEFAULT:
@ -7279,7 +7268,7 @@ void zend_compile_magic_const(znode *result, zend_ast *ast TSRMLS_DC) {
zend_ast *const_ast = zend_ast_create_unary(ZEND_AST_CONST,
zend_ast_create_zval(&const_zv));
zend_compile_const(result, const_ast TSRMLS_CC);
zend_ast_destroy(const_ast);
zval_ptr_dtor(&const_zv);
}
}

View File

@ -159,6 +159,7 @@ struct _zend_compiler_globals {
#endif
zend_ast *ast;
zend_arena *ast_arena;
};

File diff suppressed because it is too large Load Diff

View File

@ -583,12 +583,14 @@ ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type TSR
CG(active_op_array) = op_array;
zend_stack_push(&CG(context_stack), (void *) &CG(context));
zend_init_compiler_context(TSRMLS_C);
CG(ast_arena) = zend_arena_create(1024 * 32 TSRMLS_CC);
compiler_result = zendparse(TSRMLS_C);
if (compiler_result != 0) { /* parser error */
zend_bailout();
}
zend_compile_top_stmt(CG(ast) TSRMLS_CC);
zend_ast_destroy(CG(ast));
zend_arena_destroy(CG(ast_arena));
zend_do_end_compilation(TSRMLS_C);
zend_do_return(&retval_znode, 0 TSRMLS_CC);
CG(in_compilation) = original_in_compilation;
@ -753,6 +755,7 @@ zend_op_array *compile_string(zval *source_string, char *filename TSRMLS_DC)
zend_stack_push(&CG(context_stack), (void *) &CG(context));
zend_init_compiler_context(TSRMLS_C);
BEGIN(ST_IN_SCRIPTING);
CG(ast_arena) = zend_arena_create(1024 * 32 TSRMLS_CC);
compiler_result = zendparse(TSRMLS_C);
if (SCNG(script_filtered)) {
@ -769,6 +772,7 @@ zend_op_array *compile_string(zval *source_string, char *filename TSRMLS_DC)
} else {
zend_compile_top_stmt(CG(ast) TSRMLS_CC);
zend_ast_destroy(CG(ast));
zend_arena_destroy(CG(ast_arena));
zend_do_end_compilation(TSRMLS_C);
zend_do_return(NULL, 0 TSRMLS_CC);
CG(active_op_array) = original_active_op_array;