}
| PRAGMA_WARNING_ON EOL
{
- void *mem_ctx = state->linalloc;
+ linear_ctx *mem_ctx = state->linalloc;
$$ = new(mem_ctx) ast_warnings_toggle(true);
}
| PRAGMA_WARNING_OFF EOL
{
- void *mem_ctx = state->linalloc;
+ linear_ctx *mem_ctx = state->linalloc;
$$ = new(mem_ctx) ast_warnings_toggle(false);
}
;
primary_expression:
variable_identifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL);
$$->set_location(@1);
$$->primary_expression.identifier = $1;
}
| INTCONSTANT
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
$$->set_location(@1);
$$->primary_expression.int_constant = $1;
}
| UINTCONSTANT
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
$$->set_location(@1);
$$->primary_expression.uint_constant = $1;
}
| INT64CONSTANT
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_int64_constant, NULL, NULL, NULL);
$$->set_location(@1);
$$->primary_expression.int64_constant = $1;
}
| UINT64CONSTANT
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_uint64_constant, NULL, NULL, NULL);
$$->set_location(@1);
$$->primary_expression.uint64_constant = $1;
}
| FLOATCONSTANT
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
$$->set_location(@1);
$$->primary_expression.float_constant = $1;
}
| DOUBLECONSTANT
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_double_constant, NULL, NULL, NULL);
$$->set_location(@1);
$$->primary_expression.double_constant = $1;
}
| BOOLCONSTANT
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
$$->set_location(@1);
$$->primary_expression.bool_constant = $1;
primary_expression
| postfix_expression '[' integer_expression ']'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL);
$$->set_location_range(@1, @4);
}
}
| postfix_expression DOT_TOK FIELD_SELECTION
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
$$->set_location_range(@1, @3);
$$->primary_expression.identifier = $3;
}
| postfix_expression INC_OP
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
$$->set_location_range(@1, @2);
}
| postfix_expression DEC_OP
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
$$->set_location_range(@1, @2);
}
function_identifier:
type_specifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_function_expression($1);
$$->set_location(@1);
}
| postfix_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_function_expression($1);
$$->set_location(@1);
}
postfix_expression
| INC_OP unary_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
$$->set_location(@1);
}
| DEC_OP unary_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
$$->set_location(@1);
}
| unary_operator unary_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression($1, $2, NULL, NULL);
$$->set_location_range(@1, @2);
}
unary_expression
| multiplicative_expression '*' unary_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
$$->set_location_range(@1, @3);
}
| multiplicative_expression '/' unary_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
$$->set_location_range(@1, @3);
}
| multiplicative_expression '%' unary_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
$$->set_location_range(@1, @3);
}
multiplicative_expression
| additive_expression '+' multiplicative_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
$$->set_location_range(@1, @3);
}
| additive_expression '-' multiplicative_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
$$->set_location_range(@1, @3);
}
additive_expression
| shift_expression LEFT_OP additive_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
$$->set_location_range(@1, @3);
}
| shift_expression RIGHT_OP additive_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
$$->set_location_range(@1, @3);
}
shift_expression
| relational_expression '<' shift_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
$$->set_location_range(@1, @3);
}
| relational_expression '>' shift_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
$$->set_location_range(@1, @3);
}
| relational_expression LE_OP shift_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
$$->set_location_range(@1, @3);
}
| relational_expression GE_OP shift_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
$$->set_location_range(@1, @3);
}
relational_expression
| equality_expression EQ_OP relational_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
$$->set_location_range(@1, @3);
}
| equality_expression NE_OP relational_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
$$->set_location_range(@1, @3);
}
equality_expression
| and_expression '&' equality_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3);
$$->set_location_range(@1, @3);
}
and_expression
| exclusive_or_expression '^' and_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
$$->set_location_range(@1, @3);
}
exclusive_or_expression
| inclusive_or_expression '|' exclusive_or_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
$$->set_location_range(@1, @3);
}
inclusive_or_expression
| logical_and_expression AND_OP inclusive_or_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
$$->set_location_range(@1, @3);
}
logical_and_expression
| logical_xor_expression XOR_OP logical_and_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
$$->set_location_range(@1, @3);
}
logical_xor_expression
| logical_or_expression OR_OP logical_xor_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
$$->set_location_range(@1, @3);
}
logical_or_expression
| logical_or_expression '?' expression ':' assignment_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
$$->set_location_range(@1, @5);
}
conditional_expression
| unary_expression assignment_operator assignment_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression($2, $1, $3, NULL);
$$->set_location_range(@1, @3);
}
}
| expression ',' assignment_expression
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
if ($1->oper != ast_sequence) {
$$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
$$->set_location_range(@1, @3);
function_header:
fully_specified_type variable_identifier '('
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_function();
$$->set_location(@2);
$$->return_type = $1;
parameter_declarator:
type_specifier any_identifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_parameter_declarator();
$$->set_location_range(@1, @2);
$$->type = new(ctx) ast_fully_specified_type();
}
| type_specifier any_identifier array_specifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_parameter_declarator();
$$->set_location_range(@1, @3);
$$->type = new(ctx) ast_fully_specified_type();
}
| parameter_qualifier parameter_type_specifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_parameter_declarator();
$$->set_location(@2);
$$->type = new(ctx) ast_fully_specified_type();
single_declaration
| init_declarator_list ',' any_identifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_declaration *decl = new(ctx) ast_declaration($3, NULL, NULL);
decl->set_location(@3);
}
| init_declarator_list ',' any_identifier array_specifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_declaration *decl = new(ctx) ast_declaration($3, $4, NULL);
decl->set_location_range(@3, @4);
}
| init_declarator_list ',' any_identifier array_specifier '=' initializer
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_declaration *decl = new(ctx) ast_declaration($3, $4, $6);
decl->set_location_range(@3, @4);
}
| init_declarator_list ',' any_identifier '=' initializer
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_declaration *decl = new(ctx) ast_declaration($3, NULL, $5);
decl->set_location(@3);
single_declaration:
fully_specified_type
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
/* Empty declaration list is valid. */
$$ = new(ctx) ast_declarator_list($1);
$$->set_location(@1);
}
| fully_specified_type any_identifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_declaration *decl = new(ctx) ast_declaration($2, NULL, NULL);
decl->set_location(@2);
}
| fully_specified_type any_identifier array_specifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_declaration *decl = new(ctx) ast_declaration($2, $3, NULL);
decl->set_location_range(@2, @3);
}
| fully_specified_type any_identifier array_specifier '=' initializer
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_declaration *decl = new(ctx) ast_declaration($2, $3, $5);
decl->set_location_range(@2, @3);
}
| fully_specified_type any_identifier '=' initializer
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
decl->set_location(@2);
}
| INVARIANT variable_identifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_declaration *decl = new(ctx) ast_declaration($2, NULL, NULL);
decl->set_location(@2);
}
| PRECISE variable_identifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_declaration *decl = new(ctx) ast_declaration($2, NULL, NULL);
decl->set_location(@2);
fully_specified_type:
type_specifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_fully_specified_type();
$$->set_location(@1);
$$->specifier = $1;
}
| type_qualifier type_specifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_fully_specified_type();
$$->set_location_range(@1, @2);
$$->qualifier = $1;
| any_identifier '=' constant_expression
{
memset(& $$, 0, sizeof($$));
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
if ($3->oper != ast_int_constant &&
$3->oper != ast_uint_constant &&
subroutine_type_list:
any_identifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_declaration *decl = new(ctx) ast_declaration($1, NULL, NULL);
decl->set_location(@1);
}
| subroutine_type_list ',' any_identifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_declaration *decl = new(ctx) ast_declaration($3, NULL, NULL);
decl->set_location(@3);
array_specifier:
'[' ']'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_array_specifier(@1, new(ctx) ast_expression(
ast_unsized_array_dim, NULL,
NULL, NULL));
}
| '[' constant_expression ']'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_array_specifier(@1, $2);
$$->set_location_range(@1, @3);
}
| array_specifier '[' ']'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = $1;
if (state->check_arrays_of_arrays_allowed(& @1)) {
type_specifier_nonarray:
basic_type_specifier_nonarray
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_type_specifier($1);
$$->set_location(@1);
}
| struct_specifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_type_specifier($1);
$$->set_location(@1);
}
| TYPE_IDENTIFIER
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_type_specifier($1);
$$->set_location(@1);
}
struct_specifier:
STRUCT any_identifier '{' struct_declaration_list '}'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_struct_specifier($2, $4);
$$->set_location_range(@2, @5);
state->symbols->add_type($2, glsl_type::void_type);
}
| STRUCT '{' struct_declaration_list '}'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
/* All anonymous structs have the same name. This simplifies matching of
* globals whose type is an unnamed struct.
struct_declaration:
fully_specified_type struct_declarator_list ';'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_fully_specified_type *const type = $1;
type->set_location(@1);
struct_declarator:
any_identifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_declaration($1, NULL, NULL);
$$->set_location(@1);
}
| any_identifier array_specifier
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_declaration($1, $2, NULL);
$$->set_location_range(@1, @2);
}
initializer_list:
initializer
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_aggregate_initializer();
$$->set_location(@1);
$$->expressions.push_tail(& $1->link);
compound_statement:
'{' '}'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_compound_statement(true, NULL);
$$->set_location_range(@1, @2);
}
}
statement_list '}'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_compound_statement(true, $3);
$$->set_location_range(@1, @4);
state->symbols->pop_scope();
compound_statement_no_new_scope:
'{' '}'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_compound_statement(false, NULL);
$$->set_location_range(@1, @2);
}
| '{' statement_list '}'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_compound_statement(false, $2);
$$->set_location_range(@1, @3);
}
expression_statement:
';'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_statement(NULL);
$$->set_location(@1);
}
| expression ';'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_expression_statement($1);
$$->set_location(@1);
}
}
| fully_specified_type any_identifier '=' initializer
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
decl->set_location_range(@2, @4);
iteration_statement:
WHILE '(' condition ')' statement_no_new_scope
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
NULL, $3, NULL, $5);
$$->set_location_range(@1, @4);
}
| DO statement_no_new_scope WHILE '(' expression ')' ';'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
NULL, $5, NULL, $2);
$$->set_location_range(@1, @6);
}
| FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
$3, $4.cond, $4.rest, $6);
$$->set_location_range(@1, @6);
jump_statement:
CONTINUE ';'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
$$->set_location(@1);
}
| BREAK ';'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
$$->set_location(@1);
}
| RETURN ';'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
$$->set_location(@1);
}
| RETURN expression ';'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
$$->set_location_range(@1, @2);
}
| DISCARD ';' // Fragment shader only.
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
$$->set_location(@1);
}
demote_statement:
DEMOTE ';'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_demote_statement();
$$->set_location(@1);
}
function_definition:
function_prototype compound_statement_no_new_scope
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
$$ = new(ctx) ast_function_definition();
$$->set_location_range(@1, @2);
$$->prototype = $1;
member_declaration:
fully_specified_type struct_declarator_list ';'
{
- void *ctx = state->linalloc;
+ linear_ctx *ctx = state->linalloc;
ast_fully_specified_type *type = $1;
type->set_location(@1);
*/
};
+struct linear_ctx {
+ struct linear_header header;
+};
+
struct linear_size_chunk {
unsigned size; /* for realloc */
unsigned _padding;
}
void *
-linear_alloc_child(void *parent, unsigned size)
+linear_alloc_child(linear_ctx *ctx, unsigned size)
{
- linear_header *first = parent;
+ linear_header *first = &ctx->header;
linear_header *latest = first->latest;
linear_header *new_node;
linear_size_chunk *ptr;
return &ptr[1];
}
-void *
-linear_alloc_parent(void *ralloc_ctx)
+linear_ctx *
+linear_context(void *ralloc_ctx)
{
linear_header *node;
if (unlikely(!node))
return NULL;
- return node;
+ return (linear_ctx *)node;
}
void *
-linear_zalloc_child(void *parent, unsigned size)
+linear_zalloc_child(linear_ctx *ctx, unsigned size)
{
- void *ptr = linear_alloc_child(parent, size);
+ void *ptr = linear_alloc_child(ctx, size);
if (likely(ptr))
memset(ptr, 0, size);
}
void
-linear_free_parent(void *ptr)
+linear_free_context(linear_ctx *ctx)
{
- if (unlikely(!ptr))
+ if (unlikely(!ctx))
return;
- linear_header *first = ptr;
+ linear_header *first = &ctx->header;
assert(first->magic == LMAGIC);
/* Other nodes are ralloc children of the first node. */
- ralloc_free(first);
+ ralloc_free(ctx);
}
void
-ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr)
+ralloc_steal_linear_context(void *new_ralloc_ctx, linear_ctx *ctx)
{
- if (unlikely(!ptr))
+ if (unlikely(!ctx))
return;
- linear_header *first = ptr;
+ linear_header *first = &ctx->header;
assert(first->magic == LMAGIC);
/* Other nodes are ralloc children of the first node. */
- ralloc_steal(new_ralloc_ctx, first);
+ ralloc_steal(new_ralloc_ctx, ctx);
}
void *
-ralloc_parent_of_linear_parent(void *ptr)
+ralloc_parent_of_linear_context(linear_ctx *ctx)
{
- linear_header *node = ptr;
+ linear_header *node = &ctx->header;
assert(node->magic == LMAGIC);
return PTR_FROM_HEADER(get_header(node)->parent);
}
void *
-linear_realloc(void *parent, void *old, unsigned new_size)
+linear_realloc(linear_ctx *ctx, void *old, unsigned new_size)
{
unsigned old_size = 0;
ralloc_header *new_ptr;
- new_ptr = linear_alloc_child(parent, new_size);
+ new_ptr = linear_alloc_child(ctx, new_size);
if (unlikely(!old))
return new_ptr;
*/
char *
-linear_strdup(void *parent, const char *str)
+linear_strdup(linear_ctx *ctx, const char *str)
{
unsigned n;
char *ptr;
return NULL;
n = strlen(str);
- ptr = linear_alloc_child(parent, n + 1);
+ ptr = linear_alloc_child(ctx, n + 1);
if (unlikely(!ptr))
return NULL;
}
char *
-linear_asprintf(void *parent, const char *fmt, ...)
+linear_asprintf(linear_ctx *ctx, const char *fmt, ...)
{
char *ptr;
va_list args;
va_start(args, fmt);
- ptr = linear_vasprintf(parent, fmt, args);
+ ptr = linear_vasprintf(ctx, fmt, args);
va_end(args);
return ptr;
}
char *
-linear_vasprintf(void *parent, const char *fmt, va_list args)
+linear_vasprintf(linear_ctx *ctx, const char *fmt, va_list args)
{
unsigned size = u_printf_length(fmt, args) + 1;
- char *ptr = linear_alloc_child(parent, size);
+ char *ptr = linear_alloc_child(ctx, size);
if (ptr != NULL)
vsnprintf(ptr, size, fmt, args);
}
bool
-linear_asprintf_append(void *parent, char **str, const char *fmt, ...)
+linear_asprintf_append(linear_ctx *ctx, char **str, const char *fmt, ...)
{
bool success;
va_list args;
va_start(args, fmt);
- success = linear_vasprintf_append(parent, str, fmt, args);
+ success = linear_vasprintf_append(ctx, str, fmt, args);
va_end(args);
return success;
}
bool
-linear_vasprintf_append(void *parent, char **str, const char *fmt, va_list args)
+linear_vasprintf_append(linear_ctx *ctx, char **str, const char *fmt, va_list args)
{
size_t existing_length;
assert(str != NULL);
existing_length = *str ? strlen(*str) : 0;
- return linear_vasprintf_rewrite_tail(parent, str, &existing_length, fmt, args);
+ return linear_vasprintf_rewrite_tail(ctx, str, &existing_length, fmt, args);
}
bool
-linear_asprintf_rewrite_tail(void *parent, char **str, size_t *start,
+linear_asprintf_rewrite_tail(linear_ctx *ctx, char **str, size_t *start,
const char *fmt, ...)
{
bool success;
va_list args;
va_start(args, fmt);
- success = linear_vasprintf_rewrite_tail(parent, str, start, fmt, args);
+ success = linear_vasprintf_rewrite_tail(ctx, str, start, fmt, args);
va_end(args);
return success;
}
bool
-linear_vasprintf_rewrite_tail(void *parent, char **str, size_t *start,
+linear_vasprintf_rewrite_tail(linear_ctx *ctx, char **str, size_t *start,
const char *fmt, va_list args)
{
size_t new_length;
assert(str != NULL);
if (unlikely(*str == NULL)) {
- *str = linear_vasprintf(parent, fmt, args);
+ *str = linear_vasprintf(ctx, fmt, args);
*start = strlen(*str);
return true;
}
new_length = u_printf_length(fmt, args);
- ptr = linear_realloc(parent, *str, *start + new_length + 1);
+ ptr = linear_realloc(ctx, *str, *start + new_length + 1);
if (unlikely(ptr == NULL))
return false;
/* helper routine for strcat/strncat - n is the exact amount to copy */
static bool
-linear_cat(void *parent, char **dest, const char *str, unsigned n)
+linear_cat(linear_ctx *ctx, char **dest, const char *str, unsigned n)
{
char *both;
unsigned existing_length;
assert(dest != NULL && *dest != NULL);
existing_length = strlen(*dest);
- both = linear_realloc(parent, *dest, existing_length + n + 1);
+ both = linear_realloc(ctx, *dest, existing_length + n + 1);
if (unlikely(both == NULL))
return false;
}
bool
-linear_strcat(void *parent, char **dest, const char *str)
+linear_strcat(linear_ctx *ctx, char **dest, const char *str)
{
- return linear_cat(parent, dest, str, strlen(str));
+ return linear_cat(ctx, dest, str, strlen(str));
}
void *
-linear_alloc_child_array(void *parent, size_t size, unsigned count)
+linear_alloc_child_array(linear_ctx *ctx, size_t size, unsigned count)
{
if (count > SIZE_MAX/size)
return NULL;
- return linear_alloc_child(parent, size * count);
+ return linear_alloc_child(ctx, size * count);
}
void *
-linear_zalloc_child_array(void *parent, size_t size, unsigned count)
+linear_zalloc_child_array(linear_ctx *ctx, size_t size, unsigned count)
{
if (count > SIZE_MAX/size)
return NULL;
- return linear_zalloc_child(parent, size * count);
+ return linear_zalloc_child(ctx, size * count);
}
#define DECLARE_LINEAR_ALLOC_CXX_OPERATORS_TEMPLATE(TYPE, ALLOC_FUNC) \
public: \
- static void* operator new(size_t size, void *mem_ctx) \
+ static void* operator new(size_t size, linear_ctx *ctx) \
{ \
- void *p = ALLOC_FUNC(mem_ctx, size); \
+ void *p = ALLOC_FUNC(ctx, size); \
assert(p != NULL); \
static_assert(HAS_TRIVIAL_DESTRUCTOR(TYPE)); \
return p; \
#define DECLARE_LINEAR_ZALLOC_CXX_OPERATORS(type) \
DECLARE_LINEAR_ALLOC_CXX_OPERATORS_TEMPLATE(type, linear_zalloc_child)
+typedef struct linear_ctx linear_ctx;
+
/**
- * Do a fast allocation from the linear buffer, also known as the child node
+ * Do a fast allocation from the linear context, also known as the child node
* from the allocator's point of view. It can't be freed directly. You have
- * to free the parent or the ralloc parent.
+ * to free the linear context or the ralloc parent.
*
- * \param parent parent node of the linear allocator
+ * \param ctx linear context of the allocator
* \param size size to allocate (max 32 bits)
*/
-void *linear_alloc_child(void *parent, unsigned size) MALLOCLIKE;
+void *linear_alloc_child(linear_ctx *ctx, unsigned size);
/**
- * Allocate a parent node that will hold linear buffers.
+ * Allocate a linear context that will internally hold linear buffers.
* Use it for all child node allocations.
*
* \param ralloc_ctx ralloc context, must not be NULL
*/
-void *linear_alloc_parent(void *ralloc_ctx);
+linear_ctx *linear_context(void *ralloc_ctx);
/**
* Same as linear_alloc_child, but also clears memory.
*/
-void *linear_zalloc_child(void *parent, unsigned size) MALLOCLIKE;
+void *linear_zalloc_child(linear_ctx *ctx, unsigned size) MALLOCLIKE;
/**
- * Free the linear parent node. This will free all child nodes too.
- * Freeing the ralloc parent will also free this.
+ * Free a linear context. This will free all child nodes too.
+ * Alternatively, freeing the ralloc parent will also free
+ * the linear context.
*/
-void linear_free_parent(void *ptr);
+void linear_free_context(linear_ctx *ctx);
/**
- * Same as ralloc_steal, but steals the linear parent node.
+ * Same as ralloc_steal, but steals the entire linear context.
*/
-void ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr);
+void ralloc_steal_linear_context(void *new_ralloc_ctx, linear_ctx *ctx);
/**
- * Return the ralloc parent of the linear parent node.
+ * Return the ralloc parent of the linear context.
*/
-void *ralloc_parent_of_linear_parent(void *ptr);
+void *ralloc_parent_of_linear_context(linear_ctx *ctx);
/**
* Same as realloc except that the linear allocator doesn't free child nodes,
* reallocation is required. Don't use it often. It's much slower than
* realloc.
*/
-void *linear_realloc(void *parent, void *old, unsigned new_size);
+void *linear_realloc(linear_ctx *ctx, void *old, unsigned new_size);
/**
- * Do a fast allocation of an array from the linear buffer and initialize it to zero.
+ * Do a fast allocation of an array from the linear context and initialize it to zero.
*
* Similar to \c calloc, but does not initialize the memory to zero.
*
* More than a convenience function, this also checks for integer overflow when
* multiplying \p size and \p count. This is necessary for security.
*/
-void *linear_alloc_child_array(void *parent, size_t size, unsigned count) MALLOCLIKE;
+void *linear_alloc_child_array(linear_ctx *ctx, size_t size, unsigned count) MALLOCLIKE;
/**
- * Do a fast allocation of an array from the linear buffer.
+ * Do a fast allocation of an array from the linear context.
*
* Similar to \c calloc.
*
* More than a convenience function, this also checks for integer overflow when
* multiplying \p size and \p count. This is necessary for security.
*/
-void *linear_zalloc_child_array(void *parent, size_t size, unsigned count) MALLOCLIKE;
+void *linear_zalloc_child_array(linear_ctx *ctx, size_t size, unsigned count) MALLOCLIKE;
/* The functions below have the same semantics as their ralloc counterparts,
* except that they always allocate a linear child node.
*/
-char *linear_strdup(void *parent, const char *str) MALLOCLIKE;
-char *linear_asprintf(void *parent, const char *fmt, ...) PRINTFLIKE(2, 3) MALLOCLIKE;
-char *linear_vasprintf(void *parent, const char *fmt, va_list args) MALLOCLIKE;
-bool linear_asprintf_append(void *parent, char **str, const char *fmt, ...) PRINTFLIKE(3, 4);
-bool linear_vasprintf_append(void *parent, char **str, const char *fmt,
+char *linear_strdup(linear_ctx *ctx, const char *str) MALLOCLIKE;
+char *linear_asprintf(linear_ctx *ctx, const char *fmt, ...) PRINTFLIKE(2, 3) MALLOCLIKE;
+char *linear_vasprintf(linear_ctx *ctx, const char *fmt, va_list args) MALLOCLIKE;
+bool linear_asprintf_append(linear_ctx *ctx, char **str, const char *fmt, ...) PRINTFLIKE(3, 4);
+bool linear_vasprintf_append(linear_ctx *ctx, char **str, const char *fmt,
va_list args);
-bool linear_asprintf_rewrite_tail(void *parent, char **str, size_t *start,
+bool linear_asprintf_rewrite_tail(linear_ctx *ctx, char **str, size_t *start,
const char *fmt, ...) PRINTFLIKE(4, 5);
-bool linear_vasprintf_rewrite_tail(void *parent, char **str, size_t *start,
+bool linear_vasprintf_rewrite_tail(linear_ctx *ctx, char **str, size_t *start,
const char *fmt, va_list args);
-bool linear_strcat(void *parent, char **dest, const char *str);
+bool linear_strcat(linear_ctx *ctx, char **dest, const char *str);
/**
- * \def linear_alloc(parent, type)
- * Do a fast allocation from the linear buffer.
+ * \def linear_alloc(ctx, type)
+ * Do a fast allocation from the linear context.
*
* This is equivalent to:
* \code
- * ((type *) linear_alloc_child(parent, sizeof(type))
+ * ((type *) linear_alloc_child(ctx, sizeof(type))
* \endcode
*/
-#define linear_alloc(parent, type) ((type *) linear_alloc_child(parent, sizeof(type)))
+#define linear_alloc(ctx, type) ((type *) linear_alloc_child(ctx, sizeof(type)))
/**
- * \def linear_zalloc(parent, type)
- * Do a fast allocation from the linear buffer and initialize it to zero.
+ * \def linear_zalloc(ctx, type)
+ * Do a fast allocation from the linear context and initialize it to zero.
*
* This is equivalent to:
* \code
- * ((type *) linear_zalloc_child(parent, sizeof(type))
+ * ((type *) linear_zalloc_child(ctx, sizeof(type))
* \endcode
*/
-#define linear_zalloc(parent, type) ((type *) linear_zalloc_child(parent, sizeof(type)))
+#define linear_zalloc(ctx, type) ((type *) linear_zalloc_child(ctx, sizeof(type)))
/**
- * \def linear_alloc_array(parent, type, count)
- * Do a fast allocation of an array from the linear buffer.
+ * \def linear_alloc_array(ctx, type, count)
+ * Do a fast allocation of an array from the linear context.
*
* Similar to \c calloc, but does not initialize the memory to zero.
*
*
* This is equivalent to:
* \code
- * ((type *) linear_alloc_child_array(parent, sizeof(type), count)
+ * ((type *) linear_alloc_child_array(ctx, sizeof(type), count)
* \endcode
*/
-#define linear_alloc_array(parent, type, count) \
- ((type *) linear_alloc_child_array(parent, sizeof(type), count))
+#define linear_alloc_array(ctx, type, count) \
+ ((type *) linear_alloc_child_array(ctx, sizeof(type), count))
/**
- * \def linear_zalloc_array(parent, type, count)
- * Do a fast allocation of an array from the linear buffer and initialize it to zero
+ * \def linear_zalloc_array(ctx, type, count)
+ * Do a fast allocation of an array from the linear context and initialize it to zero
*
* Similar to \c calloc.
*
*
* This is equivalent to:
* \code
- * ((type *) linear_zalloc_child_array(parent, sizeof(type), count)
+ * ((type *) linear_zalloc_child_array(ctx, sizeof(type), count)
* \endcode
*/
-#define linear_zalloc_array(parent, type, count) \
- ((type *) linear_zalloc_child_array(parent, sizeof(type), count))
+#define linear_zalloc_array(ctx, type, count) \
+ ((type *) linear_zalloc_child_array(ctx, sizeof(type), count))
#ifdef __cplusplus
} /* end of extern "C" */